Skip to content

Commit

Permalink
test: update unit tests (#12)
Browse files Browse the repository at this point in the history
Enable back the tests that were not running.
  • Loading branch information
aelesbao authored Aug 28, 2023
2 parents 14fe2d6 + e4061a2 commit 70e0079
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 350 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"build:x64": "prebuildify --napi --strip --preinstall='./build.sh amd64' --arch=x64",
"build:arm64": "prebuildify --napi --strip --preinstall='./build.sh arm64' --arch=arm64",
"install": "node-gyp-build",
"test": "node --napi-modules ./test/*.js"
"test": "node --napi-modules ./test/index.js"
},
"engines": {
"node": ">=16"
Expand Down
49 changes: 0 additions & 49 deletions test/binding_file.spec.js

This file was deleted.

88 changes: 88 additions & 0 deletions test/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const keyring = require("../src/node");
const assert = require("node:assert");

// Encrypted File Store tests
assert(keyring.FileStore.set, "keyring.FileStore.set is undefined");
assert(keyring.FileStore.get, "keyring.FileStore.get is undefined");
assert(keyring.FileStore.list, "keyring.FileStore.list is undefined");
assert(keyring.FileStore.remove, "keyring.FileStore.remove is undefined");

const fileName = "test.key";
const data = "hello from file keystore";
const password = "password123";

function testSetFileStore(context) {
return () => {
const result = keyring.FileStore.set(
context.tmpDir,
fileName,
data,
password
);
console.log("keyring.FileStore.set returns:", result);
assert.strictEqual(result, "success", "Unexpected value returned");
};
}

function testGetFileStore(context) {
return () => {
const dataResult = keyring.FileStore.get(
context.tmpDir,
fileName,
password
);
console.log("keyring.FileStore.get returns:", dataResult);
assert.strictEqual(dataResult, data, "Unexpected value returned");
};
}

function testListFileStore(context) {
return () => {
const result = keyring.FileStore.list(context.tmpDir);
console.log("keyring.FileStore.list returns:", result);
assert(
Array.isArray(result) && result.includes(fileName),
"Unexpected value returned"
);
};
}

function testRemoveFileStore(context) {
return () => {
const result = keyring.FileStore.remove(context.tmpDir, fileName);
console.log("keyring.FileStore.remove returns:", result);

const list = keyring.FileStore.list(context.tmpDir);
assert(
Array.isArray(list) && !list.includes(fileName),
"Unexpected value returned"
);
};
}

function run(context) {
assert.doesNotThrow(
testSetFileStore(context),
undefined,
"testSetFileStore threw an expection"
);
assert.doesNotThrow(
testGetFileStore(context),
undefined,
"testGetFileStore threw an expection"
);
assert.doesNotThrow(
testListFileStore(context),
undefined,
"testListFileStore threw an expection"
);
assert.doesNotThrow(
testRemoveFileStore(context),
undefined,
"testRemoveFileStore threw an expection"
);
}

module.exports = {
run,
};
22 changes: 22 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");

const context = {
tmpDir: fs.mkdtempSync(path.join(os.tmpdir(), "archway-keyring-test-")),
};

["file", "os", "unencrypted"].forEach((test) => {
console.log(`\n=> Testing ${test}`);
require(`./${test}`).run(context);
});

try {
fs.rmSync(context.tmpDir, { recursive: true });
} catch (e) {
console.error(
`An error has occurred while cleaning up the temp folder at ${tmpDir}`
);
}

console.log("\nAll tests passed!");
79 changes: 79 additions & 0 deletions test/os.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const keyring = require("../src/node");
const assert = require("assert");

// OS Store tests
assert(keyring.OsStore.set, "keyring.OsStore.set is undefined");
assert(keyring.OsStore.get, "keyring.OsStore.get is undefined");
assert(keyring.OsStore.list, "keyring.OsStore.list is undefined");
assert(keyring.OsStore.remove, "keyring.OsStore.remove is undefined");

const serviceName = "archway-keyring-test";
const keyName = "test";
const data = "hello from os keyring";

function testSetOsStore(_context) {
return () => {
const result = keyring.OsStore.set(serviceName, keyName, data);
console.log("keyring.OsStore.set returns:", result);
assert.strictEqual(result, "success", "Unexpected value returned");
};
}

function testGetOsStore(_context) {
return () => {
const result = keyring.OsStore.get(serviceName, keyName);
console.log("keyring.OsStore.get returns:", result);
assert.strictEqual(result, data, "Unexpected value returned");
};
}

function testListOsStore(_context) {
return () => {
const result = keyring.OsStore.list(serviceName);
console.log("keyring.OsStore.list returns:", result);
assert(
Array.isArray(result) && result.includes(keyName),
"Unexpected value returned"
);
};
}

function testRemoveOsStore(_context) {
return () => {
const result = keyring.OsStore.remove(serviceName, keyName);
console.log("keyring.OsStore.remove returns:", result);

const list = keyring.OsStore.list(serviceName);
assert(
Array.isArray(list) && !list.includes(keyName),
"Unexpected value returned"
);
};
}

function run(context) {
assert.doesNotThrow(
testSetOsStore(context),
undefined,
"testSetOsStore threw an expection"
);
assert.doesNotThrow(
testGetOsStore(context),
undefined,
"testGetOsStore threw an expection"
);
assert.doesNotThrow(
testListOsStore(context),
undefined,
"testListOsStore threw an expection"
);
assert.doesNotThrow(
testRemoveOsStore(context),
undefined,
"testRemoveOsStore threw an expection"
);
}

module.exports = {
run,
};
Loading

0 comments on commit 70e0079

Please sign in to comment.