1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
squid-js/test/ocean/OceanAuth.test.ts

103 lines
3.0 KiB
TypeScript
Raw Normal View History

2019-06-20 00:20:09 +02:00
import { assert, expect, spy, use } from 'chai'
2019-11-07 12:24:55 +01:00
import * as spies from 'chai-spies'
2019-05-08 02:53:59 +02:00
2019-06-20 00:20:09 +02:00
import config from '../config'
import Account from '../../src/ocean/Account'
import { Ocean } from '../../src/ocean/Ocean'
import { OceanAuth } from '../../src/ocean/OceanAuth'
2019-05-08 02:53:59 +02:00
use(spies)
2019-06-20 00:20:09 +02:00
describe('OceanAuth', () => {
2019-05-08 02:53:59 +02:00
let oceanAuth: OceanAuth
let account: Account
before(async () => {
const ocean = await Ocean.getInstance(config)
oceanAuth = ocean.auth
account = (await ocean.accounts.list())[0]
})
afterEach(() => {
spy.restore()
})
2019-06-20 00:20:09 +02:00
describe('#get()', () => {
it('should return the token for a account', async () => {
2019-05-08 02:53:59 +02:00
const token = await oceanAuth.get(account)
assert.match(token, /^0x[a-f0-9]{130}-[0-9]{0,14}/i)
})
})
// Not valid using providers without support for `personal_ecRecover`
2019-06-20 00:20:09 +02:00
xdescribe('#check()', () => {
it('should return the account of a signature', async () => {
2019-05-08 02:53:59 +02:00
const token = await oceanAuth.get(account)
const address = await oceanAuth.check(token)
assert.equal(address, account.getId())
})
2019-06-20 00:20:09 +02:00
it('should return empty address if the token is expired', async () => {
2019-05-08 02:53:59 +02:00
const token = [
2019-06-20 00:20:09 +02:00
'0x0cfe59ce5c35461728b4126431096e4e021a842ca1f679532c537be5f895a3607e498',
'f2cc22f787f9c7c8a967c346d717ef50ccb9f0af418d87a86dad899e6d61b-1234567890'
].join('')
2019-05-08 02:53:59 +02:00
const address = await oceanAuth.check(token)
2019-06-20 00:20:09 +02:00
assert.equal(address, `0x${'0'.repeat(40)}`)
2019-05-08 02:53:59 +02:00
})
})
2019-06-20 00:20:09 +02:00
describe('#store()', () => {
it('should sign and store the token', async () => {
2019-09-09 12:18:54 +02:00
const writeTokenSpy = spy.on(oceanAuth as any, 'writeToken', () => {})
2019-05-08 02:53:59 +02:00
await oceanAuth.store(account)
expect(writeTokenSpy).to.has.been.called()
})
})
2019-06-20 00:20:09 +02:00
describe('#restore()', () => {
it('should return a stored token', async () => {
spy.on(oceanAuth as any, 'readToken', () => 'token')
spy.on(oceanAuth as any, 'check', () => account.getId())
2019-05-08 02:53:59 +02:00
const token = await oceanAuth.restore(account)
2019-06-20 00:20:09 +02:00
assert.equal(token, 'token')
2019-05-08 02:53:59 +02:00
})
2019-06-20 00:20:09 +02:00
it('should not return values if there is any error', async () => {
spy.on(oceanAuth as any, 'readToken', () => 'token')
spy.on(oceanAuth as any, 'check', () => '0x...')
2019-05-08 02:53:59 +02:00
const token = await oceanAuth.restore(account)
assert.isUndefined(token)
})
})
2019-06-20 00:20:09 +02:00
describe('#isStored()', () => {
it('should know if the token is stored', async () => {
spy.on(oceanAuth as any, 'restore', () => account.getId())
2019-05-08 02:53:59 +02:00
const isStored = await oceanAuth.isStored(account)
assert.isTrue(isStored)
})
2019-06-20 00:20:09 +02:00
it('should know if the token is not stored', async () => {
spy.on(oceanAuth as any, 'restore', () => undefined)
2019-05-08 02:53:59 +02:00
const isStored = await oceanAuth.isStored(account)
assert.isFalse(isStored)
})
})
})