shop-customer.e2e-spec.ts 7.8 KB

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