channel.e2e-spec.ts 17 KB

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