shop-customer.e2e-spec.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* tslint:disable:no-non-null-assertion */
  2. import { createTestEnvironment } from '@vendure/testing';
  3. import gql from 'graphql-tag';
  4. import path from 'path';
  5. import { initialData } from '../../../e2e-common/e2e-initial-data';
  6. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  7. import { AttemptLogin, GetCustomer, GetCustomerIds } from './graphql/generated-e2e-admin-types';
  8. import {
  9. CreateAddressInput,
  10. CreateAddressShop,
  11. DeleteAddressShop,
  12. UpdateAddressInput,
  13. UpdateAddressShop,
  14. UpdateCustomer,
  15. UpdateCustomerInput,
  16. UpdatePassword,
  17. } from './graphql/generated-e2e-shop-types';
  18. import { ATTEMPT_LOGIN, GET_CUSTOMER } from './graphql/shared-definitions';
  19. import {
  20. CREATE_ADDRESS,
  21. DELETE_ADDRESS,
  22. UPDATE_ADDRESS,
  23. UPDATE_CUSTOMER,
  24. UPDATE_PASSWORD,
  25. } from './graphql/shop-definitions';
  26. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  27. describe('Shop customers', () => {
  28. const { server, adminClient, shopClient } = createTestEnvironment(testConfig);
  29. let customer: GetCustomer.Customer;
  30. beforeAll(async () => {
  31. await server.init({
  32. dataDir: path.join(__dirname, '__data__'),
  33. initialData,
  34. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  35. customerCount: 2,
  36. });
  37. await adminClient.asSuperAdmin();
  38. // Fetch the first Customer and store it as the `customer` variable.
  39. const { customers } = await adminClient.query<GetCustomerIds.Query>(gql`
  40. query GetCustomerIds {
  41. customers {
  42. items {
  43. id
  44. }
  45. }
  46. }
  47. `);
  48. const result = await adminClient.query<GetCustomer.Query, GetCustomer.Variables>(GET_CUSTOMER, {
  49. id: customers.items[0].id,
  50. });
  51. customer = result.customer!;
  52. }, TEST_SETUP_TIMEOUT_MS);
  53. afterAll(async () => {
  54. await server.destroy();
  55. });
  56. it(
  57. 'updateCustomer throws if not logged in',
  58. assertThrowsWithMessage(async () => {
  59. const input: UpdateCustomerInput = {
  60. firstName: 'xyz',
  61. };
  62. await shopClient.query<UpdateCustomer.Mutation, UpdateCustomer.Variables>(UPDATE_CUSTOMER, {
  63. input,
  64. });
  65. }, 'You are not currently authorized to perform this action'),
  66. );
  67. it(
  68. 'createCustomerAddress throws if not logged in',
  69. assertThrowsWithMessage(async () => {
  70. const input: CreateAddressInput = {
  71. streetLine1: '1 Test Street',
  72. countryCode: 'GB',
  73. };
  74. await shopClient.query<CreateAddressShop.Mutation, CreateAddressShop.Variables>(CREATE_ADDRESS, {
  75. input,
  76. });
  77. }, 'You are not currently authorized to perform this action'),
  78. );
  79. it(
  80. 'updateCustomerAddress throws if not logged in',
  81. assertThrowsWithMessage(async () => {
  82. const input: UpdateAddressInput = {
  83. id: 'T_1',
  84. streetLine1: 'zxc',
  85. };
  86. await shopClient.query<UpdateAddressShop.Mutation, UpdateAddressShop.Variables>(UPDATE_ADDRESS, {
  87. input,
  88. });
  89. }, 'You are not currently authorized to perform this action'),
  90. );
  91. it(
  92. 'deleteCustomerAddress throws if not logged in',
  93. assertThrowsWithMessage(async () => {
  94. await shopClient.query<DeleteAddressShop.Mutation, DeleteAddressShop.Variables>(DELETE_ADDRESS, {
  95. id: 'T_1',
  96. });
  97. }, 'You are not currently authorized to perform this action'),
  98. );
  99. describe('logged in Customer', () => {
  100. let addressId: string;
  101. beforeAll(async () => {
  102. await shopClient.query<AttemptLogin.Mutation, AttemptLogin.Variables>(ATTEMPT_LOGIN, {
  103. username: customer.emailAddress,
  104. password: 'test',
  105. rememberMe: false,
  106. });
  107. });
  108. it('updateCustomer works', async () => {
  109. const input: UpdateCustomerInput = {
  110. firstName: 'xyz',
  111. };
  112. const result = await shopClient.query<UpdateCustomer.Mutation, UpdateCustomer.Variables>(
  113. UPDATE_CUSTOMER,
  114. { input },
  115. );
  116. expect(result.updateCustomer.firstName).toBe('xyz');
  117. });
  118. it('createCustomerAddress works', async () => {
  119. const input: CreateAddressInput = {
  120. streetLine1: '1 Test Street',
  121. countryCode: 'GB',
  122. };
  123. const { createCustomerAddress } = await shopClient.query<
  124. CreateAddressShop.Mutation,
  125. CreateAddressShop.Variables
  126. >(CREATE_ADDRESS, { input });
  127. expect(createCustomerAddress).toEqual({
  128. id: 'T_3',
  129. streetLine1: '1 Test Street',
  130. country: {
  131. code: 'GB',
  132. },
  133. });
  134. addressId = createCustomerAddress.id;
  135. });
  136. it('updateCustomerAddress works', async () => {
  137. const input: UpdateAddressInput = {
  138. id: addressId,
  139. streetLine1: '5 Test Street',
  140. countryCode: 'AT',
  141. };
  142. const result = await shopClient.query<UpdateAddressShop.Mutation, UpdateAddressShop.Variables>(
  143. UPDATE_ADDRESS,
  144. { input },
  145. );
  146. expect(result.updateCustomerAddress.streetLine1).toEqual('5 Test Street');
  147. expect(result.updateCustomerAddress.country.code).toEqual('AT');
  148. });
  149. it(
  150. 'updateCustomerAddress fails for address not owned by Customer',
  151. assertThrowsWithMessage(async () => {
  152. const input: UpdateAddressInput = {
  153. id: 'T_2',
  154. streetLine1: '1 Test Street',
  155. };
  156. await shopClient.query<UpdateAddressShop.Mutation, UpdateAddressShop.Variables>(
  157. UPDATE_ADDRESS,
  158. { input },
  159. );
  160. }, 'You are not currently authorized to perform this action'),
  161. );
  162. it('deleteCustomerAddress works', async () => {
  163. const result = await shopClient.query<DeleteAddressShop.Mutation, DeleteAddressShop.Variables>(
  164. DELETE_ADDRESS,
  165. { id: 'T_3' },
  166. );
  167. expect(result.deleteCustomerAddress).toBe(true);
  168. });
  169. it(
  170. 'deleteCustomerAddress fails for address not owned by Customer',
  171. assertThrowsWithMessage(async () => {
  172. await shopClient.query<DeleteAddressShop.Mutation, DeleteAddressShop.Variables>(
  173. DELETE_ADDRESS,
  174. { id: 'T_2' },
  175. );
  176. }, 'You are not currently authorized to perform this action'),
  177. );
  178. it(
  179. 'updatePassword fails with incorrect current password',
  180. assertThrowsWithMessage(async () => {
  181. await shopClient.query<UpdatePassword.Mutation, UpdatePassword.Variables>(UPDATE_PASSWORD, {
  182. old: 'wrong',
  183. new: 'test2',
  184. });
  185. }, 'The credentials did not match. Please check and try again'),
  186. );
  187. it('updatePassword works', async () => {
  188. const response = await shopClient.query<UpdatePassword.Mutation, UpdatePassword.Variables>(
  189. UPDATE_PASSWORD,
  190. { old: 'test', new: 'test2' },
  191. );
  192. expect(response.updateCustomerPassword).toBe(true);
  193. // Log out and log in with new password
  194. const loginResult = await shopClient.asUserWithCredentials(customer.emailAddress, 'test2');
  195. expect(loginResult.user.identifier).toBe(customer.emailAddress);
  196. });
  197. });
  198. });