From 3c28f4ec5b6bb6eaa6a79e337df3818a24742f80 Mon Sep 17 00:00:00 2001 From: mryounger6 Date: Tue, 5 Dec 2023 17:24:36 -0600 Subject: [PATCH] added updates controller and tests --- api/controllers/updates.js | 23 ++++++++ api/models/Updates.js | 45 +++++++++++++++ api/swagger/swagger.yaml | 52 +++++++++++++++++ test/controllers/updates.js | 47 ++++++++++++++++ .../cboard-api.postman_collection.json | 56 +++++++++++++++++++ 5 files changed, 223 insertions(+) create mode 100644 api/controllers/updates.js create mode 100644 api/models/Updates.js create mode 100644 test/controllers/updates.js diff --git a/api/controllers/updates.js b/api/controllers/updates.js new file mode 100644 index 00000000..529ca295 --- /dev/null +++ b/api/controllers/updates.js @@ -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 }); + } +} diff --git a/api/models/Updates.js b/api/models/Updates.js new file mode 100644 index 00000000..b76dc449 --- /dev/null +++ b/api/models/Updates.js @@ -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; diff --git a/api/swagger/swagger.yaml b/api/swagger/swagger.yaml index ba660cc8..38aad52b 100644 --- a/api/swagger/swagger.yaml +++ b/api/swagger/swagger.yaml @@ -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 @@ -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: @@ -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 diff --git a/test/controllers/updates.js b/test/controllers/updates.js new file mode 100644 index 00000000..f408e390 --- /dev/null +++ b/test/controllers/updates.js @@ -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 + }); + }); +}); diff --git a/test/postman/cboard-api.postman_collection.json b/test/postman/cboard-api.postman_collection.json index f7eb9dec..29008ddd 100644 --- a/test/postman/cboard-api.postman_collection.json +++ b/test/postman/cboard-api.postman_collection.json @@ -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": [