role.e2e-spec.ts 4.7 KB

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