Skip to content

Commit

Permalink
add spec tests to all logics b00tc4mp#406; start tailwind css b00tc4m…
Browse files Browse the repository at this point in the history
  • Loading branch information
pankelix committed Mar 13, 2024
1 parent dd1ac5a commit 05cfbd4
Show file tree
Hide file tree
Showing 66 changed files with 3,069 additions and 292 deletions.
8 changes: 4 additions & 4 deletions staff/miguel-arias/project/api/data/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ const profile = new Schema({
},
role: {
type: String,
default: 'User'
default: 'user'
},
points: {
type: Number,
default: 0
},
}/*,
avatar: {
name: {
type: String,
Expand All @@ -72,8 +72,8 @@ const profile = new Schema({
type: {
type: String
}
}
/* avatar: {
},
avatar: {
data: Buffer,
contentType: String
} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ const { NotFoundError, ContentError, CredentialsError } = errors
export default async (req, res) => {
const { name, pincode } = req.body

const token = req.headers.authorization.substring(7)
const payload = jwt.verify(token, process.env.JWT_SECRET)
const homeId = payload.sub

let profileId
try {
profileId = await logic.authenticateProfile(name, pincode)
profileId = await logic.authenticateProfile(homeId, name, pincode)

const profileToken = jwt.sign({ sub: profileId }, process.env.JWT_SECRET, { expiresIn: process.env.JWT_EXPI })

Expand Down
2 changes: 2 additions & 0 deletions staff/miguel-arias/project/api/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import registerHomeHandler from './registerHomeHandler.js'
import authenticateHomeHandler from './authenticateHomeHandler.js'
import retrieveHomeHandler from './retrieveHomeHandler.js'
import retrieveTasksHandler from './retrieveTasksHandler.js'
import retrieveProfileTasksHandler from './retrieveProfileTasksHandler.js'
import retrieveProfilesHandler from './retrieveProfilesHandler.js'
import retrieveTemplatesHandler from './retrieveTemplatesHandler.js'
import authenticateProfileHandler from './authenticateProfileHandler.js'
Expand Down Expand Up @@ -31,6 +32,7 @@ export {
authenticateHomeHandler,
retrieveHomeHandler,
retrieveTasksHandler,
retrieveProfileTasksHandler,
retrieveProfilesHandler,
retrieveTemplatesHandler,
authenticateProfileHandler,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import jwt from 'jsonwebtoken'
const { JsonWebTokenError } = jwt

import { errors } from 'com'
const { NotFoundError, ContentError, TokenError } = errors

import logic from "../logic/index.js"

export default async (req, res) => {
try {
const token = req.headers.authorization.substring(7)
const payload = jwt.verify(token, process.env.JWT_SECRET)
const homeId = payload.sub

const { profileId, week } = req.params

const tasks = await logic.retrieveProfileTasks(homeId, profileId, week)

res.json(tasks)

} catch (error) {
let status = 500

if (error instanceof ContentError || error instanceof TypeError)
status = 406

if (error instanceof NotFoundError)
status = 404

if (error instanceof JsonWebTokenError) {
status = 401
error = new TokenError(error.message)
}

res.status(status).json({ error: error.constructor.name, message: error.message })
}
}
11 changes: 7 additions & 4 deletions staff/miguel-arias/project/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dotenv.config()
import mongoose from 'mongoose'
import express from 'express'
import cors from 'cors'
import multer from 'multer'
/* import multer from 'multer' */

import {
registerHomeHandler,
Expand All @@ -15,6 +15,7 @@ import {
authenticateProfileHandler,
createTaskHandler,
retrieveTasksHandler,
retrieveProfileTasksHandler,
deleteTaskHandler,
assignTaskHandler,
delayTaskHandler,
Expand All @@ -40,8 +41,8 @@ mongoose.connect(process.env.MONGODB_URL)
.then(() => {
const server = express()
const jsonBodyParser = express.json()
const storage = multer.memoryStorage()
const upload = multer({ storage: storage })
/* const storage = multer.memoryStorage()
const upload = multer({ storage: storage }) */

server.use(cors())

Expand All @@ -61,7 +62,7 @@ mongoose.connect(process.env.MONGODB_URL)

server.post('/profiles/:profileId/delete', deleteProfileHandler)

server.patch('/profiles/:profileId/upload-avatar', upload.single('image'), uploadAvatarHandler)
/* server.patch('/profiles/:profileId/upload-avatar', upload.single('image'), uploadAvatarHandler) */

server.patch('/profiles/:profileId/edit', jsonBodyParser, editRoleHandler)

Expand All @@ -71,6 +72,8 @@ mongoose.connect(process.env.MONGODB_URL)

server.get('/tasks/:week', retrieveTasksHandler)

server.get('/tasks/:profileId/:week', retrieveProfileTasksHandler)

server.post('/tasks', jsonBodyParser, createTaskHandler)

server.post('/tasks/:taskId', jsonBodyParser, materializeTaskHandler)
Expand Down
6 changes: 3 additions & 3 deletions staff/miguel-arias/project/api/logic/assignTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ function assignTask(sessionProfileId, taskId, profileId) {
}

if (!profile)
throw new NotFoundError('profile not found')

if (profile.role !== 'admin' && assignToSelf !== true)
throw new NotFoundError('session profile not found')
debugger
if (profile.role !== 'admin'/* && assignToSelf !== true */)
throw new PermissionError('profile is not admin')

} else if (profileId) {
Expand Down
115 changes: 115 additions & 0 deletions staff/miguel-arias/project/api/logic/assignTask.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import dotenv from 'dotenv'
dotenv.config()

import mongoose from 'mongoose'
import { expect } from 'chai'

import random from './helpers/random.js'
import assignTask from './assignTask.js'
import { Profile, Template, Task } from '../data/models.js'

import { errors } from 'com'
const { NotFoundError, PermissionError } = errors
debugger
describe('assignTask', () => {
before(() => mongoose.connect('mongodb://127.0.0.1:27017/spec'))

beforeEach(() => Promise.all([Profile.deleteMany(), Template.deleteMany(), Task.deleteMany()]))

it('succeeds on existing session profile, profile and task', async () => {

const sessionProfile = await Profile.create({ home: random.id(), name: random.name(), pincode: random.pincode(), role: 'admin' })

const sessionProfileId = sessionProfile._id.toString()

const profile = await Profile.create({ home: random.id(), name: random.name(), pincode: random.pincode(), color: { name: random.name(), code: random.name() } })

const profileId = profile._id.toString()

const task = await Task.create({ home: random.id(), template: random.id(), assignee: random.id() })

const taskId = task._id.toString()

await assignTask(sessionProfileId, taskId, profileId)

const taskFound = await Task.findById(taskId)
debugger
expect(taskFound.assignee._id.toString()).to.equal(profileId)
})

it('succeeds on only existing session profile and task', async () => {

const sessionProfile = await Profile.create({ home: random.id(), name: random.name(), pincode: random.pincode(), role: 'admin' })

const sessionProfileId = sessionProfile._id.toString()

const profile = await Profile.create({ home: random.id(), name: random.name(), pincode: random.pincode(), role: 'admin', color: { name: random.name(), code: random.name() } })

const profileId = profile._id.toString()

const task = await Task.create({ home: random.id(), template: random.id(), assignee: random.id() })

const taskId = task._id.toString()

await assignTask(sessionProfileId, taskId, null)

const taskFound = await Task.findById(taskId)

expect(taskFound.assignee._id.toString()).to.equal(sessionProfileId)
})

it('fails on non existing task', async () => {
try {
await assignTask(random.id(), random.id(), random.id())
throw new Error('should not reach this point')
} catch (error) {
expect(error).to.be.instanceOf(NotFoundError)
expect(error.message).to.equal('task not found')
}
})

it('fails on non existing session profile', async () => {
const task = await Task.create({ home: random.id(), template: random.id(), assignee: random.id() })

const taskId = task._id.toString()
try {
await assignTask(random.id(), taskId, null)
throw new Error('should not reach this point')
} catch (error) {
expect(error).to.be.instanceOf(NotFoundError)
expect(error.message).to.equal('session profile not found')
}
})

it('fails on profile not admin', async () => {
const sessionProfile = await Profile.create({ home: random.id(), name: random.name(), pincode: random.id() })

const sessionProfileId = sessionProfile._id.toString()

const task = await Task.create({ home: random.id(), template: random.id(), assignee: random.id() })

const taskId = task._id.toString()
try {
await assignTask(sessionProfileId, taskId, null)
throw new Error('should not reach this point')
} catch (error) {
expect(error).to.be.instanceOf(PermissionError)
expect(error.message).to.equal('profile is not admin')
}
})

it('fails on non existing profile', async () => {
const task = await Task.create({ home: random.id(), template: random.id(), assignee: random.id() })

const taskId = task._id.toString()
try {
await assignTask(random.id(), taskId, random.id())
throw new Error('should not reach this point')
} catch (error) {
expect(error).to.be.instanceOf(NotFoundError)
expect(error.message).to.equal('profile not found')
}
})

after(async () => await mongoose.disconnect())
})
10 changes: 6 additions & 4 deletions staff/miguel-arias/project/api/logic/authenticateHome.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import authenticateHome from './authenticateHome.js'
const { NotFoundError, CredentialsError } = errors

describe('authenticateHome', () => {
before(async () => await mongoose.connect('mongodb://127.0.0.1:27017/test'))
before(async () => await mongoose.connect('mongodb://127.0.0.1:27017/spec'))

beforeEach(async () => await Home.deleteMany())

Expand All @@ -30,10 +30,12 @@ describe('authenticateHome', () => {
expect(homeId).to.equal(home.id)
})

it('fails on non correct email', async () => {
try {
await authenticateHome(random.email(), random.password())
it('fails on no home found', async () => {
const email = random.email()
const password = random.password()

try {
await authenticateHome(email, password)
throw new Error('should not reach this point')
} catch (error) {
expect(error).to.be.instanceOf(NotFoundError)
Expand Down
15 changes: 13 additions & 2 deletions staff/miguel-arias/project/api/logic/authenticateProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@ import bcrypt from 'bcryptjs'
import { validate, errors } from 'com'
const { SystemError, CredentialsError, NotFoundError } = errors

import { Profile } from '../data/models.js'
import { Home, Profile } from '../data/models.js'


function authenticateProfile(name, pincode) {
function authenticateProfile(homeId, name, pincode) {
validate.id(homeId)
validate.text(name, 'name')
validate.pincode(pincode)

return (async () => {
let home
try {
home = await Home.findById(homeId)
} catch (error) {
throw new SystemError(error.message)
}

if (!home)
throw new NotFoundError('home not found')

let profile
try {
profile = await Profile.findOne({ name })
Expand Down
Loading

0 comments on commit 05cfbd4

Please sign in to comment.