Skip to content

Commit

Permalink
add register, authenticate and retrieve spec tests b00tc4mp#406
Browse files Browse the repository at this point in the history
  • Loading branch information
pankelix committed Feb 21, 2024
1 parent 14a7679 commit a40e0ca
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 10 deletions.
6 changes: 3 additions & 3 deletions staff/miguel-arias/project/api/data/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const room = new Schema({
required: true,
unique: true
},
home: {
homeRef: {
type: ObjectId,
ref: 'home'
}
Expand All @@ -53,7 +53,7 @@ const profile = new Schema({
type: String,
default: 'user'
},
home: {
homeRef: {
type: ObjectId,
ref: 'home'
}
Expand All @@ -77,7 +77,7 @@ const template = new Schema({
})

const task = new Schema({
template: {
templateRef: {
type: ObjectId,
ref: 'template'
},
Expand Down
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/authenticateHome.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function authenticateHome(email, password) {

let match
try {
match = bcrypt.compare(password, home.password)
match = await bcrypt.compare(password, home.password)
} catch (error) {
throw new SystemError(error.message)
}
Expand Down
61 changes: 61 additions & 0 deletions staff/miguel-arias/project/api/logic/authenticateHome.spec.js
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'))
})
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/createRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function createRoom(name, homeId) {

return (async () => {
try {
const room = await Room.create({ name, home: homeId })
const room = await Room.create({ name, homeRef: homeId })

return room
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/createTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function createTask(templateId, assigneeId/* , done, date */) {

return (async () => {
try {
const task = await Task.create({ template: templateId, assignee: assigneeId/*, done, date */ })
const task = await Task.create({ templateRef: templateId, assignee: assigneeId/*, done, date */ })

return task
} catch (error) {
Expand Down
36 changes: 36 additions & 0 deletions staff/miguel-arias/project/api/logic/helpers/random.js
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
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/populate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Home, Room, Profile, Template, Task } from '../data/models.js'

(async () => {
try {
await mongoose.connect('mongodb://127.0.0.1:27017/test')
await mongoose.connect('mongodb://127.0.0.1:27017/project')
await Home.deleteMany()
await Room.deleteMany()
await Profile.deleteMany()
Expand Down
52 changes: 52 additions & 0 deletions staff/miguel-arias/project/api/logic/registerHome.spec.js
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())
})
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/registerProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function registerProfile(name, pincode, color, role, homeId ) {
try {
const hash = await bcrypt.hash(pincode, 8)

const profile = await Profile.create({ name, pincode: hash, color, role, home: homeId })
const profile = await Profile.create({ name, pincode: hash, color, role, homeRef: homeId })

return profile
} catch (error) {
Expand Down
42 changes: 42 additions & 0 deletions staff/miguel-arias/project/api/logic/retrieveHome.spec.js
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())
})
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/retrieveTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function retrieveTask(taskId) {
return (async () => {
let task
try {
task = await Task.findById(taskId, 'name').lean()
task = await Task.findById(taskId).populate('templateRef').lean()
} catch (error) {
throw new SystemError(error.message)
}
Expand Down
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/retrieveTask.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import retrieveTask from './retrieveTask.js'
try {
await mongoose.connect('mongodb://127.0.0.1:27017/test')
debugger
const task = await retrieveTask('65d606f1a72ef112179dee4b')
const task = await retrieveTask('65d61642509a026d6e3e2138')
console.log('task retrieved', task)

await mongoose.disconnect()
Expand Down

0 comments on commit a40e0ca

Please sign in to comment.