custom-permissions.e2e-spec.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { mergeConfig } from '@vendure/core';
  2. import path from 'path';
  3. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  4. import { initialData } from '../../../e2e-common/e2e-initial-data';
  5. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  6. import { createTestEnvironment } from '../../testing/lib/create-test-environment';
  7. import {
  8. sync,
  9. TestPluginWithCustomPermissions,
  10. wishlist,
  11. } from './fixtures/test-plugins/with-custom-permissions';
  12. import {
  13. crudCreateDocument,
  14. crudDeleteDocument,
  15. crudReadDocument,
  16. crudUpdateDocument,
  17. syncCustomPermissionsDocument,
  18. } from './graphql/admin-definitions';
  19. import { ResultOf } from './graphql/graphql-admin';
  20. import {
  21. createAdministratorDocument,
  22. createRoleDocument,
  23. updateRoleDocument,
  24. } from './graphql/shared-definitions';
  25. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  26. describe('Custom permissions', () => {
  27. const { server, adminClient } = createTestEnvironment(
  28. mergeConfig(testConfig(), {
  29. plugins: [TestPluginWithCustomPermissions],
  30. }),
  31. );
  32. let testRole: ResultOf<typeof createRoleDocument>['createRole'];
  33. let testAdmin: ResultOf<typeof createAdministratorDocument>['createAdministrator'];
  34. beforeAll(async () => {
  35. await server.init({
  36. initialData,
  37. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  38. customerCount: 1,
  39. });
  40. await adminClient.asSuperAdmin();
  41. // create a new role and Admin and sign in as that Admin
  42. const { createRole } = await adminClient.query(createRoleDocument, {
  43. input: {
  44. channelIds: ['T_1'],
  45. code: 'test-role',
  46. description: 'Testing custom permissions',
  47. permissions: [],
  48. },
  49. });
  50. testRole = createRole;
  51. const { createAdministrator } = await adminClient.query(createAdministratorDocument, {
  52. input: {
  53. firstName: 'Test',
  54. lastName: 'Admin',
  55. emailAddress: 'test@admin.com',
  56. password: 'test',
  57. roleIds: [testRole.id],
  58. },
  59. });
  60. testAdmin = createAdministrator;
  61. }, TEST_SETUP_TIMEOUT_MS);
  62. afterAll(async () => {
  63. await server.destroy();
  64. });
  65. describe('superadmin has custom permissions automatically', () => {
  66. beforeAll(async () => {
  67. await adminClient.asSuperAdmin();
  68. });
  69. it('single permission', async () => {
  70. const { syncWishlist } = await adminClient.query(syncCustomPermissionsDocument);
  71. expect(syncWishlist).toBe(true);
  72. });
  73. it('CRUD create permission', async () => {
  74. const { createWishlist } = await adminClient.query(crudCreateDocument);
  75. expect(createWishlist).toBe(true);
  76. });
  77. it('CRUD read permission', async () => {
  78. // eslint-disable-next-line no-shadow,@typescript-eslint/no-shadow
  79. const { wishlist } = await adminClient.query(crudReadDocument);
  80. expect(wishlist).toBe(true);
  81. });
  82. it('CRUD update permission', async () => {
  83. const { updateWishlist } = await adminClient.query(crudUpdateDocument);
  84. expect(updateWishlist).toBe(true);
  85. });
  86. it('CRUD delete permission', async () => {
  87. const { deleteWishlist } = await adminClient.query(crudDeleteDocument);
  88. expect(deleteWishlist).toBe(true);
  89. });
  90. });
  91. describe('custom permissions prevent unauthorized access', () => {
  92. beforeAll(async () => {
  93. await adminClient.asUserWithCredentials(testAdmin.emailAddress, 'test');
  94. });
  95. it(
  96. 'single permission',
  97. assertThrowsWithMessage(async () => {
  98. await adminClient.query(syncCustomPermissionsDocument);
  99. }, 'You are not currently authorized to perform this action'),
  100. );
  101. it(
  102. 'CRUD create permission',
  103. assertThrowsWithMessage(async () => {
  104. await adminClient.query(crudCreateDocument);
  105. }, 'You are not currently authorized to perform this action'),
  106. );
  107. it(
  108. 'CRUD read permission',
  109. assertThrowsWithMessage(async () => {
  110. await adminClient.query(crudReadDocument);
  111. }, 'You are not currently authorized to perform this action'),
  112. );
  113. it(
  114. 'CRUD update permission',
  115. assertThrowsWithMessage(async () => {
  116. await adminClient.query(crudUpdateDocument);
  117. }, 'You are not currently authorized to perform this action'),
  118. );
  119. it(
  120. 'CRUD delete permission',
  121. assertThrowsWithMessage(async () => {
  122. await adminClient.query(crudDeleteDocument);
  123. }, 'You are not currently authorized to perform this action'),
  124. );
  125. });
  126. describe('adding permissions enables access', () => {
  127. beforeAll(async () => {
  128. await adminClient.asSuperAdmin();
  129. await adminClient.query(updateRoleDocument, {
  130. input: {
  131. id: testRole.id,
  132. permissions: [
  133. sync.Permission,
  134. wishlist.Create,
  135. wishlist.Read,
  136. wishlist.Update,
  137. wishlist.Delete,
  138. ],
  139. },
  140. });
  141. await adminClient.asUserWithCredentials(testAdmin.emailAddress, 'test');
  142. });
  143. it('single permission', async () => {
  144. const { syncWishlist } = await adminClient.query(syncCustomPermissionsDocument);
  145. expect(syncWishlist).toBe(true);
  146. });
  147. it('CRUD create permission', async () => {
  148. const { createWishlist } = await adminClient.query(crudCreateDocument);
  149. expect(createWishlist).toBe(true);
  150. });
  151. it('CRUD read permission', async () => {
  152. // eslint-disable-next-line no-shadow,@typescript-eslint/no-shadow
  153. const { wishlist } = await adminClient.query(crudReadDocument);
  154. expect(wishlist).toBe(true);
  155. });
  156. it('CRUD update permission', async () => {
  157. const { updateWishlist } = await adminClient.query(crudUpdateDocument);
  158. expect(updateWishlist).toBe(true);
  159. });
  160. it('CRUD delete permission', async () => {
  161. const { deleteWishlist } = await adminClient.query(crudDeleteDocument);
  162. expect(deleteWishlist).toBe(true);
  163. });
  164. });
  165. });