customer-channel.e2e-spec.ts 16 KB

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