forked from b00tc4mp/isdi-parttime-202403
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1cc548
commit 7be8431
Showing
11 changed files
with
335 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
PORT = 8080 | ||
JWT_SECRET = hola | ||
MONGODB_URL = mongodb://localhost:27017/project | ||
MONGODB_URL_TEST = mongodb://localhost:27017/test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ describe('authenticateUser', () => { | |
|
||
it('succeds on existing user', () => | ||
bcrypt.hash('123123123', 8) | ||
.then(hash => User.create({ name: 'Mac', surname: 'Book', email: '[email protected]', username: 'macbook', password: hash })) | ||
.then(hash => User.create({ name: 'Mac', surname: 'Book', email: '[email protected]', username: 'macbook', password: hash, userType: 'teacher' })) | ||
.then(() => authenticateUser('macbook', '123123123')) | ||
.then(userId => { | ||
expect(userId).to.be.a.string | ||
|
@@ -33,8 +33,8 @@ describe('authenticateUser', () => { | |
authenticateUser('RandomName', '12345678') | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(error).to.be.instanceOf(CredentialsError) | ||
expect(error.message).to.equal('user not found') | ||
expect(errorThrown).to.be.instanceOf(CredentialsError) | ||
expect(errorThrown.message).to.equal('user not found') | ||
}) | ||
} | ||
) | ||
|
@@ -43,7 +43,7 @@ describe('authenticateUser', () => { | |
let errorThrown | ||
|
||
return bcrypt.hash('234234234', 8) | ||
.then(hash => User.create({ name: 'Mac', surname: 'Book', email: '[email protected]', username: 'macbook', password: hash })) | ||
.then(hash => User.create({ name: 'Mac', surname: 'Book', email: '[email protected]', username: 'macbook', password: hash, userType: 'teacher' })) | ||
.then(() => authenticateUser('macbook', '123123123')) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
staff/agustin-birman/api/logic/createCompleteSentenceExercise.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import 'dotenv/config' | ||
import mongoose, { Types } from 'mongoose' | ||
|
||
import bcrypt from 'bcryptjs' | ||
|
||
import createCompleteSentenceExercise from './createCompleteSentenceExercise.js' | ||
|
||
import { User, Activity, Exercise } from '../data/index.js' | ||
|
||
import { expect } from 'chai' | ||
import { ContentError, NotFoundError } from 'com/errors.js' | ||
|
||
const { MONGODB_URL_TEST } = process.env | ||
|
||
const { ObjectId } = Types | ||
|
||
describe('createCompleteSentenceExercise', () => { | ||
before(() => mongoose.connect(MONGODB_URL_TEST).then(() => User.deleteMany())) | ||
|
||
beforeEach(() => Promise.all([User.deleteMany(), Activity.deleteMany(), Exercise.deleteMany()])) | ||
|
||
it('succeeds on creating exercise', () => { | ||
|
||
return bcrypt.hash('12345678', 8) | ||
.then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' })) | ||
.then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) | ||
.then(activity => ({ user, activity }))) | ||
.then(({ user, activity }) => createCompleteSentenceExercise(user.id, activity.id, 'alan (hat) es gegessen')) | ||
.then(() => Exercise.findOne()) | ||
.then(exercise => { | ||
expect(exercise.sentence).to.equal('alan (hat) es gegessen') | ||
expect(exercise.answer).to.equal('hat') | ||
expect(exercise.index).to.be.a('number').and.to.equal(0) | ||
}) | ||
}) | ||
|
||
it('fails on non-existing user', () => { | ||
let errorThrown | ||
|
||
return bcrypt.hash('12345678', 8) | ||
.then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' })) | ||
.then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) | ||
.then(activity => ({ user, activity }))) | ||
.then(({ user, activity }) => createCompleteSentenceExercise(new ObjectId().toString(), activity.id, 'alan (hat) es gegessen')) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.an.instanceOf(NotFoundError) | ||
expect(errorThrown.message).to.equal('user not found') | ||
}) | ||
}) | ||
|
||
it('fails on non-existing activity', () => { | ||
let errorThrown | ||
|
||
return bcrypt.hash('12345678', 8) | ||
.then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' })) | ||
.then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) | ||
.then(activity => ({ user, activity }))) | ||
.then(({ user, activity }) => createCompleteSentenceExercise(user.id, new ObjectId().toString(), 'alan (hat) es gegessen')) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.an.instanceOf(NotFoundError) | ||
expect(errorThrown.message).to.equal('activity not found') | ||
}) | ||
}) | ||
|
||
it('fails on invalid sentence', () => { | ||
let errorThrown | ||
|
||
return bcrypt.hash('12345678', 8) | ||
.then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' })) | ||
.then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) | ||
.then(activity => ({ user, activity }))) | ||
.then(({ user, activity }) => createCompleteSentenceExercise(user.id, activity.id, 123)) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.an.instanceOf(ContentError) | ||
expect(errorThrown.message).to.equal('sentence is not valid') | ||
}) | ||
}) | ||
|
||
after(() => Promise.all([User.deleteMany(), Activity.deleteMany(), Exercise.deleteMany()]).then(() => mongoose.disconnect())) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import 'dotenv/config' | ||
import mongoose, { Types } from 'mongoose' | ||
import bcrypt from 'bcryptjs' | ||
|
||
import { expect } from 'chai' | ||
|
||
import { User, Activity } from '../data/index.js' | ||
|
||
import deleteActivity from './deleteActivity.js' | ||
import { NotFoundError } from 'com/errors.js' | ||
|
||
const { MONGODB_URL_TEST } = process.env | ||
|
||
const { ObjectId } = Types | ||
|
||
describe('deleteActivity', () => { | ||
before(() => mongoose.connect(MONGODB_URL_TEST)) | ||
|
||
beforeEach(() => Promise.all([User.deleteMany(), Activity.deleteMany()])) | ||
|
||
it('succeeds on deleting activity', () => { | ||
|
||
return bcrypt.hash('12345678', 8) | ||
.then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' })) | ||
.then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) | ||
.then(activity => ({ user, activity }))) | ||
.then(({ user, activity }) => deleteActivity(user.id, activity.id) | ||
.then(() => Activity.findById(activity.id))) | ||
.then(activity => { | ||
expect(activity).to.be.null | ||
}) | ||
}) | ||
|
||
it('fails on non-existing user', () => { | ||
let errorThrown | ||
|
||
return bcrypt.hash('12345678', 8) | ||
.then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' })) | ||
.then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) | ||
.then(activity => ({ user, activity }))) | ||
.then(({ user, activity }) => deleteActivity(new ObjectId().toString(), activity.id) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.an.instanceOf(NotFoundError) | ||
expect(errorThrown.message).to.equal('user not found') | ||
})) | ||
}) | ||
|
||
it('fails on non-existing activity', () => { | ||
let errorThrown | ||
|
||
return bcrypt.hash('12345678', 8) | ||
.then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' })) | ||
.then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) | ||
.then(activity => ({ user, activity }))) | ||
.then(({ user, activity }) => deleteActivity(user.id, new ObjectId().toString()) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.an.instanceOf(NotFoundError) | ||
expect(errorThrown.message).to.equal('activity not found') | ||
})) | ||
}) | ||
|
||
after(() => Promise.all([User.deleteMany(), Activity.deleteMany()]).then(() => mongoose.disconnect())) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import 'dotenv/config' | ||
import mongoose, { Types } from 'mongoose' | ||
import bcrypt from 'bcryptjs' | ||
import { expect } from 'chai' | ||
|
||
import { User, Activity, Exercise } from '../data/index.js' | ||
import deleteExercise from './deleteExercise.js' | ||
import { NotFoundError } from 'com/errors.js' | ||
|
||
const { MONGODB_URL_TEST } = process.env | ||
|
||
const { ObjectId } = Types | ||
|
||
describe('deleteExercise', () => { | ||
before(() => mongoose.connect(MONGODB_URL_TEST)) | ||
|
||
beforeEach(() => Promise.all([User.deleteMany(), Activity.deleteMany(), Exercise.deleteMany()])) | ||
|
||
it('succeeds on deleting exercise', () => { | ||
|
||
return bcrypt.hash('12345678', 8) | ||
.then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' })) | ||
.then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) | ||
.then(activity => ({ user, activity }))) | ||
.then(({ user, activity }) => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) | ||
.then(exercise => ({ user, exercise }))) | ||
.then(({ user, exercise }) => deleteExercise(user.id, exercise.id) | ||
.then(() => Exercise.findById(exercise.id))) | ||
.then(exercise => { | ||
expect(exercise).to.be.null | ||
}) | ||
}) | ||
|
||
it('fails on non-existing user', () => { | ||
let errorThrown | ||
|
||
return bcrypt.hash('12345678', 8) | ||
.then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' })) | ||
.then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) | ||
.then(activity => ({ user, activity }))) | ||
.then(({ user, activity }) => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 })) | ||
.then(exercise => deleteExercise(new ObjectId().toString(), exercise.id) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.an.instanceOf(NotFoundError) | ||
expect(errorThrown.message).to.equal('user not found') | ||
})) | ||
}) | ||
|
||
it('fails on non-existing exercise', () => { | ||
let errorThrown | ||
|
||
return bcrypt.hash('12345678', 8) | ||
.then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' })) | ||
.then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) | ||
.then(activity => ({ user, activity }))) | ||
.then(({ user, activity }) => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) | ||
.then(() => user)) | ||
.then(user => deleteExercise(user.id, new ObjectId().toString()) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.an.instanceOf(NotFoundError) | ||
expect(errorThrown.message).to.equal('exercise not found') | ||
})) | ||
}) | ||
|
||
|
||
after(() => Promise.all([User.deleteMany(), Activity.deleteMany(), Exercise.deleteMany()]).then(() => mongoose.disconnect())) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'dotenv/config' | ||
import mongoose, { Types } from 'mongoose' | ||
import editActivity from './editActivity.js' | ||
import bcrypt from 'bcryptjs' | ||
import { expect } from 'chai' | ||
|
||
import { Activity, User } from '../data/index.js' | ||
const { MONGODB_URL_TEST } = process.env | ||
|
||
const { ObjectId } = Types | ||
|
||
describe('editActivity', () => { | ||
|
||
before(() => mongoose.connect(MONGODB_URL_TEST)) | ||
|
||
beforeEach(() => Promise.all([User.deleteMany(), Activity.deleteMany()])) | ||
|
||
it('succeeds on editing activity', () => { | ||
|
||
return bcrypt.hash('12345678', 8) | ||
.then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' })) | ||
.then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) | ||
.then(activity => ({ user, activity }))) | ||
.then(({ user, activity }) => editActivity(user.id, activity.id, 'title2', 'description2') | ||
.then(() => activity)) | ||
.then(activity => Activity.findById(activity.id)) | ||
.then(activityEditted => { | ||
expect(activityEditted.title).to.equal('title2') | ||
expect(activityEditted.description).to.equal('description2') | ||
}) | ||
}) | ||
|
||
after(() => Promise.all([User.deleteMany(), Activity.deleteMany()]).then(() => mongoose.disconnect())) | ||
}) | ||
|
Oops, something went wrong.