authentication-strategy.e2e-spec.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import { pick } from '@vendure/common/lib/pick';
  2. import { mergeConfig } 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 { NativeAuthenticationStrategy } from '../src/config/auth/native-authentication-strategy';
  9. import { TestAuthenticationStrategy, VALID_AUTH_TOKEN } from './fixtures/test-authentication-strategies';
  10. import {
  11. Authenticate,
  12. GetCustomerHistory,
  13. GetCustomers,
  14. GetCustomerUserAuth,
  15. HistoryEntryType,
  16. Me,
  17. } from './graphql/generated-e2e-admin-types';
  18. import { Register } from './graphql/generated-e2e-shop-types';
  19. import { GET_CUSTOMER_HISTORY, ME } from './graphql/shared-definitions';
  20. import { REGISTER_ACCOUNT } from './graphql/shop-definitions';
  21. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  22. describe('AuthenticationStrategy', () => {
  23. const { server, adminClient, shopClient } = createTestEnvironment(
  24. mergeConfig(testConfig, {
  25. authOptions: {
  26. shopAuthenticationStrategy: [
  27. new NativeAuthenticationStrategy(),
  28. new TestAuthenticationStrategy(),
  29. ],
  30. },
  31. }),
  32. );
  33. beforeAll(async () => {
  34. await server.init({
  35. initialData,
  36. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  37. customerCount: 1,
  38. });
  39. await adminClient.asSuperAdmin();
  40. }, TEST_SETUP_TIMEOUT_MS);
  41. afterAll(async () => {
  42. await server.destroy();
  43. });
  44. describe('external auth', () => {
  45. const userData = {
  46. email: 'test@email.com',
  47. firstName: 'Cixin',
  48. lastName: 'Liu',
  49. };
  50. let newCustomerId: string;
  51. it(
  52. 'fails with a bad token',
  53. assertThrowsWithMessage(async () => {
  54. await shopClient.query(AUTHENTICATE, {
  55. input: {
  56. test_strategy: {
  57. token: 'bad-token',
  58. },
  59. },
  60. });
  61. }, 'The credentials did not match. Please check and try again'),
  62. );
  63. it('creates a new Customer with valid token', async () => {
  64. const { customers: before } = await adminClient.query<GetCustomers.Query>(GET_CUSTOMERS);
  65. expect(before.totalItems).toBe(1);
  66. const result = await shopClient.query<Authenticate.Mutation>(AUTHENTICATE, {
  67. input: {
  68. test_strategy: {
  69. token: VALID_AUTH_TOKEN,
  70. userData,
  71. },
  72. },
  73. });
  74. expect(result.authenticate.user.identifier).toEqual(userData.email);
  75. const { customers: after } = await adminClient.query<GetCustomers.Query>(GET_CUSTOMERS);
  76. expect(after.totalItems).toBe(2);
  77. expect(after.items.map(i => i.emailAddress)).toEqual([
  78. 'hayden.zieme12@hotmail.com',
  79. userData.email,
  80. ]);
  81. newCustomerId = after.items[1].id;
  82. });
  83. it('creates customer history entry', async () => {
  84. const { customer } = await adminClient.query<
  85. GetCustomerHistory.Query,
  86. GetCustomerHistory.Variables
  87. >(GET_CUSTOMER_HISTORY, {
  88. id: newCustomerId,
  89. });
  90. expect(customer?.history.items.map(pick(['type', 'data']))).toEqual([
  91. {
  92. type: HistoryEntryType.CUSTOMER_REGISTERED,
  93. data: {
  94. strategy: 'test_strategy',
  95. },
  96. },
  97. {
  98. type: HistoryEntryType.CUSTOMER_VERIFIED,
  99. data: {
  100. strategy: 'test_strategy',
  101. },
  102. },
  103. ]);
  104. });
  105. it('user authenticationMethod populated', async () => {
  106. const { customer } = await adminClient.query<
  107. GetCustomerUserAuth.Query,
  108. GetCustomerUserAuth.Variables
  109. >(GET_CUSTOMER_USER_AUTH, {
  110. id: newCustomerId,
  111. });
  112. expect(customer?.user?.authenticationMethods.length).toBe(1);
  113. expect(customer?.user?.authenticationMethods[0].strategy).toBe('test_strategy');
  114. });
  115. it('creates authenticated session', async () => {
  116. const { me } = await shopClient.query<Me.Query>(ME);
  117. expect(me?.identifier).toBe(userData.email);
  118. });
  119. it('log out', async () => {
  120. await shopClient.asAnonymousUser();
  121. });
  122. it('logging in again re-uses created User & Customer', async () => {
  123. const result = await shopClient.query<Authenticate.Mutation>(AUTHENTICATE, {
  124. input: {
  125. test_strategy: {
  126. token: VALID_AUTH_TOKEN,
  127. userData,
  128. },
  129. },
  130. });
  131. expect(result.authenticate.user.identifier).toEqual(userData.email);
  132. const { customers: after } = await adminClient.query<GetCustomers.Query>(GET_CUSTOMERS);
  133. expect(after.totalItems).toBe(2);
  134. expect(after.items.map(i => i.emailAddress)).toEqual([
  135. 'hayden.zieme12@hotmail.com',
  136. userData.email,
  137. ]);
  138. });
  139. it('registerCustomerAccount with external email', async () => {
  140. const { registerCustomerAccount } = await shopClient.query<Register.Mutation, Register.Variables>(
  141. REGISTER_ACCOUNT,
  142. {
  143. input: {
  144. emailAddress: userData.email,
  145. },
  146. },
  147. );
  148. expect(registerCustomerAccount).toBe(true);
  149. const { customer } = await adminClient.query<
  150. GetCustomerUserAuth.Query,
  151. GetCustomerUserAuth.Variables
  152. >(GET_CUSTOMER_USER_AUTH, {
  153. id: newCustomerId,
  154. });
  155. expect(customer?.user?.authenticationMethods.length).toBe(2);
  156. expect(customer?.user?.authenticationMethods[1].strategy).toBe('native');
  157. const { customer: customer2 } = await adminClient.query<
  158. GetCustomerHistory.Query,
  159. GetCustomerHistory.Variables
  160. >(GET_CUSTOMER_HISTORY, {
  161. id: newCustomerId,
  162. options: {
  163. skip: 2,
  164. },
  165. });
  166. expect(customer2?.history.items.map(pick(['type', 'data']))).toEqual([
  167. {
  168. type: HistoryEntryType.CUSTOMER_REGISTERED,
  169. data: {
  170. strategy: 'native',
  171. },
  172. },
  173. ]);
  174. });
  175. });
  176. });
  177. const AUTHENTICATE = gql`
  178. mutation Authenticate($input: AuthenticationInput!) {
  179. authenticate(input: $input) {
  180. user {
  181. id
  182. identifier
  183. }
  184. }
  185. }
  186. `;
  187. const GET_CUSTOMERS = gql`
  188. query GetCustomers {
  189. customers {
  190. totalItems
  191. items {
  192. id
  193. emailAddress
  194. }
  195. }
  196. }
  197. `;
  198. const GET_CUSTOMER_USER_AUTH = gql`
  199. query GetCustomerUserAuth($id: ID!) {
  200. customer(id: $id) {
  201. id
  202. user {
  203. id
  204. verified
  205. authenticationMethods {
  206. id
  207. strategy
  208. }
  209. }
  210. }
  211. }
  212. `;