-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
SessionTest.js
111 lines (97 loc) · 4.81 KB
/
SessionTest.js
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
104
105
106
107
108
109
110
111
import Onyx from 'react-native-onyx';
import {fetchAccountDetails, signIn} from '../../src/libs/actions/Session';
import * as API from '../../src/libs/API';
import HttpUtils from '../../src/libs/HttpUtils';
import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';
import ONYXKEYS from '../../src/ONYXKEYS';
// Set up manual mocks for methods used in the actions so our test does not fail.
jest.mock('../../src/libs/Notification/PushNotification', () => ({
// There is no need for a jest.fn() since we don't need to make assertions against it.
register: () => {},
deregister: () => {},
}));
// We are mocking this method so that we can later test to see if it was called and what arguments it was called with.
// We test HttpUtils.xhr() since this means that our API command turned into a network request and isn't only queued.
HttpUtils.xhr = jest.fn();
test('Authenticate is called with saved credentials when a session expires', () => {
const TEST_USER_LOGIN = 'test@testguy.com';
const TEST_USER_ACCOUNT_ID = 1;
const TEST_INITIAL_AUTH_TOKEN = 'initialAuthToken';
const TEST_REFRESHED_AUTH_TOKEN = 'refreshedAuthToken';
// Set up mock responses for all APIs that will be called. The next time this command is called it will return
// jsonCode: 200 and the response here.
HttpUtils.xhr.mockImplementation(() => Promise.resolve({
jsonCode: 200,
accountExists: true,
canAccessExpensifyCash: true,
requiresTwoFactorAuth: false,
}));
let credentials;
Onyx.connect({
key: ONYXKEYS.CREDENTIALS,
callback: val => credentials = val,
});
let session;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: val => session = val,
});
// Simulate user entering their login and populating the credentials.login
fetchAccountDetails(TEST_USER_LOGIN);
// Note: In order for this test to work we must return a promise! It will pass even with
// failing assertions if we remove the return keyword.
return waitForPromisesToResolve()
.then(() => {
// Next we will simulate signing in and make sure all API calls in this flow succeed. Every time we add
// a mockImplementationOnce() we are altering what Network.post() will return.
HttpUtils.xhr
// First call to Authenticate
.mockImplementationOnce(() => Promise.resolve({
jsonCode: 200,
accountID: TEST_USER_ACCOUNT_ID,
authToken: TEST_INITIAL_AUTH_TOKEN,
email: TEST_USER_LOGIN,
}))
// Next call to CreateLogin
.mockImplementationOnce(() => Promise.resolve({
jsonCode: 200,
accountID: TEST_USER_ACCOUNT_ID,
authToken: TEST_INITIAL_AUTH_TOKEN,
email: TEST_USER_LOGIN,
}));
signIn('Password1');
return waitForPromisesToResolve();
})
.then(() => {
// Verify that our credentials were saved and that our session data is correct
expect(credentials.login).toBe(TEST_USER_LOGIN);
expect(credentials.autoGeneratedLogin).not.toBeUndefined();
expect(credentials.autoGeneratedPassword).not.toBeUndefined();
expect(session.authToken).toBe(TEST_INITIAL_AUTH_TOKEN);
expect(session.accountID).toBe(TEST_USER_ACCOUNT_ID);
expect(session.email).toBe(TEST_USER_LOGIN);
// At this point we have an authToken. To simulate it expiring we'll just make another
// request and mock the response so it returns 407. Once this happens we should attempt
// to Re-Authenticate with the stored credentials. Our next call will be to Authenticate
// so we will mock that response with a new authToken and then verify that Onyx has our
// data.
HttpUtils.xhr
// This will make the call to API.Get() below return with an expired session code
.mockImplementationOnce(() => Promise.resolve({
jsonCode: 407,
}))
// The next call should be Authenticate since we are reauthenticating
.mockImplementationOnce(() => Promise.resolve({
jsonCode: 200,
accountID: TEST_USER_ACCOUNT_ID,
authToken: TEST_REFRESHED_AUTH_TOKEN,
email: TEST_USER_LOGIN,
}));
API.Get({returnValueList: 'chatList'});
return waitForPromisesToResolve();
})
.then(() => {
// Finally, we will check our Onyx data to make sure the new authToken is present
expect(session.authToken).toBe(TEST_REFRESHED_AUTH_TOKEN);
});
});