channel.e2e-spec.ts 16 KB

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