role.e2e-spec.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import {
  2. CreateRole,
  3. CreateRoleVariables,
  4. GetRole,
  5. GetRoles,
  6. GetRolesVariables,
  7. GetRoleVariables,
  8. Permission,
  9. Role,
  10. UpdateRole,
  11. UpdateRoleVariables,
  12. } from 'shared/generated-types';
  13. import { omit } from 'shared/omit';
  14. import { CUSTOMER_ROLE_CODE, SUPER_ADMIN_ROLE_CODE } from 'shared/shared-constants';
  15. import {
  16. CREATE_ROLE,
  17. GET_ROLE,
  18. GET_ROLES,
  19. UPDATE_ROLE,
  20. } from '../../admin-ui/src/app/data/definitions/administrator-definitions';
  21. import { TestClient } from './test-client';
  22. import { TestServer } from './test-server';
  23. describe('Role resolver', () => {
  24. const client = new TestClient();
  25. const server = new TestServer();
  26. let createdRole: Role;
  27. let defaultRoles: Role[];
  28. beforeAll(async () => {
  29. const token = await server.init({
  30. productCount: 1,
  31. customerCount: 1,
  32. });
  33. await client.init();
  34. }, 60000);
  35. afterAll(async () => {
  36. await server.destroy();
  37. });
  38. it('roles', async () => {
  39. const result = await client.query<GetRoles, GetRolesVariables>(GET_ROLES);
  40. defaultRoles = result.roles.items;
  41. expect(result.roles.items.length).toBe(2);
  42. expect(result.roles.totalItems).toBe(2);
  43. });
  44. it('createRole', async () => {
  45. const result = await client.query<CreateRole, CreateRoleVariables>(CREATE_ROLE, {
  46. input: {
  47. code: 'test',
  48. description: 'test role',
  49. permissions: [Permission.ReadCustomer, Permission.UpdateCustomer],
  50. },
  51. });
  52. createdRole = result.createRole;
  53. expect(omit(createdRole, ['channels'])).toMatchSnapshot();
  54. });
  55. it('role', async () => {
  56. const result = await client.query<GetRole, GetRoleVariables>(GET_ROLE, { id: createdRole.id });
  57. expect(result.role).toEqual(createdRole);
  58. });
  59. it('updateRole', async () => {
  60. const result = await client.query<UpdateRole, UpdateRoleVariables>(UPDATE_ROLE, {
  61. input: {
  62. id: createdRole.id,
  63. code: 'test-modified',
  64. description: 'test role modified',
  65. permissions: [Permission.ReadCustomer, Permission.UpdateCustomer, Permission.DeleteCustomer],
  66. },
  67. });
  68. expect(omit(result.updateRole, ['channels'])).toMatchSnapshot();
  69. });
  70. it('updateRole works with partial input', async () => {
  71. const result = await client.query<UpdateRole, UpdateRoleVariables>(UPDATE_ROLE, {
  72. input: {
  73. id: createdRole.id,
  74. code: 'test-modified-again',
  75. },
  76. });
  77. expect(result.updateRole.code).toBe('test-modified-again');
  78. expect(result.updateRole.description).toBe('test role modified');
  79. expect(result.updateRole.permissions).toEqual([
  80. Permission.ReadCustomer,
  81. Permission.UpdateCustomer,
  82. Permission.DeleteCustomer,
  83. ]);
  84. });
  85. it('updateRole is not allowed for SuperAdmin role', async () => {
  86. const superAdminRole = defaultRoles.find(r => r.code === SUPER_ADMIN_ROLE_CODE);
  87. if (!superAdminRole) {
  88. fail(`Could not find SuperAdmin role`);
  89. return;
  90. }
  91. try {
  92. const result = await client.query<UpdateRole, UpdateRoleVariables>(UPDATE_ROLE, {
  93. input: {
  94. id: superAdminRole.id,
  95. code: 'superadmin-modified',
  96. description: 'superadmin modified',
  97. permissions: [Permission.Authenticated],
  98. },
  99. });
  100. fail(`Should throw`);
  101. } catch (err) {
  102. expect(err.message).toEqual(
  103. expect.stringContaining(`The role '${SUPER_ADMIN_ROLE_CODE}' cannot be modified`),
  104. );
  105. }
  106. });
  107. it('updateRole is not allowed for Customer role', async () => {
  108. const customerRole = defaultRoles.find(r => r.code === CUSTOMER_ROLE_CODE);
  109. if (!customerRole) {
  110. fail(`Could not find Customer role`);
  111. return;
  112. }
  113. try {
  114. const result = await client.query<UpdateRole, UpdateRoleVariables>(UPDATE_ROLE, {
  115. input: {
  116. id: customerRole.id,
  117. code: 'customer-modified',
  118. description: 'customer modified',
  119. permissions: [Permission.Authenticated, Permission.DeleteAdministrator],
  120. },
  121. });
  122. fail(`Should throw`);
  123. } catch (err) {
  124. expect(err.message).toEqual(
  125. expect.stringContaining(`The role '${CUSTOMER_ROLE_CODE}' cannot be modified`),
  126. );
  127. }
  128. });
  129. });