plugin.e2e-spec.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 { TestPluginWithConfig } from './fixtures/test-plugins/with-config';
  11. import { PluginWithGlobalProviders } from './fixtures/test-plugins/with-global-providers';
  12. import { TestLazyExtensionPlugin } from './fixtures/test-plugins/with-lazy-api-extensions';
  13. import { TestPluginWithProvider } from './fixtures/test-plugins/with-provider';
  14. import { TestRestPlugin } from './fixtures/test-plugins/with-rest-controller';
  15. describe('Plugins', () => {
  16. const onConstructorFn = jest.fn();
  17. const activeConfig = testConfig();
  18. const { server, adminClient, shopClient } = createTestEnvironment({
  19. ...activeConfig,
  20. plugins: [
  21. TestPluginWithAllLifecycleHooks.init(onConstructorFn),
  22. TestPluginWithConfig.setup(),
  23. TestAPIExtensionPlugin,
  24. TestPluginWithProvider,
  25. TestLazyExtensionPlugin,
  26. TestRestPlugin,
  27. PluginWithGlobalProviders,
  28. ],
  29. });
  30. beforeAll(async () => {
  31. await server.init({
  32. initialData,
  33. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  34. customerCount: 1,
  35. });
  36. await adminClient.asSuperAdmin();
  37. }, TEST_SETUP_TIMEOUT_MS);
  38. afterAll(async () => {
  39. await server.destroy();
  40. });
  41. it('can modify the config in configure()', () => {
  42. const configService = server.app.get(ConfigService);
  43. expect(configService instanceof ConfigService).toBe(true);
  44. expect(configService.defaultLanguageCode).toBe(LanguageCode.zh);
  45. expect(TestPluginWithConfig.configSpy).toHaveBeenCalledTimes(1);
  46. });
  47. it('extends the admin API', async () => {
  48. const result = await adminClient.query(gql`
  49. query {
  50. foo
  51. }
  52. `);
  53. expect(result.foo).toEqual(['bar']);
  54. });
  55. it('extends the shop API', async () => {
  56. const result = await shopClient.query(gql`
  57. query {
  58. baz
  59. }
  60. `);
  61. expect(result.baz).toEqual(['quux']);
  62. });
  63. it('allows lazy evaluation of API extension', async () => {
  64. const result = await shopClient.query(gql`
  65. query {
  66. lazy
  67. }
  68. `);
  69. expect(result.lazy).toEqual('sleeping');
  70. });
  71. it('generates ListQueryOptions for api extensions', async () => {
  72. const result = await adminClient.query(gql`
  73. query {
  74. barList(options: { skip: 0, take: 1 }) {
  75. items {
  76. id
  77. name
  78. }
  79. totalItems
  80. }
  81. }
  82. `);
  83. expect(result.barList).toEqual({
  84. items: [{ id: 'T_1', name: 'Test' }],
  85. totalItems: 1,
  86. });
  87. });
  88. it('DI works with defined providers', async () => {
  89. const result = await shopClient.query(gql`
  90. query {
  91. names
  92. }
  93. `);
  94. expect(result.names).toEqual(['seon', 'linda', 'hong']);
  95. });
  96. describe('REST plugins', () => {
  97. const restControllerUrl = `http://localhost:${activeConfig.apiOptions.port}/test`;
  98. it('public route', async () => {
  99. const response = await shopClient.fetch(restControllerUrl + '/public');
  100. const body = await response.text();
  101. expect(body).toBe('success');
  102. });
  103. it('permission-restricted route forbidden', async () => {
  104. const response = await shopClient.fetch(restControllerUrl + '/restricted');
  105. expect(response.status).toBe(403);
  106. const result = await response.json();
  107. expect(result.message).toContain('FORBIDDEN');
  108. });
  109. it('permission-restricted route forbidden allowed', async () => {
  110. await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
  111. const response = await shopClient.fetch(restControllerUrl + '/restricted');
  112. expect(response.status).toBe(200);
  113. const result = await response.text();
  114. expect(result).toBe('success');
  115. });
  116. it('handling I18nErrors', async () => {
  117. const response = await shopClient.fetch(restControllerUrl + '/bad');
  118. expect(response.status).toBe(500);
  119. const result = await response.json();
  120. expect(result.message).toContain('uh oh!');
  121. });
  122. });
  123. });
  124. describe('Multiple bootstraps in same process', () => {
  125. const activeConfig = testConfig();
  126. const { server, adminClient, shopClient } = createTestEnvironment({
  127. ...activeConfig,
  128. plugins: [TestPluginWithConfig.setup()],
  129. });
  130. beforeAll(async () => {
  131. await server.init({
  132. initialData,
  133. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  134. customerCount: 1,
  135. });
  136. await adminClient.asSuperAdmin();
  137. }, TEST_SETUP_TIMEOUT_MS);
  138. it('plugin `configure` function called only once', async () => {
  139. expect(TestPluginWithConfig.configSpy).toHaveBeenCalledTimes(1);
  140. });
  141. });