Skip to content

Commit

Permalink
Add support for isolated instances by passing dataDirectory: false
Browse files Browse the repository at this point in the history
Depends-on: balena-io-modules/balena-settings-storage#37
Change-type: minor
  • Loading branch information
thgreasi committed Jul 28, 2023
1 parent 624fa45 commit 2cc4958
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ It accepts the following params:
| Param | Type | Description |
| --- | --- | --- |
| options | <code>Object</code> | options |
| options.dataDirectory | <code>string</code> | the directory to use for storage in Node.js. Ignored in the browser. |
| options.tokenKey | <code>string</code> | the key used to store the last token in the storage. `token` by default. |
| [options.dataDirectory] | <code>string</code> \| <code>false</code> | 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] | <code>string</code> | the key used to store the last token in the storage. `token` by default. |

**Example**
```js
Expand Down
4 changes: 2 additions & 2 deletions doc/README.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ It accepts the following params:
| Param | Type | Description |
| --- | --- | --- |
| options | <code>Object</code> | options |
| options.dataDirectory | <code>string</code> | the directory to use for storage in Node.js. Ignored in the browser. |
| options.tokenKey | <code>string</code> | the key used to store the last token in the storage. `token` by default. |
| [options.dataDirectory] | <code>string</code> \| <code>false</code> | 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] | <code>string</code> | the key used to store the last token in the storage. `token` by default. |
**Example**
```js
Expand Down
4 changes: 3 additions & 1 deletion lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <hello@balena.io>",
Expand Down Expand Up @@ -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"
},
Expand Down
129 changes: 129 additions & 0 deletions tests/03-auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,5 +262,134 @@ describe('BalenaAuth', () => {
});
});
});

describe('using dataDirectory: false and the same tokenKey', function () {
let authIsolated1: InstanceType<typeof BalenaAuth>;
let authIsolated2: InstanceType<typeof BalenaAuth>;
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,
);
});
});
});
});
});
});
2 changes: 2 additions & 0 deletions tests/fixtures/api-keys.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const apiKey = 'askjdhas632hsa8shda6';
export const apiKey2 = 'bskjdhas632hsa8shda6';
export const apiKey3 = 'cskjdhas632hsa8shda6';

0 comments on commit 2cc4958

Please sign in to comment.