custom-permissions.e2e-spec.ts 6.8 KB

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