@amedia/user 0.13.32 → 0.13.34

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 (64) hide show
  1. package/build.js +3 -2
  2. package/dist/actions/authentication.spec.d.ts +0 -3
  3. package/dist/autologin/index.spec.d.ts +0 -3
  4. package/dist/cache/index.spec.d.ts +0 -3
  5. package/dist/clients/mercury.spec.d.ts +0 -3
  6. package/dist/clients/mercuryLog.spec.d.ts +0 -3
  7. package/dist/eventHandlers/state.spec.d.ts +0 -3
  8. package/dist/eventHandlers/storage.spec.d.ts +0 -3
  9. package/dist/eventHandlers/userAttributes.spec.d.ts +0 -3
  10. package/dist/index-authentcation.spec.d.ts +0 -3
  11. package/dist/index-storage.spec.d.ts +0 -3
  12. package/dist/index-subscription.spec.d.ts +0 -3
  13. package/dist/index.d.ts +13 -42
  14. package/dist/index.esm.js +1 -1
  15. package/dist/index.esm.js.map +3 -3
  16. package/dist/index.js +1 -1
  17. package/dist/index.js.map +3 -3
  18. package/dist/janitor/index.spec.d.ts +1 -0
  19. package/dist/janitor/jobs/accessCookieRefresher.spec.d.ts +0 -3
  20. package/dist/janitor/jobs/daxsubCookie.spec.d.ts +0 -3
  21. package/dist/lib/PaywallUnlockRequest.spec.d.ts +0 -3
  22. package/dist/utils/expectBootLogs.d.ts +2 -0
  23. package/dist/utils/localhostCheck.d.ts +1 -0
  24. package/eik.json +1 -1
  25. package/jest.config.js +3 -2
  26. package/package.json +10 -3
  27. package/setupJest.js +0 -5
  28. package/src/actions/authentication.spec.ts +0 -6
  29. package/src/actions/authorization.spec.ts +0 -4
  30. package/src/actions/preferences.spec.ts +0 -1
  31. package/src/actions/userData.spec.ts +0 -6
  32. package/src/autologin/index.spec.ts +1 -11
  33. package/src/autologin/thirdPartyCookie.spec.ts +0 -5
  34. package/src/cache/index.spec.ts +0 -6
  35. package/src/clients/europa.spec.ts +0 -3
  36. package/src/clients/jupiter.spec.ts +0 -3
  37. package/src/clients/mercury.spec.ts +0 -10
  38. package/src/clients/mercuryLog.spec.ts +0 -10
  39. package/src/clients/nebula.spec.ts +0 -3
  40. package/src/clients/pluto.spec.ts +0 -3
  41. package/src/eventHandlers/activation.ts +5 -1
  42. package/src/eventHandlers/state.spec.ts +1 -12
  43. package/src/eventHandlers/storage.spec.ts +1 -12
  44. package/src/eventHandlers/userAttributes.spec.ts +1 -12
  45. package/src/index-authentcation.spec.ts +0 -14
  46. package/src/index-storage.spec.ts +1 -15
  47. package/src/index-subscription.spec.ts +1 -15
  48. package/src/index.ts +7 -4
  49. package/src/janitor/index.spec.ts +18 -0
  50. package/src/janitor/index.ts +9 -0
  51. package/src/janitor/jobs/accessCookieRefresher.spec.ts +0 -8
  52. package/src/janitor/jobs/daxsubCookie.spec.ts +1 -15
  53. package/src/janitor/jobs/removeUserStorage.spec.ts +0 -5
  54. package/src/lib/PaywallUnlockRequest.spec.ts +17 -14
  55. package/src/lib/PaywallUnlockRequest.ts +6 -0
  56. package/src/lib/UserDataRequest.spec.ts +16 -11
  57. package/src/lib/UserDataRequest.ts +9 -1
  58. package/src/utils/debouncer.spec.ts +0 -5
  59. package/src/utils/expectBootLogs.ts +9 -0
  60. package/src/utils/getOrUpdate.spec.ts +0 -2
  61. package/src/utils/localhostCheck.ts +9 -0
  62. package/src/utils/log.spec.ts +0 -5
  63. package/testSetup.js +18 -0
  64. /package/{checkVersion.js → checkVersion.cjs} +0 -0
@@ -1,33 +1,20 @@
1
- /**
2
- * @jest-environment jsdom
3
- */
4
-
5
- import fetch from 'jest-fetch-mock';
6
1
  import PaywallUnlockRequest, { UnlockResponse } from './PaywallUnlockRequest';
7
2
  import * as authorization from '../actions/authorization';
8
3
  import * as location from '../utils/location';
9
4
  import * as autologin from '../autologin';
5
+ import * as localhostCheck from '../utils/localhostCheck';
10
6
  import store from '../store';
11
7
  import PaywallError from '../exceptions/PaywallError';
12
- import { setRootElement } from '../utils/customEventWrapper';
13
8
  import { listenForUserAttributeRequests } from '../eventHandlers/userAttributes';
14
9
  import { listenForStateRequests } from '../eventHandlers/state';
15
10
  import { removeAllCookies } from './cookie';
16
- import restoreAllMocks = jest.restoreAllMocks;
17
11
  import spyOn = jest.spyOn;
18
12
 
19
13
  beforeEach(() => {
20
- restoreAllMocks();
21
- fetch.resetMocks();
22
14
  store.clear();
23
15
 
24
- // We cannot use window as our event root during testing
25
- // since tests are executed in parallel.
26
- setRootElement(new Document().createElement('div'));
27
16
  removeAllCookies();
28
17
 
29
- // Also, since we keep creating new root elements, we also
30
- // need to attach to these new roots.
31
18
  listenForStateRequests();
32
19
  listenForUserAttributeRequests();
33
20
  });
@@ -65,6 +52,22 @@ describe('PaywallUnlocker', () => {
65
52
  spyOn(location, 'replace').mockReturnValue();
66
53
  });
67
54
 
55
+ describe('When running on localhost', () => {
56
+ beforeEach(() => {
57
+ jest.spyOn(localhostCheck, 'isRunningOnLocalhost').mockReturnValue(true);
58
+ });
59
+
60
+ it('should fail with expected error', async () => {
61
+ try {
62
+ await new PaywallUnlockRequest().tryUnlock();
63
+ } catch (e) {
64
+ expect(e).toEqual(
65
+ new Error('@amedia/user cannot unlock paywall on localhost')
66
+ );
67
+ }
68
+ });
69
+ });
70
+
68
71
  describe('When not logged in', () => {
69
72
  let unlockResponse: UnlockResponse;
70
73
  beforeEach(async () => {
@@ -11,6 +11,7 @@ import PaywallError from '../exceptions/PaywallError';
11
11
  import UserDataRequest from './UserDataRequest';
12
12
  import { attemptPaywallAutoLogin } from '../autologin';
13
13
  import { AccessFeatures } from '../clients/jupiter';
14
+ import { isRunningOnLocalhost } from '../utils/localhostCheck';
14
15
 
15
16
  export type UnlockResponse = {
16
17
  isLoggedIn: boolean;
@@ -41,6 +42,11 @@ class PaywallUnlockRequest {
41
42
  }
42
43
 
43
44
  public async tryUnlock(): Promise<UnlockResponse> {
45
+ if (isRunningOnLocalhost()) {
46
+ return Promise.reject(
47
+ new Error('@amedia/user cannot unlock paywall on localhost')
48
+ );
49
+ }
44
50
  const {
45
51
  state: { isLoggedIn },
46
52
  } = await new UserDataRequest().withAttributes(['name']).fetch();
@@ -1,29 +1,34 @@
1
- /**
2
- * @jest-environment jsdom
3
- */
4
1
  import store from '../store';
5
- import { UserDataRequest } from '../index';
2
+ import UserDataRequest from './UserDataRequest';
6
3
  import { listenForUserAttributeRequests } from '../eventHandlers/userAttributes';
7
4
  import { listenForStateRequests } from '../eventHandlers/state';
8
5
  import { listenForStorageRequests } from '../eventHandlers/storage';
9
- import { setRootElement } from '../utils/customEventWrapper';
10
6
  import * as europa from '../clients/europa';
7
+ import * as localhostCheck from '../utils/localhostCheck';
11
8
 
12
9
  beforeEach(() => {
13
- // We cannot use window as our event root during testing
14
- // since tests are executed in parallel.
15
- setRootElement(new Document().createElement('div'));
16
-
17
10
  store.clear();
18
11
 
19
- // Also, since we keep creating new root elements, we also
20
- // need to attach to these new roots.
21
12
  listenForUserAttributeRequests();
22
13
  listenForStateRequests();
23
14
  listenForStorageRequests();
24
15
  });
25
16
 
26
17
  describe('UserDataRequest', () => {
18
+ describe('When running on localhost', () => {
19
+ beforeEach(() => {
20
+ jest.spyOn(localhostCheck, 'isRunningOnLocalhost').mockReturnValue(true);
21
+ });
22
+
23
+ it('should fail with expected error', async () => {
24
+ try {
25
+ await new UserDataRequest().withAttributes(['name']).fetch();
26
+ } catch (e) {
27
+ expect(e).toEqual(new Error('@amedia/user cannot run on localhost'));
28
+ }
29
+ });
30
+ });
31
+
27
32
  describe('when fetching', () => {
28
33
  describe('and is logged in', () => {
29
34
  beforeEach(() => {
@@ -3,6 +3,7 @@ import { State, UserAttributes } from '../types';
3
3
  import { dispatch, listenForAllEvents } from '../utils/customEventWrapper';
4
4
  import { AmediaUserEvents } from '../eventHandlers/eventMap';
5
5
  import { deepEquals } from '../utils/deepEquals';
6
+ import { isRunningOnLocalhost } from '../utils/localhostCheck';
6
7
 
7
8
  function isMissingData(requestedKeys?: string[], data?: Object) {
8
9
  if (requestedKeys && requestedKeys.length > 0) {
@@ -62,6 +63,10 @@ class UserDataRequest<
62
63
 
63
64
  subscribe(onData: (data: SubscribeResponse) => void): void {
64
65
  let previousData = this.setupLocalCache();
66
+ if (isRunningOnLocalhost()) {
67
+ onData(previousData);
68
+ return;
69
+ }
65
70
  listenForAllEvents(AmediaUserEvents.ON_STATE_UPDATED, (evt) => {
66
71
  const state = evt.detail;
67
72
  if (
@@ -122,7 +127,10 @@ class UserDataRequest<
122
127
  }
123
128
 
124
129
  fetch(): Promise<SubscribeResponse> {
125
- return new Promise<SubscribeResponse>((resolve) => {
130
+ return new Promise<SubscribeResponse>((resolve, reject) => {
131
+ if (isRunningOnLocalhost()) {
132
+ reject(new Error('@amedia/user cannot run on localhost'));
133
+ }
126
134
  this.subscribe((data: any) => {
127
135
  // @ts-ignore
128
136
  if (typeof data.state.isLoggedIn === 'undefined') {
@@ -1,10 +1,5 @@
1
- import restoreAllMocks = jest.restoreAllMocks;
2
1
  import Debouncer from './debouncer';
3
2
 
4
- beforeEach(async () => {
5
- restoreAllMocks();
6
- });
7
-
8
3
  describe('Debouncer', () => {
9
4
  describe('when params is list of arrays and response is key value object', () => {
10
5
  let debouncer: Debouncer<string[], Record<string, string>>;
@@ -0,0 +1,9 @@
1
+ import log from './log';
2
+
3
+ jest.spyOn(log, 'warn').mockImplementation();
4
+
5
+ export default () => {
6
+ expect(log.warn).toHaveBeenCalledWith(
7
+ '@amedia/user started on localhost. In this scenario we have no way to resolve any login-, access- or userdata. Expect errors or redirects if you try.'
8
+ );
9
+ };
@@ -3,12 +3,10 @@ import cache from '../cache';
3
3
  import getOrUpdate from './getOrUpdate';
4
4
  import queue from './queue';
5
5
  import spyOn = jest.spyOn;
6
- import restoreAllMocks = jest.restoreAllMocks;
7
6
 
8
7
  const schema = Partial({ field: String, extraField: String });
9
8
 
10
9
  beforeEach(async () => {
11
- restoreAllMocks();
12
10
  await cache.clear();
13
11
  });
14
12
  describe('getOrUpdate', () => {
@@ -0,0 +1,9 @@
1
+ import log from './log';
2
+
3
+ export const isRunningOnLocalhost = (): boolean => {
4
+ if (globalThis.location.hostname === 'localhost') {
5
+ log.error('@amedia/user cannot work on localhost.');
6
+ return true;
7
+ }
8
+ return false;
9
+ };
@@ -1,9 +1,4 @@
1
1
  import log, { Logger, setLogger } from './log';
2
- import restoreAllMocks = jest.restoreAllMocks;
3
-
4
- beforeEach(() => {
5
- restoreAllMocks();
6
- });
7
2
 
8
3
  describe('Log', () => {
9
4
  let logger: Logger;
package/testSetup.js CHANGED
@@ -1 +1,19 @@
1
1
  require('jest-extended');
2
+ const fetchMocker = require('jest-fetch-mock');
3
+ const localhostCheck = require('./src/utils/localhostCheck');
4
+ const { setLogger } = require('./src/utils/log');
5
+ const { setRootElement } = require('./src/utils/customEventWrapper');
6
+
7
+ setLogger(console);
8
+ self.amediaUserTestMode = true;
9
+
10
+ fetchMocker.enableMocks();
11
+
12
+ beforeEach(() => {
13
+ fetchMocker.resetMocks();
14
+ jest.spyOn(localhostCheck, 'isRunningOnLocalhost').mockReturnValue(false);
15
+
16
+ // We cannot use window/globalThis as our event root during testing
17
+ // since tests are executed in parallel.
18
+ setRootElement(new Document().createElement('div'));
19
+ });
File without changes