channel.e2e-spec.ts 14 KB

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