plugin.e2e-spec.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 { initialData } from '../../../e2e-common/e2e-initial-data';
  7. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  8. import { TestPluginWithAllLifecycleHooks } from './fixtures/test-plugins/with-all-lifecycle-hooks';
  9. import { TestAPIExtensionPlugin } from './fixtures/test-plugins/with-api-extensions';
  10. import { TestPluginWithConfigAndBootstrap } from './fixtures/test-plugins/with-config-and-bootstrap';
  11. import { TestLazyExtensionPlugin } from './fixtures/test-plugins/with-lazy-api-extensions';
  12. import { TestPluginWithProvider } from './fixtures/test-plugins/with-provider';
  13. import { TestRestPlugin } from './fixtures/test-plugins/with-rest-controller';
  14. import { TestProcessContextPlugin } from './fixtures/test-plugins/with-worker-controller';
  15. describe('Plugins', () => {
  16. const bootstrapMockFn = jest.fn();
  17. const onConstructorFn = jest.fn();
  18. const beforeBootstrapFn = jest.fn();
  19. const beforeWorkerBootstrapFn = jest.fn();
  20. const onBootstrapFn = jest.fn();
  21. const onWorkerBootstrapFn = jest.fn();
  22. const onCloseFn = jest.fn();
  23. const onWorkerCloseFn = jest.fn();
  24. const { server, adminClient, shopClient } = createTestEnvironment({
  25. ...testConfig,
  26. plugins: [
  27. TestPluginWithAllLifecycleHooks.init(
  28. onConstructorFn,
  29. beforeBootstrapFn,
  30. beforeWorkerBootstrapFn,
  31. onBootstrapFn,
  32. onWorkerBootstrapFn,
  33. onCloseFn,
  34. onWorkerCloseFn,
  35. ),
  36. TestPluginWithConfigAndBootstrap.setup(bootstrapMockFn),
  37. TestAPIExtensionPlugin,
  38. TestPluginWithProvider,
  39. TestLazyExtensionPlugin,
  40. TestRestPlugin,
  41. TestProcessContextPlugin,
  42. ],
  43. });
  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. }, TEST_SETUP_TIMEOUT_MS);
  52. afterAll(async () => {
  53. await server.destroy();
  54. });
  55. it('constructs one instance for each process', () => {
  56. expect(onConstructorFn).toHaveBeenCalledTimes(2);
  57. });
  58. it('calls beforeVendureBootstrap', () => {
  59. expect(beforeBootstrapFn).toHaveBeenCalledTimes(1);
  60. expect(beforeBootstrapFn).toHaveBeenCalledWith(server.app);
  61. });
  62. it('calls beforeVendureWorkerBootstrap', () => {
  63. expect(beforeWorkerBootstrapFn).toHaveBeenCalledTimes(1);
  64. expect(beforeWorkerBootstrapFn).toHaveBeenCalledWith(server.worker);
  65. });
  66. it('calls onVendureBootstrap', () => {
  67. expect(onBootstrapFn).toHaveBeenCalledTimes(1);
  68. });
  69. it('calls onWorkerVendureBootstrap', () => {
  70. expect(onWorkerBootstrapFn).toHaveBeenCalledTimes(1);
  71. });
  72. it('can modify the config in configure()', () => {
  73. expect(bootstrapMockFn).toHaveBeenCalledTimes(1);
  74. const configService: ConfigService = bootstrapMockFn.mock.calls[0][0];
  75. expect(configService instanceof ConfigService).toBe(true);
  76. expect(configService.defaultLanguageCode).toBe(LanguageCode.zh);
  77. });
  78. it('extends the admin API', async () => {
  79. const result = await adminClient.query(gql`
  80. query {
  81. foo
  82. }
  83. `);
  84. expect(result.foo).toEqual(['bar']);
  85. });
  86. it('extends the shop API', async () => {
  87. const result = await shopClient.query(gql`
  88. query {
  89. baz
  90. }
  91. `);
  92. expect(result.baz).toEqual(['quux']);
  93. });
  94. it('allows lazy evaluation of API extension', async () => {
  95. const result = await shopClient.query(gql`
  96. query {
  97. lazy
  98. }
  99. `);
  100. expect(result.lazy).toEqual('sleeping');
  101. });
  102. it('generates ListQueryOptions for api extensions', async () => {
  103. const result = await adminClient.query(gql`
  104. query {
  105. barList(options: { skip: 0, take: 1 }) {
  106. items {
  107. id
  108. name
  109. }
  110. totalItems
  111. }
  112. }
  113. `);
  114. expect(result.barList).toEqual({
  115. items: [{ id: 'T_1', name: 'Test' }],
  116. totalItems: 1,
  117. });
  118. });
  119. it('DI works with defined providers', async () => {
  120. const result = await shopClient.query(gql`
  121. query {
  122. names
  123. }
  124. `);
  125. expect(result.names).toEqual(['seon', 'linda', 'hong']);
  126. });
  127. describe('REST plugins', () => {
  128. const restControllerUrl = `http://localhost:${testConfig.apiOptions.port}/test`;
  129. it('public route', async () => {
  130. const response = await shopClient.fetch(restControllerUrl + '/public');
  131. const body = await response.text();
  132. expect(body).toBe('success');
  133. });
  134. it('permission-restricted route forbidden', async () => {
  135. const response = await shopClient.fetch(restControllerUrl + '/restricted');
  136. expect(response.status).toBe(403);
  137. const result = await response.json();
  138. expect(result.message).toContain('FORBIDDEN');
  139. });
  140. it('permission-restricted route forbidden allowed', async () => {
  141. await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
  142. const response = await shopClient.fetch(restControllerUrl + '/restricted');
  143. expect(response.status).toBe(200);
  144. const result = await response.text();
  145. expect(result).toBe('success');
  146. });
  147. it('handling I18nErrors', async () => {
  148. const response = await shopClient.fetch(restControllerUrl + '/bad');
  149. expect(response.status).toBe(500);
  150. const result = await response.json();
  151. expect(result.message).toContain('uh oh!');
  152. });
  153. });
  154. describe('processContext', () => {
  155. it('server context', async () => {
  156. const response = await shopClient.fetch(
  157. `http://localhost:${testConfig.apiOptions.port}/process-context/server`,
  158. );
  159. const body = await response.text();
  160. expect(body).toBe('true');
  161. });
  162. it('worker context', async () => {
  163. const response = await shopClient.fetch(
  164. `http://localhost:${testConfig.apiOptions.port}/process-context/worker`,
  165. );
  166. const body = await response.text();
  167. expect(body).toBe('true');
  168. });
  169. });
  170. describe('on app close', () => {
  171. beforeAll(async () => {
  172. await server.destroy();
  173. });
  174. it('calls onVendureClose', () => {
  175. expect(onCloseFn).toHaveBeenCalled();
  176. });
  177. it('calls onWorkerVendureClose', () => {
  178. expect(onWorkerCloseFn).toHaveBeenCalled();
  179. });
  180. });
  181. });