Skip to content

Commit

Permalink
add spec to api logic b00tc4mp#182
Browse files Browse the repository at this point in the history
  • Loading branch information
AgusBirman committed Aug 8, 2024
1 parent b1cc548 commit 7be8431
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 18 deletions.
1 change: 1 addition & 0 deletions staff/agustin-birman/api/.env
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
8 changes: 4 additions & 4 deletions staff/agustin-birman/api/logic/authenticateUser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
})
}
)
Expand All @@ -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(() => {
Expand Down
61 changes: 60 additions & 1 deletion staff/agustin-birman/api/logic/createActivity.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dotenv/config'
import mongoose from 'mongoose'
import mongoose, { Types } from 'mongoose'
import bcrypt from 'bcryptjs'

import { expect } from 'chai'
Expand All @@ -11,6 +11,8 @@ import createActivity from './createActivity.js'

const { MONGODB_URL_TEST } = process.env

const { ObjectId } = Types

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

Expand All @@ -28,4 +30,61 @@ describe('createActivity', () => {
})
)
)

it('fails on non-existing user', () => {
let errorThrown


createActivity(new ObjectId().toString(), 'title', 'description')
.catch(error => errorThrown = error)
.finally(() => {
expect(errorThrown).to.be.an.instanceOf(NotFoundError)
expect(errorThrown.message).to.equal('user not found')
})
})

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

try {
createActivity(12345, 'title', 'description')
} catch (error) {
errorThrown = error

} finally {
expect(errorThrown).to.be.instanceOf(ContentError)
expect(errorThrown.message).to.equal('userId is not valid')
}
})

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

try {
createActivity(new ObjectId().toString(), 1234, 'description')
} 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

try {
createActivity(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(() => User.deleteMany().then(() => mongoose.disconnect()))
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NotFoundError, SystemError } from 'com/errors.js'

const ANSWER_REGEX = /\(([^)]+)\)/;

const createExercise = (userId, activityId, sentence) => {
const createCompleteSentenceExercise = (userId, activityId, sentence) => {
validate.id(userId, 'userId')
validate.id(activityId, 'activityId')
validate.text(sentence, 'sentence', 200)
Expand Down Expand Up @@ -49,4 +49,4 @@ const createExercise = (userId, activityId, sentence) => {

}

export default createExercise
export default createCompleteSentenceExercise
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()))
})
65 changes: 65 additions & 0 deletions staff/agustin-birman/api/logic/deleteActivity.spec.js
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()))
})
69 changes: 69 additions & 0 deletions staff/agustin-birman/api/logic/deleteExercise.spec.js
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()))
})
35 changes: 35 additions & 0 deletions staff/agustin-birman/api/logic/editActivity.spec.js
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()))
})

Loading

0 comments on commit 7be8431

Please sign in to comment.