Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added updates controller and tests #337

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions api/controllers/updates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Updates = require('../models/Updates');

module.exports = {
getUpdates: getUpdates
};


async function getUpdates(req, res) {
try {
const updates = await Updates.find().exec();

const response = updates.map((update) => ({
id: update.id,
title: update.title,
content: update.content,
time: update.time,
}));

res.status(200).json(response);
} catch (err) {
res.status(500).json({ message: 'Error retrieving updates', error: err.message });
}
}
45 changes: 45 additions & 0 deletions api/models/Updates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const mongoose = require('mongoose');
const { stringify } = require('yamljs');
const Schema = mongoose.Schema;

const UPDATES_SCHEMA_DEFINITION = {
id: {
type: String,
required: true,
},
title: {
type: String,
required: true,
},
content: {
type: String,
required: true,
},
time: {
type: Date,
default: Date.now,
},
};

const UPDATES_SCHEMA_OPTIONS = {
toObject: {
virtuals: true,
},
toJSON: {
virtuals: true,
versionKey: false,
transform: function (doc, ret) {
ret.id = ret._id;
delete ret._id;
},
},
};

const updatesSchema = new Schema(
UPDATES_SCHEMA_DEFINITION,
UPDATES_SCHEMA_OPTIONS
);

const Updates = mongoose.model('Updates', updatesSchema);

module.exports = Updates;
52 changes: 52 additions & 0 deletions api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ parameters:
required: true
schema:
$ref: "#/definitions/AnalyticsReport"
Updates:
name: updates
in: body
description: updates data
required: true
schema:
$ref: '#/definitions/Updates'
Board:
name: board
in: body
Expand Down Expand Up @@ -179,6 +186,24 @@ paths:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/updates:
x-swagger-router-controller: updates
get:
operationId: getUpdates
description: Returns a list of updates.
parameters:
- $ref: '#/parameters/LimitParameter'
responses:
"200":
description: Success
schema:
type: array
items:
$ref: "#/definitions/Updates"
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/board:
x-swagger-router-controller: board
post:
Expand Down Expand Up @@ -1216,6 +1241,33 @@ definitions:
type: object
data:
type: object
Updates:
type: object
properties:
id:
type: string
title:
type: string
content:
type: string
time:
type: string
UpdatesResponse:
type: object
properties:
id:
type: string
description: The unique identifier for the created update.
title:
type: string
description: The title of the created update.
content:
type: string
description: The content of the created update.
time:
type: string
format: date-time
description: The timestamp indicating when the update was created.
GetLanguageResponse:
required:
- locale
Expand Down
47 changes: 47 additions & 0 deletions test/controllers/updates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
process.env.NODE_ENV = 'test';

const request = require('supertest');
const chai = require('chai');

const helper = require('../helper');

//Parent block
describe('Updates API calls', function () {
let server;

before(async function () {
server = require('../../app');
});


describe('GET /updates', function () {
it('it should return a list of updates', async function () {
const res = await request(server)
.get('/updates')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200);

const updates = res.body;
updates.should.be.an('array');

updates.forEach((update) => {
update.should.be.an('object').with.all.keys(
'id',
'title',
'content',
'time'
);

// Example assertions for property types
update.id.should.be.a('string');
update.title.should.be.a('string');
update.content.should.be.a('string');
update.time.should.be.a('string').and.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/);
// Add more assertions for other property types
});

// Add more global assertions if needed
});
});
});
56 changes: 56 additions & 0 deletions test/postman/cboard-api.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,62 @@
},
"response": []
},
{
"name": "/updates/",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"});"
],
"type": "text/javascript"
}
},
{
"listen": "prerequest",
"script": {
"exec": [

],
"type": "text/javascript"
}
}
],
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "GET",
"header": [
{
"key": "Authorization",
"value": "Bearer {{token}}"
},
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "{{url}}/updates/",
"host": [
"{{url}}"
],
"path": [
"updates",
""
]
}
},
"response": []
},
{
"name": "/user/logout/",
"event": [
Expand Down