Skip to content

Commit

Permalink
add new specs(getAnswers, addStudent, getStudents) and fixed anothers…
Browse files Browse the repository at this point in the history
… specs b00tc4mp#182
  • Loading branch information
AgusBirman committed Aug 18, 2024
1 parent 0e993a2 commit 13b635d
Show file tree
Hide file tree
Showing 41 changed files with 742 additions and 84 deletions.
3 changes: 2 additions & 1 deletion staff/agustin-birman/api/data/Answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ const answer = new Schema({

const Answer = model('Answer', answer)

export default Answer
export default Answer

2 changes: 1 addition & 1 deletion staff/agustin-birman/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ mongoose.connect(MONGODB_URL)

api.patch('/users', jsonBodyParser, addStudentHandler)

api.delete('users/:studentId', removeStudentHandler)
api.delete('/users/:studentId', removeStudentHandler)

api.post('/activity', jsonBodyParser, createActivityHandler)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import bcrypt from 'bcryptjs'
import { expect } from 'chai'

import { User, Activity } from '../../data/index.js'
import { ContentError, CredentialsError } from 'com/errors.js'
import { ContentError } from 'com/errors.js'

import createActivity from './createActivity.js'

Expand Down Expand Up @@ -34,7 +34,6 @@ describe('createActivity', () => {
it('fails on non-existing user', () => {
let errorThrown


createActivity(new ObjectId().toString(), 'title', 'description')
.catch(error => errorThrown = error)
.finally(() => {
Expand Down
8 changes: 6 additions & 2 deletions staff/agustin-birman/api/logic/activity/deleteActivity.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import validate from 'com/validate.js';
import { Activity, Exercise, User } from '../../data/index.js';
import { Activity, Answer, Exercise, User } from '../../data/index.js';
import { MatchError, NotFoundError, SystemError } from 'com/errors.js';
import { Types } from 'mongoose';

Expand Down Expand Up @@ -30,7 +30,11 @@ const deleteActivity = (userId, activityId) => {

return Exercise.deleteMany({ activity: new ObjectId(activityId) })
.catch(error => { throw new SystemError(error.message) })
.then(() => { })
.then(() => {
return Answer.deleteMany({ activity: new ObjectId(activityId) })
.catch(error => { throw new SystemError(error.message) })
.then(() => { })
})
})
})
})
Expand Down
28 changes: 27 additions & 1 deletion staff/agustin-birman/api/logic/activity/deleteActivity.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { expect } from 'chai'
import { User, Activity } from '../../data/index.js'

import deleteActivity from './deleteActivity.js'
import { NotFoundError } from 'com/errors.js'
import { ContentError, NotFoundError } from 'com/errors.js'

const { MONGODB_URL_TEST } = process.env

Expand Down Expand Up @@ -46,6 +46,18 @@ describe('deleteActivity', () => {
}))
})

it('fails on invalid user', () => {
let errorThrown
try {
deleteActivity(123, 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 non-existing activity', () => {
let errorThrown

Expand All @@ -61,5 +73,19 @@ describe('deleteActivity', () => {
}))
})

it('fails on invalid activity', () => {
let errorThrown
try {
deleteActivity(new ObjectId().toString(), 123)
} catch (error) {
errorThrown = error
} finally {
expect(errorThrown).to.be.instanceOf(ContentError)
expect(errorThrown.message).to.equal('activityId is not valid')
}
})



after(() => Promise.all([User.deleteMany(), Activity.deleteMany()]).then(() => mongoose.disconnect()))
})
24 changes: 24 additions & 0 deletions staff/agustin-birman/api/logic/activity/editActivity.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ describe('editActivity', () => {
})
})

it('fails on invalid userId', () => {
let errorThrown
try {
editActivity(123, new ObjectId().toString(), 'title2', 'description2')
} catch (error) {
errorThrown = error
} finally {
expect(errorThrown).to.be.instanceOf(ContentError)
expect(errorThrown.message).to.equal('userId is not valid')
}
})

it('fails on invalid activityId', () => {
let errorThrown
try {
editActivity(new ObjectId().toString(), 123, 'title2', 'description2')
} catch (error) {
errorThrown = error
} finally {
expect(errorThrown).to.be.instanceOf(ContentError)
expect(errorThrown.message).to.equal('activityId is not valid')
}
})

it('fails on invalid title', () => {
let errorThrown

Expand Down
10 changes: 9 additions & 1 deletion staff/agustin-birman/api/logic/activity/getActivities.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ const getActivities = (userId) => {

return Activity.find({ teacher: userId })
.catch(error => { throw new SystemError(error.message) })
.then(activities => activities)
.then(activities => {
const transformedActivities = activities.map(activity => {
const activityObj = activity.toObject()
activityObj.id = activityObj._id
delete activityObj._id
return activityObj
})
return transformedActivities
})
})
}

Expand Down
13 changes: 12 additions & 1 deletion staff/agustin-birman/api/logic/activity/getActivities.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { expect } from 'chai'

import { User, Activity } from '../../data/index.js'
import getActivities from './getActivities.js'
import { NotFoundError } from 'com/errors.js'
import { ContentError, NotFoundError } from 'com/errors.js'

const { ObjectId } = Types

Expand Down Expand Up @@ -56,6 +56,17 @@ describe('getActivities', () => {
})
})

it('fails on invalid userId', () => {
let errorThrown
try {
getActivities(123)
} catch (error) {
errorThrown = error
} finally {
expect(errorThrown).to.be.instanceOf(ContentError)
expect(errorThrown.message).to.equal('userId is not valid')
}
})

after(() => Promise.all([User.deleteMany(), Activity.deleteMany()]).then(() => mongoose.disconnect()))
})
8 changes: 5 additions & 3 deletions staff/agustin-birman/api/logic/activity/getActivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Activity, User } from '../../data/index.js'
import { NotFoundError, SystemError } from 'com/errors.js'

const getActivity = (userId, activityId) => {
validate.id(userId, 'userid')
validate.id(userId, 'userId')
validate.id(activityId, 'activityId')

return User.findById(userId).lean()
Expand All @@ -17,8 +17,10 @@ const getActivity = (userId, activityId) => {
.then(activity => {
if (!activity)
throw new NotFoundError('activity not found')

return activity
const activityObj = activity.toObject()
activityObj.id = activityObj._id
delete activityObj._id
return activityObj
})
})
}
Expand Down
27 changes: 26 additions & 1 deletion staff/agustin-birman/api/logic/activity/getActivity.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { expect } from 'chai'

import { User, Activity } from '../../data/index.js'
import getActivity from './getActivity.js'
import { NotFoundError } from 'com/errors.js'
import { ContentError, NotFoundError } from 'com/errors.js'

const { ObjectId } = Types

Expand Down Expand Up @@ -59,6 +59,31 @@ describe('getActivity', () => {
})
})

it('fails on invalid userId', () => {
let errorThrown
try {
getActivity(123, 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 activityId', () => {
let errorThrown
try {
getActivity(new ObjectId().toString(), 123)
} catch (error) {
errorThrown = error
} finally {
expect(errorThrown).to.be.instanceOf(ContentError)
expect(errorThrown.message).to.equal('activityId is not valid')
}
})



after(() => Promise.all([User.deleteMany(), Activity.deleteMany()]).then(() => mongoose.disconnect()))
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { MONGODB_URL } = process.env
mongoose.connect(MONGODB_URL)
.then(() => {
try {
getActivity('66a94dcb34505782bcd8cfd0', '66afa3a0a5ccc42af55a6f25')
getActivity('66a94dcb34505782bcd8cfd0', '66c1fbba04735a9cfdd94859')
.then(activities => console.log('activity retrieved', activities))
.catch(error => console.error(error))
} catch (error) {
Expand Down
103 changes: 102 additions & 1 deletion staff/agustin-birman/api/logic/answer/deleteAnswers.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,104 @@
import 'dotenv/config'
import bcrypt from 'bcryptjs'
import mongoose, { Types } from 'mongoose'
import deleteAnswers from './deleteAnswers.js'
import { expect } from 'chai'
import { User, Activity, Exercise, Answer } from '../../data/index.js'
import { ContentError, NotFoundError } from 'com/errors.js'
const { MONGODB_URL_TEST } = process.env

const { ObjectId } = Types


describe('deleteAnswers', () => {
before
before(() => mongoose.connect(MONGODB_URL_TEST))

beforeEach(() => Promise.all([User.deleteMany(), Activity.deleteMany(), Exercise.deleteMany(), Answer.deleteMany()]))

// it('suceeds on deleting answers', () => {
// 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(exercise1 => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 })
// .then(exercise2 => ({ user, activity, exercise1, exercise2 }))))
// .then(({ user, activity, exercise1, exercise2 }) => Answer.create({ student: user.id, exercise: exercise1.id, activity: activity.id, answer: 'hat' })
// .then(answer1 => Answer.create({ student: user.id, exercise: exercise2.id, activity: activity.id, answer: 'haben' })
// .then(answer2 => ({ user, activity, exercise1, exercise2, answer1, answer2 }))))
// .then(({ user, activity, exercise1, exercise2, answer1, answer2 }) => console.log(answer1, answer2))
// })

it('suceeds on deleting answers', () => {
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 => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 })
.then(exercise1 => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 })
.then(exercise2 => Answer.create({ student: user.id, exercise: exercise1.id, activity: activity.id, answer: 'hat' })
.then(answer1 => Answer.create({ student: user.id, exercise: exercise2.id, activity: activity.id, answer: 'haben' })
.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', () => {
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 => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 })
.then(exercise1 => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 })
.then(exercise2 => Answer.create({ student: user.id, exercise: exercise1.id, activity: activity.id, answer: 'hat' })
.then(answer1 => Answer.create({ student: user.id, exercise: exercise2.id, activity: activity.id, answer: 'haben' })
.then(answer2 => deleteAnswers(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 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 => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 })
.then(exercise1 => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 })
.then(exercise2 => Answer.create({ student: user.id, exercise: exercise1.id, activity: activity.id, answer: 'hat' })
.then(answer1 => Answer.create({ student: user.id, exercise: exercise2.id, activity: activity.id, answer: 'haben' })
.then(answer2 => deleteAnswers(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')
}))))))
})

it('fails on invalid userId', () => {
let errorThrown
try {
deleteAnswers(123, 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 userId', () => {
let errorThrown
try {
deleteAnswers(new ObjectId().toString(), 123)
} catch (error) {
errorThrown = error
} finally {
expect(errorThrown).to.be.instanceOf(ContentError)
expect(errorThrown.message).to.equal('activityId is not valid')
}
})
after(() => () => Promise.all([User.deleteMany(), Activity.deleteMany(), Exercise.deleteMany(), Answer.deleteMany()]).then(() => mongoose.disconnect()))
})
11 changes: 6 additions & 5 deletions staff/agustin-birman/api/logic/answer/getAnswers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ const getAnswers = (userId, exerciseId) => {
return Answer.find({ exercise: new ObjectId(exerciseId) })
.catch(error => { throw new SystemError(error.message) })
.then(answers => {
answers.id = answers._id

delete answers._id

return answers
return answers.map(answer => {
const answerObj = answer.toObject()
answerObj.id = answerObj._id
delete answerObj._id
return answerObj
})
})
})
})
Expand Down
Loading

0 comments on commit 13b635d

Please sign in to comment.