@amedia/user 0.12.18 → 0.12.20

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.
Files changed (44) hide show
  1. package/dist/actions/authentication.d.ts +0 -1
  2. package/dist/actions/authorization.d.ts +8 -3
  3. package/dist/clients/jupiter.d.ts +2 -2
  4. package/dist/eventHandlers/eventMap.d.ts +25 -0
  5. package/dist/eventHandlers/state.d.ts +1 -0
  6. package/dist/eventHandlers/state.spec.d.ts +4 -0
  7. package/dist/eventHandlers/storage.d.ts +1 -0
  8. package/dist/eventHandlers/storage.spec.d.ts +4 -0
  9. package/dist/eventHandlers/userAttributes.d.ts +0 -23
  10. package/dist/exceptions/PaywallError.d.ts +1 -0
  11. package/dist/index.d.ts +2 -2
  12. package/dist/index.js +1 -2
  13. package/dist/lib/PaywallUnlockRequest.d.ts +16 -0
  14. package/dist/lib/PaywallUnlockRequest.spec.d.ts +4 -0
  15. package/dist/lib/UserDataRequest.d.ts +1 -0
  16. package/dist/lib/UserDataRequest.spec.d.ts +4 -0
  17. package/dist/store/index.d.ts +1 -1
  18. package/package.json +1 -1
  19. package/src/actions/authentication.ts +0 -3
  20. package/src/actions/authorization.ts +17 -9
  21. package/src/clients/jupiter.spec.ts +4 -4
  22. package/src/clients/jupiter.ts +3 -4
  23. package/src/eventHandlers/eventMap.ts +29 -0
  24. package/src/eventHandlers/state.spec.ts +59 -0
  25. package/src/eventHandlers/state.ts +23 -0
  26. package/src/eventHandlers/storage.spec.ts +106 -0
  27. package/src/eventHandlers/storage.ts +35 -0
  28. package/src/eventHandlers/userAttributes.spec.ts +5 -103
  29. package/src/eventHandlers/userAttributes.ts +1 -66
  30. package/src/exceptions/PaywallError.ts +3 -0
  31. package/src/index-storage.spec.ts +2 -2
  32. package/src/index-subscription.spec.ts +2 -0
  33. package/src/index.ts +3 -4
  34. package/src/janitor/jobs/accessCookieRefresher.spec.ts +6 -2
  35. package/src/janitor/jobs/removeUserStorage.spec.ts +4 -1
  36. package/src/lib/PaywallUnlockRequest.spec.ts +343 -0
  37. package/src/lib/PaywallUnlockRequest.ts +121 -0
  38. package/src/lib/UserDataRequest.spec.ts +58 -0
  39. package/src/lib/UserDataRequest.ts +40 -3
  40. package/src/store/index.ts +1 -1
  41. package/src/utils/loginState.ts +3 -1
  42. package/amedia-user-js.iml +0 -9
  43. package/src/lib/PaywallUnlocker.spec.ts +0 -196
  44. package/src/lib/PaywallUnlocker.ts +0 -113
@@ -1,196 +0,0 @@
1
- /**
2
- * @jest-environment jsdom
3
- */
4
-
5
- import fetch from 'jest-fetch-mock';
6
- import PaywallUnlocker from './PaywallUnlocker';
7
- import * as authentication from '../actions/authentication';
8
- import * as authorization from '../actions/authorization';
9
- import * as pluto from '../clients/pluto';
10
- import * as location from '../utils/location';
11
- import store from '../store';
12
- import PaywallError from '../exceptions/PaywallError';
13
- import restoreAllMocks = jest.restoreAllMocks;
14
- import spyOn = jest.spyOn;
15
-
16
- beforeEach(() => {
17
- restoreAllMocks();
18
- fetch.resetMocks();
19
- });
20
-
21
- describe('PaywallUnlocker', () => {
22
- it('should fail if already tried to unlock paywall', () => {
23
- spyOn(authorization, 'hasRecentlyTriedToUnlockPaywall').mockReturnValue(
24
- true
25
- );
26
- expect(new PaywallUnlocker().tryUnlock()).rejects.toThrow(
27
- new PaywallError(
28
- 'Unlock check already performed.',
29
- PaywallError.ERROR_RELOADED_NO_ACCESS
30
- )
31
- );
32
- });
33
-
34
- it('should return no access if not logged in', () => {
35
- spyOn(store, 'get').mockImplementation((key) => {
36
- if (key === 'state') {
37
- return { isLoggedIn: false };
38
- }
39
- throw new Error('Mock not setup for this');
40
- });
41
- spyOn(pluto, 'getCurrentSession').mockResolvedValue({});
42
-
43
- expect(new PaywallUnlocker().tryUnlock())
44
- .resolves.toEqual({
45
- isLoggedIn: false,
46
- hasAccess: false,
47
- accessFeatures: [],
48
- })
49
- .then(() => {
50
- expect(pluto.getCurrentSession).toHaveBeenCalledTimes(1);
51
- });
52
- });
53
-
54
- it('should return positive of has access', () => {
55
- spyOn(authentication, 'isLoggedIn').mockReturnValue(true);
56
- spyOn(authorization, 'getAccess').mockResolvedValue({
57
- access: ['newspaper'],
58
- });
59
- spyOn(authorization, 'setAccessCookies').mockResolvedValue(true);
60
- spyOn(authorization, 'setHasTriedToUnlockPaywall');
61
-
62
- expect(new PaywallUnlocker().tryUnlock())
63
- .resolves.toEqual({
64
- isLoggedIn: true,
65
- hasAccess: true,
66
- accessFeatures: ['newspaper'],
67
- })
68
- .then(() =>
69
- expect(authorization.setHasTriedToUnlockPaywall).toHaveBeenCalled()
70
- );
71
- });
72
-
73
- it('should try to activate if missing access', (done) => {
74
- spyOn(authentication, 'isLoggedIn').mockReturnValue(true);
75
- spyOn(authorization, 'getAccess')
76
- .mockResolvedValueOnce({ access: [] })
77
- .mockResolvedValueOnce({ access: ['newspaper'] });
78
- spyOn(authorization, 'setAccessCookies').mockResolvedValue(true);
79
- spyOn(authorization, 'setHasTriedToUnlockPaywall');
80
- spyOn(authorization, 'activate').mockResolvedValue(true);
81
-
82
- expect(new PaywallUnlocker().tryUnlock())
83
- .resolves.toEqual({
84
- isLoggedIn: true,
85
- hasAccess: true,
86
- accessFeatures: ['newspaper'],
87
- })
88
- .then(() => {
89
- expect(authorization.activate).toHaveBeenCalled();
90
- done();
91
- });
92
- });
93
-
94
- it('should try to activate if missing access, but really has no access', (done) => {
95
- spyOn(authentication, 'isLoggedIn').mockReturnValue(true);
96
- spyOn(authorization, 'getAccess').mockResolvedValueOnce({ access: [] });
97
- spyOn(authorization, 'activate').mockResolvedValue(false);
98
-
99
- expect(new PaywallUnlocker().tryUnlock())
100
- .resolves.toEqual({
101
- isLoggedIn: true,
102
- hasAccess: false,
103
- accessFeatures: [],
104
- })
105
- .then(() => {
106
- expect(authorization.activate).toHaveBeenCalled();
107
- done();
108
- });
109
- });
110
-
111
- it("should return what the user has access to even if it's not enough", (done) => {
112
- spyOn(authentication, 'isLoggedIn').mockReturnValue(true);
113
- spyOn(authorization, 'getAccess').mockResolvedValueOnce({
114
- access: ['newspaper'],
115
- });
116
- spyOn(authorization, 'activate').mockResolvedValue(false);
117
-
118
- expect(new PaywallUnlocker().withAccessFeatures(['plussalt']).tryUnlock())
119
- .resolves.toEqual({
120
- isLoggedIn: true,
121
- hasAccess: false,
122
- accessFeatures: ['newspaper'],
123
- })
124
- .then(() => {
125
- expect(authorization.activate).toHaveBeenCalled();
126
- done();
127
- });
128
- });
129
-
130
- it('should throw error if failing to set access cookies', () => {
131
- spyOn(authentication, 'isLoggedIn').mockReturnValue(true);
132
- spyOn(authorization, 'getAccess').mockResolvedValue({
133
- access: ['newspaper'],
134
- });
135
- spyOn(authorization, 'setAccessCookies').mockResolvedValue(false);
136
- spyOn(authorization, 'setHasTriedToUnlockPaywall');
137
-
138
- expect(new PaywallUnlocker().tryUnlock())
139
- .rejects.toThrow(
140
- new PaywallError(
141
- 'Could not set access cookies',
142
- PaywallError.ERROR_ENABLING_ACCESS_FAILED
143
- )
144
- )
145
- .then(() =>
146
- expect(authorization.setHasTriedToUnlockPaywall).not.toHaveBeenCalled()
147
- );
148
- });
149
-
150
- it('should perform reload if requested', (done) => {
151
- spyOn(authentication, 'isLoggedIn').mockReturnValue(true);
152
- spyOn(authorization, 'getAccess').mockResolvedValue({
153
- access: ['newspaper'],
154
- });
155
- spyOn(authorization, 'setAccessCookies').mockResolvedValue(true);
156
- spyOn(location, 'reload').mockReturnValue();
157
- spyOn(location, 'replace').mockReturnValue();
158
-
159
- new PaywallUnlocker()
160
- .reloadOnAccess()
161
- .tryUnlock()
162
- .then(() => {
163
- expect(location.reload).toHaveBeenCalled();
164
- expect(location.replace).not.toHaveBeenCalled();
165
- done();
166
- });
167
- });
168
-
169
- it('should perform redirect if requested', async () => {
170
- spyOn(authentication, 'isLoggedIn').mockReturnValue(true);
171
- spyOn(authorization, 'getAccess').mockResolvedValue({
172
- access: ['newspaper'],
173
- });
174
- spyOn(authorization, 'setAccessCookies').mockResolvedValue(true);
175
- spyOn(location, 'reload').mockReturnValue();
176
- spyOn(location, 'replace').mockReturnValue();
177
-
178
- await new PaywallUnlocker().redirectOnAccess('/some/url').tryUnlock();
179
- expect(location.reload).not.toHaveBeenCalled();
180
- expect(location.replace).toHaveBeenCalledWith('/some/url');
181
- });
182
-
183
- it('should not perform redirect or reload if not requested', async () => {
184
- spyOn(authentication, 'isLoggedIn').mockReturnValue(true);
185
- spyOn(authorization, 'getAccess').mockResolvedValue({
186
- access: ['newspaper'],
187
- });
188
- spyOn(authorization, 'setAccessCookies').mockResolvedValue(true);
189
- spyOn(location, 'reload').mockReturnValue();
190
- spyOn(location, 'replace').mockReturnValue();
191
-
192
- await new PaywallUnlocker().tryUnlock();
193
- expect(location.reload).not.toHaveBeenCalled();
194
- expect(location.replace).not.toHaveBeenCalled();
195
- });
196
- });
@@ -1,113 +0,0 @@
1
- import {
2
- activate,
3
- getAccess,
4
- hasAccess,
5
- hasRecentlyTriedToUnlockPaywall,
6
- setAccessCookies,
7
- setHasTriedToUnlockPaywall,
8
- } from '../actions/authorization';
9
- import { isLoggedIn } from '../actions/authentication';
10
- import { getCurrentSession } from '../clients/pluto';
11
- import { getCurrentPublicationDomain } from '../utils/url';
12
- import * as location from '../utils/location';
13
- import PaywallError from '../exceptions/PaywallError';
14
-
15
- class PaywallUnlocker {
16
- private requestedAccessFeatures: string[] = ['newspaper'];
17
-
18
- private reload: boolean = false;
19
-
20
- private requestedUrl: string | undefined;
21
-
22
- public withAccessFeatures(accessFeatures: string[]): this {
23
- this.requestedAccessFeatures = accessFeatures;
24
- return this;
25
- }
26
-
27
- public reloadOnAccess(): this {
28
- this.reload = true;
29
- return this;
30
- }
31
-
32
- public redirectOnAccess(requestedUrl: string): this {
33
- this.requestedUrl = requestedUrl;
34
- return this;
35
- }
36
-
37
- public async tryUnlock(): Promise<{
38
- isLoggedIn: boolean;
39
- hasAccess: boolean;
40
- accessFeatures: string[];
41
- }> {
42
- if (hasRecentlyTriedToUnlockPaywall()) {
43
- throw new PaywallError(
44
- 'Unlock check already performed.',
45
- PaywallError.ERROR_RELOADED_NO_ACCESS
46
- );
47
- }
48
-
49
- if (
50
- !isLoggedIn() &&
51
- (await getCurrentSession().then(() => !isLoggedIn()))
52
- ) {
53
- return {
54
- isLoggedIn: false,
55
- hasAccess: false,
56
- accessFeatures: [],
57
- };
58
- }
59
-
60
- return this.resolveActualAccessFeatures().then((accessFeatures) =>
61
- hasAccess(accessFeatures, this.requestedAccessFeatures)
62
- ? this.setAccessCookiesAndPerformRequestedActions(accessFeatures)
63
- : {
64
- isLoggedIn: true,
65
- hasAccess: false,
66
- accessFeatures,
67
- }
68
- );
69
- }
70
-
71
- private async resolveActualAccessFeatures() {
72
- let accessFeatures = await getAccess(this.requestedAccessFeatures).then(
73
- (a) => a.access || []
74
- );
75
-
76
- if (!hasAccess(accessFeatures, this.requestedAccessFeatures)) {
77
- if (!(await activate(this.requestedAccessFeatures))) {
78
- return accessFeatures;
79
- }
80
- accessFeatures = await getAccess(this.requestedAccessFeatures).then(
81
- (a) => a.access || []
82
- );
83
- }
84
- return accessFeatures;
85
- }
86
-
87
- private setAccessCookiesAndPerformRequestedActions(
88
- actualAccessFeatures: string[]
89
- ) {
90
- return setAccessCookies(getCurrentPublicationDomain()).then((success) => {
91
- if (!success) {
92
- throw new PaywallError(
93
- 'Could not set access cookies',
94
- PaywallError.ERROR_ENABLING_ACCESS_FAILED
95
- );
96
- }
97
- setHasTriedToUnlockPaywall();
98
- if (this.reload) {
99
- location.reload();
100
- }
101
- if (this.requestedUrl) {
102
- location.replace(this.requestedUrl);
103
- }
104
- return {
105
- isLoggedIn: true,
106
- hasAccess: true,
107
- accessFeatures: actualAccessFeatures,
108
- };
109
- });
110
- }
111
- }
112
-
113
- export default PaywallUnlocker;