import UUID from 'uuid/v1' import Merchant from '../../src/models/merchant' import { deleteAll } from '../helpers/database' import { isNil } from 'ramda' describe(`Test merchant's class`, () => { beforeAll(async () => { await deleteAll() }) afterAll(async () => { await deleteAll() }) it('Instanciate class', () => { expect(new Merchant()).toBeInstanceOf(Merchant) }) it('Create a new Merchant', async () => { try { const opt = { id: UUID() } const merchant = await Merchant.create(opt) expect(merchant).toBeInstanceOf(Merchant) expect(merchant.updatedAt).toBeNull() expect(merchant.id).toBe(opt.id) } catch (err) { expect(isNil(err)).toBe(true) } }) it('Fails creating two same merchants', async () => { try { const opt = { id: UUID() } const merchant = await Merchant.create(opt) expect(merchant).toBeInstanceOf(Merchant) expect(merchant.updatedAt).toBeNull() expect(merchant.id).toBe(opt.id) try { await Merchant.create(opt) } catch (e) { expect(e).toBeInstanceOf(Error) expect(e.message).toBe('duplicate key value violates unique constraint "merchants_pkey"') } } catch (err) { expect(isNil(err)).toBe(true) } }) }) describe('Testing secrets', () => { it('Renew secrets', async () => { try { const merchant = await Merchant.create({ id: UUID() }) const initKeys = { first: merchant.firstKey, second: merchant.secondKey, } await merchant.renewKeys() const renewedKeys = { first: merchant.firstKey, second: merchant.secondKey, } expect(renewedKeys).not.toEqual(initKeys) } catch (err) { expect(isNil(err)).toBe(true) } }) it('Signs a string', async () => { try { const merchant = await Merchant.create({ id: UUID() }) const decodedString = 'decoded string' const token = await merchant.sign(decodedString) const decoded = await merchant.verify(token) expect(decoded).toBe(decodedString) } catch (err) { expect(isNil(err)).toBe(true) } }) it('Failed decoding a string due to new keys', async () => { try { const merchant = await Merchant.create({ id: UUID() }) const decodedString = 'decoded string' const token = await merchant.sign(decodedString) await merchant.renewKeys() const decoded = await merchant.verify(token) expect(decoded).not.toBe(decodedString) } catch (err) { expect(err.name).toBe('JsonWebTokenError') expect(err.message).toBe('invalid signature') } }) it('decodes a string after rotate keys', async () => { try { const merchant = await Merchant.create({ id: UUID() }) const decodedString = 'decoded string' const token = await merchant.sign(decodedString) await merchant.rotateKeys() const decoded = await merchant.verify(token) expect(decoded).toBe(decodedString) } catch (err) { expect(isNil(err)).toBe(true) } }) it('fails decoding a string after 2 rotation', async () => { try { const merchant = await Merchant.create({ id: UUID() }) const decodedString = 'decoded string' const token = await merchant.sign(decodedString) await merchant.rotateKeys() const decoded = await merchant.verify(token) expect(decoded).toBe(decodedString) try { await merchant.rotateKeys() } catch (err) { expect(err.name).toBe('JsonWebTokenError') expect(err.message).toBe('invalid signature') } } catch (err) { expect(isNil(err)).toBe(true) } }) })