customer-channel.e2e-spec.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  2. import { createTestEnvironment, E2E_DEFAULT_CHANNEL_TOKEN } from '@vendure/testing';
  3. import path from 'path';
  4. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  5. import { initialData } from '../../../e2e-common/e2e-initial-data';
  6. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  7. import * as Codegen from './graphql/generated-e2e-admin-types';
  8. import { CurrencyCode, LanguageCode } from './graphql/generated-e2e-admin-types';
  9. import { RegisterMutation, RegisterMutationVariables } from './graphql/generated-e2e-shop-types';
  10. import {
  11. ADD_CUSTOMERS_TO_GROUP,
  12. CREATE_ADDRESS,
  13. CREATE_CHANNEL,
  14. CREATE_CUSTOMER,
  15. CREATE_CUSTOMER_GROUP,
  16. DELETE_CUSTOMER,
  17. GET_CUSTOMER_GROUP,
  18. GET_CUSTOMER_LIST,
  19. ME,
  20. REMOVE_CUSTOMERS_FROM_GROUP,
  21. UPDATE_ADDRESS,
  22. UPDATE_CUSTOMER,
  23. } from './graphql/shared-definitions';
  24. import { DELETE_ADDRESS, REGISTER_ACCOUNT } from './graphql/shop-definitions';
  25. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  26. type CustomerListItem = Codegen.GetCustomerListQuery['customers']['items'][number];
  27. describe('ChannelAware Customers', () => {
  28. const { server, adminClient, shopClient } = createTestEnvironment(testConfig());
  29. const SECOND_CHANNEL_TOKEN = 'second_channel_token';
  30. let firstCustomer: CustomerListItem;
  31. let secondCustomer: CustomerListItem;
  32. let thirdCustomer: CustomerListItem;
  33. const numberOfCustomers = 3;
  34. let customerGroupId: string;
  35. beforeAll(async () => {
  36. await server.init({
  37. initialData,
  38. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  39. customerCount: numberOfCustomers,
  40. });
  41. await adminClient.asSuperAdmin();
  42. const { customers } = await adminClient.query<
  43. Codegen.GetCustomerListQuery,
  44. Codegen.GetCustomerListQueryVariables
  45. >(GET_CUSTOMER_LIST, {
  46. options: { take: numberOfCustomers },
  47. });
  48. firstCustomer = customers.items[0];
  49. secondCustomer = customers.items[1];
  50. thirdCustomer = customers.items[2];
  51. await adminClient.query<Codegen.CreateChannelMutation, Codegen.CreateChannelMutationVariables>(
  52. CREATE_CHANNEL,
  53. {
  54. input: {
  55. code: 'second-channel',
  56. token: SECOND_CHANNEL_TOKEN,
  57. defaultLanguageCode: LanguageCode.en,
  58. currencyCode: CurrencyCode.GBP,
  59. pricesIncludeTax: true,
  60. defaultShippingZoneId: 'T_1',
  61. defaultTaxZoneId: 'T_1',
  62. },
  63. },
  64. );
  65. const { createCustomerGroup } = await adminClient.query<
  66. Codegen.CreateCustomerGroupMutation,
  67. Codegen.CreateCustomerGroupMutationVariables
  68. >(CREATE_CUSTOMER_GROUP, {
  69. input: {
  70. name: 'TestGroup',
  71. },
  72. });
  73. customerGroupId = createCustomerGroup.id;
  74. }, TEST_SETUP_TIMEOUT_MS);
  75. afterAll(async () => {
  76. await server.destroy();
  77. });
  78. describe('Address manipulation', () => {
  79. it(
  80. 'throws when updating address from customer from other channel',
  81. assertThrowsWithMessage(async () => {
  82. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  83. await adminClient.query<
  84. Codegen.UpdateAddressMutation,
  85. Codegen.UpdateAddressMutationVariables
  86. >(UPDATE_ADDRESS, {
  87. input: {
  88. id: 'T_1',
  89. streetLine1: 'Dummy street',
  90. },
  91. });
  92. }, 'No Address with the id "1" could be found'),
  93. );
  94. it(
  95. 'throws when creating address for customer from other channel',
  96. assertThrowsWithMessage(async () => {
  97. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  98. await adminClient.query<
  99. Codegen.CreateAddressMutation,
  100. Codegen.CreateAddressMutationVariables
  101. >(CREATE_ADDRESS, {
  102. id: firstCustomer.id,
  103. input: {
  104. streetLine1: 'Dummy street',
  105. countryCode: 'BE',
  106. },
  107. });
  108. }, 'No Customer with the id "1" could be found'),
  109. );
  110. it(
  111. 'throws when deleting address from customer from other channel',
  112. assertThrowsWithMessage(async () => {
  113. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  114. await adminClient.query<
  115. Codegen.DeleteCustomerAddressMutation,
  116. Codegen.DeleteCustomerAddressMutationVariables
  117. >(DELETE_ADDRESS, {
  118. id: 'T_1',
  119. });
  120. }, 'No Address with the id "1" could be found'),
  121. );
  122. });
  123. describe('Customer manipulation', () => {
  124. it(
  125. 'throws when deleting customer from other channel',
  126. assertThrowsWithMessage(async () => {
  127. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  128. await adminClient.query<
  129. Codegen.DeleteCustomerMutation,
  130. Codegen.DeleteCustomerMutationVariables
  131. >(DELETE_CUSTOMER, {
  132. id: firstCustomer.id,
  133. });
  134. }, 'No Customer with the id "1" could be found'),
  135. );
  136. it(
  137. 'throws when updating customer from other channel',
  138. assertThrowsWithMessage(async () => {
  139. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  140. await adminClient.query<
  141. Codegen.UpdateCustomerMutation,
  142. Codegen.UpdateCustomerMutationVariables
  143. >(UPDATE_CUSTOMER, {
  144. input: {
  145. id: firstCustomer.id,
  146. firstName: 'John',
  147. lastName: 'Doe',
  148. },
  149. });
  150. }, 'No Customer with the id "1" could be found'),
  151. );
  152. it('creates customers on current and default channel', async () => {
  153. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  154. await adminClient.query<Codegen.CreateCustomerMutation, Codegen.CreateCustomerMutationVariables>(
  155. CREATE_CUSTOMER,
  156. {
  157. input: {
  158. firstName: 'John',
  159. lastName: 'Doe',
  160. emailAddress: 'john.doe@test.com',
  161. },
  162. },
  163. );
  164. const customersSecondChannel = await adminClient.query<
  165. Codegen.GetCustomerListQuery,
  166. Codegen.GetCustomerListQueryVariables
  167. >(GET_CUSTOMER_LIST);
  168. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  169. const customersDefaultChannel = await adminClient.query<
  170. Codegen.GetCustomerListQuery,
  171. Codegen.GetCustomerListQueryVariables
  172. >(GET_CUSTOMER_LIST);
  173. expect(customersSecondChannel.customers.totalItems).toBe(1);
  174. expect(customersDefaultChannel.customers.totalItems).toBe(numberOfCustomers + 1);
  175. });
  176. it('only shows customers from current channel', async () => {
  177. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  178. const { customers } = await adminClient.query<
  179. Codegen.GetCustomerListQuery,
  180. Codegen.GetCustomerListQueryVariables
  181. >(GET_CUSTOMER_LIST);
  182. expect(customers.totalItems).toBe(1);
  183. });
  184. it('shows all customers on default channel', async () => {
  185. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  186. const { customers } = await adminClient.query<
  187. Codegen.GetCustomerListQuery,
  188. Codegen.GetCustomerListQueryVariables
  189. >(GET_CUSTOMER_LIST);
  190. expect(customers.totalItems).toBe(numberOfCustomers + 1);
  191. });
  192. it('brings customer to current channel when creating with existing emailAddress', async () => {
  193. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  194. let customersDefaultChannel = await adminClient.query<
  195. Codegen.GetCustomerListQuery,
  196. Codegen.GetCustomerListQueryVariables
  197. >(GET_CUSTOMER_LIST);
  198. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  199. let customersSecondChannel = await adminClient.query<
  200. Codegen.GetCustomerListQuery,
  201. Codegen.GetCustomerListQueryVariables
  202. >(GET_CUSTOMER_LIST);
  203. expect(customersDefaultChannel.customers.items.map(customer => customer.emailAddress)).toContain(
  204. firstCustomer.emailAddress,
  205. );
  206. expect(
  207. customersSecondChannel.customers.items.map(customer => customer.emailAddress),
  208. ).not.toContain(firstCustomer.emailAddress);
  209. await adminClient.query<Codegen.CreateCustomerMutation, Codegen.CreateCustomerMutationVariables>(
  210. CREATE_CUSTOMER,
  211. {
  212. input: {
  213. firstName: firstCustomer.firstName + '_new',
  214. lastName: firstCustomer.lastName + '_new',
  215. emailAddress: firstCustomer.emailAddress,
  216. },
  217. },
  218. );
  219. customersSecondChannel = await adminClient.query<
  220. Codegen.GetCustomerListQuery,
  221. Codegen.GetCustomerListQueryVariables
  222. >(GET_CUSTOMER_LIST);
  223. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  224. customersDefaultChannel = await adminClient.query<
  225. Codegen.GetCustomerListQuery,
  226. Codegen.GetCustomerListQueryVariables
  227. >(GET_CUSTOMER_LIST);
  228. const firstCustomerOnNewChannel = customersSecondChannel.customers.items.find(
  229. customer => customer.emailAddress === firstCustomer.emailAddress,
  230. );
  231. const firstCustomerOnDefaultChannel = customersDefaultChannel.customers.items.find(
  232. customer => customer.emailAddress === firstCustomer.emailAddress,
  233. );
  234. expect(firstCustomerOnNewChannel).not.toBeNull();
  235. expect(firstCustomerOnNewChannel?.emailAddress).toBe(firstCustomer.emailAddress);
  236. expect(firstCustomerOnNewChannel?.firstName).toBe(firstCustomer.firstName + '_new');
  237. expect(firstCustomerOnNewChannel?.lastName).toBe(firstCustomer.lastName + '_new');
  238. expect(firstCustomerOnDefaultChannel).not.toBeNull();
  239. expect(firstCustomerOnDefaultChannel?.emailAddress).toBe(firstCustomer.emailAddress);
  240. expect(firstCustomerOnDefaultChannel?.firstName).toBe(firstCustomer.firstName + '_new');
  241. expect(firstCustomerOnDefaultChannel?.lastName).toBe(firstCustomer.lastName + '_new');
  242. });
  243. });
  244. describe('Shop API', () => {
  245. it('assigns authenticated customers to the channels they visit', async () => {
  246. shopClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  247. await shopClient.asUserWithCredentials(secondCustomer.emailAddress, 'test');
  248. await shopClient.query<Codegen.MeQuery>(ME);
  249. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  250. const { customers } = await adminClient.query<
  251. Codegen.GetCustomerListQuery,
  252. Codegen.GetCustomerListQueryVariables
  253. >(GET_CUSTOMER_LIST);
  254. expect(customers.totalItems).toBe(3);
  255. expect(customers.items.map(customer => customer.emailAddress)).toContain(
  256. secondCustomer.emailAddress,
  257. );
  258. });
  259. it('assigns newly registered customers to channel', async () => {
  260. shopClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  261. await shopClient.asAnonymousUser();
  262. await shopClient.query<RegisterMutation, RegisterMutationVariables>(REGISTER_ACCOUNT, {
  263. input: {
  264. emailAddress: 'john.doe.2@test.com',
  265. },
  266. });
  267. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  268. const { customers } = await adminClient.query<
  269. Codegen.GetCustomerListQuery,
  270. Codegen.GetCustomerListQueryVariables
  271. >(GET_CUSTOMER_LIST);
  272. expect(customers.totalItems).toBe(4);
  273. expect(customers.items.map(customer => customer.emailAddress)).toContain('john.doe.2@test.com');
  274. });
  275. // https://github.com/vendure-ecommerce/vendure/issues/834
  276. it('handles concurrent assignments to a new channel', async () => {
  277. const THIRD_CHANNEL_TOKEN = 'third_channel_token';
  278. await adminClient.query<Codegen.CreateChannelMutation, Codegen.CreateChannelMutationVariables>(
  279. CREATE_CHANNEL,
  280. {
  281. input: {
  282. code: 'third-channel',
  283. token: THIRD_CHANNEL_TOKEN,
  284. defaultLanguageCode: LanguageCode.en,
  285. currencyCode: CurrencyCode.GBP,
  286. pricesIncludeTax: true,
  287. defaultShippingZoneId: 'T_1',
  288. defaultTaxZoneId: 'T_1',
  289. },
  290. },
  291. );
  292. await shopClient.asUserWithCredentials(secondCustomer.emailAddress, 'test');
  293. shopClient.setChannelToken(THIRD_CHANNEL_TOKEN);
  294. try {
  295. await Promise.all([
  296. shopClient.query<Codegen.MeQuery>(ME),
  297. shopClient.query<Codegen.MeQuery>(ME),
  298. ]);
  299. } catch (e: any) {
  300. fail('Threw: ' + (e.message as string));
  301. }
  302. adminClient.setChannelToken(THIRD_CHANNEL_TOKEN);
  303. const { customers } = await adminClient.query<
  304. Codegen.GetCustomerListQuery,
  305. Codegen.GetCustomerListQueryVariables
  306. >(GET_CUSTOMER_LIST);
  307. expect(customers.totalItems).toBe(1);
  308. expect(customers.items.map(customer => customer.emailAddress)).toContain(
  309. secondCustomer.emailAddress,
  310. );
  311. });
  312. });
  313. describe('Customergroup manipulation', () => {
  314. it('does not add a customer from another channel to customerGroup', async () => {
  315. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  316. await adminClient.query<
  317. Codegen.AddCustomersToGroupMutation,
  318. Codegen.AddCustomersToGroupMutationVariables
  319. >(ADD_CUSTOMERS_TO_GROUP, {
  320. groupId: customerGroupId,
  321. customerIds: [thirdCustomer.id],
  322. });
  323. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  324. const { customerGroup } = await adminClient.query<
  325. Codegen.GetCustomerGroupQuery,
  326. Codegen.GetCustomerGroupQueryVariables
  327. >(GET_CUSTOMER_GROUP, {
  328. id: customerGroupId,
  329. });
  330. expect(customerGroup!.customers.totalItems).toBe(0);
  331. });
  332. it('only shows customers from current channel in customerGroup', async () => {
  333. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  334. await adminClient.query<
  335. Codegen.AddCustomersToGroupMutation,
  336. Codegen.AddCustomersToGroupMutationVariables
  337. >(ADD_CUSTOMERS_TO_GROUP, {
  338. groupId: customerGroupId,
  339. customerIds: [secondCustomer.id, thirdCustomer.id],
  340. });
  341. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  342. const { customerGroup } = await adminClient.query<
  343. Codegen.GetCustomerGroupQuery,
  344. Codegen.GetCustomerGroupQueryVariables
  345. >(GET_CUSTOMER_GROUP, {
  346. id: customerGroupId,
  347. });
  348. expect(customerGroup!.customers.totalItems).toBe(1);
  349. expect(customerGroup!.customers.items.map(customer => customer.id)).toContain(secondCustomer.id);
  350. });
  351. it('throws when deleting customer from other channel from customerGroup', async () => {
  352. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  353. await adminClient.query<
  354. Codegen.RemoveCustomersFromGroupMutation,
  355. Codegen.RemoveCustomersFromGroupMutationVariables
  356. >(REMOVE_CUSTOMERS_FROM_GROUP, {
  357. groupId: customerGroupId,
  358. customerIds: [thirdCustomer.id],
  359. });
  360. });
  361. });
  362. });