forked from b00tc4mp/isdi-parttime-202309
-
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.
add register, authenticate and retrieve spec tests b00tc4mp#406
- Loading branch information
Showing
12 changed files
with
201 additions
and
10 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
61 changes: 61 additions & 0 deletions
61
staff/miguel-arias/project/api/logic/authenticateHome.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,61 @@ | ||
import mongoose from 'mongoose' | ||
import { expect } from 'chai' | ||
import bcrypt from 'bcryptjs' | ||
|
||
import random from './helpers/random.js' | ||
import { Home } from '../data/models.js' | ||
|
||
import { errors } from 'com' | ||
import authenticateHome from './authenticateHome.js' | ||
const { NotFoundError, CredentialsError } = errors | ||
|
||
describe('authenticateHome', () => { | ||
before(async () => await mongoose.connect('mongodb://127.0.0.1:27017/test')) | ||
|
||
beforeEach(async () => await Home.deleteMany()) | ||
|
||
it('succeeds on correct credentials', async () => { | ||
const name = random.name() | ||
const email = random.email() | ||
const password = random.password() | ||
|
||
const hash = await bcrypt.hash(password, 8) | ||
|
||
const home = await Home.create({ name, email, password: hash }) | ||
|
||
const homeId = await authenticateHome(email, password) | ||
|
||
expect(homeId).to.be.a('string') | ||
expect(homeId).to.have.lengthOf(24) | ||
expect(homeId).to.equal(home.id) | ||
}) | ||
|
||
it('fails on non correct email', async () => { | ||
try { | ||
await authenticateHome(random.email(), random.password()) | ||
|
||
throw new Error('should not reach this point') | ||
} catch (error) { | ||
expect(error).to.be.instanceOf(NotFoundError) | ||
expect(error.message).to.equal('home not found') | ||
} | ||
}) | ||
|
||
it('fails on wrong password', async () => { | ||
const name = random.name() | ||
const email = random.email() | ||
const password = random.password() | ||
|
||
await Home.create({ name, email, password }) | ||
debugger | ||
try { | ||
await authenticateHome(email, password + '-wrong') | ||
throw new Error('should not reach this point') | ||
} catch (error) { | ||
expect(error).to.be.instanceOf(CredentialsError) | ||
expect(error.message).to.equal('wrong password') | ||
} | ||
}) | ||
|
||
after(async () => await mongoose.disconnect('mongodb://127.0.0.1:27017/test')) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import mongoose from 'mongoose' | ||
|
||
function name() { | ||
return `name-${Math.random()}` | ||
} | ||
function email() { | ||
return `email-${Math.random()}@mail.com` | ||
} | ||
function password() { | ||
return `password-${Math.random()}` | ||
} | ||
|
||
const { ObjectId } = mongoose.Types | ||
|
||
function id() { | ||
return new ObjectId().toString() | ||
} | ||
|
||
function image() { | ||
return `image-${Math.random()}` | ||
} | ||
|
||
function text() { | ||
return `text-${Math.random()}` | ||
} | ||
|
||
const random = { | ||
name, | ||
email, | ||
password, | ||
id, | ||
image, | ||
text | ||
} | ||
|
||
export default random |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import mongoose from 'mongoose' | ||
import { expect } from 'chai' | ||
import bcrypt from 'bcryptjs' | ||
|
||
import { Home } from '../data/models.js' | ||
import registerHome from './registerHome.js' | ||
|
||
import random from './helpers/random.js' | ||
import { errors } from 'com' | ||
const { DuplicityError } = errors | ||
|
||
describe('registerHome', () => { | ||
before(async () => await mongoose.connect('mongodb://127.0.0.1:27017/test')) | ||
|
||
beforeEach(async () => await Home.deleteMany()) | ||
|
||
it('succeeds on correct credentials', async () => { | ||
const name = random.name() | ||
const email = random.email() | ||
const password = random.password() | ||
|
||
await registerHome(name, email, password) | ||
|
||
const home = await Home.findOne({ email }) | ||
|
||
expect(home).to.exist | ||
expect(home.name).to.equal(name) | ||
expect(home.email).to.equal(email) | ||
|
||
const match = await bcrypt.compare(password, home.password) | ||
|
||
expect(match).to.be.true | ||
}) | ||
|
||
it('fails on already existing home', async () => { | ||
const name = random.name() | ||
const email = random.email() | ||
const password = random.password() | ||
|
||
await Home.create({ name, email, password }) | ||
|
||
try { | ||
await registerHome(name, email, password) | ||
throw new Error('should not reach this point') | ||
} catch (error) { | ||
expect(error).to.be.instanceOf(DuplicityError) | ||
expect(error.message).to.equal('home already exists') | ||
} | ||
}) | ||
|
||
after(async () => await 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import mongoose from 'mongoose' | ||
import { expect } from 'chai' | ||
|
||
import random from './helpers/random.js' | ||
import retrieveHome from './retrieveHome.js' | ||
import { Home } from '../data/models.js' | ||
import { errors } from 'com' | ||
const { NotFoundError } = errors | ||
|
||
describe('retrieveHome', () => { | ||
before(async () => await mongoose.connect('mongodb://127.0.0.1:27017/test')) | ||
|
||
beforeEach(async () => await Home.deleteMany()) | ||
|
||
it('succeeds on existing home', async () => { | ||
const name = random.name() | ||
const email = random.email() | ||
const password = random.password() | ||
|
||
const home = await Home.create({ name, email, password }) | ||
|
||
const retrievedHome = await retrieveHome(home.id) | ||
|
||
expect(retrievedHome.name).to.be.a('string') | ||
expect(retrievedHome.name).to.equal(name) | ||
expect(retrievedHome.id).to.be.undefined | ||
expect(retrievedHome.email).to.be.undefined | ||
expect(retrievedHome.password).to.be.undefined | ||
}) | ||
|
||
it ('fails on non-existing home', async () => { | ||
try { | ||
await retrieveHome(random.id()) | ||
throw new Error('should not reach this point') | ||
} catch (error) { | ||
expect(error).to.be.instanceOf(NotFoundError) | ||
expect(error.message).to.equal('home not found') | ||
} | ||
}) | ||
|
||
after(async () => await 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