shop-customer.e2e-spec.ts 7.8 KB

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