role.e2e-spec.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 is not allowed for SuperAdmin role', async () => {
  71. const superAdminRole = defaultRoles.find(r => r.code === SUPER_ADMIN_ROLE_CODE);
  72. if (!superAdminRole) {
  73. fail(`Could not find SuperAdmin role`);
  74. return;
  75. }
  76. try {
  77. const result = await client.query<UpdateRole, UpdateRoleVariables>(UPDATE_ROLE, {
  78. input: {
  79. id: superAdminRole.id,
  80. code: 'superadmin-modified',
  81. description: 'superadmin modified',
  82. permissions: [Permission.Authenticated],
  83. },
  84. });
  85. fail(`Should throw`);
  86. } catch (err) {
  87. expect(err.message).toEqual(
  88. expect.stringContaining(`The role '${SUPER_ADMIN_ROLE_CODE}' cannot be modified`),
  89. );
  90. }
  91. });
  92. it('updateRole is not allowed for Customer role', async () => {
  93. const customerRole = defaultRoles.find(r => r.code === CUSTOMER_ROLE_CODE);
  94. if (!customerRole) {
  95. fail(`Could not find Customer role`);
  96. return;
  97. }
  98. try {
  99. const result = await client.query<UpdateRole, UpdateRoleVariables>(UPDATE_ROLE, {
  100. input: {
  101. id: customerRole.id,
  102. code: 'customer-modified',
  103. description: 'customer modified',
  104. permissions: [Permission.Authenticated, Permission.DeleteAdministrator],
  105. },
  106. });
  107. fail(`Should throw`);
  108. } catch (err) {
  109. expect(err.message).toEqual(
  110. expect.stringContaining(`The role '${CUSTOMER_ROLE_CODE}' cannot be modified`),
  111. );
  112. }
  113. });
  114. });