role.e2e-spec.ts 4.6 KB

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