plugin.e2e-spec.ts 5.9 KB

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