auth.e2e-spec.ts 5.7 KB

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