From 4d73eb869705a26bf8a04da7acaba220fbe9a83a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Wed, 8 May 2019 12:07:45 +0200 Subject: [PATCH] Add integration test for Auth module. --- integration/mocha.opts | 1 + integration/ocean/AuthenticationToken.test.ts | 68 +++++++++++++++++++ integration/tsconfig.json | 3 +- package.json | 6 +- src/ocean/OceanAuth.ts | 2 +- test/mocha.opts | 1 + 6 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 integration/ocean/AuthenticationToken.test.ts diff --git a/integration/mocha.opts b/integration/mocha.opts index f4674be..7c3437b 100644 --- a/integration/mocha.opts +++ b/integration/mocha.opts @@ -1,5 +1,6 @@ --require ts-node/register --require source-map-support/register +--require mock-local-storage --full-trace --exit --timeout 300000 diff --git a/integration/ocean/AuthenticationToken.test.ts b/integration/ocean/AuthenticationToken.test.ts new file mode 100644 index 0000000..47324e9 --- /dev/null +++ b/integration/ocean/AuthenticationToken.test.ts @@ -0,0 +1,68 @@ +import { assert } from "chai" + +import { config } from "../config" + +import { Ocean, Account } from "../../src" // @oceanprotocol/squid + +describe("Authentication Token", () => { + let ocean: Ocean + + let account1: Account + let account2: Account + + before(async () => { + localStorage.clear() + + ocean = await Ocean.getInstance(config) + + // Accounts + const accounts = await ocean.accounts.list() + account1 = accounts[0] + account2 = accounts[1] + }) + + it("should generate a token", async () => { + const token = await ocean.auth.get(account1) + + assert.match(token, /^0x[a-f0-9]{130}-[0-9]{0,14}/i) + }) + + it("should return the account that signed the token", async () => { + const token = await ocean.auth.get(account1) + + const address = await ocean.auth.check(token) + + assert.equal(address, account1.getId()) + }) + + it("should store the token for a user", async () => { + await ocean.auth.store(account1) + }) + + it("should restore the token for a user", async () => { + const token = await ocean.auth.restore(account1) + + assert.match(token, /^0x[a-f0-9]{130}-[0-9]{0,14}/i) + }) + + it("should return undefined when is not stored", async () => { + const token = await ocean.auth.restore(account2) + + assert.isUndefined(token) + }) + + it("should know if the is stored", async () => { + let acc1Stored + let acc2Stored + acc1Stored = await ocean.auth.isStored(account1) + acc2Stored = await ocean.auth.isStored(account2) + + assert.isTrue(acc1Stored) + assert.isFalse(acc2Stored) + + await ocean.auth.store(account2) + + acc2Stored = await ocean.auth.isStored(account2) + assert.isTrue(acc2Stored) + }) +}) diff --git a/integration/tsconfig.json b/integration/tsconfig.json index ef87166..e2f2ad2 100644 --- a/integration/tsconfig.json +++ b/integration/tsconfig.json @@ -3,7 +3,8 @@ "resolveJsonModule": true, "lib": [ "es6", - "es7" + "es7", + "dom" ], "noUnusedLocals": true } diff --git a/package.json b/package.json index 1e23389..26be674 100644 --- a/package.json +++ b/package.json @@ -6,9 +6,9 @@ "typings": "./dist/node/squid.d.ts", "browser": "./dist/browser/squid.cjs2.min.js", "scripts": { - "test": "mocha --require mock-local-storage", - "test:watch": "mocha --require mock-local-storage -w --watch-extensions js,ts,json", - "test:cover": "nyc --report-dir coverage/unit mocha --require mock-local-storage", + "test": "mocha", + "test:watch": "mocha -w --watch-extensions js,ts,json", + "test:cover": "nyc --report-dir coverage/unit mocha", "integration": "mocha --opts integration/mocha.opts", "integration:nile": "export NETWORK_NAME=nile; mocha --opts integration/mocha.opts", "integration:duero": "export NETWORK_NAME=duero; mocha --opts integration/mocha.opts", diff --git a/src/ocean/OceanAuth.ts b/src/ocean/OceanAuth.ts index 7a13d7c..1acd9f8 100644 --- a/src/ocean/OceanAuth.ts +++ b/src/ocean/OceanAuth.ts @@ -96,7 +96,7 @@ export class OceanAuth extends Instantiable { * @return {Promise} Is stored and valid. */ public async isStored(account: Account): Promise { - return !!this.restore(account) + return !!await this.restore(account) } private writeToken(address: string, token: string) { diff --git a/test/mocha.opts b/test/mocha.opts index 2e94ff1..c2d7346 100644 --- a/test/mocha.opts +++ b/test/mocha.opts @@ -1,5 +1,6 @@ --require ts-node/register --require source-map-support/register +--require mock-local-storage --full-trace --bail --exit