From 2cc4958bb2916775fecbc258f43b23c46333b661 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Fri, 28 Jul 2023 17:09:30 +0300 Subject: [PATCH] Add support for isolated instances by passing dataDirectory: false Depends-on: https://github.com/balena-io-modules/balena-settings-storage/pull/37 Change-type: minor --- README.md | 4 +- doc/README.hbs | 4 +- lib/auth.ts | 4 +- package.json | 5 +- tests/03-auth.spec.ts | 129 +++++++++++++++++++++++++++++++++++++ tests/fixtures/api-keys.ts | 2 + 6 files changed, 140 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index da255ab..bc4eed2 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,8 @@ It accepts the following params: | Param | Type | Description | | --- | --- | --- | | options | Object | options | -| options.dataDirectory | string | the directory to use for storage in Node.js. Ignored in the browser. | -| options.tokenKey | string | the key used to store the last token in the storage. `token` by default. | +| [options.dataDirectory] | string \| false | the directory to use for storage in Node.js or false to create an isolated in memory instance. Values other than false are ignored in the browser. | +| [options.tokenKey] | string | the key used to store the last token in the storage. `token` by default. | **Example** ```js diff --git a/doc/README.hbs b/doc/README.hbs index 4c615a9..8ef329d 100644 --- a/doc/README.hbs +++ b/doc/README.hbs @@ -37,8 +37,8 @@ It accepts the following params: | Param | Type | Description | | --- | --- | --- | | options | Object | options | -| options.dataDirectory | string | the directory to use for storage in Node.js. Ignored in the browser. | -| options.tokenKey | string | the key used to store the last token in the storage. `token` by default. | +| [options.dataDirectory] | string \| false | the directory to use for storage in Node.js or false to create an isolated in memory instance. Values other than false are ignored in the browser. | +| [options.tokenKey] | string | the key used to store the last token in the storage. `token` by default. | **Example** ```js diff --git a/lib/auth.ts b/lib/auth.ts index 6bbe3ef..d887f7b 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -25,8 +25,10 @@ import { APIKey } from './api-key'; import { JWT } from './jwt'; import { Token, TokenType } from './token'; +export { TokenType } from './token'; + interface BalenaAuthOptions { - dataDirectory?: string; + dataDirectory?: string | false; tokenKey?: string; } diff --git a/package.json b/package.json index 5bb7a72..bf097a5 100644 --- a/package.json +++ b/package.json @@ -30,11 +30,10 @@ "test:node": "mocha -r ts-node/register --reporter spec tests/**/*.spec.ts", "test:browser": "karma start", "prebuild": "rimraf ./build && npm run lint-fix", - "build": "tsc", + "build": "tsc && npm run readme", "pretest": "npm run build && npm run lint", "test": "npm run test:node && npm run test:browser", "prepack": "npm run build", - "prereadme": "npm run build", "readme": "jsdoc2md --template doc/README.hbs build/*.js > README.md" }, "author": "Balena Team ", @@ -73,7 +72,7 @@ "dependencies": { "@types/jwt-decode": "^2.2.1", "balena-errors": "^4.7.1", - "balena-settings-storage": "^8.0.0", + "balena-settings-storage": "^8.1.0", "jwt-decode": "^2.2.0", "tslib": "^2.0.0" }, diff --git a/tests/03-auth.spec.ts b/tests/03-auth.spec.ts index d915657..117d2ba 100644 --- a/tests/03-auth.spec.ts +++ b/tests/03-auth.spec.ts @@ -262,5 +262,134 @@ describe('BalenaAuth', () => { }); }); }); + + describe('using dataDirectory: false and the same tokenKey', function () { + let authIsolated1: InstanceType; + let authIsolated2: InstanceType; + before(async function () { + await auth.setKey(apiKeyFixtures.apiKey); + + authIsolated1 = new BalenaAuth({ + dataDirectory: false, + tokenKey: 'token-test', + }); + authIsolated2 = new BalenaAuth({ + dataDirectory: false, + tokenKey: 'token-test', + }); + }); + describe('.hasKey()', () => { + it('should return true on the original instance', async () => { + expect(await auth.hasKey()).to.equal(true); + }); + it('should return false on the isolated instance', async () => { + expect(await authIsolated1.hasKey()).to.equal(false); + }); + }); + describe('.getKey()', () => { + it('should return the key on the original instance', async () => { + expect(await auth.getKey()).to.equal(apiKeyFixtures.apiKey); + }); + it('should reject on the isolated instance', async () => { + await expect(authIsolated1.getKey()).to.eventually.rejected; + }); + }); + describe('.setKey()', () => { + it('should work', async function () { + await authIsolated1.setKey(apiKeyFixtures.apiKey2); + expect(await authIsolated1.getKey()).to.equal(apiKeyFixtures.apiKey2); + expect(await authIsolated1.hasKey()).to.equal(true); + }); + }); + describe('given a key set by the first isolated instance', function () { + before(async function () { + await authIsolated1.setKey(apiKeyFixtures.apiKey2); + }); + describe('.hasKey()', () => { + it('should return true on the original instance', async () => { + expect(await auth.hasKey()).to.equal(true); + }); + it('should return true when called on the first isolated instance', async () => { + expect(await authIsolated1.hasKey()).to.equal(true); + }); + it('should return false when called on the second isolated instance', async () => { + expect(await authIsolated2.hasKey()).to.equal(false); + }); + }); + describe('.getKey()', () => { + it('should return the key on the original instance', async () => { + expect(await auth.getKey()).to.equal(apiKeyFixtures.apiKey); + }); + it('should return the provided key when called on the first isolated instance', async () => { + expect(await authIsolated1.getKey()).to.equal( + apiKeyFixtures.apiKey2, + ); + }); + it('should reject when called on the second isolated instance', async () => { + await expect(authIsolated2.getKey()).to.eventually.rejected; + }); + }); + }); + describe('given a key set by the second isolated instance', function () { + before(async function () { + await authIsolated2.setKey(apiKeyFixtures.apiKey3); + }); + describe('.hasKey()', () => { + it('should return true on the original instance', async () => { + expect(await auth.hasKey()).to.equal(true); + }); + it('should return true when called on the first isolated instance', async () => { + expect(await authIsolated1.hasKey()).to.equal(true); + }); + it('should return true when called on the second isolated instance', async () => { + expect(await authIsolated2.hasKey()).to.equal(true); + }); + }); + describe('.getKey()', () => { + it('should return the key on the original instance', async () => { + expect(await auth.getKey()).to.equal(apiKeyFixtures.apiKey); + }); + it('should return the provided key when called on the first isolated instance ', async () => { + expect(await authIsolated1.getKey()).to.equal( + apiKeyFixtures.apiKey2, + ); + }); + it('should return the provided key when called on the second isolated instance ', async () => { + expect(await authIsolated2.getKey()).to.equal( + apiKeyFixtures.apiKey3, + ); + }); + }); + }); + describe('when the first isolated instance removes its key', function () { + before(async function () { + await authIsolated1.removeKey(); + }); + describe('.hasKey()', () => { + it('should return true on the original instance', async () => { + expect(await auth.hasKey()).to.equal(true); + }); + it('should return false when called on the first isolated instance', async () => { + expect(await authIsolated1.hasKey()).to.equal(false); + }); + it('should return true when called on the second isolated instance', async () => { + expect(await authIsolated2.hasKey()).to.equal(true); + }); + }); + describe('.getKey()', () => { + it('should return the key on the original instance', async () => { + expect(await auth.getKey()).to.equal(apiKeyFixtures.apiKey); + }); + it('should reject when called on the first isolated instance', async () => { + await expect(authIsolated1.getKey()).to.eventually.rejected; + }); + it('should return the provided key when called on the second isolated instance ', async () => { + expect(await authIsolated2.getKey()).to.equal( + apiKeyFixtures.apiKey3, + ); + }); + }); + }); + }); }); }); diff --git a/tests/fixtures/api-keys.ts b/tests/fixtures/api-keys.ts index 9706cb4..bfa8eba 100644 --- a/tests/fixtures/api-keys.ts +++ b/tests/fixtures/api-keys.ts @@ -1 +1,3 @@ export const apiKey = 'askjdhas632hsa8shda6'; +export const apiKey2 = 'bskjdhas632hsa8shda6'; +export const apiKey3 = 'cskjdhas632hsa8shda6';