@amedia/user 0.14.0 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/autologin/index.spec.ts +13 -0
- package/src/autologin/index.ts +10 -0
- package/src/clients/pluto.spec.ts +96 -1
- package/src/clients/pluto.ts +8 -0
package/package.json
CHANGED
|
@@ -148,6 +148,19 @@ describe('attemptAutoLoginJob', () => {
|
|
|
148
148
|
itShouldNotNavigateAway();
|
|
149
149
|
});
|
|
150
150
|
});
|
|
151
|
+
|
|
152
|
+
describe('when in aid emergency mode', () => {
|
|
153
|
+
beforeEach(async () => {
|
|
154
|
+
store.merge('state', {
|
|
155
|
+
emergencyMode: ['aid'],
|
|
156
|
+
});
|
|
157
|
+
await attemptAutoLoginJob();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('should not try third party auto login', () => {
|
|
161
|
+
expect(thirdPartyCookie.default).not.toHaveBeenCalled();
|
|
162
|
+
});
|
|
163
|
+
});
|
|
151
164
|
});
|
|
152
165
|
});
|
|
153
166
|
|
package/src/autologin/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { getAidDomain, getCurrentUrl } from '../utils/url';
|
|
|
3
3
|
import { navigateTo } from '../utils/location';
|
|
4
4
|
import { getCookie, removeCookie, setCookie } from '../lib/cookie';
|
|
5
5
|
import UserDataRequest from '../lib/UserDataRequest';
|
|
6
|
+
import store from '../store';
|
|
6
7
|
|
|
7
8
|
export type Strategies = Array<{
|
|
8
9
|
name: string;
|
|
@@ -35,7 +36,16 @@ function hasAlreadyAttemptedAutoLogin() {
|
|
|
35
36
|
return false;
|
|
36
37
|
}
|
|
37
38
|
|
|
39
|
+
function isInAidEmergencyMode() {
|
|
40
|
+
return store.get('state')?.emergencyMode?.includes('aid');
|
|
41
|
+
}
|
|
42
|
+
|
|
38
43
|
const attemptAutologin = async (strategies: Strategies) => {
|
|
44
|
+
if (isInAidEmergencyMode()) {
|
|
45
|
+
// Avoid auto-login if we are in aid emergency mode
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
39
49
|
if (typeof window === 'undefined' || inIframe()) {
|
|
40
50
|
// Avoid auto-login on preview for dr edition and in workers.
|
|
41
51
|
return;
|
|
@@ -4,6 +4,7 @@ import store from '../store';
|
|
|
4
4
|
import { getCurrentSession, removeSession } from './pluto';
|
|
5
5
|
import { Logger, setLogger } from '../utils/log';
|
|
6
6
|
import { UserAttributes } from '../types';
|
|
7
|
+
import anything = jasmine.anything;
|
|
7
8
|
|
|
8
9
|
beforeEach(async () => {
|
|
9
10
|
await cache.clear();
|
|
@@ -42,7 +43,7 @@ function itShouldNotHaveLoggedAnything() {
|
|
|
42
43
|
});
|
|
43
44
|
}
|
|
44
45
|
|
|
45
|
-
describe('Pluto
|
|
46
|
+
describe('Pluto client', () => {
|
|
46
47
|
describe('getCurrentSession()', () => {
|
|
47
48
|
beforeEach(() => {
|
|
48
49
|
jest.spyOn(store, 'merge');
|
|
@@ -66,6 +67,21 @@ describe('Pluto service', () => {
|
|
|
66
67
|
});
|
|
67
68
|
});
|
|
68
69
|
}
|
|
70
|
+
function itShouldNotSetEmergencyModeState() {
|
|
71
|
+
it('should not set emergency mode state', () => {
|
|
72
|
+
expect(store.merge).not.toHaveBeenCalledWith('state', {
|
|
73
|
+
emergencyMode: anything(),
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function itShouldSetEmergencyModeState(emergencyModes: string[]) {
|
|
79
|
+
it('should set emergency mode states', () => {
|
|
80
|
+
expect(store.merge).toHaveBeenCalledWith('state', {
|
|
81
|
+
emergencyMode: emergencyModes,
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
69
85
|
|
|
70
86
|
describe('when user is logged in ', () => {
|
|
71
87
|
describe('When cache is empty', () => {
|
|
@@ -77,6 +93,7 @@ describe('Pluto service', () => {
|
|
|
77
93
|
expect(fetch).toHaveBeenCalledTimes(1);
|
|
78
94
|
});
|
|
79
95
|
itShouldReturnSessionData();
|
|
96
|
+
itShouldNotSetEmergencyModeState();
|
|
80
97
|
});
|
|
81
98
|
describe('When cache is not empty', () => {
|
|
82
99
|
beforeEach(async () => {
|
|
@@ -87,8 +104,46 @@ describe('Pluto service', () => {
|
|
|
87
104
|
expect(fetch).toHaveBeenCalledTimes(0);
|
|
88
105
|
});
|
|
89
106
|
itShouldReturnSessionData();
|
|
107
|
+
itShouldNotSetEmergencyModeState();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('when single emergency mode is set', () => {
|
|
111
|
+
beforeEach(async () => {
|
|
112
|
+
fetch.mockResponseOnce(() =>
|
|
113
|
+
Promise.resolve({
|
|
114
|
+
init: {
|
|
115
|
+
status: 200,
|
|
116
|
+
headers: {
|
|
117
|
+
'x-emergency-mode': 'aid',
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
body: JSON.stringify(plutoLoggedInResponse),
|
|
121
|
+
})
|
|
122
|
+
);
|
|
123
|
+
currentSession = await getCurrentSession();
|
|
124
|
+
});
|
|
125
|
+
itShouldSetEmergencyModeState(['aid']);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe('when multiple emergency modes is set', () => {
|
|
129
|
+
beforeEach(async () => {
|
|
130
|
+
fetch.mockResponseOnce(() =>
|
|
131
|
+
Promise.resolve({
|
|
132
|
+
init: {
|
|
133
|
+
status: 200,
|
|
134
|
+
headers: {
|
|
135
|
+
'x-emergency-mode': 'aid,paywall',
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
body: JSON.stringify(plutoLoggedInResponse),
|
|
139
|
+
})
|
|
140
|
+
);
|
|
141
|
+
currentSession = await getCurrentSession();
|
|
142
|
+
});
|
|
143
|
+
itShouldSetEmergencyModeState(['aid', 'paywall']);
|
|
90
144
|
});
|
|
91
145
|
});
|
|
146
|
+
|
|
92
147
|
describe('when user is not logged in', () => {
|
|
93
148
|
describe('When cache is empty', () => {
|
|
94
149
|
beforeEach(async () => {
|
|
@@ -105,6 +160,7 @@ describe('Pluto service', () => {
|
|
|
105
160
|
});
|
|
106
161
|
itShouldNotHaveLoggedAnything();
|
|
107
162
|
itShouldReturnNoSessionData();
|
|
163
|
+
itShouldNotSetEmergencyModeState();
|
|
108
164
|
});
|
|
109
165
|
describe('When cache is not empty', () => {
|
|
110
166
|
beforeEach(async () => {
|
|
@@ -116,6 +172,7 @@ describe('Pluto service', () => {
|
|
|
116
172
|
});
|
|
117
173
|
itShouldNotHaveLoggedAnything();
|
|
118
174
|
itShouldReturnNoSessionData();
|
|
175
|
+
itShouldNotSetEmergencyModeState();
|
|
119
176
|
});
|
|
120
177
|
describe('when session is invalid for domain', () => {
|
|
121
178
|
beforeEach(async () => {
|
|
@@ -137,6 +194,7 @@ describe('Pluto service', () => {
|
|
|
137
194
|
}
|
|
138
195
|
);
|
|
139
196
|
});
|
|
197
|
+
itShouldNotSetEmergencyModeState();
|
|
140
198
|
});
|
|
141
199
|
describe('when session is no longer valid', () => {
|
|
142
200
|
beforeEach(async () => {
|
|
@@ -149,6 +207,43 @@ describe('Pluto service', () => {
|
|
|
149
207
|
currentSession = await getCurrentSession();
|
|
150
208
|
});
|
|
151
209
|
itShouldNotHaveLoggedAnything();
|
|
210
|
+
itShouldNotSetEmergencyModeState();
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
describe('when single emergency mode is set', () => {
|
|
214
|
+
beforeEach(async () => {
|
|
215
|
+
fetch.mockResponseOnce(() =>
|
|
216
|
+
Promise.resolve({
|
|
217
|
+
init: {
|
|
218
|
+
status: 403,
|
|
219
|
+
headers: {
|
|
220
|
+
'x-emergency-mode': 'aid',
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
body: '{"errors": [{"code": "session.invalid_for_domain"}]}',
|
|
224
|
+
})
|
|
225
|
+
);
|
|
226
|
+
currentSession = await getCurrentSession();
|
|
227
|
+
});
|
|
228
|
+
itShouldSetEmergencyModeState(['aid']);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
describe('when multiple emergency modes is set', () => {
|
|
232
|
+
beforeEach(async () => {
|
|
233
|
+
fetch.mockResponseOnce(() =>
|
|
234
|
+
Promise.resolve({
|
|
235
|
+
init: {
|
|
236
|
+
status: 403,
|
|
237
|
+
headers: {
|
|
238
|
+
'x-emergency-mode': 'aid,paywall',
|
|
239
|
+
},
|
|
240
|
+
},
|
|
241
|
+
body: '{"errors": [{"code": "session.invalid_for_domain"}]}',
|
|
242
|
+
})
|
|
243
|
+
);
|
|
244
|
+
currentSession = await getCurrentSession();
|
|
245
|
+
});
|
|
246
|
+
itShouldSetEmergencyModeState(['aid', 'paywall']);
|
|
152
247
|
});
|
|
153
248
|
});
|
|
154
249
|
});
|
package/src/clients/pluto.ts
CHANGED
|
@@ -71,6 +71,14 @@ export const resolveBadPlutoResponse = async (response: Response) => {
|
|
|
71
71
|
const fetchCurrentSession = () =>
|
|
72
72
|
fetch(`/api/pluto/v1/sessions/current`)
|
|
73
73
|
.then((response) => {
|
|
74
|
+
const emergencyMode = response.headers
|
|
75
|
+
.get('x-emergency-mode')
|
|
76
|
+
?.split(',');
|
|
77
|
+
if (emergencyMode) {
|
|
78
|
+
store.merge('state', {
|
|
79
|
+
emergencyMode,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
74
82
|
if (!response.ok) {
|
|
75
83
|
return resolveBadPlutoResponse(response).then(() => ({}));
|
|
76
84
|
}
|