session-management.e2e-spec.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* tslint:disable:no-non-null-assertion */
  2. import { CachedSession, mergeConfig, SessionCacheStrategy } from '@vendure/core';
  3. import { createTestEnvironment } from '@vendure/testing';
  4. import gql from 'graphql-tag';
  5. import path from 'path';
  6. import { initialData } from '../../../e2e-common/e2e-initial-data';
  7. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  8. import { SUPER_ADMIN_USER_IDENTIFIER, SUPER_ADMIN_USER_PASSWORD } from '../../common/src/shared-constants';
  9. import { AttemptLogin, Me } from './graphql/generated-e2e-admin-types';
  10. import { ATTEMPT_LOGIN, ME } from './graphql/shared-definitions';
  11. const testSessionCache = new Map<string, CachedSession>();
  12. const getSpy = jest.fn();
  13. const setSpy = jest.fn();
  14. const clearSpy = jest.fn();
  15. const deleteSpy = jest.fn();
  16. class TestingSessionCacheStrategy implements SessionCacheStrategy {
  17. clear() {
  18. clearSpy();
  19. testSessionCache.clear();
  20. }
  21. delete(sessionToken: string) {
  22. deleteSpy(sessionToken);
  23. testSessionCache.delete(sessionToken);
  24. }
  25. get(sessionToken: string) {
  26. getSpy(sessionToken);
  27. return testSessionCache.get(sessionToken);
  28. }
  29. set(session: CachedSession) {
  30. setSpy(session);
  31. testSessionCache.set(session.token, session);
  32. }
  33. }
  34. describe('Session caching', () => {
  35. const { server, adminClient } = createTestEnvironment(
  36. mergeConfig(testConfig(), {
  37. authOptions: {
  38. sessionCacheStrategy: new TestingSessionCacheStrategy(),
  39. sessionCacheTTL: 2,
  40. },
  41. }),
  42. );
  43. beforeAll(async () => {
  44. await server.init({
  45. initialData,
  46. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  47. customerCount: 1,
  48. });
  49. testSessionCache.clear();
  50. }, TEST_SETUP_TIMEOUT_MS);
  51. afterAll(async () => {
  52. await server.destroy();
  53. });
  54. it('populates the cache on login', async () => {
  55. setSpy.mockClear();
  56. expect(setSpy.mock.calls.length).toBe(0);
  57. expect(testSessionCache.size).toBe(0);
  58. await adminClient.query<AttemptLogin.Mutation, AttemptLogin.Variables>(ATTEMPT_LOGIN, {
  59. username: SUPER_ADMIN_USER_IDENTIFIER,
  60. password: SUPER_ADMIN_USER_PASSWORD,
  61. });
  62. expect(testSessionCache.size).toBe(1);
  63. expect(setSpy.mock.calls.length).toBe(1);
  64. });
  65. it('takes user data from cache on next request', async () => {
  66. getSpy.mockClear();
  67. const { me } = await adminClient.query<Me.Query>(ME);
  68. expect(getSpy.mock.calls.length).toBe(1);
  69. });
  70. it('sets fresh data after TTL expires', async () => {
  71. setSpy.mockClear();
  72. await adminClient.query<Me.Query>(ME);
  73. expect(setSpy.mock.calls.length).toBe(0);
  74. await adminClient.query<Me.Query>(ME);
  75. expect(setSpy.mock.calls.length).toBe(0);
  76. await pause(2000);
  77. await adminClient.query<Me.Query>(ME);
  78. expect(setSpy.mock.calls.length).toBe(1);
  79. });
  80. it('clears cache for that user on logout', async () => {
  81. deleteSpy.mockClear();
  82. expect(deleteSpy.mock.calls.length).toBe(0);
  83. await adminClient.query(
  84. gql`
  85. mutation Logout {
  86. logout {
  87. success
  88. }
  89. }
  90. `,
  91. );
  92. expect(testSessionCache.size).toBe(0);
  93. expect(deleteSpy.mock.calls.length).toBeGreaterThan(0);
  94. });
  95. });
  96. describe('Session expiry', () => {
  97. const { server, adminClient } = createTestEnvironment(
  98. mergeConfig(testConfig(), {
  99. authOptions: {
  100. sessionDuration: '3s',
  101. sessionCacheTTL: 1,
  102. },
  103. }),
  104. );
  105. beforeAll(async () => {
  106. await server.init({
  107. initialData,
  108. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  109. customerCount: 1,
  110. });
  111. await adminClient.asSuperAdmin();
  112. }, TEST_SETUP_TIMEOUT_MS);
  113. afterAll(async () => {
  114. await server.destroy();
  115. });
  116. it('session does not expire with continued use', async () => {
  117. await adminClient.asSuperAdmin();
  118. await pause(1000);
  119. await adminClient.query(ME);
  120. await pause(1000);
  121. await adminClient.query(ME);
  122. await pause(1000);
  123. await adminClient.query(ME);
  124. await pause(1000);
  125. await adminClient.query(ME);
  126. }, 10000);
  127. it('session expires when not used for longer than sessionDuration', async () => {
  128. await adminClient.asSuperAdmin();
  129. await pause(3500);
  130. try {
  131. await adminClient.query(ME);
  132. fail('Should have thrown');
  133. } catch (e) {
  134. expect(e.message).toContain('You are not currently authorized to perform this action');
  135. }
  136. }, 10000);
  137. });
  138. function pause(ms: number): Promise<void> {
  139. return new Promise(resolve => setTimeout(resolve, ms));
  140. }