role.e2e-spec.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 {
  8. CreateRole,
  9. GetRole,
  10. GetRoles,
  11. Permission,
  12. Role,
  13. UpdateRole,
  14. } from './graphql/generated-e2e-admin-types';
  15. import { CREATE_ROLE } from './graphql/shared-definitions';
  16. import { TestAdminClient } from './test-client';
  17. import { TestServer } from './test-server';
  18. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  19. describe('Role resolver', () => {
  20. const client = new TestAdminClient();
  21. const server = new TestServer();
  22. let createdRole: Role.Fragment;
  23. let defaultRoles: Role.Fragment[];
  24. beforeAll(async () => {
  25. const token = await server.init({
  26. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  27. customerCount: 1,
  28. });
  29. await client.init();
  30. }, TEST_SETUP_TIMEOUT_MS);
  31. afterAll(async () => {
  32. await server.destroy();
  33. });
  34. it('roles', async () => {
  35. const result = await client.query<GetRoles.Query, GetRoles.Variables>(GET_ROLES);
  36. defaultRoles = result.roles.items;
  37. expect(result.roles.items.length).toBe(2);
  38. expect(result.roles.totalItems).toBe(2);
  39. });
  40. it(
  41. 'createRole with invalid permission',
  42. assertThrowsWithMessage(async () => {
  43. await client.query<CreateRole.Mutation, CreateRole.Variables>(CREATE_ROLE, {
  44. input: {
  45. code: 'test',
  46. description: 'test role',
  47. permissions: ['bad permission' as any],
  48. },
  49. });
  50. }, 'Variable "$input" got invalid value "bad permission" at "input.permissions[0]"; Expected type Permission.'),
  51. );
  52. it('createRole with no permissions includes Authenticated', async () => {
  53. const { createRole } = await client.query<CreateRole.Mutation, CreateRole.Variables>(CREATE_ROLE, {
  54. input: {
  55. code: 'test',
  56. description: 'test role',
  57. permissions: [],
  58. },
  59. });
  60. expect(omit(createRole, ['channels'])).toEqual({
  61. code: 'test',
  62. description: 'test role',
  63. id: 'T_3',
  64. permissions: [Permission.Authenticated],
  65. });
  66. });
  67. it('createRole deduplicates permissions', async () => {
  68. const { createRole } = await client.query<CreateRole.Mutation, CreateRole.Variables>(CREATE_ROLE, {
  69. input: {
  70. code: 'test2',
  71. description: 'test role2',
  72. permissions: [Permission.ReadSettings, Permission.ReadSettings],
  73. },
  74. });
  75. expect(omit(createRole, ['channels'])).toEqual({
  76. code: 'test2',
  77. description: 'test role2',
  78. id: 'T_4',
  79. permissions: [Permission.Authenticated, Permission.ReadSettings],
  80. });
  81. });
  82. it('createRole with permissions', async () => {
  83. const result = await client.query<CreateRole.Mutation, CreateRole.Variables>(CREATE_ROLE, {
  84. input: {
  85. code: 'test',
  86. description: 'test role',
  87. permissions: [Permission.ReadCustomer, Permission.UpdateCustomer],
  88. },
  89. });
  90. createdRole = result.createRole;
  91. expect(omit(createdRole, ['channels'])).toEqual({
  92. code: 'test',
  93. description: 'test role',
  94. id: 'T_5',
  95. permissions: [Permission.Authenticated, Permission.ReadCustomer, Permission.UpdateCustomer],
  96. });
  97. });
  98. it('role', async () => {
  99. const result = await client.query<GetRole.Query, GetRole.Variables>(GET_ROLE, { id: createdRole.id });
  100. expect(result.role).toEqual(createdRole);
  101. });
  102. it('updateRole', async () => {
  103. const result = await client.query<UpdateRole.Mutation, UpdateRole.Variables>(UPDATE_ROLE, {
  104. input: {
  105. id: createdRole.id,
  106. code: 'test-modified',
  107. description: 'test role modified',
  108. permissions: [Permission.ReadCustomer, Permission.UpdateCustomer, Permission.DeleteCustomer],
  109. },
  110. });
  111. expect(omit(result.updateRole, ['channels'])).toEqual({
  112. code: 'test-modified',
  113. description: 'test role modified',
  114. id: 'T_5',
  115. permissions: [
  116. Permission.Authenticated,
  117. Permission.ReadCustomer,
  118. Permission.UpdateCustomer,
  119. Permission.DeleteCustomer,
  120. ],
  121. });
  122. });
  123. it('updateRole works with partial input', async () => {
  124. const result = await client.query<UpdateRole.Mutation, UpdateRole.Variables>(UPDATE_ROLE, {
  125. input: {
  126. id: createdRole.id,
  127. code: 'test-modified-again',
  128. },
  129. });
  130. expect(result.updateRole.code).toBe('test-modified-again');
  131. expect(result.updateRole.description).toBe('test role modified');
  132. expect(result.updateRole.permissions).toEqual([
  133. Permission.Authenticated,
  134. Permission.ReadCustomer,
  135. Permission.UpdateCustomer,
  136. Permission.DeleteCustomer,
  137. ]);
  138. });
  139. it('updateRole deduplicates permissions', async () => {
  140. const result = await client.query<UpdateRole.Mutation, UpdateRole.Variables>(UPDATE_ROLE, {
  141. input: {
  142. id: createdRole.id,
  143. permissions: [
  144. Permission.Authenticated,
  145. Permission.Authenticated,
  146. Permission.ReadCustomer,
  147. Permission.ReadCustomer,
  148. ],
  149. },
  150. });
  151. expect(result.updateRole.permissions).toEqual([Permission.Authenticated, Permission.ReadCustomer]);
  152. });
  153. it(
  154. 'updateRole is not allowed for SuperAdmin role',
  155. assertThrowsWithMessage(async () => {
  156. const superAdminRole = defaultRoles.find(r => r.code === SUPER_ADMIN_ROLE_CODE);
  157. if (!superAdminRole) {
  158. fail(`Could not find SuperAdmin role`);
  159. return;
  160. }
  161. return client.query<UpdateRole.Mutation, UpdateRole.Variables>(UPDATE_ROLE, {
  162. input: {
  163. id: superAdminRole.id,
  164. code: 'superadmin-modified',
  165. description: 'superadmin modified',
  166. permissions: [Permission.Authenticated],
  167. },
  168. });
  169. }, `The role '${SUPER_ADMIN_ROLE_CODE}' cannot be modified`),
  170. );
  171. it(
  172. 'updateRole is not allowed for Customer role',
  173. assertThrowsWithMessage(async () => {
  174. const customerRole = defaultRoles.find(r => r.code === CUSTOMER_ROLE_CODE);
  175. if (!customerRole) {
  176. fail(`Could not find Customer role`);
  177. return;
  178. }
  179. return client.query<UpdateRole.Mutation, UpdateRole.Variables>(UPDATE_ROLE, {
  180. input: {
  181. id: customerRole.id,
  182. code: 'customer-modified',
  183. description: 'customer modified',
  184. permissions: [Permission.Authenticated, Permission.DeleteAdministrator],
  185. },
  186. });
  187. }, `The role '${CUSTOMER_ROLE_CODE}' cannot be modified`),
  188. );
  189. });
  190. export const GET_ROLES = gql`
  191. query GetRoles($options: RoleListOptions) {
  192. roles(options: $options) {
  193. items {
  194. ...Role
  195. }
  196. totalItems
  197. }
  198. }
  199. ${ROLE_FRAGMENT}
  200. `;
  201. export const GET_ROLE = gql`
  202. query GetRole($id: ID!) {
  203. role(id: $id) {
  204. ...Role
  205. }
  206. }
  207. ${ROLE_FRAGMENT}
  208. `;
  209. export const UPDATE_ROLE = gql`
  210. mutation UpdateRole($input: UpdateRoleInput!) {
  211. updateRole(input: $input) {
  212. ...Role
  213. }
  214. }
  215. ${ROLE_FRAGMENT}
  216. `;