auth.e2e-spec.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. rememberMe: false,
  51. });
  52. });
  53. });
  54. describe('ReadCatalog', () => {
  55. beforeAll(async () => {
  56. await client.asSuperAdmin();
  57. const { identifier, password } = await createAdministratorWithPermissions('ReadCatalog', [
  58. Permission.ReadCatalog,
  59. ]);
  60. await client.asUserWithCredentials(identifier, password);
  61. });
  62. it('can read', async () => {
  63. await assertRequestAllowed(GET_PRODUCT_LIST);
  64. });
  65. it('cannot uppdate', async () => {
  66. await assertRequestForbidden<UpdateProductVariables>(UPDATE_PRODUCT, {
  67. input: {
  68. id: '1',
  69. translations: [],
  70. },
  71. });
  72. });
  73. it('cannot create', async () => {
  74. await assertRequestForbidden<CreateProductVariables>(CREATE_PRODUCT, {
  75. input: {
  76. translations: [],
  77. },
  78. });
  79. });
  80. });
  81. describe('CRUD on Customers', () => {
  82. beforeAll(async () => {
  83. await client.asSuperAdmin();
  84. const { identifier, password } = await createAdministratorWithPermissions('CRUDCustomer', [
  85. Permission.CreateCustomer,
  86. Permission.ReadCustomer,
  87. Permission.UpdateCustomer,
  88. Permission.DeleteCustomer,
  89. ]);
  90. await client.asUserWithCredentials(identifier, password);
  91. });
  92. it('can create', async () => {
  93. await assertRequestAllowed(
  94. gql`
  95. mutation CreateCustomer($input: CreateCustomerInput!) {
  96. createCustomer(input: $input) {
  97. id
  98. }
  99. }
  100. `,
  101. { input: { emailAddress: '', firstName: '', lastName: '' } },
  102. );
  103. });
  104. it('can read', async () => {
  105. await assertRequestAllowed(gql`
  106. query {
  107. customers {
  108. totalItems
  109. }
  110. }
  111. `);
  112. });
  113. });
  114. async function assertRequestAllowed<V>(operation: DocumentNode, variables?: V) {
  115. try {
  116. const status = await client.queryStatus(operation, variables);
  117. expect(status).toBe(200);
  118. } catch (e) {
  119. const status = getErrorStatusCode(e);
  120. if (!status) {
  121. fail(`Unexpected failure: ${e}`);
  122. } else {
  123. fail(`Operation should be allowed, got status ${getErrorStatusCode(e)}`);
  124. }
  125. }
  126. }
  127. async function assertRequestForbidden<V>(operation: DocumentNode, variables: V) {
  128. try {
  129. const status = await client.queryStatus(operation, variables);
  130. fail(`Should have thrown with 403 error, got ${status}`);
  131. } catch (e) {
  132. expect(getErrorStatusCode(e)).toBe(403);
  133. }
  134. }
  135. function getErrorStatusCode(err: any): number {
  136. return err.response.errors[0].message.statusCode;
  137. }
  138. async function createAdministratorWithPermissions(
  139. code: string,
  140. permissions: Permission[],
  141. ): Promise<{ identifier: string; password: string }> {
  142. const roleResult = await client.query<CreateRole, CreateRoleVariables>(CREATE_ROLE, {
  143. input: {
  144. code,
  145. description: '',
  146. permissions,
  147. },
  148. });
  149. const role = roleResult.createRole;
  150. const identifier = `${code}@${Math.random()
  151. .toString(16)
  152. .substr(2, 8)}`;
  153. const password = `test`;
  154. const adminResult = await client.query<CreateAdministrator, CreateAdministratorVariables>(
  155. CREATE_ADMINISTRATOR,
  156. {
  157. input: {
  158. emailAddress: identifier,
  159. firstName: code,
  160. lastName: 'Admin',
  161. password,
  162. roleIds: [role.id],
  163. },
  164. },
  165. );
  166. const admin = adminResult.createAdministrator;
  167. return {
  168. identifier,
  169. password,
  170. };
  171. }
  172. });