asset-channel.e2e-spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  2. import { CurrencyCode, DeletionResult, LanguageCode } from '@vendure/common/lib/generated-types';
  3. import { createTestEnvironment, E2E_DEFAULT_CHANNEL_TOKEN } from '@vendure/testing';
  4. import path from 'path';
  5. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  6. import { initialData } from '../../../e2e-common/e2e-initial-data';
  7. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  8. import { assignAssetsToChannelDocument, getCollectionWithAssetsDocument } from './graphql/admin-definitions';
  9. import {
  10. assignCollectionsToChannelDocument,
  11. assignProductToChannelDocument,
  12. createAssetsDocument,
  13. createChannelDocument,
  14. deleteAssetDocument,
  15. getAssetDocument,
  16. getProductWithVariantsDocument,
  17. updateCollectionDocument,
  18. updateProductDocument,
  19. } from './graphql/shared-definitions';
  20. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  21. const { server, adminClient } = createTestEnvironment(testConfig());
  22. const SECOND_CHANNEL_TOKEN = 'second_channel_token';
  23. let createdAssetId: string;
  24. let channel2Id: string;
  25. let featuredAssetId: string;
  26. beforeAll(async () => {
  27. await server.init({
  28. initialData,
  29. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  30. customerCount: 1,
  31. });
  32. await adminClient.asSuperAdmin();
  33. const { createChannel } = await adminClient.query(createChannelDocument, {
  34. input: {
  35. code: 'second-channel',
  36. token: SECOND_CHANNEL_TOKEN,
  37. defaultLanguageCode: LanguageCode.en,
  38. currencyCode: CurrencyCode.GBP,
  39. pricesIncludeTax: true,
  40. defaultShippingZoneId: 'T_1',
  41. defaultTaxZoneId: 'T_1',
  42. },
  43. });
  44. if ('id' in createChannel) {
  45. channel2Id = createChannel.id;
  46. }
  47. }, TEST_SETUP_TIMEOUT_MS);
  48. afterAll(async () => {
  49. await server.destroy();
  50. });
  51. describe('ChannelAware Assets', () => {
  52. it('Create asset in default channel', async () => {
  53. const filesToUpload = [path.join(__dirname, 'fixtures/assets/pps2.jpg')];
  54. const { createAssets } = await adminClient.fileUploadMutation({
  55. mutation: createAssetsDocument,
  56. filePaths: filesToUpload,
  57. mapVariables: filePaths => ({
  58. input: filePaths.map(() => ({ file: null })),
  59. }),
  60. });
  61. expect(createAssets.length).toBe(1);
  62. createdAssetId = createAssets[0].id;
  63. expect(createdAssetId).toBeDefined();
  64. });
  65. it('Get asset from default channel', async () => {
  66. const { asset } = await adminClient.query(getAssetDocument, {
  67. id: createdAssetId,
  68. });
  69. expect(asset?.id).toEqual(createdAssetId);
  70. });
  71. it('Asset is not in channel2', async () => {
  72. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  73. const { asset } = await adminClient.query(getAssetDocument, {
  74. id: createdAssetId,
  75. });
  76. expect(asset).toBe(null);
  77. });
  78. it('Add asset to channel2', async () => {
  79. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  80. const { assignAssetsToChannel: assets } = await adminClient.query(assignAssetsToChannelDocument, {
  81. input: {
  82. assetIds: [createdAssetId],
  83. channelId: channel2Id,
  84. },
  85. });
  86. expect(assets[0].id).toBe(createdAssetId);
  87. });
  88. it('Get asset from channel2', async () => {
  89. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  90. const { asset } = await adminClient.query(getAssetDocument, {
  91. id: createdAssetId,
  92. });
  93. expect(asset?.id).toBe(createdAssetId);
  94. });
  95. it('Delete asset from channel2', async () => {
  96. const { deleteAsset } = await adminClient.query(deleteAssetDocument, {
  97. input: {
  98. assetId: createdAssetId,
  99. },
  100. });
  101. expect(deleteAsset.result).toBe(DeletionResult.DELETED);
  102. });
  103. it('Asset is available in default channel', async () => {
  104. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  105. const { asset } = await adminClient.query(getAssetDocument, {
  106. id: createdAssetId,
  107. });
  108. expect(asset?.id).toEqual(createdAssetId);
  109. });
  110. it('Add asset to channel2', async () => {
  111. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  112. const { assignAssetsToChannel: assets } = await adminClient.query(assignAssetsToChannelDocument, {
  113. input: {
  114. assetIds: [createdAssetId],
  115. channelId: channel2Id,
  116. },
  117. });
  118. expect(assets[0].id).toBe(createdAssetId);
  119. });
  120. it(
  121. 'Delete asset from all channels with insufficient permission',
  122. assertThrowsWithMessage(async () => {
  123. await adminClient.asAnonymousUser();
  124. await adminClient.query(deleteAssetDocument, {
  125. input: {
  126. assetId: createdAssetId,
  127. deleteFromAllChannels: true,
  128. },
  129. });
  130. }, 'You are not currently authorized to perform this action'),
  131. );
  132. it('Delete asset from all channels as superadmin', async () => {
  133. await adminClient.asSuperAdmin();
  134. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  135. const { deleteAsset } = await adminClient.query(deleteAssetDocument, {
  136. input: {
  137. assetId: createdAssetId,
  138. deleteFromAllChannels: true,
  139. },
  140. });
  141. expect(deleteAsset.result).toEqual(DeletionResult.DELETED);
  142. });
  143. it('Asset is also deleted in default channel', async () => {
  144. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  145. const { asset } = await adminClient.query(getAssetDocument, {
  146. id: createdAssetId,
  147. });
  148. expect(asset?.id).toBeUndefined();
  149. });
  150. });
  151. describe('Product related assets', () => {
  152. it('Featured asset is available in default channel', async () => {
  153. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  154. const { product } = await adminClient.query(getProductWithVariantsDocument, {
  155. id: 'T_1',
  156. });
  157. featuredAssetId = product!.featuredAsset!.id;
  158. expect(featuredAssetId).toBeDefined();
  159. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  160. const { asset } = await adminClient.query(getAssetDocument, {
  161. id: featuredAssetId,
  162. });
  163. expect(asset?.id).toEqual(featuredAssetId);
  164. });
  165. it('Featured asset is not available in channel2', async () => {
  166. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  167. const { asset } = await adminClient.query(getAssetDocument, {
  168. id: featuredAssetId,
  169. });
  170. expect(asset?.id).toBeUndefined();
  171. });
  172. it('Add Product to channel2', async () => {
  173. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  174. const { assignProductsToChannel } = await adminClient.query(assignProductToChannelDocument, {
  175. input: {
  176. channelId: channel2Id,
  177. productIds: ['T_1'],
  178. },
  179. });
  180. expect(assignProductsToChannel[0].id).toEqual('T_1');
  181. expect(assignProductsToChannel[0].channels.map(c => c.id)).toContain(channel2Id);
  182. });
  183. it('Get featured asset from channel2', async () => {
  184. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  185. const { asset } = await adminClient.query(getAssetDocument, {
  186. id: featuredAssetId,
  187. });
  188. expect(asset?.id).toEqual(featuredAssetId);
  189. });
  190. it('Add Product 2 to channel2', async () => {
  191. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  192. const { assignProductsToChannel } = await adminClient.query(assignProductToChannelDocument, {
  193. input: {
  194. channelId: channel2Id,
  195. productIds: ['T_2'],
  196. },
  197. });
  198. expect(assignProductsToChannel[0].id).toEqual('T_2');
  199. expect(assignProductsToChannel[0].channels.map(c => c.id)).toContain(channel2Id);
  200. });
  201. it('Add asset A to Product 2 in default channel', async () => {
  202. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  203. const { updateProduct } = await adminClient.query(updateProductDocument, {
  204. input: {
  205. id: 'T_2',
  206. assetIds: ['T_3'],
  207. },
  208. });
  209. expect(updateProduct.assets.map(a => a.id)).toContain('T_3');
  210. });
  211. it('Channel2 does not have asset A', async () => {
  212. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  213. const { product } = await adminClient.query(getProductWithVariantsDocument, {
  214. id: 'T_2',
  215. });
  216. expect(product!.assets.find(a => a.id === 'T_3')).toBeUndefined();
  217. });
  218. });
  219. describe('Collection related assets', () => {
  220. let collectionFeaturedAssetId: string;
  221. it('Featured asset is available in default channel', async () => {
  222. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  223. await adminClient.query(updateCollectionDocument, {
  224. input: {
  225. id: 'T_2',
  226. featuredAssetId: 'T_3',
  227. assetIds: ['T_3'],
  228. },
  229. });
  230. const { collection } = await adminClient.query(getCollectionWithAssetsDocument, {
  231. id: 'T_2',
  232. });
  233. collectionFeaturedAssetId = collection!.featuredAsset!.id;
  234. expect(collectionFeaturedAssetId).toBeDefined();
  235. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  236. const { asset } = await adminClient.query(getAssetDocument, {
  237. id: collectionFeaturedAssetId,
  238. });
  239. expect(asset?.id).toEqual(collectionFeaturedAssetId);
  240. });
  241. it('Featured asset is not available in channel2', async () => {
  242. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  243. const { asset } = await adminClient.query(getAssetDocument, {
  244. id: collectionFeaturedAssetId,
  245. });
  246. expect(asset?.id).toBeUndefined();
  247. });
  248. it('Add Collection to channel2', async () => {
  249. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  250. const { assignCollectionsToChannel } = await adminClient.query(assignCollectionsToChannelDocument, {
  251. input: {
  252. channelId: channel2Id,
  253. collectionIds: ['T_2'],
  254. },
  255. });
  256. expect(assignCollectionsToChannel[0].id).toEqual('T_2');
  257. });
  258. it('Get featured asset from channel2', async () => {
  259. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  260. const { asset } = await adminClient.query(getAssetDocument, {
  261. id: collectionFeaturedAssetId,
  262. });
  263. expect(asset?.id).toEqual(collectionFeaturedAssetId);
  264. });
  265. });