Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #55 from ndr-brt/fix/fix-utils-tests
Browse files Browse the repository at this point in the history
fix(test): fix utils.js tests
  • Loading branch information
confused-Techie authored Oct 23, 2022
2 parents bf40f57 + 9235c38 commit f80aaba
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
28 changes: 22 additions & 6 deletions src/tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
jest.mock('../storage.js');
const getBanList = require('../storage.js').getBanList;

const utils = require("../utils");

describe("isPackageNameBanned Tests", () => {
// Now since the editor is started in development mode for testing, we know
// banned package lists will fallback to a static list here.

test("Returns true correctly for banned item", async () => {
let name = "situs-slot-gacor";
getBanList.mockResolvedValue({ ok: true, content: ['banned-item'] });
let name = "banned-item";

let isBanned = await utils.isPackageNameBanned(name);
expect(isBanned).toBeTruthy();

expect(isBanned.ok).toBeTruthy();
});

test("Returns false correctly for non-banned item", async () => {
let name = "innocent-name";
getBanList.mockResolvedValue({ ok: true, content: ['banned-item'] });
let name = "not-banned-item";

let isBanned = await utils.isPackageNameBanned(name);
expect(isBanned).toBeFalsy();

expect(isBanned.ok).toBeFalsy();
});

test("Returns true if no banned list can be retrieved", async () => {
getBanList.mockResolvedValue({ ok: false });

let isBanned = await utils.isPackageNameBanned('any');

expect(isBanned.ok).toBeTruthy();
})
});
17 changes: 5 additions & 12 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,16 @@ const { server_url } = require("./config.js").getConfig();
* @returns {boolean} Returns true if the given name is banned. False otherwise.
*/
async function isPackageNameBanned(name) {
let names = await storage.getBanList();

if (!names.ok) {
let banList = await storage.getBanList();
if (!banList.ok) {
// we failed to find the ban list. For now we will just return ok.
logger.warningLog(null, null, "Unable to locate Name Ban List");
return { ok: true };
}

for (let i = 0; i < names.content.length; i++) {
if (name === names.content[i]) {
// it was found on a ban list.
return { ok: false };
}
}

// name wasn't found on any ban lists.
return { ok: true };
return banList.content.find(b => name === b)
? { ok: true }
: { ok: false };
}

/**
Expand Down

0 comments on commit f80aaba

Please sign in to comment.