forked from b00tc4mp/isdi-parttime-202403
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new spec(getUserInfo, removeStudent)and refactor old spec and R…
…eact components b00tc4mp#182
- Loading branch information
1 parent
13b635d
commit f29ec20
Showing
40 changed files
with
583 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: '[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) | ||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
|
@@ -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) | ||
|
@@ -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())) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', () => { | ||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: '[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 { | ||
|
@@ -89,7 +116,7 @@ describe('deleteAnswers', () => { | |
} | ||
}) | ||
|
||
it('fails on invalid userId', () => { | ||
it('fails on invalid activityId', () => { | ||
let errorThrown | ||
try { | ||
deleteAnswers(new ObjectId().toString(), 123) | ||
|
Oops, something went wrong.