customer-channel.e2e-spec.ts 17 KB

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