session-management.e2e-spec.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 { TEST_SETUP_TIMEOUT_MS, testConfig } 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. }
  88. `,
  89. );
  90. expect(testSessionCache.size).toBe(0);
  91. expect(deleteSpy.mock.calls.length).toBeGreaterThan(0);
  92. });
  93. });
  94. describe('Session expiry', () => {
  95. const { server, adminClient } = createTestEnvironment(
  96. mergeConfig(testConfig, {
  97. authOptions: {
  98. sessionDuration: '3s',
  99. sessionCacheTTL: 1,
  100. },
  101. }),
  102. );
  103. beforeAll(async () => {
  104. await server.init({
  105. initialData,
  106. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  107. customerCount: 1,
  108. });
  109. await adminClient.asSuperAdmin();
  110. }, TEST_SETUP_TIMEOUT_MS);
  111. afterAll(async () => {
  112. await server.destroy();
  113. });
  114. it('session does not expire with continued use', async () => {
  115. await adminClient.asSuperAdmin();
  116. await pause(1000);
  117. await adminClient.query(ME);
  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. }, 10000);
  125. it('session expires when not used for longer than sessionDuration', async () => {
  126. await adminClient.asSuperAdmin();
  127. await pause(3500);
  128. try {
  129. await adminClient.query(ME);
  130. fail('Should have thrown');
  131. } catch (e) {
  132. expect(e.message).toContain('You are not currently authorized to perform this action');
  133. }
  134. }, 10000);
  135. });
  136. function pause(ms: number): Promise<void> {
  137. return new Promise(resolve => setTimeout(resolve, ms));
  138. }