2019-06-20 00:20:09 +02:00
|
|
|
import { assert } from 'chai'
|
|
|
|
import { config } from '../config'
|
2020-01-31 00:15:55 +01:00
|
|
|
import { Ocean, Account } from '../../../src' // @oceanprotocol/squid
|
2019-05-08 12:07:45 +02:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
describe('Authentication Token', () => {
|
2019-05-08 12:07:45 +02:00
|
|
|
let ocean: Ocean
|
|
|
|
|
|
|
|
let account1: Account
|
|
|
|
let account2: Account
|
|
|
|
|
|
|
|
before(async () => {
|
2019-05-08 12:29:32 +02:00
|
|
|
try {
|
|
|
|
localStorage.clear()
|
2019-06-20 00:20:09 +02:00
|
|
|
} catch {}
|
2019-05-08 12:07:45 +02:00
|
|
|
|
|
|
|
ocean = await Ocean.getInstance(config)
|
|
|
|
|
|
|
|
// Accounts
|
2019-06-24 13:06:38 +02:00
|
|
|
;[account1, account2] = await ocean.accounts.list()
|
2019-05-08 12:07:45 +02:00
|
|
|
})
|
|
|
|
|
2019-09-13 13:38:23 +02:00
|
|
|
after(async () => {
|
|
|
|
try {
|
|
|
|
localStorage.clear()
|
|
|
|
} catch {}
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should generate a token', async () => {
|
2019-05-08 12:07:45 +02:00
|
|
|
const token = await ocean.auth.get(account1)
|
|
|
|
|
|
|
|
assert.match(token, /^0x[a-f0-9]{130}-[0-9]{0,14}/i)
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should return the account that signed the token', async () => {
|
2019-05-08 12:07:45 +02:00
|
|
|
const token = await ocean.auth.get(account1)
|
|
|
|
|
|
|
|
const address = await ocean.auth.check(token)
|
|
|
|
|
|
|
|
assert.equal(address, account1.getId())
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should store the token for a user', async () => {
|
2019-05-09 16:34:44 +02:00
|
|
|
assert.isUndefined(await account1.getToken())
|
|
|
|
|
2019-05-08 12:07:45 +02:00
|
|
|
await ocean.auth.store(account1)
|
2019-05-09 16:34:44 +02:00
|
|
|
|
|
|
|
assert.match(await account1.getToken(), /^0x[a-f0-9]{130}-[0-9]{0,14}/i)
|
2019-05-08 12:07:45 +02:00
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should restore the token for a user', async () => {
|
2019-05-08 12:07:45 +02:00
|
|
|
const token = await ocean.auth.restore(account1)
|
|
|
|
|
|
|
|
assert.match(token, /^0x[a-f0-9]{130}-[0-9]{0,14}/i)
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should return undefined when is not stored', async () => {
|
2019-05-08 12:07:45 +02:00
|
|
|
const token = await ocean.auth.restore(account2)
|
|
|
|
|
|
|
|
assert.isUndefined(token)
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should know if the token is stored', async () => {
|
2019-08-15 14:13:21 +02:00
|
|
|
let acc1Stored: boolean
|
|
|
|
let acc2Stored: boolean
|
|
|
|
|
|
|
|
// eslint-disable-next-line
|
2019-05-08 12:07:45 +02:00
|
|
|
acc1Stored = await ocean.auth.isStored(account1)
|
|
|
|
acc2Stored = await ocean.auth.isStored(account2)
|
|
|
|
|
|
|
|
assert.isTrue(acc1Stored)
|
2019-05-09 16:34:44 +02:00
|
|
|
assert.isTrue(await account1.isTokenStored())
|
2019-05-08 12:07:45 +02:00
|
|
|
assert.isFalse(acc2Stored)
|
2019-05-09 16:34:44 +02:00
|
|
|
assert.isFalse(await account2.isTokenStored())
|
2019-05-08 12:07:45 +02:00
|
|
|
|
2019-05-20 11:33:15 +02:00
|
|
|
await account2.authenticate()
|
2019-05-08 12:07:45 +02:00
|
|
|
|
|
|
|
acc2Stored = await ocean.auth.isStored(account2)
|
|
|
|
assert.isTrue(acc2Stored)
|
2019-05-09 16:34:44 +02:00
|
|
|
assert.isTrue(await account2.isTokenStored())
|
2019-05-08 12:07:45 +02:00
|
|
|
})
|
|
|
|
})
|