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

News documentation

parent 9f8fc61c
Pipeline #4726 passed with stages
in 2 minutes and 7 seconds
......@@ -6,24 +6,24 @@ const { validateKeys } = require('../../../utils/testHelpers')
const { News } = require('../newsModel')
const endpointUrl = '/api/v1/news'
const fakeUserJson = {
internal_id: 'fakeId',
schacc: 'fakeUser',
fullName: 'faker Janos',
secondaryEmail: 'faker@fake.com',
}
let fakeNewsJson={
title:'A legjobb hír',
body:'A legjobb hír ever man!',
creator:'fakeUSer'
}
const defaultKeys={
_id:true,
creator:true,
title:true,
body:true
}
internal_id: 'fakeId',
schacc: 'fakeUser',
fullName: 'faker Janos',
secondaryEmail: 'faker@fake.com',
}
let fakeNewsJson = {
title: 'A legjobb hír',
body: 'A legjobb hír ever man!',
creator: 'fakeUSer',
}
const defaultKeys = {
_id: true,
creator: true,
title: true,
body: true,
}
describe('/news "Mentor" Functionality', () => {
describe('/news "Mentor" Functionality', () => {
let authSession
// Login as mentor
beforeEach(async function (done) {
......@@ -32,57 +32,58 @@ let fakeNewsJson={
if (err) return done(err)
authSession = testSession
return done()
})})
//readone
})
})
//readone
test(`GET existing news`, async () => {
const newNews = await News.create(fakeNewsJson)
let response = await authSession.get(`${endpointUrl}/${newNews._id}`)
let response = await authSession.get(`${endpointUrl}/id/${newNews._id}`)
expect(response.statusCode).toBe(200)
})
test('GET invalid news',async()=>{
let response=await authSession.get(`${endpointUrl}/almafa`)
test('GET invalid news', async () => {
let response = await authSession.get(`${endpointUrl}/id/almafa`)
expect(response.statusCode).toBe(422)
})
// Get Many
test(`GET many returns with allowed keys`, async () => {
await News.create(fakeNewsJson)
await News.create(fakeNewsJson)
let response = await authSession.get(endpointUrl)
expect(response.statusCode).toBe(200)
response.body.data.forEach((eachData) => {
validateKeys(eachData, defaultKeys)
})})
// Create
// Get Many
test(`GET many returns with allowed keys`, async () => {
await News.create(fakeNewsJson)
await News.create(fakeNewsJson)
let response = await authSession.get(endpointUrl)
expect(response.statusCode).toBe(200)
response.body.data.forEach((eachData) => {
validateKeys(eachData, defaultKeys)
})
})
// Create
test(`Create news`, async () => {
const newNews = await News.create(fakeNewsJson)
let response =await authSession.post(`${endpointUrl}/`).send({
title:newNews.title,
body:newNews.body,
creator:newNews.creator,
let response = await authSession.post(`${endpointUrl}/`).send({
title: newNews.title,
body: newNews.body,
creator: newNews.creator,
})
expect(response.statusCode).toBe(201)
validateKeys(response.body.data, defaultKeys)
})
//Update
test(`Update news`,async()=>{
const newNews=await News.create(fakeNewsJson)
let response=await authSession
.put(`${endpointUrl}/${newNews._id}`)
.send({
title:'updatedtitle',
})
test(`Update news`, async () => {
const newNews = await News.create(fakeNewsJson)
let response = await authSession
.put(`${endpointUrl}/id/${newNews._id}`)
.send({
title: 'updatedtitle',
})
expect(response.statusCode).toBe(200)
expect(response.body.data.title).toBe('updatedtitle')
})
//Delete
test(`Delete news`,async ()=>{
test(`Delete news`, async () => {
const newNews = await News.create(fakeNewsJson)
let response=await authSession.delete(`${endpointUrl}/${newNews._id}`)
let response = await authSession.delete(`${endpointUrl}/id/${newNews._id}`)
expect(response.statusCode).toBe(200)
})
})
describe('/news "Accepted" Functionality',()=>{
describe('/news "Accepted" Functionality', () => {
let authSession
// Login as accepted
beforeEach(async function (done) {
......@@ -91,28 +92,27 @@ describe('/news "Accepted" Functionality',()=>{
if (err) return done(err)
authSession = testSession
return done()
})})
//readone
})
})
//readone
test(`GET existing news`, async () => {
const newNews = await News.create(fakeNewsJson)
let response = await authSession.get(`${endpointUrl}/${newNews._id}`)
let response = await authSession.get(`${endpointUrl}/id/${newNews._id}`)
expect(response.statusCode).toBe(200)
})
test('GET invalid news',async()=>{
let response=await authSession.get(`${endpointUrl}/almafa`)
test('GET invalid news', async () => {
let response = await authSession.get(`${endpointUrl}/id/almafa`)
expect(response.statusCode).toBe(422)
})
// Get Many
test(`GET many returns with allowed keys`, async () => {
await News.create(fakeNewsJson)
await News.create(fakeNewsJson)
let response = await authSession.get(endpointUrl)
expect(response.statusCode).toBe(200)
response.body.data.forEach((eachData) => {
validateKeys(eachData, defaultKeys)
})
// Get Many
test(`GET many returns with allowed keys`, async () => {
await News.create(fakeNewsJson)
await News.create(fakeNewsJson)
let response = await authSession.get(endpointUrl)
expect(response.statusCode).toBe(200)
response.body.data.forEach((eachData) => {
validateKeys(eachData, defaultKeys)
})
})
})
//
......@@ -12,13 +12,28 @@ const fakeNewsJson = {
}
describe('/news Permission tests', () => {
crudPermTest(
app,
endpointUrl + '/id',
News,
'news',
fakeNewsJson,
[false, false, true, true, true],
[
// [role, create, readAll, readOne, update, delete]
['none', false, false, false, false, false],
['normal', false, true, true, false, false],
['accepted', false, true, true, false, false],
['mentor', true, true, true, true, true],
]
)
crudPermTest(
app,
endpointUrl,
News,
'news',
fakeNewsJson,
[true, true, true, true, true],
[true, true, false, false, false],
[
// [role, create, readAll, readOne, update, delete]
['none', false, false, false, false, false],
......
openapi: '3.0.2'
info:
title: 'News Endpoint'
version: '1.0'
paths:
/news:
get:
tags:
- 'News'
summary: 'Get a List of news'
description: 'Have to be logged in.'
operationId: 'getAllNews'
responses:
'200':
description: OK
content:
application/json:
schema:
type: 'array'
items:
$ref: '#/components/schemas/News'
post:
tags:
- 'News'
summary: 'Create a news'
description: 'Have to be mentor for this.'
operationId: 'createNews'
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/News'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/News'
/news/id/{id}:
get:
tags:
- 'News'
summary: 'Get a news by ID'
description: 'Have to be logged in for this.'
operationId: 'getNews'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/News'
put:
tags:
- 'News'
summary: 'Update a news by ID'
description: 'Only mentors can update a news.'
operationId: 'updateOneNews'
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/News'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/News'
delete:
tags:
- 'News'
summary: 'Delete a news by ID'
description: 'Only mentors can delete a news.'
operationId: 'deleteNews'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/News'
components:
schemas:
News:
type: object
properties:
title:
type: string
body:
type: string
creator:
type: string
description: schacc of the user
required:
- title
- body
- creator
......@@ -5,15 +5,15 @@ const { isLoggedIn, isMentor } = require('../../middlewares/auth')
const router = Router()
// /api/item
// /api/v1/news
router
.route('/')
.get(isLoggedIn, controllers.default.getMany)
.post(isLoggedIn, isMentor, controllers.default.createOne)
// /api/item/:id
// /api/v1/news/id/:id
router
.route('/:id')
.route('/id/:id')
.get(isLoggedIn, controllers.default.getOne)
.put(isLoggedIn, isMentor, controllers.default.updateOne)
.delete(isLoggedIn, isMentor, controllers.default.removeOne)
......
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