plugin.e2e-spec.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { LanguageCode } from '@vendure/common/lib/generated-types';
  2. import { ConfigService } from '@vendure/core';
  3. import { createTestEnvironment } from '@vendure/testing';
  4. import gql from 'graphql-tag';
  5. import path from 'path';
  6. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  7. import { vi } from 'vitest';
  8. import { initialData } from '../../../e2e-common/e2e-initial-data';
  9. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  10. import { TestPluginWithAllLifecycleHooks } from './fixtures/test-plugins/with-all-lifecycle-hooks';
  11. import { TestAPIExtensionPlugin } from './fixtures/test-plugins/with-api-extensions';
  12. import { TestPluginWithConfig } from './fixtures/test-plugins/with-config';
  13. import { PluginWithGlobalProviders } from './fixtures/test-plugins/with-global-providers';
  14. import { TestLazyExtensionPlugin } from './fixtures/test-plugins/with-lazy-api-extensions';
  15. import { WithNewConfigObjectReferencePlugin } from './fixtures/test-plugins/with-new-config-object-reference';
  16. import { TestPluginWithProvider } from './fixtures/test-plugins/with-provider';
  17. import { TestRestPlugin } from './fixtures/test-plugins/with-rest-controller';
  18. describe('Plugins', () => {
  19. const onConstructorFn = vi.fn();
  20. const activeConfig = testConfig();
  21. const { server, adminClient, shopClient } = createTestEnvironment({
  22. ...activeConfig,
  23. plugins: [
  24. TestPluginWithAllLifecycleHooks.init(onConstructorFn),
  25. TestPluginWithConfig.setup(),
  26. TestAPIExtensionPlugin,
  27. TestPluginWithProvider,
  28. TestLazyExtensionPlugin,
  29. TestRestPlugin,
  30. PluginWithGlobalProviders,
  31. WithNewConfigObjectReferencePlugin,
  32. ],
  33. });
  34. beforeAll(async () => {
  35. await server.init({
  36. initialData,
  37. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  38. customerCount: 1,
  39. });
  40. await adminClient.asSuperAdmin();
  41. }, TEST_SETUP_TIMEOUT_MS);
  42. afterAll(async () => {
  43. await server.destroy();
  44. });
  45. it('can modify the config in configure()', () => {
  46. const configService = server.app.get(ConfigService);
  47. expect(configService instanceof ConfigService).toBe(true);
  48. expect(configService.defaultLanguageCode).toBe(LanguageCode.zh);
  49. });
  50. // https://github.com/vendure-ecommerce/vendure/issues/2906
  51. it('handles plugins that return new config object references', async () => {
  52. const configService = server.app.get(ConfigService);
  53. expect(configService.customFields.Customer).toEqual([
  54. {
  55. name: 'testField',
  56. type: 'string',
  57. },
  58. ]);
  59. });
  60. it('extends the admin API', async () => {
  61. const result = await adminClient.query(gql`
  62. query {
  63. foo
  64. }
  65. `);
  66. expect(result.foo).toEqual(['bar']);
  67. });
  68. it('extends the shop API', async () => {
  69. const result = await shopClient.query(gql`
  70. query {
  71. baz
  72. }
  73. `);
  74. expect(result.baz).toEqual(['quux']);
  75. });
  76. it('custom scalar', async () => {
  77. const result = await adminClient.query(gql`
  78. query {
  79. barList(options: { skip: 0, take: 1 }) {
  80. items {
  81. id
  82. pizzaType
  83. }
  84. }
  85. }
  86. `);
  87. expect(result.barList).toEqual({
  88. items: [{ id: 'T_1', pizzaType: 'Cheese pizza!' }],
  89. });
  90. });
  91. it('allows lazy evaluation of API extension', async () => {
  92. const result = await shopClient.query(gql`
  93. query {
  94. lazy
  95. }
  96. `);
  97. expect(result.lazy).toEqual('sleeping');
  98. });
  99. it('generates ListQueryOptions for api extensions', async () => {
  100. const result = await adminClient.query(gql`
  101. query {
  102. barList(options: { skip: 0, take: 1 }) {
  103. items {
  104. id
  105. name
  106. }
  107. totalItems
  108. }
  109. }
  110. `);
  111. expect(result.barList).toEqual({
  112. items: [{ id: 'T_1', name: 'Test' }],
  113. totalItems: 1,
  114. });
  115. });
  116. it('DI works with defined providers', async () => {
  117. const result = await shopClient.query(gql`
  118. query {
  119. names
  120. }
  121. `);
  122. expect(result.names).toEqual(['seon', 'linda', 'hong']);
  123. });
  124. describe('REST plugins', () => {
  125. const restControllerUrl = `http://localhost:${activeConfig.apiOptions.port}/test`;
  126. it('public route', async () => {
  127. const response = await shopClient.fetch(restControllerUrl + '/public');
  128. const body = await response.text();
  129. expect(body).toBe('success');
  130. });
  131. it('permission-restricted route forbidden', async () => {
  132. const response = await shopClient.fetch(restControllerUrl + '/restricted');
  133. expect(response.status).toBe(403);
  134. const result = await response.json();
  135. expect(result.message).toContain('FORBIDDEN');
  136. });
  137. it('permission-restricted route forbidden allowed', async () => {
  138. await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
  139. const response = await shopClient.fetch(restControllerUrl + '/restricted');
  140. expect(response.status).toBe(200);
  141. const result = await response.text();
  142. expect(result).toBe('success');
  143. });
  144. it('handling I18nErrors', async () => {
  145. const response = await shopClient.fetch(restControllerUrl + '/bad');
  146. expect(response.status).toBe(500);
  147. const result = await response.json();
  148. expect(result.message).toContain('uh oh!');
  149. });
  150. });
  151. });