shop-customer.e2e-spec.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* tslint:disable:no-non-null-assertion */
  2. import { pick } from '@vendure/common/lib/pick';
  3. import { createErrorResultGuard, createTestEnvironment, ErrorResultGuard } 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 * as Codegen from './graphql/generated-e2e-admin-types';
  9. import { HistoryEntryType } from './graphql/generated-e2e-admin-types';
  10. import * as CodegenShop from './graphql/generated-e2e-shop-types';
  11. import { CreateAddressInput, ErrorCode, UpdateAddressInput } from './graphql/generated-e2e-shop-types';
  12. import { ATTEMPT_LOGIN, GET_CUSTOMER, GET_CUSTOMER_HISTORY } from './graphql/shared-definitions';
  13. import {
  14. CREATE_ADDRESS,
  15. DELETE_ADDRESS,
  16. UPDATE_ADDRESS,
  17. UPDATE_CUSTOMER,
  18. UPDATE_PASSWORD,
  19. } from './graphql/shop-definitions';
  20. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  21. describe('Shop customers', () => {
  22. const { server, adminClient, shopClient } = createTestEnvironment(testConfig());
  23. let customer: NonNullable<Codegen.GetCustomerQuery['customer']>;
  24. const successErrorGuard: ErrorResultGuard<{ success: boolean }> = createErrorResultGuard(
  25. input => input.success != null,
  26. );
  27. beforeAll(async () => {
  28. await server.init({
  29. initialData,
  30. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  31. customerCount: 2,
  32. });
  33. await adminClient.asSuperAdmin();
  34. // Fetch the first Customer and store it as the `customer` variable.
  35. const { customers } = await adminClient.query<Codegen.GetCustomerIdsQuery>(gql`
  36. query GetCustomerIds {
  37. customers {
  38. items {
  39. id
  40. }
  41. }
  42. }
  43. `);
  44. const result = await adminClient.query<Codegen.GetCustomerQuery, Codegen.GetCustomerQueryVariables>(
  45. GET_CUSTOMER,
  46. {
  47. id: customers.items[0].id,
  48. },
  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: CodegenShop.UpdateCustomerInput = {
  59. firstName: 'xyz',
  60. };
  61. await shopClient.query<
  62. CodegenShop.UpdateCustomerMutation,
  63. CodegenShop.UpdateCustomerMutationVariables
  64. >(UPDATE_CUSTOMER, {
  65. input,
  66. });
  67. }, 'You are not currently authorized to perform this action'),
  68. );
  69. it(
  70. 'createCustomerAddress throws if not logged in',
  71. assertThrowsWithMessage(async () => {
  72. const input: CreateAddressInput = {
  73. streetLine1: '1 Test Street',
  74. countryCode: 'GB',
  75. };
  76. await shopClient.query<
  77. CodegenShop.CreateAddressShopMutation,
  78. CodegenShop.CreateAddressShopMutationVariables
  79. >(CREATE_ADDRESS, {
  80. input,
  81. });
  82. }, 'You are not currently authorized to perform this action'),
  83. );
  84. it(
  85. 'updateCustomerAddress throws if not logged in',
  86. assertThrowsWithMessage(async () => {
  87. const input: UpdateAddressInput = {
  88. id: 'T_1',
  89. streetLine1: 'zxc',
  90. };
  91. await shopClient.query<
  92. CodegenShop.UpdateAddressShopMutation,
  93. CodegenShop.UpdateAddressShopMutationVariables
  94. >(UPDATE_ADDRESS, {
  95. input,
  96. });
  97. }, 'You are not currently authorized to perform this action'),
  98. );
  99. it(
  100. 'deleteCustomerAddress throws if not logged in',
  101. assertThrowsWithMessage(async () => {
  102. await shopClient.query<
  103. CodegenShop.DeleteAddressShopMutation,
  104. CodegenShop.DeleteAddressShopMutationVariables
  105. >(DELETE_ADDRESS, {
  106. id: 'T_1',
  107. });
  108. }, 'You are not currently authorized to perform this action'),
  109. );
  110. describe('logged in Customer', () => {
  111. let addressId: string;
  112. beforeAll(async () => {
  113. await shopClient.query<Codegen.AttemptLoginMutation, Codegen.AttemptLoginMutationVariables>(
  114. ATTEMPT_LOGIN,
  115. {
  116. username: customer.emailAddress,
  117. password: 'test',
  118. rememberMe: false,
  119. },
  120. );
  121. });
  122. it('updateCustomer works', async () => {
  123. const input: CodegenShop.UpdateCustomerInput = {
  124. firstName: 'xyz',
  125. };
  126. const result = await shopClient.query<
  127. CodegenShop.UpdateCustomerMutation,
  128. CodegenShop.UpdateCustomerMutationVariables
  129. >(UPDATE_CUSTOMER, { input });
  130. expect(result.updateCustomer.firstName).toBe('xyz');
  131. });
  132. it('customer history for CUSTOMER_DETAIL_UPDATED', async () => {
  133. const result = await adminClient.query<
  134. Codegen.GetCustomerHistoryQuery,
  135. Codegen.GetCustomerHistoryQueryVariables
  136. >(GET_CUSTOMER_HISTORY, {
  137. id: customer.id,
  138. options: {
  139. // skip populated CUSTOMER_ADDRESS_CREATED entry
  140. skip: 3,
  141. },
  142. });
  143. expect(result.customer?.history.items.map(pick(['type', 'data']))).toEqual([
  144. {
  145. type: HistoryEntryType.CUSTOMER_DETAIL_UPDATED,
  146. data: {
  147. input: { firstName: 'xyz', id: 'T_1' },
  148. },
  149. },
  150. ]);
  151. });
  152. it('createCustomerAddress works', async () => {
  153. const input: CreateAddressInput = {
  154. streetLine1: '1 Test Street',
  155. countryCode: 'GB',
  156. };
  157. const { createCustomerAddress } = await shopClient.query<
  158. CodegenShop.CreateAddressShopMutation,
  159. CodegenShop.CreateAddressShopMutationVariables
  160. >(CREATE_ADDRESS, { input });
  161. expect(createCustomerAddress).toEqual({
  162. id: 'T_3',
  163. streetLine1: '1 Test Street',
  164. country: {
  165. code: 'GB',
  166. },
  167. });
  168. addressId = createCustomerAddress.id;
  169. });
  170. it('customer history for CUSTOMER_ADDRESS_CREATED', async () => {
  171. const result = await adminClient.query<
  172. Codegen.GetCustomerHistoryQuery,
  173. Codegen.GetCustomerHistoryQueryVariables
  174. >(GET_CUSTOMER_HISTORY, {
  175. id: customer.id,
  176. options: {
  177. // skip populated CUSTOMER_ADDRESS_CREATED, CUSTOMER_DETAIL_UPDATED entries
  178. skip: 4,
  179. },
  180. });
  181. expect(result.customer?.history.items.map(pick(['type', 'data']))).toEqual([
  182. {
  183. type: HistoryEntryType.CUSTOMER_ADDRESS_CREATED,
  184. data: {
  185. address: '1 Test Street, United Kingdom',
  186. },
  187. },
  188. ]);
  189. });
  190. it('updateCustomerAddress works', async () => {
  191. const input: UpdateAddressInput = {
  192. id: addressId,
  193. streetLine1: '5 Test Street',
  194. countryCode: 'AT',
  195. };
  196. const result = await shopClient.query<
  197. CodegenShop.UpdateAddressShopMutation,
  198. CodegenShop.UpdateAddressShopMutationVariables
  199. >(UPDATE_ADDRESS, { input });
  200. expect(result.updateCustomerAddress.streetLine1).toEqual('5 Test Street');
  201. expect(result.updateCustomerAddress.country.code).toEqual('AT');
  202. });
  203. it('customer history for CUSTOMER_ADDRESS_UPDATED', async () => {
  204. const result = await adminClient.query<
  205. Codegen.GetCustomerHistoryQuery,
  206. Codegen.GetCustomerHistoryQueryVariables
  207. >(GET_CUSTOMER_HISTORY, { id: customer.id, options: { skip: 5 } });
  208. expect(result.customer?.history.items.map(pick(['type', 'data']))).toEqual([
  209. {
  210. type: HistoryEntryType.CUSTOMER_ADDRESS_UPDATED,
  211. data: {
  212. address: '5 Test Street, Austria',
  213. input: {
  214. id: addressId,
  215. streetLine1: '5 Test Street',
  216. countryCode: 'AT',
  217. },
  218. },
  219. },
  220. ]);
  221. });
  222. it(
  223. 'updateCustomerAddress fails for address not owned by Customer',
  224. assertThrowsWithMessage(async () => {
  225. const input: UpdateAddressInput = {
  226. id: 'T_2',
  227. streetLine1: '1 Test Street',
  228. };
  229. await shopClient.query<
  230. CodegenShop.UpdateAddressShopMutation,
  231. CodegenShop.UpdateAddressShopMutationVariables
  232. >(UPDATE_ADDRESS, { input });
  233. }, 'You are not currently authorized to perform this action'),
  234. );
  235. it('deleteCustomerAddress works', async () => {
  236. const { deleteCustomerAddress } = await shopClient.query<
  237. CodegenShop.DeleteAddressShopMutation,
  238. CodegenShop.DeleteAddressShopMutationVariables
  239. >(DELETE_ADDRESS, { id: 'T_3' });
  240. expect(deleteCustomerAddress.success).toBe(true);
  241. });
  242. it('customer history for CUSTOMER_ADDRESS_DELETED', async () => {
  243. const result = await adminClient.query<
  244. Codegen.GetCustomerHistoryQuery,
  245. Codegen.GetCustomerHistoryQueryVariables
  246. >(GET_CUSTOMER_HISTORY, { id: customer!.id, options: { skip: 6 } });
  247. expect(result.customer?.history.items.map(pick(['type', 'data']))).toEqual([
  248. {
  249. type: HistoryEntryType.CUSTOMER_ADDRESS_DELETED,
  250. data: {
  251. address: '5 Test Street, Austria',
  252. },
  253. },
  254. ]);
  255. });
  256. it(
  257. 'deleteCustomerAddress fails for address not owned by Customer',
  258. assertThrowsWithMessage(async () => {
  259. await shopClient.query<
  260. CodegenShop.DeleteAddressShopMutation,
  261. CodegenShop.DeleteAddressShopMutationVariables
  262. >(DELETE_ADDRESS, { id: 'T_2' });
  263. }, 'You are not currently authorized to perform this action'),
  264. );
  265. it('updatePassword return error result with incorrect current password', async () => {
  266. const { updateCustomerPassword } = await shopClient.query<
  267. CodegenShop.UpdatePasswordMutation,
  268. CodegenShop.UpdatePasswordMutationVariables
  269. >(UPDATE_PASSWORD, {
  270. old: 'wrong',
  271. new: 'test2',
  272. });
  273. successErrorGuard.assertErrorResult(updateCustomerPassword);
  274. expect(updateCustomerPassword.message).toBe('The provided credentials are invalid');
  275. expect(updateCustomerPassword.errorCode).toBe(ErrorCode.INVALID_CREDENTIALS_ERROR);
  276. });
  277. it('updatePassword works', async () => {
  278. const { updateCustomerPassword } = await shopClient.query<
  279. CodegenShop.UpdatePasswordMutation,
  280. CodegenShop.UpdatePasswordMutationVariables
  281. >(UPDATE_PASSWORD, { old: 'test', new: 'test2' });
  282. successErrorGuard.assertSuccess(updateCustomerPassword);
  283. expect(updateCustomerPassword.success).toBe(true);
  284. // Log out and log in with new password
  285. const loginResult = await shopClient.asUserWithCredentials(customer.emailAddress, 'test2');
  286. expect(loginResult.identifier).toBe(customer.emailAddress);
  287. });
  288. it('customer history for CUSTOMER_PASSWORD_UPDATED', async () => {
  289. const result = await adminClient.query<
  290. Codegen.GetCustomerHistoryQuery,
  291. Codegen.GetCustomerHistoryQueryVariables
  292. >(GET_CUSTOMER_HISTORY, { id: customer.id, options: { skip: 7 } });
  293. expect(result.customer?.history.items.map(pick(['type', 'data']))).toEqual([
  294. {
  295. type: HistoryEntryType.CUSTOMER_PASSWORD_UPDATED,
  296. data: {},
  297. },
  298. ]);
  299. });
  300. });
  301. });