plugin.e2e-spec.ts 5.4 KB

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