auth.e2e-spec.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { DocumentNode } from 'graphql';
  2. import gql from 'graphql-tag';
  3. import {
  4. AssignRoleToAdministrator,
  5. AssignRoleToAdministratorVariables,
  6. AttemptLoginVariables,
  7. CreateAdministrator,
  8. CreateAdministratorVariables,
  9. CreateProductVariables,
  10. CreateRole,
  11. CreateRoleVariables,
  12. Permission,
  13. UpdateProductVariables,
  14. } from 'shared/generated-types';
  15. import { SUPER_ADMIN_USER_IDENTIFIER, SUPER_ADMIN_USER_PASSWORD } from 'shared/shared-constants';
  16. import {
  17. ASSIGN_ROLE_TO_ADMINISTRATOR,
  18. CREATE_ADMINISTRATOR,
  19. CREATE_ROLE,
  20. } from '../../admin-ui/src/app/data/definitions/administrator-definitions';
  21. import { ATTEMPT_LOGIN } from '../../admin-ui/src/app/data/definitions/auth-definitions';
  22. import {
  23. CREATE_PRODUCT,
  24. GET_PRODUCT_LIST,
  25. UPDATE_PRODUCT,
  26. } from '../../admin-ui/src/app/data/definitions/product-definitions';
  27. import { TestClient } from './test-client';
  28. import { TestServer } from './test-server';
  29. describe('Authorization & permissions', () => {
  30. const client = new TestClient();
  31. const server = new TestServer();
  32. beforeAll(async () => {
  33. const token = await server.init({
  34. productCount: 1,
  35. customerCount: 1,
  36. });
  37. await client.init();
  38. }, 60000);
  39. afterAll(async () => {
  40. await server.destroy();
  41. });
  42. describe('Anonymous user', () => {
  43. beforeAll(() => {
  44. client.asAnonymousUser();
  45. });
  46. it('can attempt login', async () => {
  47. await assertRequestAllowed<AttemptLoginVariables>(ATTEMPT_LOGIN, {
  48. username: SUPER_ADMIN_USER_IDENTIFIER,
  49. password: SUPER_ADMIN_USER_PASSWORD,
  50. });
  51. });
  52. });
  53. describe('ReadCatalog', () => {
  54. beforeAll(async () => {
  55. await client.asSuperAdmin();
  56. const { identifier, password } = await createAdministratorWithPermissions('ReadCatalog', [
  57. Permission.ReadCatalog,
  58. ]);
  59. await client.asUserWithCredentials(identifier, password);
  60. });
  61. it('can read', async () => {
  62. await assertRequestAllowed(GET_PRODUCT_LIST);
  63. });
  64. it('cannot uppdate', async () => {
  65. await assertRequestForbidden<UpdateProductVariables>(UPDATE_PRODUCT, {
  66. input: {
  67. id: '1',
  68. translations: [],
  69. },
  70. });
  71. });
  72. it('cannot create', async () => {
  73. await assertRequestForbidden<CreateProductVariables>(CREATE_PRODUCT, {
  74. input: {
  75. translations: [],
  76. },
  77. });
  78. });
  79. });
  80. describe('CRUD on Customers', () => {
  81. beforeAll(async () => {
  82. await client.asSuperAdmin();
  83. const { identifier, password } = await createAdministratorWithPermissions('CRUDCustomer', [
  84. Permission.CreateCustomer,
  85. Permission.ReadCustomer,
  86. Permission.UpdateCustomer,
  87. Permission.DeleteCustomer,
  88. ]);
  89. await client.asUserWithCredentials(identifier, password);
  90. });
  91. it('can create', async () => {
  92. await assertRequestAllowed(
  93. gql`
  94. mutation CreateCustomer($input: CreateCustomerInput!) {
  95. createCustomer(input: $input) {
  96. id
  97. }
  98. }
  99. `,
  100. { input: { emailAddress: '', firstName: '', lastName: '' } },
  101. );
  102. });
  103. it('can read', async () => {
  104. await assertRequestAllowed(gql`
  105. query {
  106. customers {
  107. totalItems
  108. }
  109. }
  110. `);
  111. });
  112. });
  113. async function assertRequestAllowed<V>(operation: DocumentNode, variables?: V) {
  114. try {
  115. const status = await client.queryStatus(operation, variables);
  116. expect(status).toBe(200);
  117. } catch (e) {
  118. const status = getErrorStatusCode(e);
  119. if (!status) {
  120. fail(`Unexpected failure: ${e}`);
  121. } else {
  122. fail(`Operation should be allowed, got status ${getErrorStatusCode(e)}`);
  123. }
  124. }
  125. }
  126. async function assertRequestForbidden<V>(operation: DocumentNode, variables: V) {
  127. try {
  128. const status = await client.queryStatus(operation, variables);
  129. fail(`Should have thrown with 403 error, got ${status}`);
  130. } catch (e) {
  131. expect(getErrorStatusCode(e)).toBe(403);
  132. }
  133. }
  134. function getErrorStatusCode(err: any): number {
  135. return err.response.errors[0].message.statusCode;
  136. }
  137. async function createAdministratorWithPermissions(
  138. code: string,
  139. permissions: Permission[],
  140. ): Promise<{ identifier: string; password: string }> {
  141. const roleResult = await client.query<CreateRole, CreateRoleVariables>(CREATE_ROLE, {
  142. input: {
  143. code,
  144. description: '',
  145. permissions,
  146. },
  147. });
  148. const role = roleResult.createRole;
  149. const identifier = `${code}@${Math.random()
  150. .toString(16)
  151. .substr(2, 8)}`;
  152. const password = `test`;
  153. const adminResult = await client.query<CreateAdministrator, CreateAdministratorVariables>(
  154. CREATE_ADMINISTRATOR,
  155. {
  156. input: {
  157. emailAddress: identifier,
  158. firstName: code,
  159. lastName: 'Admin',
  160. password,
  161. roleIds: [role.id],
  162. },
  163. },
  164. );
  165. const admin = adminResult.createAdministrator;
  166. return {
  167. identifier,
  168. password,
  169. };
  170. }
  171. });