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.
continue with the test and fix them b00tc4mp#140
- Loading branch information
1 parent
3dbfcae
commit 3b6693a
Showing
8 changed files
with
204 additions
and
53 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
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
105 changes: 105 additions & 0 deletions
105
staff/adrian-martin/socialcode/api/logic/getUserName.spec.js
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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import "dotenv/config" | ||
import mongoose, { get, Types } from "mongoose" | ||
|
||
import bcrypt from "bcryptjs" | ||
|
||
|
||
import getUserName from "./getUserName.js" | ||
import { User } from "../data/index.js" | ||
import { NotFoundError, ContentError } from "com/error.js" | ||
|
||
import { expect } from "chai" | ||
|
||
const { MONGODB_URL_TEST } = process.env | ||
|
||
const { ObjectId } = Types | ||
|
||
// npm run test-inspect | ||
|
||
describe('getUserName', () => { | ||
before(() => mongoose.connect(MONGODB_URL_TEST).then(() => User.deleteMany())) | ||
beforeEach(() => User.deleteMany()) | ||
|
||
it('succeeds get username from existing user', () => | ||
bcrypt.hash('123132123', 8) | ||
.then(hash => Promise.all([User.create({ | ||
name: "Mocha", | ||
surname: "Chai", | ||
email: "[email protected]", | ||
username: "MochaChai", | ||
password: hash | ||
}), User.create({ | ||
name: "Test", | ||
surname: "User", | ||
email: "[email protected]", | ||
username: "testuser", | ||
password: hash | ||
})])) | ||
.then(([user, targetUser]) => getUserName(user.id, targetUser.id)) | ||
.then(name => { | ||
expect(name).to.be.a.string | ||
expect(name).to.be.equal('Test') | ||
}) | ||
|
||
) | ||
|
||
it("fails on non-existing user", () => { | ||
let errorThrown | ||
|
||
return bcrypt.hash("321321321", 8) | ||
.then(hash => User.create({ | ||
name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'machachai', | ||
password: hash | ||
})) | ||
.then(targetUserId => getUserName(new ObjectId().toString(), targetUserId.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('123456789', 8) | ||
.then(hash => User.create({ | ||
name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'machachai', | ||
password: hash | ||
})) | ||
.then(user => getUserName(user.id, new ObjectId().toString())) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.instanceOf(NotFoundError) | ||
expect(errorThrown.message).to.equal('TargetUser not found') | ||
}) | ||
}) | ||
|
||
// it('fails on invalid userId', () => { | ||
// let errorThrown | ||
|
||
// try { | ||
// getUserName(123456789, 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 targetUserid', () => { | ||
// let errorThrown | ||
|
||
// try { | ||
// getUserName(new ObjectId().toString(), 123456789) | ||
// } catch (error) { | ||
// errorThrown = error | ||
// } finally { | ||
// expect(errorThrown).to.be.instanceOf(ContentError) | ||
// expect(errorThrown.message).to.equal('targetUserId is not valid') | ||
// } | ||
// }) | ||
|
||
after(() => User.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
62 changes: 62 additions & 0 deletions
62
staff/adrian-martin/socialcode/api/logic/registerUser.spec.js
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import 'dotenv/config' | ||
import registerUser from './registerUser.js' | ||
import mongoose from 'mongoose' | ||
import bcrypt from 'bcryptjs' | ||
|
||
import { expect } from 'chai' | ||
|
||
import { User } from '../data/index.js' | ||
import { ContentError, CredentialError, DuplicityError } from 'com/error.js' | ||
|
||
const { MONGODB_URL_TEST } = process.env | ||
|
||
debugger // npm run test-inspect | ||
|
||
describe('registerUser', () => { | ||
before(() => mongoose.connect(MONGODB_URL_TEST).then(() => User.deleteMany())) | ||
beforeEach(() => User.deleteMany()) | ||
|
||
it('succceds on register user', () => { | ||
registerUser('Esme', 'Ralda', '[email protected]', 'esmeralda', '123123123', '123123123') | ||
.then(() => User.findOne()) | ||
.then(user => { | ||
expect(user.name).to.equal('Esme') | ||
expect(user.surname).to.equal('Ralda') | ||
expect(user.email).to.equal('[email protected]') | ||
expect(user.username).to.equal('esmeralda') | ||
expect(user.password).to.equal('123123123') | ||
|
||
return bcrypt.compare('123123123', user.password) | ||
}) | ||
.then((match) => expect(match).to.be.true) | ||
}) | ||
|
||
it('fails on existing user', () => { | ||
let errorThrown | ||
|
||
return bcrypt.hash('1234', 8) | ||
.then(hash => User.create({ name: 'Esme', surname: 'Ralda', email: '[email protected]', username: 'esmeralda', password: hash })) | ||
.then(() => registerUser('Esme', 'Ralda', '[email protected]', 'esmeralda', '1234', '1234')) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.instanceOf(DuplicityError) | ||
expect(errorThrown.message).to.equal('user already exists') | ||
}) | ||
}) | ||
|
||
it("fails on existing user", () => { | ||
let errorThrown | ||
|
||
return bcrypt.hash("1234", 8) | ||
.then(hash => User.create({ name: "Mocha", surname: "Chai", email: "[email protected]", username: "MochaChai", password: hash })) | ||
.then(() => registerUser("Mocha", "Chai", "[email protected]", "MochaChai", "1234", "1234")) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.instanceOf(DuplicityError) | ||
expect(errorThrown.message).to.equal("❌ Users already exists ❌") | ||
}) | ||
}) | ||
after(() => User.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