role.e2e-spec.ts 4.6 KB

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