plugin.e2e-spec.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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('allows lazy evaluation of API extension', async () => {
  63. const result = await shopClient.query(gql`
  64. query {
  65. lazy
  66. }
  67. `);
  68. expect(result.lazy).toEqual('sleeping');
  69. });
  70. it('generates ListQueryOptions for api extensions', async () => {
  71. const result = await adminClient.query(gql`
  72. query {
  73. barList(options: { skip: 0, take: 1 }) {
  74. items {
  75. id
  76. name
  77. }
  78. totalItems
  79. }
  80. }
  81. `);
  82. expect(result.barList).toEqual({
  83. items: [{ id: 'T_1', name: 'Test' }],
  84. totalItems: 1,
  85. });
  86. });
  87. it('DI works with defined providers', async () => {
  88. const result = await shopClient.query(gql`
  89. query {
  90. names
  91. }
  92. `);
  93. expect(result.names).toEqual(['seon', 'linda', 'hong']);
  94. });
  95. describe('REST plugins', () => {
  96. const restControllerUrl = `http://localhost:${activeConfig.apiOptions.port}/test`;
  97. it('public route', async () => {
  98. const response = await shopClient.fetch(restControllerUrl + '/public');
  99. const body = await response.text();
  100. expect(body).toBe('success');
  101. });
  102. it('permission-restricted route forbidden', async () => {
  103. const response = await shopClient.fetch(restControllerUrl + '/restricted');
  104. expect(response.status).toBe(403);
  105. const result = await response.json();
  106. expect(result.message).toContain('FORBIDDEN');
  107. });
  108. it('permission-restricted route forbidden allowed', async () => {
  109. await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
  110. const response = await shopClient.fetch(restControllerUrl + '/restricted');
  111. expect(response.status).toBe(200);
  112. const result = await response.text();
  113. expect(result).toBe('success');
  114. });
  115. it('handling I18nErrors', async () => {
  116. const response = await shopClient.fetch(restControllerUrl + '/bad');
  117. expect(response.status).toBe(500);
  118. const result = await response.json();
  119. expect(result.message).toContain('uh oh!');
  120. });
  121. });
  122. });