plugin.e2e-spec.ts 6.6 KB

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