role.e2e-spec.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { omit } from '@vendure/common/lib/omit';
  2. import { CUSTOMER_ROLE_CODE, SUPER_ADMIN_ROLE_CODE } from '@vendure/common/lib/shared-constants';
  3. import gql from 'graphql-tag';
  4. import path from 'path';
  5. import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
  6. import { ROLE_FRAGMENT } from './graphql/fragments';
  7. import { CreateRole, GetRole, GetRoles, Permission, Role, UpdateRole } from './graphql/generated-e2e-admin-types';
  8. import { CREATE_ROLE } from './graphql/shared-definitions';
  9. import { TestAdminClient } from './test-client';
  10. import { TestServer } from './test-server';
  11. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  12. describe('Role resolver', () => {
  13. const client = new TestAdminClient();
  14. const server = new TestServer();
  15. let createdRole: Role.Fragment;
  16. let defaultRoles: Role.Fragment[];
  17. beforeAll(async () => {
  18. const token = await server.init({
  19. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  20. customerCount: 1,
  21. });
  22. await client.init();
  23. }, TEST_SETUP_TIMEOUT_MS);
  24. afterAll(async () => {
  25. await server.destroy();
  26. });
  27. it('roles', async () => {
  28. const result = await client.query<GetRoles.Query, GetRoles.Variables>(GET_ROLES);
  29. defaultRoles = result.roles.items;
  30. expect(result.roles.items.length).toBe(2);
  31. expect(result.roles.totalItems).toBe(2);
  32. });
  33. it('createRole', async () => {
  34. const result = await client.query<CreateRole.Mutation, CreateRole.Variables>(CREATE_ROLE, {
  35. input: {
  36. code: 'test',
  37. description: 'test role',
  38. permissions: [Permission.ReadCustomer, Permission.UpdateCustomer],
  39. },
  40. });
  41. createdRole = result.createRole;
  42. expect(omit(createdRole, ['channels'])).toMatchSnapshot();
  43. });
  44. it('role', async () => {
  45. const result = await client.query<GetRole.Query, GetRole.Variables>(GET_ROLE, { id: createdRole.id });
  46. expect(result.role).toEqual(createdRole);
  47. });
  48. it('updateRole', async () => {
  49. const result = await client.query<UpdateRole.Mutation, UpdateRole.Variables>(UPDATE_ROLE, {
  50. input: {
  51. id: createdRole.id,
  52. code: 'test-modified',
  53. description: 'test role modified',
  54. permissions: [Permission.ReadCustomer, Permission.UpdateCustomer, Permission.DeleteCustomer],
  55. },
  56. });
  57. expect(omit(result.updateRole, ['channels'])).toMatchSnapshot();
  58. });
  59. it('updateRole works with partial input', async () => {
  60. const result = await client.query<UpdateRole.Mutation, UpdateRole.Variables>(UPDATE_ROLE, {
  61. input: {
  62. id: createdRole.id,
  63. code: 'test-modified-again',
  64. },
  65. });
  66. expect(result.updateRole.code).toBe('test-modified-again');
  67. expect(result.updateRole.description).toBe('test role modified');
  68. expect(result.updateRole.permissions).toEqual([
  69. Permission.ReadCustomer,
  70. Permission.UpdateCustomer,
  71. Permission.DeleteCustomer,
  72. ]);
  73. });
  74. it(
  75. 'updateRole is not allowed for SuperAdmin role',
  76. assertThrowsWithMessage(async () => {
  77. const superAdminRole = defaultRoles.find(r => r.code === SUPER_ADMIN_ROLE_CODE);
  78. if (!superAdminRole) {
  79. fail(`Could not find SuperAdmin role`);
  80. return;
  81. }
  82. return 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. }, `The role '${SUPER_ADMIN_ROLE_CODE}' cannot be modified`),
  91. );
  92. it(
  93. 'updateRole is not allowed for Customer role',
  94. assertThrowsWithMessage(async () => {
  95. const customerRole = defaultRoles.find(r => r.code === CUSTOMER_ROLE_CODE);
  96. if (!customerRole) {
  97. fail(`Could not find Customer role`);
  98. return;
  99. }
  100. return client.query<UpdateRole.Mutation, UpdateRole.Variables>(UPDATE_ROLE, {
  101. input: {
  102. id: customerRole.id,
  103. code: 'customer-modified',
  104. description: 'customer modified',
  105. permissions: [Permission.Authenticated, Permission.DeleteAdministrator],
  106. },
  107. });
  108. }, `The role '${CUSTOMER_ROLE_CODE}' cannot be modified`),
  109. );
  110. });
  111. export const GET_ROLES = gql`
  112. query GetRoles($options: RoleListOptions) {
  113. roles(options: $options) {
  114. items {
  115. ...Role
  116. }
  117. totalItems
  118. }
  119. }
  120. ${ROLE_FRAGMENT}
  121. `;
  122. export const GET_ROLE = gql`
  123. query GetRole($id: ID!) {
  124. role(id: $id) {
  125. ...Role
  126. }
  127. }
  128. ${ROLE_FRAGMENT}
  129. `;
  130. export const UPDATE_ROLE = gql`
  131. mutation UpdateRole($input: UpdateRoleInput!) {
  132. updateRole(input: $input) {
  133. ...Role
  134. }
  135. }
  136. ${ROLE_FRAGMENT}
  137. `;