diff --git a/staff/miguel-arias/project/api/data/models.js b/staff/miguel-arias/project/api/data/models.js index 730ba393b..1cb742489 100644 --- a/staff/miguel-arias/project/api/data/models.js +++ b/staff/miguel-arias/project/api/data/models.js @@ -28,7 +28,7 @@ const room = new Schema({ required: true, unique: true }, - home: { + homeRef: { type: ObjectId, ref: 'home' } @@ -53,7 +53,7 @@ const profile = new Schema({ type: String, default: 'user' }, - home: { + homeRef: { type: ObjectId, ref: 'home' } @@ -77,7 +77,7 @@ const template = new Schema({ }) const task = new Schema({ - template: { + templateRef: { type: ObjectId, ref: 'template' }, diff --git a/staff/miguel-arias/project/api/logic/authenticateHome.js b/staff/miguel-arias/project/api/logic/authenticateHome.js index b546ac49b..8424bd6a1 100644 --- a/staff/miguel-arias/project/api/logic/authenticateHome.js +++ b/staff/miguel-arias/project/api/logic/authenticateHome.js @@ -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) } diff --git a/staff/miguel-arias/project/api/logic/authenticateHome.spec.js b/staff/miguel-arias/project/api/logic/authenticateHome.spec.js new file mode 100644 index 000000000..1a95a567c --- /dev/null +++ b/staff/miguel-arias/project/api/logic/authenticateHome.spec.js @@ -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')) +}) diff --git a/staff/miguel-arias/project/api/logic/createRoom.js b/staff/miguel-arias/project/api/logic/createRoom.js index ca2878841..7950211a5 100644 --- a/staff/miguel-arias/project/api/logic/createRoom.js +++ b/staff/miguel-arias/project/api/logic/createRoom.js @@ -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) { diff --git a/staff/miguel-arias/project/api/logic/createTask.js b/staff/miguel-arias/project/api/logic/createTask.js index b9977a073..fa62dc991 100644 --- a/staff/miguel-arias/project/api/logic/createTask.js +++ b/staff/miguel-arias/project/api/logic/createTask.js @@ -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) { diff --git a/staff/miguel-arias/project/api/logic/helpers/random.js b/staff/miguel-arias/project/api/logic/helpers/random.js new file mode 100644 index 000000000..115a79262 --- /dev/null +++ b/staff/miguel-arias/project/api/logic/helpers/random.js @@ -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 \ No newline at end of file diff --git a/staff/miguel-arias/project/api/logic/populate.js b/staff/miguel-arias/project/api/logic/populate.js index 6595c4e57..11bac34c6 100644 --- a/staff/miguel-arias/project/api/logic/populate.js +++ b/staff/miguel-arias/project/api/logic/populate.js @@ -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() diff --git a/staff/miguel-arias/project/api/logic/registerHome.spec.js b/staff/miguel-arias/project/api/logic/registerHome.spec.js new file mode 100644 index 000000000..224d6b275 --- /dev/null +++ b/staff/miguel-arias/project/api/logic/registerHome.spec.js @@ -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()) +}) \ No newline at end of file diff --git a/staff/miguel-arias/project/api/logic/registerProfile.js b/staff/miguel-arias/project/api/logic/registerProfile.js index b966db43a..36632285a 100644 --- a/staff/miguel-arias/project/api/logic/registerProfile.js +++ b/staff/miguel-arias/project/api/logic/registerProfile.js @@ -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) { diff --git a/staff/miguel-arias/project/api/logic/retrieveHome.spec.js b/staff/miguel-arias/project/api/logic/retrieveHome.spec.js new file mode 100644 index 000000000..394e10598 --- /dev/null +++ b/staff/miguel-arias/project/api/logic/retrieveHome.spec.js @@ -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()) +}) \ No newline at end of file diff --git a/staff/miguel-arias/project/api/logic/retrieveTask.js b/staff/miguel-arias/project/api/logic/retrieveTask.js index e28c590be..a61c3b93e 100644 --- a/staff/miguel-arias/project/api/logic/retrieveTask.js +++ b/staff/miguel-arias/project/api/logic/retrieveTask.js @@ -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) } diff --git a/staff/miguel-arias/project/api/logic/retrieveTask.test.js b/staff/miguel-arias/project/api/logic/retrieveTask.test.js index 70cdd72ab..4257c049d 100644 --- a/staff/miguel-arias/project/api/logic/retrieveTask.test.js +++ b/staff/miguel-arias/project/api/logic/retrieveTask.test.js @@ -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()