diff --git a/staff/agustin-birman/api/logic/activity/deleteActivity.spec.js b/staff/agustin-birman/api/logic/activity/deleteActivity.spec.js index 19a3952ed..77c0f0ea0 100644 --- a/staff/agustin-birman/api/logic/activity/deleteActivity.spec.js +++ b/staff/agustin-birman/api/logic/activity/deleteActivity.spec.js @@ -7,7 +7,7 @@ import { expect } from 'chai' import { User, Activity } from '../../data/index.js' import deleteActivity from './deleteActivity.js' -import { ContentError, NotFoundError } from 'com/errors.js' +import { ContentError, MatchError, NotFoundError } from 'com/errors.js' const { MONGODB_URL_TEST } = process.env @@ -31,14 +31,42 @@ describe('deleteActivity', () => { }) }) + it('fails on non-matching user', () => { + let errorThrown + + return bcrypt.hash('12345678', 8) + .then(hash => Promise.all([User.create({ + name: 'Mocha', + surname: 'Chai', + email: 'Mocha@Chai.com', + username: 'MochaChai', + password: hash, + userType: 'teacher' + }), User.create({ + name: 'Test', + surname: 'User', + email: 'test@user.com', + username: 'testuser', + password: hash, + userType: 'teacher' + })])) + .then(([user1, user2]) => Activity.create({ teacher: user1.id, title: 'title', description: 'description' }) + .then(activity => ({ user1, user2, activity }))) + .then(({ user2, activity }) => deleteActivity(user2.id, activity.id) + .catch(error => errorThrown = error) + .finally(() => { + expect(errorThrown).to.be.an.instanceOf(MatchError) + expect(errorThrown.message).to.equal('you are not the owner of the activity') + })) + }) + it('fails on non-existing user', () => { let errorThrown return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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) + .then(activity => deleteActivity(new ObjectId().toString(), activity.id)) .catch(error => errorThrown = error) .finally(() => { expect(errorThrown).to.be.an.instanceOf(NotFoundError) @@ -64,8 +92,7 @@ describe('deleteActivity', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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()) + .then(() => deleteActivity(user.id, new ObjectId().toString())) .catch(error => errorThrown = error) .finally(() => { expect(errorThrown).to.be.an.instanceOf(NotFoundError) diff --git a/staff/agustin-birman/api/logic/activity/editActivity.spec.js b/staff/agustin-birman/api/logic/activity/editActivity.spec.js index c0ba316bb..6a7ad2656 100644 --- a/staff/agustin-birman/api/logic/activity/editActivity.spec.js +++ b/staff/agustin-birman/api/logic/activity/editActivity.spec.js @@ -21,10 +21,8 @@ describe('editActivity', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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(activity => editActivity(user.id, activity.id, 'title2', 'description2') + .then(() => Activity.findById(activity.id)))) .then(activityEditted => { expect(activityEditted.title).to.equal('title2') expect(activityEditted.description).to.equal('description2') @@ -53,8 +51,7 @@ describe('editActivity', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', username: 'mochachai', password: hash, userType: 'teacher' })) .then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) - .then(() => user)) - .then(user => editActivity(user.id, new ObjectId().toString(), 'title2', 'description2')) + .then(() => editActivity(user.id, new ObjectId().toString(), 'title2', 'description2'))) .catch(error => errorThrown = error) .finally(() => { expect(errorThrown).to.be.an.instanceOf(NotFoundError) @@ -88,32 +85,26 @@ describe('editActivity', () => { it('fails on invalid title', () => { let errorThrown - - return bcrypt.hash('12345678', 8) - .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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, 12345, 'description2')) - .catch(error => errorThrown = error) - .finally(() => { - expect(errorThrown).to.be.an.instanceOf(ContentError) - expect(errorThrown.message).to.equal('title is not valid') - }) + try { + editActivity(new ObjectId().toString(), new ObjectId().toString(), 123, 'description2') + } catch (error) { + errorThrown = error + } finally { + expect(errorThrown).to.be.instanceOf(ContentError) + expect(errorThrown.message).to.equal('title is not valid') + } }) it('fails on invalid description', () => { let errorThrown - - return bcrypt.hash('12345678', 8) - .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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, 'title', 12345)) - .catch(error => errorThrown = error) - .finally(() => { - expect(errorThrown).to.be.an.instanceOf(ContentError) - expect(errorThrown.message).to.equal('description is not valid') - }) + try { + editActivity(new ObjectId().toString(), new ObjectId().toString(), 'title', 123) + } catch (error) { + errorThrown = error + } finally { + expect(errorThrown).to.be.instanceOf(ContentError) + expect(errorThrown.message).to.equal('description is not valid') + } }) after(() => Promise.all([User.deleteMany(), Activity.deleteMany()]).then(() => mongoose.disconnect())) diff --git a/staff/agustin-birman/api/logic/activity/getActivities.spec.js b/staff/agustin-birman/api/logic/activity/getActivities.spec.js index e945f29e8..9d75f916a 100644 --- a/staff/agustin-birman/api/logic/activity/getActivities.spec.js +++ b/staff/agustin-birman/api/logic/activity/getActivities.spec.js @@ -23,8 +23,7 @@ describe('getActivities', () => { .then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) .then(() => Activity.create({ teacher: user.id, title: 'title2', description: 'description2' })) - .then(() => user)) - .then(user => getActivities(user.id)) + .then(() => getActivities(user.id))) .then(activities => { expect(activities).to.be.an('array') expect(activities).to.have.lengthOf(2) diff --git a/staff/agustin-birman/api/logic/activity/getActivity.js b/staff/agustin-birman/api/logic/activity/getActivity.js index 15a64c1b1..eaa55035f 100644 --- a/staff/agustin-birman/api/logic/activity/getActivity.js +++ b/staff/agustin-birman/api/logic/activity/getActivity.js @@ -17,6 +17,7 @@ const getActivity = (userId, activityId) => { .then(activity => { if (!activity) throw new NotFoundError('activity not found') + const activityObj = activity.toObject() activityObj.id = activityObj._id delete activityObj._id diff --git a/staff/agustin-birman/api/logic/activity/getActivity.spec.js b/staff/agustin-birman/api/logic/activity/getActivity.spec.js index b44ce15bd..5e516227d 100644 --- a/staff/agustin-birman/api/logic/activity/getActivity.spec.js +++ b/staff/agustin-birman/api/logic/activity/getActivity.spec.js @@ -21,13 +21,12 @@ describe('getActivity', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', username: 'mochachai', password: hash, userType: 'teacher' })) .then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) - .then(activity => ({ user, activity }))) - .then(({ user, activity }) => getActivity(user.id, activity.id)) - .then(activity => { - expect(activity.teacher).to.equal(activity.teacher) - expect(activity.title).to.equal('title') - expect(activity.description).to.equal('description') - }) + .then(activity => getActivity(user.id, activity.id) + .then(activityInfo => { + expect(activityInfo.teacher.toString()).to.equal(activity.teacher.toString()) + expect(activityInfo.title).to.equal('title') + expect(activityInfo.description).to.equal('description') + }))) }) it('fails on non-existing user', () => { @@ -50,8 +49,7 @@ describe('getActivity', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', username: 'mochachai', password: hash, userType: 'teacher' })) .then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) - .then(() => user)) - .then(user => getActivity(user.id, new ObjectId().toString())) + .then(() => getActivity(user.id, new ObjectId().toString()))) .catch(error => errorThrown = error) .finally(() => { expect(errorThrown).to.be.an.instanceOf(NotFoundError) diff --git a/staff/agustin-birman/api/logic/answer/deleteAnswers.js b/staff/agustin-birman/api/logic/answer/deleteAnswers.js index e0744bbfe..0ab040cc5 100644 --- a/staff/agustin-birman/api/logic/answer/deleteAnswers.js +++ b/staff/agustin-birman/api/logic/answer/deleteAnswers.js @@ -20,14 +20,16 @@ const deleteAnswers = (userId, activityId) => { return Exercise.find({ activity: activityId }) .catch(error => { throw new SystemError(error.message) }) - .then(exercise => { - if (!exercise) + .then(exercises => { + if (!exercises || exercises.length === 0) throw new NotFoundError('exercise not found') - return Answer.find({ exercise: exercise._id }) + const exerciseIds = exercises.map(exercise => exercise._id) + + return Answer.find({ exercise: exerciseIds }) .catch(error => { throw new SystemError(error.message) }) .then(answers => { - if (!answers) + if (!answers || answers.length === 0) throw new NotFoundError('answer not found') return Answer.deleteMany(answers._id) diff --git a/staff/agustin-birman/api/logic/answer/deleteAnswers.spec.js b/staff/agustin-birman/api/logic/answer/deleteAnswers.spec.js index 783d167f5..55fda0888 100644 --- a/staff/agustin-birman/api/logic/answer/deleteAnswers.spec.js +++ b/staff/agustin-birman/api/logic/answer/deleteAnswers.spec.js @@ -9,6 +9,7 @@ const { MONGODB_URL_TEST } = process.env const { ObjectId } = Types +debugger describe('deleteAnswers', () => { before(() => mongoose.connect(MONGODB_URL_TEST)) @@ -40,7 +41,6 @@ describe('deleteAnswers', () => { .then(answer2 => deleteAnswers(user.id, activity.id) .then(() => Answer.find({ activity: activity.id }) .then(answers => expect(answers).to.be.an('array').that.is.empty)))))))) - }) it('fails non-existing user', () => { @@ -77,6 +77,33 @@ describe('deleteAnswers', () => { })))))) }) + it('fails non-existing exercise', () => { + let errorThrown + return bcrypt.hash('12345678', 8) + .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', username: 'mochachai', password: hash, userType: 'teacher' })) + .then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) + .then(activity => deleteAnswers(user.id, activity.id)) + .catch(error => errorThrown = error) + .finally(() => { + expect(errorThrown).to.be.an.instanceOf(NotFoundError) + expect(errorThrown.message).to.equal('exercise not found') + })) + }) + + it('fails non-existing answer', () => { + let errorThrown + return bcrypt.hash('12345678', 8) + .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', username: 'mochachai', password: hash, userType: 'teacher' })) + .then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' }) + .then(activity => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) + .then(() => deleteAnswers(user.id, activity.id)) + .catch(error => errorThrown = error) + .finally(() => { + expect(errorThrown).to.be.an.instanceOf(NotFoundError) + expect(errorThrown.message).to.equal('answer not found') + }))) + }) + it('fails on invalid userId', () => { let errorThrown try { @@ -89,7 +116,7 @@ describe('deleteAnswers', () => { } }) - it('fails on invalid userId', () => { + it('fails on invalid activityId', () => { let errorThrown try { deleteAnswers(new ObjectId().toString(), 123) diff --git a/staff/agustin-birman/api/logic/answer/submitAnswer.spec.js b/staff/agustin-birman/api/logic/answer/submitAnswer.spec.js index 335bfa7ef..a2a472f1e 100644 --- a/staff/agustin-birman/api/logic/answer/submitAnswer.spec.js +++ b/staff/agustin-birman/api/logic/answer/submitAnswer.spec.js @@ -7,7 +7,7 @@ import submitAnswer from './submitAnswer.js' import { User, Activity, Exercise, Answer } from '../../data/index.js' import { expect } from 'chai' -import { ContentError, NotFoundError } from 'com/errors.js' +import { ContentError, DuplicityError, NotFoundError } from 'com/errors.js' const { MONGODB_URL_TEST } = process.env @@ -23,10 +23,8 @@ describe('submitAnswer', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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, activity, exercise }))) - .then(({ user, activity, exercise }) => submitAnswer(user.id, activity.id, exercise.id, 'hat')) + .then(activity => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) + .then(exercise => submitAnswer(user.id, activity.id, exercise.id, 'hat')))) .then(answer => Answer.findById(answer.id)) .then(answer => { expect(answer.answer).to.equal('hat') @@ -39,10 +37,8 @@ describe('submitAnswer', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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 => ({ exercise, activity }))) - .then(({ activity, exercise }) => submitAnswer(new ObjectId().toString(), activity.id, exercise.id, 'hat')) + .then(activity => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) + .then(exercise => submitAnswer(new ObjectId().toString(), activity.id, exercise.id, 'hat')))) .catch(error => errorThrown = error) .finally(() => { expect(errorThrown).to.be.an.instanceOf(NotFoundError) @@ -56,10 +52,8 @@ describe('submitAnswer', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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 }) => submitAnswer(user.id, new ObjectId().toString(), exercise.id, 'hat')) + .then(activity => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) + .then(exercise => submitAnswer(user.id, new ObjectId().toString(), exercise.id, 'hat')))) .catch(error => errorThrown = error) .finally(() => { expect(errorThrown).to.be.an.instanceOf(NotFoundError) @@ -73,10 +67,8 @@ describe('submitAnswer', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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, activity }))) - .then(({ user, activity }) => submitAnswer(user.id, activity.id, new ObjectId().toString(), 'hat')) + .then(activity => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) + .then(() => submitAnswer(user.id, activity.id, new ObjectId().toString(), 'hat')))) .catch(error => errorThrown = error) .finally(() => { expect(errorThrown).to.be.an.instanceOf(NotFoundError) @@ -84,27 +76,37 @@ describe('submitAnswer', () => { }) }) - it('fails on invalid answer', () => { + it('fails on existing answer', () => { let errorThrown - return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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, activity, exercise }))) - .then(({ user, activity, exercise }) => submitAnswer(user.id, activity.id, exercise.id, 123)) + .then(activity => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) + .then(exercise => Answer.create({ student: user.id, exercise: exercise.id, activity: activity.id, answer: 'hat' }) + .then(() => submitAnswer(user.id, activity.id, exercise.id, 'hat'))))) .catch(error => errorThrown = error) .finally(() => { - expect(errorThrown).to.be.an.instanceOf(ContentError) - expect(errorThrown.message).to.equal('answer is not valid') + expect(errorThrown).to.be.an.instanceOf(DuplicityError) + expect(errorThrown.message).to.equal('Answer already exists for this exercise') }) }) + it('fails on invalid answer', () => { + let errorThrown + try { + submitAnswer(new ObjectId().toString(), new ObjectId().toString(), new ObjectId().toString(), 123) + } catch (error) { + errorThrown = error + } finally { + expect(errorThrown).to.be.instanceOf(ContentError) + expect(errorThrown.message).to.equal('answer is not valid') + } + }) + it('fails on invalid userId', () => { let errorThrown try { - submitAnswer(123, new ObjectId().toString(), new ObjectId().toString(), 123) + submitAnswer(123, new ObjectId().toString(), new ObjectId().toString(), '123') } catch (error) { errorThrown = error } finally { diff --git a/staff/agustin-birman/api/logic/exercise/createCompleteSentenceExercise.js b/staff/agustin-birman/api/logic/exercise/createCompleteSentenceExercise.js index afd697418..67e273527 100644 --- a/staff/agustin-birman/api/logic/exercise/createCompleteSentenceExercise.js +++ b/staff/agustin-birman/api/logic/exercise/createCompleteSentenceExercise.js @@ -7,7 +7,7 @@ const ANSWER_REGEX = /\(([^)]+)\)/; const createCompleteSentenceExercise = (userId, activityId, sentence) => { validate.id(userId, 'userId') validate.id(activityId, 'activityId') - validate.text(sentence, 'sentence', 200) + validate.textCompleteSentence(sentence, 'sentence', 200) return User.findById(userId).lean() .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/agustin-birman/api/logic/exercise/createCompleteSentenceExercise.spec.js b/staff/agustin-birman/api/logic/exercise/createCompleteSentenceExercise.spec.js index e8c04e384..b1a99a2b2 100644 --- a/staff/agustin-birman/api/logic/exercise/createCompleteSentenceExercise.spec.js +++ b/staff/agustin-birman/api/logic/exercise/createCompleteSentenceExercise.spec.js @@ -24,8 +24,7 @@ describe('createCompleteSentenceExercise', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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(activity => createCompleteSentenceExercise(user.id, activity.id, 'alan (hat) es gegessen'))) .then(() => Exercise.findOne()) .then(exercise => { expect(exercise.sentence).to.equal('alan (hat) es gegessen') @@ -40,8 +39,7 @@ describe('createCompleteSentenceExercise', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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')) + .then(activity => createCompleteSentenceExercise(new ObjectId().toString(), activity.id, 'alan (hat) es gegessen'))) .catch(error => errorThrown = error) .finally(() => { expect(errorThrown).to.be.an.instanceOf(NotFoundError) @@ -55,8 +53,7 @@ describe('createCompleteSentenceExercise', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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')) + .then(() => createCompleteSentenceExercise(user.id, new ObjectId().toString(), 'alan (hat) es gegessen'))) .catch(error => errorThrown = error) .finally(() => { expect(errorThrown).to.be.an.instanceOf(NotFoundError) @@ -90,17 +87,14 @@ describe('createCompleteSentenceExercise', () => { it('fails on invalid sentence', () => { let errorThrown - - return bcrypt.hash('12345678', 8) - .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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') - }) + try { + createCompleteSentenceExercise(new ObjectId().toString(), new ObjectId().toString(), 123) + } catch (error) { + errorThrown = error + } finally { + expect(errorThrown).to.be.instanceOf(ContentError) + expect(errorThrown.message).to.equal('sentence is not valid') + } }) after(() => Promise.all([User.deleteMany(), Activity.deleteMany(), Exercise.deleteMany()]).then(() => mongoose.disconnect())) diff --git a/staff/agustin-birman/api/logic/exercise/deleteExercise.spec.js b/staff/agustin-birman/api/logic/exercise/deleteExercise.spec.js index a8efea1f1..efade8b2e 100644 --- a/staff/agustin-birman/api/logic/exercise/deleteExercise.spec.js +++ b/staff/agustin-birman/api/logic/exercise/deleteExercise.spec.js @@ -5,7 +5,7 @@ import { expect } from 'chai' import { User, Activity, Exercise } from '../../data/index.js' import deleteExercise from './deleteExercise.js' -import { ContentError, NotFoundError } from 'com/errors.js' +import { ContentError, MatchError, NotFoundError } from 'com/errors.js' const { MONGODB_URL_TEST } = process.env @@ -21,25 +21,34 @@ describe('deleteExercise', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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(activity => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) + .then(exercise => deleteExercise(user.id, exercise.id) + .then(() => Exercise.findById(exercise.id))))) .then(exercise => { expect(exercise).to.be.null }) }) + it('fails on non-existing activity', () => { + return bcrypt.hash('12345678', 8) + .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', username: 'mochachai', password: hash, userType: 'teacher' })) + .then(user => Exercise.create({ teacher: user.id, activity: new ObjectId(), sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) + .then(exercise => deleteExercise(user.id, exercise.id) + .catch(error => { + expect(error).to.be.instanceOf(NotFoundError) + expect(error.message).to.equal('activity not found') + }) + )) + }) + it('fails on non-existing user', () => { let errorThrown return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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) + .then(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) @@ -53,10 +62,8 @@ describe('deleteExercise', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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()) + .then(activity => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) + .then(() => deleteExercise(user.id, new ObjectId().toString()))) .catch(error => errorThrown = error) .finally(() => { expect(errorThrown).to.be.an.instanceOf(NotFoundError) @@ -64,6 +71,35 @@ describe('deleteExercise', () => { })) }) + it('fails on different user', () => { + let errorThrown + + bcrypt.hash('1234', 8) + .then(hash => Promise.all([User.create({ + name: 'Mocha', + surname: 'Chai', + email: 'Mocha@Chai.com', + username: 'MochaChai', + password: hash, + userType: 'teacher' + }), User.create({ + name: 'Test', + surname: 'User', + email: 'test@user.com', + username: 'testuser', + password: hash, + userType: 'teacher' + })])) + .then(([user1, user2]) => Activity.create({ teacher: user1.id, title: 'title', description: 'description' }) + .then(activity => Exercise.create({ teacher: user1.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) + .then(exercise => deleteExercise(user2.id, exercise.id)))) + .catch(error => errorThrown = error) + .finally(() => { + expect(errorThrown).to.be.an.instanceOf(MatchError) + expect(errorThrown.message).to.equal('you are not the owner of the exercise') + }) + }) + it('fails on invalid userId', () => { let errorThrown try { @@ -88,6 +124,5 @@ describe('deleteExercise', () => { } }) - after(() => Promise.all([User.deleteMany(), Activity.deleteMany(), Exercise.deleteMany()]).then(() => mongoose.disconnect())) }) \ No newline at end of file diff --git a/staff/agustin-birman/api/logic/exercise/editExercise.spec.js b/staff/agustin-birman/api/logic/exercise/editExercise.spec.js index b451fe503..a83e3e592 100644 --- a/staff/agustin-birman/api/logic/exercise/editExercise.spec.js +++ b/staff/agustin-birman/api/logic/exercise/editExercise.spec.js @@ -22,11 +22,9 @@ describe('editExercise', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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 }) => editExercise(user.id, exercise.id, 'ich (habe) es gesagt') - .then(() => Exercise.findById(exercise.id))) + .then(activity => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) + .then(exercise => editExercise(user.id, exercise.id, 'ich (habe) es gesagt') + .then(() => Exercise.findById(exercise.id))))) .then(exercise => { expect(exercise.sentence).to.equal('ich (habe) es gesagt') expect(exercise.answer).to.equal('habe') diff --git a/staff/agustin-birman/api/logic/exercise/getExercises.spec.js b/staff/agustin-birman/api/logic/exercise/getExercises.spec.js index a1a16c240..ce4a44f00 100644 --- a/staff/agustin-birman/api/logic/exercise/getExercises.spec.js +++ b/staff/agustin-birman/api/logic/exercise/getExercises.spec.js @@ -20,33 +20,27 @@ describe('getExercises', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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.create({ teacher: user.id, activity: activity.id, sentence: 'pepe (hat) es gesagt', answer: 'hat', index: 1 })) - .then(() => ({ user, activity })) - ) - .then(({ user, activity }) => getExercises(user.id, activity.id) - .then(result => ({ activity, result })) - ) - .then(({ result, activity }) => { - const { exercises, count } = result - - expect(exercises).to.be.an('array') - expect(count).to.be.a('number') - - expect(exercises).to.have.lengthOf(2) - - expect(exercises[0].activity.toString()).to.equal(activity.id) - expect(exercises[0].sentence).to.equal('alan (hat) es gegessen') - expect(exercises[0].answer).to.equal('hat') - expect(exercises[0].index).to.equal(0) - - expect(exercises[1].activity.toString()).to.equal(activity.id) - expect(exercises[1].sentence).to.equal('pepe (hat) es gesagt') - expect(exercises[1].answer).to.equal('hat') - expect(exercises[1].index).to.equal(1) - }) + .then(activity => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) + .then(() => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'pepe (hat) es gesagt', answer: 'hat', index: 1 }) + .then(() => getExercises(user.id, activity.id) + .then(result => { + const { exercises, count } = result + + expect(exercises).to.be.an('array') + expect(count).to.be.a('number') + + expect(exercises).to.have.lengthOf(2) + + expect(exercises[0].activity.toString()).to.equal(activity.id) + expect(exercises[0].sentence).to.equal('alan (hat) es gegessen') + expect(exercises[0].answer).to.equal('hat') + expect(exercises[0].index).to.equal(0) + + expect(exercises[1].activity.toString()).to.equal(activity.id) + expect(exercises[1].sentence).to.equal('pepe (hat) es gesagt') + expect(exercises[1].answer).to.equal('hat') + expect(exercises[1].index).to.equal(1) + }))))) }) it('fails on non-existing user', () => { @@ -55,13 +49,9 @@ describe('getExercises', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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.create({ teacher: user.id, activity: activity.id, sentence: 'pepe (hat) es gesagt', answer: 'hat', index: 1 })) - .then(() => activity) - ) - .then(activity => getExercises(new ObjectId().toString(), activity.id)) + .then(activity => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) + .then(() => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'pepe (hat) es gesagt', answer: 'hat', index: 1 }) + .then(() => getExercises(new ObjectId().toString(), activity.id))))) .catch(error => errorThrown = error) .finally(() => { expect(errorThrown).to.be.an.instanceOf(NotFoundError) @@ -75,13 +65,9 @@ describe('getExercises', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: 'mocha@chai.es', 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.create({ teacher: user.id, activity: activity.id, sentence: 'pepe (hat) es gesagt', answer: 'hat', index: 1 })) - .then(() => user) - ) - .then(user => getExercises(user.id, new ObjectId().toString())) + .then(activity => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 }) + .then(() => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'pepe (hat) es gesagt', answer: 'hat', index: 1 }) + .then(() => getExercises(user.id, new ObjectId().toString()))))) .catch(error => errorThrown = error) .finally(() => { expect(errorThrown).to.be.an.instanceOf(NotFoundError) diff --git a/staff/agustin-birman/api/logic/user/addStudent.spec.js b/staff/agustin-birman/api/logic/user/addStudent.spec.js index 89ab37619..b1f632c1c 100644 --- a/staff/agustin-birman/api/logic/user/addStudent.spec.js +++ b/staff/agustin-birman/api/logic/user/addStudent.spec.js @@ -21,16 +21,13 @@ describe('addStudent', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mac', surname: 'Book', email: 'mac@book.com', username: 'macbook', password: hash, userType: 'teacher' }) .then(user => User.create({ name: 'Wind', surname: 'Book', email: 'wind@book.com', username: 'windbook', password: hash, userType: 'teacher' }) - .then(student => ({ user, student })))) - .then(({ user, student }) => addStudent(user.id, student.id) - .then(() => User.findById(user.id)) - .then(newUser => ({ newUser, student }))) - .then(({ newUser, student }) => { - expect(newUser.student).to.include(student.id) - }) + .then(student => addStudent(user.id, student.id) + .then(() => User.findById(user.id)) + .then(newUser => { + expect(newUser.student).to.include(student.id) + })))) }) - it('fails on non-existing user', () => { let errorThrown @@ -63,11 +60,9 @@ describe('addStudent', () => { return bcrypt.hash('12345678', 8) .then(hash => User.create({ name: 'Mac', surname: 'Book', email: 'mac@book.com', username: 'macbook', password: hash, userType: 'teacher' }) .then(user => User.create({ name: 'Wind', surname: 'Book', email: 'wind@book.com', username: 'windbook', password: hash, userType: 'teacher' }) - .then(student => ({ user, student })))) - .then(({ user, student }) => addStudent(user.id, student.id) - .then(() => User.findById(user.id)) - .then(user => ({ user, student }))) - .then(({ user, student }) => addStudent(user.id, student.id)) + .then(student => addStudent(user.id, student.id) + .then(() => User.findById(user.id) + .then(() => addStudent(user.id, student.id)))))) .catch(error => errorThrown = error) .finally(() => { expect(errorThrown).to.be.an.instanceOf(DuplicityError) diff --git a/staff/agustin-birman/api/logic/user/getStudents.spec.js b/staff/agustin-birman/api/logic/user/getStudents.spec.js index 078f4c349..d5de60b24 100644 --- a/staff/agustin-birman/api/logic/user/getStudents.spec.js +++ b/staff/agustin-birman/api/logic/user/getStudents.spec.js @@ -47,18 +47,17 @@ describe('getStudents', () => { })])) .then(([teacher, student1, student2]) => addStudent(teacher.id, student1.id) .then(() => addStudent(teacher.id, student2.id) - .then(() => teacher))) - .then(teacher => User.findById(teacher.id)) - .then(user => getStudents(user.id)) - .then(listStudents => { - expect(listStudents).to.be.an('array') - expect(listStudents).to.have.lengthOf(2) - - const usernames = listStudents.map(student => student.username) - - expect(usernames).to.include('testuser') - expect(usernames).to.include('testuser2') - })) + .then(() => User.findById(teacher.id) + .then(user => getStudents(user.id)))) + .then(listStudents => { + expect(listStudents).to.be.an('array') + expect(listStudents).to.have.lengthOf(2) + + const usernames = listStudents.map(student => student.username) + + expect(usernames).to.include('testuser') + expect(usernames).to.include('testuser2') + }))) it('fails on non-existing user', () => { diff --git a/staff/agustin-birman/api/logic/user/getUserInfo.spec.js b/staff/agustin-birman/api/logic/user/getUserInfo.spec.js new file mode 100644 index 000000000..5f2494cd3 --- /dev/null +++ b/staff/agustin-birman/api/logic/user/getUserInfo.spec.js @@ -0,0 +1,114 @@ +import 'dotenv/config' +import mongoose, { Types } from 'mongoose' + +import bcrypt from 'bcryptjs' +import getUserInfo from './getUserInfo.js' +import { User } from '../../data/index.js' +import { NotFoundError, ContentError } from 'com/errors.js' + +import { expect } from 'chai' + +const { MONGODB_URL_TEST } = process.env + +const { ObjectId } = Types + + +describe('getUserInfo', () => { + before(() => mongoose.connect(MONGODB_URL_TEST).then(() => User.deleteMany())) + + beforeEach(() => User.deleteMany()) + + it('succeeds get userName from existing user', () => + bcrypt.hash('1234', 8) + .then(hash => Promise.all([User.create({ + name: 'Mocha', + surname: 'Chai', + email: 'Mocha@Chai.com', + username: 'MochaChai', + password: hash, + userType: 'teacher' + }), User.create({ + name: 'Test', + surname: 'User', + email: 'test@user.com', + username: 'testuser', + password: hash, + userType: 'teacher' + })])) + .then(([user, targetUser]) => getUserInfo(user.id, targetUser.id) + .then(userInfo => { + expect(userInfo).to.have.property('name').that.equals('Test'); + expect(userInfo).to.have.property('surname').that.equals('User'); + expect(userInfo).to.have.property('email').that.equals('test@user.com'); + expect(userInfo).to.have.property('username').that.equals('testuser'); + }) + )) + + it('fails on non-existing user', () => { + let errorThrown + return bcrypt.hash('12345678', 8) + .then(hash => User.create({ + name: 'Test', + surname: 'User', + email: 'test@user.com', + username: 'testuser', + password: hash, + userType: 'teacher' + }) + .then(userInfo => getUserInfo(new ObjectId().toString(), userInfo.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 targetUser', () => { + let errorThrown + return bcrypt.hash('12345678', 8) + .then(hash => User.create({ + name: 'Test', + surname: 'User', + email: 'test@user.com', + username: 'testuser', + password: hash, + userType: 'teacher' + }) + .then(user => getUserInfo(user.id, new ObjectId().toString()))) + .catch(error => errorThrown = error) + .finally(() => { + expect(errorThrown).to.be.an.instanceOf(NotFoundError) + expect(errorThrown.message).to.equal('userInfo not found') + }) + }) + + it('fails on invalid userId', () => { + let errorThrown + + try { + getUserInfo(12345, new ObjectId().toString()) + } catch (error) { + errorThrown = error + + } finally { + expect(errorThrown).to.be.instanceOf(ContentError) + expect(errorThrown.message).to.equal('userId is not valid') + } + }) + + it('fails on invalid userInfo', () => { + let errorThrown + + try { + getUserInfo(new ObjectId().toString(), 12345) + } catch (error) { + errorThrown = error + + } finally { + expect(errorThrown).to.be.instanceOf(ContentError) + expect(errorThrown.message).to.equal('userInfoId is not valid') + } + }) + + after(() => User.deleteMany().then(() => mongoose.disconnect())) +}) \ No newline at end of file diff --git a/staff/agustin-birman/api/logic/user/registerUser.spec.js b/staff/agustin-birman/api/logic/user/registerUser.spec.js index e95f435af..c22ae1c68 100644 --- a/staff/agustin-birman/api/logic/user/registerUser.spec.js +++ b/staff/agustin-birman/api/logic/user/registerUser.spec.js @@ -117,6 +117,18 @@ describe('registerUser', () => { } }) + it('fails on invalid userType', () => { + let errorThrown + try { + registerUser('Mocha', 'Chai', 'Mocha@Chai.com', 'MochaChai', '12345678', '12345678', 12345678) + } catch (error) { + errorThrown = error + } finally { + expect(errorThrown).to.be.instanceOf(ContentError) + expect(errorThrown.message).to.equal('userType is not valid') + } + }) + after(() => User.deleteMany().then(() => mongoose.disconnect())) diff --git a/staff/agustin-birman/api/logic/user/removeStudent.spec.js b/staff/agustin-birman/api/logic/user/removeStudent.spec.js new file mode 100644 index 000000000..86b3595a0 --- /dev/null +++ b/staff/agustin-birman/api/logic/user/removeStudent.spec.js @@ -0,0 +1,115 @@ +import 'dotenv/config' +import mongoose, { Types } from 'mongoose' +import { expect } from 'chai' +import bcrypt from 'bcryptjs' + +import { User } from '../../data/index.js' +import removeStudent from './removeStudent.js' +import { ContentError, NotFoundError } from 'com/errors.js' + +const { MONGODB_URL_TEST } = process.env +const { ObjectId } = Types + +describe('removeStudent', () => { + before(() => mongoose.connect(MONGODB_URL_TEST)) + + beforeEach(() => User.deleteMany()) + + it('succeeds on removing student', () => + bcrypt.hash('12345678', 8) + .then(hash => User.create({ + name: 'Test', + surname: 'User', + email: 'test@user.com', + username: 'testuser', + password: hash, + userType: 'teacher' + }) + .then(studentInfo => User.create({ + name: 'Mocha', + surname: 'Chai', + email: 'Mocha@Chai.com', + username: 'MochaChai', + password: hash, + userType: 'teacher', + student: studentInfo.id + }) + .then(teacher => removeStudent(teacher.id, studentInfo.id) + .then(() => User.findById(teacher.id))))) + .then(teacherInfo => { + expect(teacherInfo.student).to.be.an('array') + expect(teacherInfo.student).to.have.lengthOf(0) + }) + ) + + it('fails on non-existing teacher', () => { + + bcrypt.hash('12345678', 8) + .then(hash => User.create({ + name: 'Test', + surname: 'User', + email: 'test@user.com', + username: 'testuser', + password: hash, + userType: 'teacher' + }) + .then(studentInfo => removeStudent(new ObjectId().toString(), studentInfo.id) + .then(() => User.findById(teacher.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 student', () => { + + bcrypt.hash('12345678', 8) + .then(hash => User.create({ + name: 'Test', + surname: 'User', + email: 'test@user.com', + username: 'testuser', + password: hash, + userType: 'teacher' + }) + .then(teacher => removeStudent(teacher.id, new ObjectId().toString()) + .then(() => User.findById(teacher.id)))) + .catch(error => errorThrown = error) + .finally(() => { + expect(errorThrown).to.be.an.instanceOf(NotFoundError) + expect(errorThrown.message).to.equal('student is not in your list') + }) + }) + + it('fails on invalid userId', () => { + let errorThrown + + try { + removeStudent(12345, new ObjectId().toString()) + } catch (error) { + errorThrown = error + + } finally { + expect(errorThrown).to.be.instanceOf(ContentError) + expect(errorThrown.message).to.equal('userId is not valid') + } + }) + + it('fails on invalid studentId', () => { + let errorThrown + + try { + removeStudent(new ObjectId().toString(), 12345) + } catch (error) { + errorThrown = error + + } finally { + expect(errorThrown).to.be.instanceOf(ContentError) + expect(errorThrown.message).to.equal('studentId is not valid') + } + }) + + + after(() => User.deleteMany().then(() => mongoose.disconnect())) +}) \ No newline at end of file diff --git a/staff/agustin-birman/app/src/App.jsx b/staff/agustin-birman/app/src/App.jsx index 3b3c9c62f..81bc11613 100644 --- a/staff/agustin-birman/app/src/App.jsx +++ b/staff/agustin-birman/app/src/App.jsx @@ -4,6 +4,7 @@ import Register from './views/Register' import Login from './views/Login' import Home from './views/Home' import logic from './logic' +import './global.css' function App() { console.log('App -> render') diff --git a/staff/agustin-birman/app/src/components/core/Button/index.css b/staff/agustin-birman/app/src/components/core/Button/index.css index 9fc39466d..6c646479d 100644 --- a/staff/agustin-birman/app/src/components/core/Button/index.css +++ b/staff/agustin-birman/app/src/components/core/Button/index.css @@ -19,11 +19,3 @@ padding: 0.3rem; } -.hola{ - width: 90%; - height: 15%; - padding: 10px; - border: 1px solid black; - border-radius: 20px; - margin-bottom: 1rem; -} \ No newline at end of file diff --git a/staff/agustin-birman/app/src/components/core/SubmitButton/index.css b/staff/agustin-birman/app/src/components/core/SubmitButton/index.css index d1abcb0ea..e69de29bb 100644 --- a/staff/agustin-birman/app/src/components/core/SubmitButton/index.css +++ b/staff/agustin-birman/app/src/components/core/SubmitButton/index.css @@ -1,3 +0,0 @@ -.Image { - width: 100%; -} \ No newline at end of file diff --git a/staff/agustin-birman/app/src/components/core/SubmitButton/index.jsx b/staff/agustin-birman/app/src/components/core/SubmitButton/index.jsx index 663e65019..d0eb7e7d5 100644 --- a/staff/agustin-birman/app/src/components/core/SubmitButton/index.jsx +++ b/staff/agustin-birman/app/src/components/core/SubmitButton/index.jsx @@ -1,6 +1,5 @@ import Button from '../Button' -import './index.css' function SubmitButton({ children }) { return diff --git a/staff/agustin-birman/app/src/components/core/Text/index.css b/staff/agustin-birman/app/src/components/core/Text/index.css index d1abcb0ea..e69de29bb 100644 --- a/staff/agustin-birman/app/src/components/core/Text/index.css +++ b/staff/agustin-birman/app/src/components/core/Text/index.css @@ -1,3 +0,0 @@ -.Image { - width: 100%; -} \ No newline at end of file diff --git a/staff/agustin-birman/app/src/components/library/Exercises/index.jsx b/staff/agustin-birman/app/src/components/library/Exercises/index.jsx new file mode 100644 index 000000000..401a74702 --- /dev/null +++ b/staff/agustin-birman/app/src/components/library/Exercises/index.jsx @@ -0,0 +1,68 @@ +import { useEffect, useState } from "react" +import View from "../View" +import Heading from "../../core/Heading" +import Text from "../../core/Text" +import Button from "../../core/Button" +import logic from "../../../logic" + +function Exercises({ activityId, onEditButton, updateExercises }) { + const [exercises, setExercises] = useState([]) + + useEffect(() => + loadExercises() + , [updateExercises]) + + const loadExercises = () => { + try { + logic.getExercises(activityId) + .then(data => { + const { exercises } = data + setExercises(exercises) + }) + .catch(error => { + console.error(error) + + alert(error.message) //TODO hacer un alert mejor + }) + } catch (error) { + console.error(error) + + alert(error.message) + } + } + + const handleDeletedExericise = (exerciseId) => { + try { + logic.deleteExercise(exerciseId) + .then(() => loadExercises()) + .catch(error => { + console.error(error) + + alert(error.message) + }) + } catch (error) { + console.error(error) + + alert(error.message) + } + } + + return + List Exercises + {exercises.map(exercise => + + + {exercise.index + 1} + {exercise.sentence} + {exercise.answer} + + + + + + + )} + +} + +export default Exercises \ No newline at end of file diff --git a/staff/agustin-birman/app/src/components/library/MenuItem/index.css b/staff/agustin-birman/app/src/components/library/MenuItem/index.css index be7b5f87a..e69de29bb 100644 --- a/staff/agustin-birman/app/src/components/library/MenuItem/index.css +++ b/staff/agustin-birman/app/src/components/library/MenuItem/index.css @@ -1,7 +0,0 @@ -/* .menuItem { - width: 90%; - margin-top: 10%; - height: 15%; - border: 1px solid black; - border-radius: 10px; -} */ \ No newline at end of file diff --git a/staff/agustin-birman/app/src/global.css b/staff/agustin-birman/app/src/global.css index acd510241..71af91d0b 100644 --- a/staff/agustin-birman/app/src/global.css +++ b/staff/agustin-birman/app/src/global.css @@ -19,3 +19,5 @@ } } + + diff --git a/staff/agustin-birman/app/src/main.jsx b/staff/agustin-birman/app/src/main.jsx index a130dbca3..b286afd19 100644 --- a/staff/agustin-birman/app/src/main.jsx +++ b/staff/agustin-birman/app/src/main.jsx @@ -4,6 +4,7 @@ import App from './App.jsx' import { BrowserRouter as Router } from 'react-router-dom' + ReactDOM.createRoot(document.getElementById('root')).render( // diff --git a/staff/agustin-birman/app/src/views/Login.css b/staff/agustin-birman/app/src/views/Login.css index 52ca26366..dfae11b4b 100644 --- a/staff/agustin-birman/app/src/views/Login.css +++ b/staff/agustin-birman/app/src/views/Login.css @@ -9,12 +9,13 @@ main { height: 100%; } -h1 { +h1{ margin-top: 10%; font-size: 2rem ; font-weight: bold; } + .LoginForm { margin-top: 20%; padding: 1rem; diff --git a/staff/agustin-birman/app/src/views/Login.jsx b/staff/agustin-birman/app/src/views/Login.jsx index bdae639fa..62b1d3249 100644 --- a/staff/agustin-birman/app/src/views/Login.jsx +++ b/staff/agustin-birman/app/src/views/Login.jsx @@ -1,7 +1,6 @@ import { useState } from 'react' import logic from '../logic' -import '../global.css' import './Login.css' import Field from '../components/core/Field' diff --git a/staff/agustin-birman/app/src/views/Register.jsx b/staff/agustin-birman/app/src/views/Register.jsx index a3343dd61..b0035550d 100644 --- a/staff/agustin-birman/app/src/views/Register.jsx +++ b/staff/agustin-birman/app/src/views/Register.jsx @@ -1,4 +1,3 @@ -import '../global.css' import logic from '../logic' import './Register.css' diff --git a/staff/agustin-birman/app/src/views/components/AddStudent/index.jsx b/staff/agustin-birman/app/src/views/components/AddStudent/index.jsx index 3fa90dae9..80df04d45 100644 --- a/staff/agustin-birman/app/src/views/components/AddStudent/index.jsx +++ b/staff/agustin-birman/app/src/views/components/AddStudent/index.jsx @@ -2,13 +2,14 @@ import logic from '../../../logic' import View from '../../../components/library/View' import Heading from '../../../components/core/Heading' import Text from '../../../components/core/Text' -import { useParams } from 'react-router-dom' +import { useNavigate, useParams } from 'react-router-dom' import { useEffect, useState } from 'react' import Button from '../../../components/core/Button' function AddStudent() { const [userInfo, setUserInfo] = useState('') const { userInfoId } = useParams() + const navigate = useNavigate() useEffect(() => { getUserInfo() @@ -33,7 +34,7 @@ function AddStudent() { const handleAddStudent = () => { try { logic.addStudent(userInfoId) - .then(user => setUserInfo(user)) + .then(() => navigate('/users/students')) .catch(error => { console.error(error) diff --git a/staff/agustin-birman/app/src/views/components/CompleteSentence/index.jsx b/staff/agustin-birman/app/src/views/components/CompleteSentence/index.jsx index 49b2dbc41..add06d77d 100644 --- a/staff/agustin-birman/app/src/views/components/CompleteSentence/index.jsx +++ b/staff/agustin-birman/app/src/views/components/CompleteSentence/index.jsx @@ -12,23 +12,20 @@ import Input from '../../../components/core/Input' import logic from '../../../logic' import View from '../../../components/library/View' +import Exercises from '../../../components/library/Exercises' const ANSWER_REGEX = /\(([^)]+)\)/ function CreateCompleteSentence() { const [message, setMessage] = useState('') const [answerInput, setAnswerInput] = useState('Not answer found') - const [exercises, setExercises] = useState([]) const [editView, setEditView] = useState(false) const [sentence, setSentence] = useState('') const [selectedExercise, setSelectedExercise] = useState(null) + const [updateExercises, setUpdateExercises] = useState(0) const { activityId } = useParams() - useEffect(() => - loadExercises() - , []) - useEffect(() => { if (sentence) { const removeAnswer = sentence.match(ANSWER_REGEX) @@ -41,46 +38,11 @@ function CreateCompleteSentence() { } , [editView]) - const loadExercises = () => { - try { - logic.getExercises(activityId) - .then(data => { - const { exercises } = data - setExercises(exercises) - }) - .catch(error => { - console.error(error) - - alert(error.message) //TODO hacer un alert mejor - }) - } catch (error) { - console.error(error) - - alert(error.message) - } - } - - const handleDeletedExericise = (exerciseId) => { - try { - logic.deleteExercise(exerciseId) - .then(() => loadExercises()) - .catch(error => { - console.error(error) - - alert(error.message) - }) - } catch (error) { - console.error(error) - - alert(error.message) - } - } - const handleEditedExercise = () => { try { - logic.editExercise(selectedExercise._id, sentence) + logic.editExercise(selectedExercise.id, sentence) .then(() => { - loadExercises() + setUpdateExercises(prev => prev + 1) setEditView(false) }) .catch(error => { @@ -105,7 +67,7 @@ function CreateCompleteSentence() { .then(() => { form.reset() setAnswerInput('Answer not found') - loadExercises() + setUpdateExercises(prev => prev + 1) }) .catch(error => { console.error(error) @@ -142,9 +104,10 @@ function CreateCompleteSentence() { -Complete the sentence {editView === false - ? {(exercises && exercises.length > 0 && exercises[exercises.length - 1].index !== undefined) - ? exercises[exercises.length - 1].index + 2 - : 1}. Sentence + // ? {(exercises && exercises.length > 0 && exercises[exercises.length - 1].index !== undefined) + // ? exercises[exercises.length - 1].index + 2 + // : 1}. Sentence + ? Sentence : <>{selectedExercise.index + 1}. Sentence { setSentence(e.target.value); handleAnswerInput(e) }} />} -Answer @@ -166,24 +129,7 @@ function CreateCompleteSentence() { } - {editView === false && <> - List Exercises - - {exercises.map(exercise => - //TODO modificar el id - - - {exercise.index + 1} - {exercise.sentence} - {exercise.answer} - - - - - - - )} - + {editView === false && <> } diff --git a/staff/agustin-birman/app/src/views/components/CompleteSentenceInfo/index.css b/staff/agustin-birman/app/src/views/components/CompleteSentenceInfo/index.css index b804d15a4..e69de29bb 100644 --- a/staff/agustin-birman/app/src/views/components/CompleteSentenceInfo/index.css +++ b/staff/agustin-birman/app/src/views/components/CompleteSentenceInfo/index.css @@ -1,3 +0,0 @@ -.Text{ - width: 90%; -} \ No newline at end of file diff --git a/staff/agustin-birman/app/src/views/components/DoActivity/index.jsx b/staff/agustin-birman/app/src/views/components/DoActivity/index.jsx index 555b8feff..3d6176546 100644 --- a/staff/agustin-birman/app/src/views/components/DoActivity/index.jsx +++ b/staff/agustin-birman/app/src/views/components/DoActivity/index.jsx @@ -106,8 +106,8 @@ function DoActivity() { {isLastPage === false - ? - : } + ? + : } {message} Page {currentPage} of {Math.ceil(exercises.length / pageSize)} diff --git a/staff/agustin-birman/app/src/views/components/ListActivities/index.jsx b/staff/agustin-birman/app/src/views/components/ListActivities/index.jsx index 3930e8332..f837b8d49 100644 --- a/staff/agustin-birman/app/src/views/components/ListActivities/index.jsx +++ b/staff/agustin-birman/app/src/views/components/ListActivities/index.jsx @@ -35,9 +35,8 @@ function ListActivities() { Activities List
{activities.map(activity => - //TODO modificar el id -
- +
+ kind activity {activity.title} diff --git a/staff/agustin-birman/app/src/views/components/ListExercises/index.jsx b/staff/agustin-birman/app/src/views/components/ListExercises/index.jsx index 9fbd14ed7..84872d147 100644 --- a/staff/agustin-birman/app/src/views/components/ListExercises/index.jsx +++ b/staff/agustin-birman/app/src/views/components/ListExercises/index.jsx @@ -36,28 +36,12 @@ function ListExercises() { } } - const handleExerciseDeleted = (exerciseId) => { - try { - logic.deleteExercise(exerciseId) - .then(() => { loadExercises() }) - .catch(error => { - console.error(error) - - alert(error.message) - }) - } catch (error) { - console.error(error) - - alert(error.message) - } - } - return Exercises List {exercises.map(exercise => //TODO modificar el id - + {exercise.index + 1} {exercise.sentence} diff --git a/staff/agustin-birman/app/src/views/components/ListStudents/index.jsx b/staff/agustin-birman/app/src/views/components/ListStudents/index.jsx index 77a14ced3..f2139bdee 100644 --- a/staff/agustin-birman/app/src/views/components/ListStudents/index.jsx +++ b/staff/agustin-birman/app/src/views/components/ListStudents/index.jsx @@ -49,17 +49,16 @@ function ListStudents() { } - console.log(students) return Students List {students.map(student => //TODO modificar el id - + {student.name} {student.surname} - + diff --git a/staff/agustin-birman/app/src/views/components/MenuItems/index.css b/staff/agustin-birman/app/src/views/components/MenuItems/index.css index e69de29bb..5e8294737 100644 --- a/staff/agustin-birman/app/src/views/components/MenuItems/index.css +++ b/staff/agustin-birman/app/src/views/components/MenuItems/index.css @@ -0,0 +1,8 @@ +.MenuItem{ + width: 90%; + height: 15%; + padding: 10px; + border: 1px solid black; + border-radius: 20px; + margin-bottom: 1rem; +} \ No newline at end of file diff --git a/staff/agustin-birman/app/src/views/components/MenuItems/index.jsx b/staff/agustin-birman/app/src/views/components/MenuItems/index.jsx index dd0f93b35..ec677910d 100644 --- a/staff/agustin-birman/app/src/views/components/MenuItems/index.jsx +++ b/staff/agustin-birman/app/src/views/components/MenuItems/index.jsx @@ -1,16 +1,17 @@ import Button from '../../../components/core/Button' import View from '../../../components/library/View' import { Link } from 'react-router-dom' +import './index.css' import extractPayloadFromJWT from '../../../utils/extractPayloadFromJWT' function MenuItem() { const { sub: userId } = extractPayloadFromJWT(localStorage.token) return - - - - + + + + diff --git a/staff/agustin-birman/app/src/views/components/ShowExerciseResults/index.jsx b/staff/agustin-birman/app/src/views/components/ShowExerciseResults/index.jsx index 384307701..f64c53235 100644 --- a/staff/agustin-birman/app/src/views/components/ShowExerciseResults/index.jsx +++ b/staff/agustin-birman/app/src/views/components/ShowExerciseResults/index.jsx @@ -23,7 +23,7 @@ function ShowExerciseResults() { .then(data => { const { exercises } = data const promises = exercises.map(exercise => - logic.getAnswers(exercise._id) + logic.getAnswers(exercise.id) .then(answers => ({ ...exercise, answers @@ -57,7 +57,7 @@ function ShowExerciseResults() { Results {exercisesWithAnswers.map(exercise => ( - + {exercise.index + 1} {exercise.sentence}