Commit 72284342 authored by rlacko's avatar rlacko 💬
Browse files

settings endpoint and documentation

parent dfb375b2
Pipeline #4713 passed with stages
in 1 minute and 43 seconds
......@@ -26,7 +26,8 @@ paths:
summary: 'Update an application or create it if doesnt exist'
description: 'Have to be logged in.
To update someones application as a mentor, have to pass in the creator.
After deadline only the mentor can create or update one.'
After deadline only the mentor can create or update one.
Group names have to be passed instead of ids.'
operationId: 'createAnApplication'
requestBody:
content:
......@@ -93,7 +94,7 @@ components:
type: array
items:
type: string
description: References to a Group names
description: References to Group Ids
state:
type: string
enum:
......
......@@ -4,11 +4,11 @@ const { pick } = require('lodash')
const pickedKeys = ['_id', 'activity', 'user', 'state', 'comment']
exports.default = crudControllers(Attendance, pickedKeys)
module.exports = crudControllers(Attendance, pickedKeys)
// On create update activity
exports.default.getOne = async (req, res) => {
module.exports.getOne = async (req, res) => {
try {
const attendance = await Attendance.findOne({ _id: req.params.id })
.populate('_user', ['_id', 'fullName', 'schacc'])
......@@ -32,7 +32,7 @@ exports.default.getOne = async (req, res) => {
}
}
exports.default.getMany = async (req, res) => {
module.exports.getMany = async (req, res) => {
try {
const attendances = await Attendance.find()
.populate('_user', ['_id', 'fullName', 'schacc'])
......@@ -52,7 +52,7 @@ exports.default.getMany = async (req, res) => {
}
}
exports.default.updateOne = async (req, res) => {
module.exports.updateOne = async (req, res) => {
try {
const updatedAttendance = await Attendance.findOneAndUpdate(
{
......
......@@ -7,14 +7,14 @@ const router = Router()
// /api/item
router
.route('/')
.get(isLoggedIn, isMentor, controllers.default.getMany)
.post(isLoggedIn, isMentor, controllers.default.createOne)
.get(isLoggedIn, isMentor, controllers.getMany)
.post(isLoggedIn, isMentor, controllers.createOne)
// /api/item/:id
router
.route('/:id')
.get(isLoggedIn, isMentor, controllers.default.getOne)
.put(isLoggedIn, isMentor, controllers.default.updateOne)
.delete(isLoggedIn, isMentor, controllers.default.removeOne)
.get(isLoggedIn, isMentor, controllers.getOne)
.put(isLoggedIn, isMentor, controllers.updateOne)
.delete(isLoggedIn, isMentor, controllers.removeOne)
exports.default = router
const { app } = require('../../../server')
const session = require('supertest-session')
const { Settings } = require('../settingsModel')
const endpointUrl = '/api/v1/settings'
const fakeSettingsJson = {
applicationEndDate: '2020-12-11',
}
describe('/settings Functionality', () => {
let authSession
// Login as mentor
beforeEach(function (done) {
authSession = session(app)
authSession.get(`/api/v1/login/mock/mentor`).end(function (err) {
if (err) return done(err)
return done()
})
})
// Create
test(`Create settings`, async () => {
let response = await authSession
.put(`${endpointUrl}`)
.send(fakeSettingsJson)
expect(response.statusCode).toBe(201)
expect(response.body.data.applicationEndDate).toBe(
'2020-12-11T00:00:00.000Z'
)
})
// Get
test(`Get settings`, async () => {
await Settings.create(fakeSettingsJson)
let response = await authSession.get(endpointUrl)
expect(response.statusCode).toBe(200)
expect(response.body.data.applicationEndDate).toBe(
'2020-12-11T00:00:00.000Z'
)
})
})
const { app } = require('../../../server')
const { crudPermTest } = require('../../../utils/testHelpers')
const { Settings } = require('../settingsModel')
const endpointUrl = '/api/v1/settings'
const fakeSettingsJson = {
applicationEndDate: '2020-12-11',
}
// put isn't tested, but should be okay
describe('/settings Permission tests', () => {
crudPermTest(
app,
endpointUrl,
Settings,
'settings',
fakeSettingsJson,
[false, true, false, false, false],
[
// [role, create, readAll, readOne, update, delete]
['none', false, true, false, false, false],
['normal', false, true, false, false, false],
['accepted', false, true, false, false, false],
['mentor', false, true, false, true, false],
]
)
})
......@@ -2,42 +2,34 @@ const { crudControllers } = require('../../utils/crud')
const { Settings } = require('./settingsModel')
const { pick } = require('lodash')
exports.default = crudControllers(Settings, ['applicationEndDate'])
module.exports = crudControllers(Settings, ['applicationEndDate'])
// Make sure that only one setting exists
exports.default.createOne = async (req, res) => {
module.exports.getOne = async (req, res) => {
try {
if (await Settings.findOne().lean().exec())
throw {
name: 'alreadyExists',
message: `Already exists a setting, update it!`,
}
const doc = await Settings.findOne().lean().exec()
const doc = await Settings.create({ ...req.body })
return res.status(201).json({ data: pick(doc, ['applicationEndDate']) })
if (!doc) {
return res.status(404).end()
}
res.status(200).json({ data: pick(doc, ['applicationEndDate']) })
} catch (err) {
if (err.name == 'ValidationError') {
// Mongoose validation errors
let messages = []
for (field in err.errors) {
messages.push(err.errors[field].message)
}
return res.status(422).json({ messages })
if (err.name == 'CastError') {
return res.status(422).json('Invalid ID provided')
} else {
console.error(err)
res.status(400).end()
}
if (err.name == 'alreadyExists')
return res.status(409).json({ messages: err.message })
console.error(err)
return res.status(400).end()
}
}
exports.default.updateOne = async (req, res) => {
// If there is no settings, then create it
module.exports.updateOne = async (req, res) => {
try {
const oldSettings = await Settings.findOne().lean().exec()
if (!oldSettings)
return res
.status(404)
.json({ messages: 'There is no settings to update' })
if (!oldSettings) {
const doc = await Settings.create({ ...req.body })
return res.status(201).json({ data: pick(doc, ['applicationEndDate']) })
}
const updatedSettings = await Settings.findOneAndUpdate(
{
_id: oldSettings._id,
......
openapi: '3.0.2'
info:
title: 'Settings Endpoint'
version: '1.0'
paths:
/settings:
get:
tags:
- 'Settings'
summary: 'Get the global settings'
description: 'Can be get by all user. Contains the global settings.'
operationId: 'getSettings'
responses:
'200':
description: OK
content:
application/json:
schema:
type: 'array'
items:
$ref: '#/components/schemas/Settings'
put:
tags:
- 'Settings'
summary: 'Update or create if doesnt exist the global settings'
description: 'Have to be mentor.'
operationId: 'updateSettings'
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Settings'
responses:
'201':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Settings'
components:
schemas:
Settings:
type: object
properties:
applicationEndDate:
type: string
format: date-time
required:
- applicationEndDate
......@@ -8,15 +8,7 @@ const router = Router()
// /api/settings
router
.route('/')
.get(isLoggedIn, isMentor, controllers.default.getMany)
.post(isLoggedIn, isMentor, controllers.default.createOne)
.put(isLoggedIn, isMentor, controllers.default.updateOne)
// /api/settings/:id
router
.route('/:id')
.get(isLoggedIn, isMentor, controllers.default.getOne)
.put(isLoggedIn, isMentor, controllers.default.updateOne)
.delete(isLoggedIn, isMentor, controllers.default.removeOne)
.get(controllers.getOne)
.put(isLoggedIn, isMentor, controllers.updateOne)
exports.default = router
......@@ -85,6 +85,7 @@ const crudPermTest = async (
const response = await authSession.put(
`${endpointUrl}/${Object.values(keyField)[0]}`
)
console.log(response.statusCode)
if (role === 'none') expect(response.statusCode != 401).toBe(perm[3])
else expect(response.statusCode != 403).toBe(perm[3])
expect(response.statusCode).not.toBeGreaterThanOrEqual(500)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment