custom-permissions.e2e-spec.ts 7.1 KB

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