summaryrefslogtreecommitdiffhomepage
path: root/test/mocks/rpc.js
blob: 4b37ca35aa343eb5dbc8f2a44e073fa0c24e78fe (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
96
97
98
99
100
101
102
103
// @flow
import type {
  DaemonRpcProtocol,
  AccountToken,
  AccountData,
  BackendState,
} from '../../app/lib/daemon-rpc';

interface MockRpc {
  sendNewState: (BackendState) => void;
  -getAccountData: (AccountToken) => Promise<AccountData>;
  -connectTunnel: () => Promise<void>;
  -getAccount: () => Promise<?AccountToken>;
  -authenticate: (string) => Promise<void>;
}

export function newMockRpc() {
  const stateListeners = [];
  const openListeners = [];
  const closeListeners = [];

  const mockIpc: DaemonRpcProtocol & MockRpc = {
    setConnectionString: (_str: string) => {},
    getAccountData: (accountToken) =>
      Promise.resolve({
        accountToken: accountToken,
        expiry: '',
      }),
    getRelayLocations: () =>
      Promise.resolve({
        countries: [],
      }),
    getAccount: () => Promise.resolve('1111'),
    setAccount: () => Promise.resolve(),
    updateRelaySettings: () => Promise.resolve(),
    getRelaySettings: () =>
      Promise.resolve({
        custom_tunnel_endpoint: {
          host: 'www.example.com',
          tunnel: {
            openvpn: {
              port: 1301,
              protocol: 'udp',
            },
          },
        },
      }),
    setAllowLan: (_allowLan: boolean) => Promise.resolve(),
    getAllowLan: () => Promise.resolve(true),
    connect: () => {
      for (const listener of openListeners) {
        listener();
      }
    },
    disconnect: () => {
      for (const listener of closeListeners) {
        listener();
      }
    },
    connectTunnel: () => Promise.resolve(),
    disconnectTunnel: () => Promise.resolve(),
    getLocation: () =>
      Promise.resolve({
        ip: '',
        country: '',
        city: '',
        latitude: 0.0,
        longitude: 0.0,
        mullvad_exit_ip: false,
      }),
    getState: () =>
      Promise.resolve({
        state: 'unsecured',
        target_state: 'unsecured',
      }),
    subscribeStateListener: (listener: (state: ?BackendState, error: ?Error) => void) => {
      stateListeners.push(listener);
      return Promise.resolve();
    },
    sendNewState: (state: BackendState) => {
      for (const listener of stateListeners) {
        listener(state);
      }
    },
    addOpenConnectionObserver: (listener: () => void) => {
      openListeners.push(listener);
      return {
        unsubscribe: () => {},
      };
    },
    addCloseConnectionObserver: (listener: (error: ?Error) => void) => {
      closeListeners.push(listener);
      return {
        unsubscribe: () => {},
      };
    },
    authenticate: (_secret: string) => Promise.resolve(),
    getAccountHistory: () => Promise.resolve([]),
    removeAccountFromHistory: (_accountToken) => Promise.resolve(),
  };

  return mockIpc;
}