plugin.e2e-spec.ts 4.8 KB

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