From 59f439977c999419888419360269d50b2a3f338c Mon Sep 17 00:00:00 2001 From: confused-Techie Date: Mon, 25 Sep 2023 18:50:59 -0700 Subject: [PATCH] Implement testing for featured packages, minor cleanup --- .../deletePackagesPackageNameStar.js | 7 +- src/controllers/getPackagesFeatured.js | 10 ++- tests/http/getPackagesFeatured.test.js | 68 +++++++++++++++++++ tests/models/packageObjectFullArray.js | 2 +- 4 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 tests/http/getPackagesFeatured.test.js diff --git a/src/controllers/deletePackagesPackageNameStar.js b/src/controllers/deletePackagesPackageNameStar.js index 97c39f40..55b55e94 100644 --- a/src/controllers/deletePackagesPackageNameStar.js +++ b/src/controllers/deletePackagesPackageNameStar.js @@ -4,7 +4,12 @@ module.exports = { docs: { - summary: "Unstar a package." + summary: "Unstar a package.", + responses: { + 204: { + description: "An empty response, indicating success." + } + } }, endpoint: { method: "DELETE", diff --git a/src/controllers/getPackagesFeatured.js b/src/controllers/getPackagesFeatured.js index 59338924..7f7ec78b 100644 --- a/src/controllers/getPackagesFeatured.js +++ b/src/controllers/getPackagesFeatured.js @@ -4,7 +4,15 @@ module.exports = { docs: { - summary: "Returns all featured packages. Previously undocumented endpoint." + summary: "Returns all featured packages. Previously undocumented endpoint.", + responses: { + 200: { + description: "An array of features packages.", + content: { + "application/json": "$packageObjectShortArray" + } + } + } }, endpoint: { method: "GET", diff --git a/tests/http/getPackagesFeatured.test.js b/tests/http/getPackagesFeatured.test.js new file mode 100644 index 00000000..1131a872 --- /dev/null +++ b/tests/http/getPackagesFeatured.test.js @@ -0,0 +1,68 @@ +const endpoint = require("../../src/controllers/getPackagesFeatured.js"); +const database = require("../../src/database.js"); +const context = require("../../src/context.js"); + +describe("Behaves as expected", () => { + test("Calls the correct function", async () => { + const localContext = context; + const spy = jest.spyOn(localContext.database, "getFeaturedPackages"); + + await endpoint.logic({}, localContext); + + expect(spy).toBeCalledTimes(1); + + spy.mockClear(); + }); + + test("Returns not found with no packages present", async () => { + const sso = await endpoint.logic({}, context); + + expect(sso.ok).toBe(false); + expect(sso.content.short).toBe("not_found"); + }); + + test("Returns proper data on success", async () => { + const addPack = await database.insertNewPackage({ + // We know a currently featured package is 'x-terminal-reloaded' + name: "x-terminal-reloaded", + repository: { + url: "https://github.com/Spiker985/x-terminal-reloaded", + type: "git" + }, + creation_method: "Test Package", + releases: { + latest: "1.1.0" + }, + readme: "This is a readme!", + metadata: { + name: "atom-material-ui" + }, + versions: { + "1.1.0": { + dist: { + tarball: "download-url", + sha: "1234" + }, + name: "x-terminal-reloaded" + }, + "1.0.0": { + dist: { + tarball: "download-url", + sha: "1234" + }, + name: "x-terminal-reloaded" + } + } + }); + + const sso = await endpoint.logic({}, context); + + expect(sso.ok).toBe(true); + expect(sso.content).toBeArray(); + expect(sso.content.length).toBe(1); + expect(sso.content[0].name).toBe("x-terminal-reloaded"); + expect(sso).toMatchEndpointSuccessObject(endpoint); + + await database.removePackageByName("x-terminal-reloaded", true); + }); +}); diff --git a/tests/models/packageObjectFullArray.js b/tests/models/packageObjectFullArray.js index 6945e887..6e397930 100644 --- a/tests/models/packageObjectFullArray.js +++ b/tests/models/packageObjectFullArray.js @@ -4,7 +4,7 @@ module.exports = { }, example: [ require("./packageObjectFull.js").example - ] + ], test: Joi.array().items( require("./packageObjectFull.js").test