plugin.e2e-spec.ts 4.7 KB

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