channel.e2e-spec.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* tslint:disable:no-non-null-assertion */
  2. import { DEFAULT_CHANNEL_CODE } from '@vendure/common/lib/shared-constants';
  3. import { createTestEnvironment, E2E_DEFAULT_CHANNEL_TOKEN } 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 { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  8. import {
  9. AssignProductsToChannel,
  10. CreateAdministrator,
  11. CreateChannel,
  12. CreateRole,
  13. CurrencyCode,
  14. DeleteChannel,
  15. DeletionResult,
  16. GetChannels,
  17. GetCustomerList,
  18. GetProductWithVariants,
  19. LanguageCode,
  20. Me,
  21. Permission,
  22. RemoveProductsFromChannel,
  23. } from './graphql/generated-e2e-admin-types';
  24. import {
  25. ASSIGN_PRODUCT_TO_CHANNEL,
  26. CREATE_ADMINISTRATOR,
  27. CREATE_CHANNEL,
  28. CREATE_ROLE,
  29. GET_CUSTOMER_LIST,
  30. GET_PRODUCT_WITH_VARIANTS,
  31. ME,
  32. REMOVE_PRODUCT_FROM_CHANNEL,
  33. } from './graphql/shared-definitions';
  34. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  35. describe('Channels', () => {
  36. const { server, adminClient, shopClient } = createTestEnvironment(testConfig);
  37. const SECOND_CHANNEL_TOKEN = 'second_channel_token';
  38. const THIRD_CHANNEL_TOKEN = 'third_channel_token';
  39. let secondChannelAdminRole: CreateRole.CreateRole;
  40. let customerUser: GetCustomerList.Items;
  41. beforeAll(async () => {
  42. await server.init({
  43. dataDir: path.join(__dirname, '__data__'),
  44. initialData,
  45. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  46. customerCount: 1,
  47. });
  48. await adminClient.asSuperAdmin();
  49. const { customers } = await adminClient.query<GetCustomerList.Query, GetCustomerList.Variables>(
  50. GET_CUSTOMER_LIST,
  51. {
  52. options: { take: 1 },
  53. },
  54. );
  55. customerUser = customers.items[0];
  56. }, TEST_SETUP_TIMEOUT_MS);
  57. afterAll(async () => {
  58. await server.destroy();
  59. });
  60. it('create a new Channel', async () => {
  61. const { createChannel } = await adminClient.query<CreateChannel.Mutation, CreateChannel.Variables>(
  62. CREATE_CHANNEL,
  63. {
  64. input: {
  65. code: 'second-channel',
  66. token: SECOND_CHANNEL_TOKEN,
  67. defaultLanguageCode: LanguageCode.en,
  68. currencyCode: CurrencyCode.GBP,
  69. pricesIncludeTax: true,
  70. defaultShippingZoneId: 'T_1',
  71. defaultTaxZoneId: 'T_1',
  72. },
  73. },
  74. );
  75. expect(createChannel).toEqual({
  76. id: 'T_2',
  77. code: 'second-channel',
  78. token: SECOND_CHANNEL_TOKEN,
  79. currencyCode: 'GBP',
  80. defaultLanguageCode: 'en',
  81. defaultShippingZone: {
  82. id: 'T_1',
  83. },
  84. defaultTaxZone: {
  85. id: 'T_1',
  86. },
  87. pricesIncludeTax: true,
  88. });
  89. });
  90. it('superadmin has all permissions on new channel', async () => {
  91. const { me } = await adminClient.query<Me.Query>(ME);
  92. expect(me!.channels.length).toBe(2);
  93. const secondChannelData = me!.channels.find(c => c.token === SECOND_CHANNEL_TOKEN);
  94. const nonOwnerPermissions = Object.values(Permission).filter(p => p !== Permission.Owner);
  95. expect(secondChannelData!.permissions).toEqual(nonOwnerPermissions);
  96. });
  97. it('customer has Authenticated permission on new channel', async () => {
  98. await shopClient.asUserWithCredentials(customerUser.emailAddress, 'test');
  99. const { me } = await shopClient.query<Me.Query>(ME);
  100. expect(me!.channels.length).toBe(2);
  101. const secondChannelData = me!.channels.find(c => c.token === SECOND_CHANNEL_TOKEN);
  102. expect(me!.channels).toEqual([
  103. {
  104. code: DEFAULT_CHANNEL_CODE,
  105. permissions: ['Authenticated'],
  106. token: E2E_DEFAULT_CHANNEL_TOKEN,
  107. },
  108. {
  109. code: 'second-channel',
  110. permissions: ['Authenticated'],
  111. token: SECOND_CHANNEL_TOKEN,
  112. },
  113. ]);
  114. });
  115. it('createRole on second Channel', async () => {
  116. const { createRole } = await adminClient.query<CreateRole.Mutation, CreateRole.Variables>(
  117. CREATE_ROLE,
  118. {
  119. input: {
  120. description: 'second channel admin',
  121. code: 'second-channel-admin',
  122. channelIds: ['T_2'],
  123. permissions: [
  124. Permission.ReadCatalog,
  125. Permission.ReadSettings,
  126. Permission.ReadAdministrator,
  127. Permission.CreateAdministrator,
  128. Permission.UpdateAdministrator,
  129. ],
  130. },
  131. },
  132. );
  133. expect(createRole.channels).toEqual([
  134. {
  135. id: 'T_2',
  136. code: 'second-channel',
  137. token: SECOND_CHANNEL_TOKEN,
  138. },
  139. ]);
  140. secondChannelAdminRole = createRole;
  141. });
  142. it('createAdministrator with second-channel-admin role', async () => {
  143. const { createAdministrator } = await adminClient.query<
  144. CreateAdministrator.Mutation,
  145. CreateAdministrator.Variables
  146. >(CREATE_ADMINISTRATOR, {
  147. input: {
  148. firstName: 'Admin',
  149. lastName: 'Two',
  150. emailAddress: 'admin2@test.com',
  151. password: 'test',
  152. roleIds: [secondChannelAdminRole.id],
  153. },
  154. });
  155. expect(createAdministrator.user.roles.map(r => r.description)).toEqual(['second channel admin']);
  156. });
  157. it(
  158. 'cannot create role on channel for which admin does not have CreateAdministrator permission',
  159. assertThrowsWithMessage(async () => {
  160. await adminClient.asUserWithCredentials('admin2@test.com', 'test');
  161. await adminClient.query<CreateRole.Mutation, CreateRole.Variables>(CREATE_ROLE, {
  162. input: {
  163. description: 'read default channel catalog',
  164. code: 'read default channel catalog',
  165. channelIds: ['T_1'],
  166. permissions: [Permission.ReadCatalog],
  167. },
  168. });
  169. }, 'You are not currently authorized to perform this action'),
  170. );
  171. it('can create role on channel for which admin has CreateAdministrator permission', async () => {
  172. const { createRole } = await adminClient.query<CreateRole.Mutation, CreateRole.Variables>(
  173. CREATE_ROLE,
  174. {
  175. input: {
  176. description: 'read second channel catalog',
  177. code: 'read-second-channel-catalog',
  178. channelIds: ['T_2'],
  179. permissions: [Permission.ReadCatalog],
  180. },
  181. },
  182. );
  183. expect(createRole.channels).toEqual([
  184. {
  185. id: 'T_2',
  186. code: 'second-channel',
  187. token: SECOND_CHANNEL_TOKEN,
  188. },
  189. ]);
  190. });
  191. it('createRole with no channelId implicitly uses active channel', async () => {
  192. const { createRole } = await adminClient.query<CreateRole.Mutation, CreateRole.Variables>(
  193. CREATE_ROLE,
  194. {
  195. input: {
  196. description: 'update second channel catalog',
  197. code: 'update-second-channel-catalog',
  198. permissions: [Permission.UpdateCatalog],
  199. },
  200. },
  201. );
  202. expect(createRole.channels).toEqual([
  203. {
  204. id: 'T_2',
  205. code: 'second-channel',
  206. token: SECOND_CHANNEL_TOKEN,
  207. },
  208. ]);
  209. });
  210. describe('assigning Product to Channels', () => {
  211. let product1: GetProductWithVariants.Product;
  212. beforeAll(async () => {
  213. await adminClient.asSuperAdmin();
  214. await adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  215. await adminClient.query<CreateChannel.Mutation, CreateChannel.Variables>(CREATE_CHANNEL, {
  216. input: {
  217. code: 'third-channel',
  218. token: THIRD_CHANNEL_TOKEN,
  219. defaultLanguageCode: LanguageCode.en,
  220. currencyCode: CurrencyCode.GBP,
  221. pricesIncludeTax: true,
  222. defaultShippingZoneId: 'T_1',
  223. defaultTaxZoneId: 'T_1',
  224. },
  225. });
  226. const { product } = await adminClient.query<
  227. GetProductWithVariants.Query,
  228. GetProductWithVariants.Variables
  229. >(GET_PRODUCT_WITH_VARIANTS, {
  230. id: 'T_1',
  231. });
  232. product1 = product!;
  233. });
  234. it(
  235. 'throws if attempting to assign Product to channel to which the admin has no access',
  236. assertThrowsWithMessage(async () => {
  237. await adminClient.asUserWithCredentials('admin2@test.com', 'test');
  238. await adminClient.query<AssignProductsToChannel.Mutation, AssignProductsToChannel.Variables>(
  239. ASSIGN_PRODUCT_TO_CHANNEL,
  240. {
  241. input: {
  242. channelId: 'T_3',
  243. productIds: [product1.id],
  244. },
  245. },
  246. );
  247. }, 'You are not currently authorized to perform this action'),
  248. );
  249. it('assigns Product to Channel and applies price factor', async () => {
  250. const PRICE_FACTOR = 0.5;
  251. await adminClient.asSuperAdmin();
  252. const { assignProductsToChannel } = await adminClient.query<
  253. AssignProductsToChannel.Mutation,
  254. AssignProductsToChannel.Variables
  255. >(ASSIGN_PRODUCT_TO_CHANNEL, {
  256. input: {
  257. channelId: 'T_2',
  258. productIds: [product1.id],
  259. priceFactor: PRICE_FACTOR,
  260. },
  261. });
  262. expect(assignProductsToChannel[0].channels.map(c => c.id)).toEqual(['T_1', 'T_2']);
  263. await adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  264. const { product } = await adminClient.query<
  265. GetProductWithVariants.Query,
  266. GetProductWithVariants.Variables
  267. >(GET_PRODUCT_WITH_VARIANTS, {
  268. id: product1.id,
  269. });
  270. expect(product!.variants.map(v => v.price)).toEqual(
  271. product1.variants.map(v => v.price * PRICE_FACTOR),
  272. );
  273. // Second Channel is configured to include taxes in price, so they should be the same.
  274. expect(product!.variants.map(v => v.priceWithTax)).toEqual(
  275. product1.variants.map(v => v.price * PRICE_FACTOR),
  276. );
  277. });
  278. it('does not assign Product to same channel twice', async () => {
  279. const { assignProductsToChannel } = await adminClient.query<
  280. AssignProductsToChannel.Mutation,
  281. AssignProductsToChannel.Variables
  282. >(ASSIGN_PRODUCT_TO_CHANNEL, {
  283. input: {
  284. channelId: 'T_2',
  285. productIds: [product1.id],
  286. },
  287. });
  288. expect(assignProductsToChannel[0].channels.map(c => c.id)).toEqual(['T_1', 'T_2']);
  289. });
  290. it(
  291. 'throws if attempting to remove Product from default Channel',
  292. assertThrowsWithMessage(async () => {
  293. await adminClient.query<
  294. RemoveProductsFromChannel.Mutation,
  295. RemoveProductsFromChannel.Variables
  296. >(REMOVE_PRODUCT_FROM_CHANNEL, {
  297. input: {
  298. productIds: [product1.id],
  299. channelId: 'T_1',
  300. },
  301. });
  302. }, 'Products cannot be removed from the default Channel'),
  303. );
  304. it('removes Product from Channel', async () => {
  305. await adminClient.asSuperAdmin();
  306. await adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  307. const { removeProductsFromChannel } = await adminClient.query<
  308. RemoveProductsFromChannel.Mutation,
  309. RemoveProductsFromChannel.Variables
  310. >(REMOVE_PRODUCT_FROM_CHANNEL, {
  311. input: {
  312. productIds: [product1.id],
  313. channelId: 'T_2',
  314. },
  315. });
  316. expect(removeProductsFromChannel[0].channels.map(c => c.id)).toEqual(['T_1']);
  317. });
  318. });
  319. it('deleteChannel', async () => {
  320. const PROD_ID = 'T_1';
  321. const { assignProductsToChannel } = await adminClient.query<
  322. AssignProductsToChannel.Mutation,
  323. AssignProductsToChannel.Variables
  324. >(ASSIGN_PRODUCT_TO_CHANNEL, {
  325. input: {
  326. channelId: 'T_2',
  327. productIds: [PROD_ID],
  328. },
  329. });
  330. expect(assignProductsToChannel[0].channels.map(c => c.id)).toEqual(['T_1', 'T_2']);
  331. const { deleteChannel } = await adminClient.query<DeleteChannel.Mutation, DeleteChannel.Variables>(
  332. DELETE_CHANNEL,
  333. {
  334. id: 'T_2',
  335. },
  336. );
  337. expect(deleteChannel.result).toBe(DeletionResult.DELETED);
  338. const { channels } = await adminClient.query<GetChannels.Query>(GET_CHANNELS);
  339. expect(channels.map(c => c.id)).toEqual(['T_1', 'T_3']);
  340. const { product } = await adminClient.query<
  341. GetProductWithVariants.Query,
  342. GetProductWithVariants.Variables
  343. >(GET_PRODUCT_WITH_VARIANTS, {
  344. id: PROD_ID,
  345. });
  346. expect(product!.channels.map(c => c.id)).toEqual(['T_1']);
  347. });
  348. });
  349. const GET_CHANNELS = gql`
  350. query GetChannels {
  351. channels {
  352. id
  353. code
  354. token
  355. }
  356. }
  357. `;
  358. const DELETE_CHANNEL = gql`
  359. mutation DeleteChannel($id: ID!) {
  360. deleteChannel(id: $id) {
  361. message
  362. result
  363. }
  364. }
  365. `;