plugin.e2e-spec.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. });
  46. it('extends the admin API', async () => {
  47. const result = await adminClient.query(gql`
  48. query {
  49. foo
  50. }
  51. `);
  52. expect(result.foo).toEqual(['bar']);
  53. });
  54. it('extends the shop API', async () => {
  55. const result = await shopClient.query(gql`
  56. query {
  57. baz
  58. }
  59. `);
  60. expect(result.baz).toEqual(['quux']);
  61. });
  62. it('custom scalar', async () => {
  63. const result = await adminClient.query(gql`
  64. query {
  65. barList(options: { skip: 0, take: 1 }) {
  66. items {
  67. id
  68. pizzaType
  69. }
  70. }
  71. }
  72. `);
  73. expect(result.barList).toEqual({
  74. items: [{ id: 'T_1', pizzaType: 'Cheese pizza!' }],
  75. });
  76. });
  77. it('allows lazy evaluation of API extension', async () => {
  78. const result = await shopClient.query(gql`
  79. query {
  80. lazy
  81. }
  82. `);
  83. expect(result.lazy).toEqual('sleeping');
  84. });
  85. it('generates ListQueryOptions for api extensions', async () => {
  86. const result = await adminClient.query(gql`
  87. query {
  88. barList(options: { skip: 0, take: 1 }) {
  89. items {
  90. id
  91. name
  92. }
  93. totalItems
  94. }
  95. }
  96. `);
  97. expect(result.barList).toEqual({
  98. items: [{ id: 'T_1', name: 'Test' }],
  99. totalItems: 1,
  100. });
  101. });
  102. it('DI works with defined providers', async () => {
  103. const result = await shopClient.query(gql`
  104. query {
  105. names
  106. }
  107. `);
  108. expect(result.names).toEqual(['seon', 'linda', 'hong']);
  109. });
  110. describe('REST plugins', () => {
  111. const restControllerUrl = `http://localhost:${activeConfig.apiOptions.port}/test`;
  112. it('public route', async () => {
  113. const response = await shopClient.fetch(restControllerUrl + '/public');
  114. const body = await response.text();
  115. expect(body).toBe('success');
  116. });
  117. it('permission-restricted route forbidden', async () => {
  118. const response = await shopClient.fetch(restControllerUrl + '/restricted');
  119. expect(response.status).toBe(403);
  120. const result = await response.json();
  121. expect(result.message).toContain('FORBIDDEN');
  122. });
  123. it('permission-restricted route forbidden allowed', async () => {
  124. await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
  125. const response = await shopClient.fetch(restControllerUrl + '/restricted');
  126. expect(response.status).toBe(200);
  127. const result = await response.text();
  128. expect(result).toBe('success');
  129. });
  130. it('handling I18nErrors', async () => {
  131. const response = await shopClient.fetch(restControllerUrl + '/bad');
  132. expect(response.status).toBe(500);
  133. const result = await response.json();
  134. expect(result.message).toContain('uh oh!');
  135. });
  136. });
  137. });