forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionTest.js
108 lines (94 loc) · 5.03 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
import {beforeEach, jest, test} from '@jest/globals';
import Onyx from 'react-native-onyx';
import CONST from '../../src/CONST';
import * as App from '../../src/libs/actions/App';
import OnyxUpdateManager from '../../src/libs/actions/OnyxUpdateManager';
import HttpUtils from '../../src/libs/HttpUtils';
import PushNotification from '../../src/libs/Notification/PushNotification';
// This lib needs to be imported, but it has nothing to export since all it contains is an Onyx connection
// eslint-disable-next-line no-unused-vars
import subscribePushNotification from '../../src/libs/Notification/PushNotification/subscribePushNotification';
import ONYXKEYS from '../../src/ONYXKEYS';
import * as TestHelper from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
// 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();
// Mocked to ensure push notifications are subscribed/unsubscribed as the session changes
jest.mock('../../src/libs/Notification/PushNotification');
Onyx.init({
keys: ONYXKEYS,
registerStorageEventListener: () => {},
});
OnyxUpdateManager();
beforeEach(() => Onyx.clear().then(waitForBatchedUpdates));
describe('Session', () => {
test('Authenticate is called with saved credentials when a session expires', () => {
// Given a test user and set of authToken with subscriptions to session and credentials
const TEST_USER_LOGIN = '[email protected]';
const TEST_USER_ACCOUNT_ID = 1;
const TEST_INITIAL_AUTH_TOKEN = 'initialAuthToken';
const TEST_REFRESHED_AUTH_TOKEN = 'refreshedAuthToken';
let credentials;
Onyx.connect({
key: ONYXKEYS.CREDENTIALS,
callback: (val) => (credentials = val || {}),
});
let session;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (val) => (session = val),
});
// When we sign in with the test user
return TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN, 'Password1', TEST_INITIAL_AUTH_TOKEN)
.then(waitForBatchedUpdates)
.then(() => {
// Then our re-authentication credentials should be generated and our session data
// have the correct information + initial authToken.
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 OpenApp below return with an expired session code
.mockImplementationOnce(() =>
Promise.resolve({
jsonCode: CONST.JSON_CODE.NOT_AUTHENTICATED,
}),
)
// The next call should be Authenticate since we are reauthenticating
.mockImplementationOnce(() =>
Promise.resolve({
jsonCode: CONST.JSON_CODE.SUCCESS,
accountID: TEST_USER_ACCOUNT_ID,
authToken: TEST_REFRESHED_AUTH_TOKEN,
email: TEST_USER_LOGIN,
}),
);
// When we attempt to fetch the initial app data via the API
App.confirmReadyToOpenApp();
App.openApp();
return waitForBatchedUpdates();
})
.then(() => {
// Then it should fail and reauthenticate the user adding the new authToken to the session
// data in Onyx
expect(session.authToken).toBe(TEST_REFRESHED_AUTH_TOKEN);
});
});
test('Push notifications are subscribed after signing in', () =>
TestHelper.signInWithTestUser()
.then(waitForBatchedUpdates)
.then(() => expect(PushNotification.register).toBeCalled()));
test('Push notifications are unsubscribed after signing out', () =>
TestHelper.signInWithTestUser()
.then(TestHelper.signOutTestUser)
.then(() => expect(PushNotification.deregister).toBeCalled()));
});