forked from FormidableLabs/react-native-app-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.js
131 lines (111 loc) · 3.67 KB
/
index.spec.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import AppAuth from './';
jest.mock('react-native', () => ({
NativeModules: {
RNAppAuth: {
authorize: jest.fn(),
refresh: jest.fn()
}
}
}));
describe('AppAuth', () => {
let mockAuthorize;
let mockRefresh;
const config = {
issuer: 'test-issuer',
redirectUrl: 'test-redirectUrl',
clientId: 'test-clientId'
};
beforeAll(() => {
mockAuthorize = require('react-native').NativeModules.RNAppAuth.authorize;
mockAuthorize.mockReturnValue('AUTHORIZED');
mockRefresh = require('react-native').NativeModules.RNAppAuth.refresh;
mockRefresh.mockReturnValue('REFRESHED');
});
describe('when initialising a new instance', () => {
it('saves the config correctly', () => {
const appAuth = new AppAuth(config);
expect(appAuth.getConfig()).toEqual(config);
});
it('throws an error when issuer is not a string', () => {
expect(() => {
new AppAuth({ ...config, issuer: () => ({}) }); // eslint-disable-line no-new
}).toThrow('Config error: issuer must be a string');
});
it('throws an error when redirectUrl is not a string', () => {
expect(() => {
new AppAuth({ ...config, redirectUrl: {} }); // eslint-disable-line no-new
}).toThrow('Config error: redirectUrl must be a string');
});
it('throws an error when clientId is not a string', () => {
expect(() => {
new AppAuth({ ...config, clientId: 123 }); // eslint-disable-line no-new
}).toThrow('Config error: clientId must be a string');
});
});
describe('authorize', () => {
beforeEach(() => {
mockAuthorize.mockReset();
mockRefresh.mockReset();
});
const scopes = ['my-scope'];
it('throws an error when no scopes are passed in', () => {
const appAuth = new AppAuth(config);
expect(() => {
appAuth.authorize();
}).toThrow('Scope error: please add at least one scope');
});
it('throws an error when an empty scope array is passed in', () => {
const appAuth = new AppAuth(config);
expect(() => {
appAuth.authorize([]);
}).toThrow('Scope error: please add at least one scope');
});
it('calls the native wrapper with the correct args', () => {
const appAuth = new AppAuth(config);
appAuth.authorize(scopes);
expect(mockAuthorize).toHaveBeenCalledWith(
config.issuer,
config.redirectUrl,
config.clientId,
scopes
);
});
});
describe('refresh', () => {
beforeEach(() => {
mockAuthorize.mockReset();
mockRefresh.mockReset();
});
const refreshToken = 'my-sample-token';
const scopes = ['my-scope'];
it('throws an error when no refreshToken is passed in', () => {
const appAuth = new AppAuth(config);
expect(() => {
appAuth.refresh();
}).toThrow('Please pass in a refresh token');
});
it('throws an error when no scopes are passed in', () => {
const appAuth = new AppAuth(config);
expect(() => {
appAuth.refresh(refreshToken);
}).toThrow('Scope error: please add at least one scope');
});
it('throws an error when an empty scope array is passed in', () => {
const appAuth = new AppAuth(config);
expect(() => {
appAuth.refresh(refreshToken, []);
}).toThrow('Scope error: please add at least one scope');
});
it('calls the native wrapper with the correct args', () => {
const appAuth = new AppAuth(config);
appAuth.refresh(refreshToken, scopes);
expect(mockRefresh).toHaveBeenCalledWith(
config.issuer,
config.redirectUrl,
config.clientId,
refreshToken,
scopes
);
});
});
});