Skip to content

Commit

Permalink
added new spec(getUserInfo, removeStudent)and refactor old spec and R…
Browse files Browse the repository at this point in the history
…eact components b00tc4mp#182
  • Loading branch information
AgusBirman committed Aug 18, 2024
1 parent 13b635d commit f29ec20
Show file tree
Hide file tree
Showing 40 changed files with 583 additions and 304 deletions.
37 changes: 32 additions & 5 deletions 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 { ContentError, NotFoundError } from 'com/errors.js'
import { ContentError, MatchError, NotFoundError } from 'com/errors.js'

const { MONGODB_URL_TEST } = process.env

Expand All @@ -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: '[email protected]',
username: 'MochaChai',
password: hash,
userType: 'teacher'
}), User.create({
name: 'Test',
surname: 'User',
email: '[email protected]',
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: '[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)
.then(activity => deleteActivity(new ObjectId().toString(), activity.id))
.catch(error => errorThrown = error)
.finally(() => {
expect(errorThrown).to.be.an.instanceOf(NotFoundError)
Expand All @@ -64,8 +92,7 @@ describe('deleteActivity', () => {
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())
.then(() => deleteActivity(user.id, new ObjectId().toString()))
.catch(error => errorThrown = error)
.finally(() => {
expect(errorThrown).to.be.an.instanceOf(NotFoundError)
Expand Down
47 changes: 19 additions & 28 deletions staff/agustin-birman/api/logic/activity/editActivity.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ describe('editActivity', () => {
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(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')
Expand Down Expand Up @@ -53,8 +51,7 @@ describe('editActivity', () => {
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(() => 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)
Expand Down Expand Up @@ -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: '[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, 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: '[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, '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()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions staff/agustin-birman/api/logic/activity/getActivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 7 additions & 9 deletions staff/agustin-birman/api/logic/activity/getActivity.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ describe('getActivity', () => {
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 }) => 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', () => {
Expand All @@ -50,8 +49,7 @@ describe('getActivity', () => {
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(() => 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)
Expand Down
10 changes: 6 additions & 4 deletions staff/agustin-birman/api/logic/answer/deleteAnswers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 29 additions & 2 deletions staff/agustin-birman/api/logic/answer/deleteAnswers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { MONGODB_URL_TEST } = process.env

const { ObjectId } = Types

debugger

describe('deleteAnswers', () => {
before(() => mongoose.connect(MONGODB_URL_TEST))
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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: '[email protected]', 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: '[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(() => 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 {
Expand All @@ -89,7 +116,7 @@ describe('deleteAnswers', () => {
}
})

it('fails on invalid userId', () => {
it('fails on invalid activityId', () => {
let errorThrown
try {
deleteAnswers(new ObjectId().toString(), 123)
Expand Down
Loading

0 comments on commit f29ec20

Please sign in to comment.