summaryrefslogtreecommitdiffhomepage
path: root/test/auth.spec.js
blob: c3fd78c83d99550260dd84658c262fbca002ce60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// @flow

import { expect } from 'chai';
import { setupIpcAndStore, setupBackendAndStore, failFast, checkNextTick } from './helpers/ipc-helpers';
import { IpcChain } from './helpers/IpcChain';
import { Backend } from '../app/lib/backend';

describe('authentication', () => {

  it('authenticates before ipc call if unauthenticated', (done) => {
    const { store, mockIpc } = setupIpcAndStore();
    const credentials = {
      sharedSecret: 'foo',
      connectionString: '',
    };


    const chain = new IpcChain(mockIpc);
    chain.require('authenticate')
      .withInputValidation( secret => {
        expect(secret).to.equal(credentials.sharedSecret);
      })
      .done();

    chain.require('connect')
      .done();

    chain.onSuccessOrFailure(done);


    const backend = new Backend(store, credentials, mockIpc);
    backend.connect();
  });

  it('reauthenticates on reconnect', (done) => {
    const { mockIpc, backend } = setupBackendAndStore();

    let authCount = 0;
    mockIpc.authenticate = () => {
      authCount++;
      return Promise.resolve();
    };


    mockIpc.killWebSocket();
    failFast(() => {
      expect(authCount).to.equal(0);
    }, done);


    backend.connect();
    checkNextTick(() => {
      expect(authCount).to.equal(1);
    }, done);
  });
});