summaryrefslogtreecommitdiffhomepage
path: root/test/connect.spec.js
blob: a7f991ec502d5caa8005950e2acaaaa5f72ed46c (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// @flow

import { expect } from 'chai';
import connectionActions from '../app/redux/connection/actions';
import { setupBackendAndStore, checkNextTick } from './helpers/ipc-helpers';
import { IpcChain } from './helpers/IpcChain';

describe('connect', () => {

  it('should invoke update_relay_settings and then connect in the backend', (done) => {
    const { store, mockIpc, backend } = setupBackendAndStore();

    const chain = new IpcChain(mockIpc);
    chain.require('updateRelaySettings')
      .withInputValidation(
        relayEndpoint => {
          if (relayEndpoint) {
            expect(relayEndpoint.custom_tunnel_endpoint.host).to.equal(arbitraryRelay);
          } else {
            expect.fail();
          }
        },
      )
      .done();

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

    chain.onSuccessOrFailure(done);

    store.dispatch(connectionActions.connect(backend, arbitraryRelay));
  });

  it('should set the connection state to \'disconnected\' on failed attempts', (done) => {
    const { store, mockIpc, backend } = setupBackendAndStore();

    mockIpc.connect = () => new Promise((_, reject) => reject('Some error'));

    store.dispatch(connectionActions.connected());


    expect(store.getState().connection.status).not.to.equal('disconnected');

    store.dispatch(connectionActions.connect(backend, arbitraryRelay));


    checkNextTick(() => {
      expect(store.getState().connection.status).to.equal('disconnected');
    }, done);
  });

  it('should update the state with the server address', () => {
    const { store, backend } = setupBackendAndStore();

    return backend.connect('www.example.com', 'udp', 1301)
      .then( () => {
        const state = store.getState().connection;
        expect(state.status).to.equal('connecting');
        expect(state.serverAddress).to.equal('www.example.com');
      });
  });

  it('should correctly deduce \'connected\' from backend states', (done) => {
    const { store, mockIpc } = setupBackendAndStore();

    checkNextTick( () => {
      expect(store.getState().connection.status).not.to.equal('connected');
      mockIpc.sendNewState({ state: 'secured', target_state: 'secured' });
      expect(store.getState().connection.status).to.equal('connected');
    }, done);
  });

  it('should correctly deduce \'connecting\' from backend states', (done) => {
    const { store, mockIpc } = setupBackendAndStore();

    checkNextTick( () => {
      expect(store.getState().connection.status).not.to.equal('connecting');
      mockIpc.sendNewState({ state: 'unsecured', target_state: 'secured' });
      expect(store.getState().connection.status).to.equal('connecting');
    }, done);
  });

  it('should correctly deduce \'disconnected\' from backend states', (done) => {
    const { store, mockIpc } = setupBackendAndStore();
    store.dispatch(connectionActions.connected());

    checkNextTick( () => {
      expect(store.getState().connection.status).not.to.equal('disconnected');
      mockIpc.sendNewState({ state: 'unsecured', target_state: 'unsecured' });
      expect(store.getState().connection.status).to.equal('disconnected');
    }, done);
  });
});

const arbitraryRelay = 'www.example.com';