1
0

auth.e2e-spec.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import {
  2. CreateAdministrator,
  3. CreateProductMutationArgs,
  4. CreateRole,
  5. LoginMutationArgs,
  6. Permission,
  7. UpdateProductMutationArgs,
  8. } from '@vendure/common/lib/generated-types';
  9. import { SUPER_ADMIN_USER_IDENTIFIER, SUPER_ADMIN_USER_PASSWORD } from '@vendure/common/lib/shared-constants';
  10. import { DocumentNode } from 'graphql';
  11. import gql from 'graphql-tag';
  12. import path from 'path';
  13. import {
  14. CREATE_ADMINISTRATOR,
  15. CREATE_ROLE,
  16. } from '../../../admin-ui/src/app/data/definitions/administrator-definitions';
  17. import { ATTEMPT_LOGIN } from '../../../admin-ui/src/app/data/definitions/auth-definitions';
  18. import {
  19. CREATE_PRODUCT,
  20. GET_PRODUCT_LIST,
  21. UPDATE_PRODUCT,
  22. } from '../../../admin-ui/src/app/data/definitions/product-definitions';
  23. import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
  24. import { TestAdminClient } from './test-client';
  25. import { TestServer } from './test-server';
  26. describe('Authorization & permissions', () => {
  27. const client = new TestAdminClient();
  28. const server = new TestServer();
  29. beforeAll(async () => {
  30. const token = await server.init({
  31. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  32. customerCount: 1,
  33. });
  34. await client.init();
  35. }, TEST_SETUP_TIMEOUT_MS);
  36. afterAll(async () => {
  37. await server.destroy();
  38. });
  39. describe('admin permissions', () => {
  40. describe('Anonymous user', () => {
  41. beforeAll(async () => {
  42. await client.asAnonymousUser();
  43. });
  44. it('can attempt login', async () => {
  45. await assertRequestAllowed<LoginMutationArgs>(ATTEMPT_LOGIN, {
  46. username: SUPER_ADMIN_USER_IDENTIFIER,
  47. password: SUPER_ADMIN_USER_PASSWORD,
  48. rememberMe: false,
  49. });
  50. });
  51. });
  52. describe('ReadCatalog', () => {
  53. beforeAll(async () => {
  54. await client.asSuperAdmin();
  55. const { identifier, password } = await createAdministratorWithPermissions('ReadCatalog', [
  56. Permission.ReadCatalog,
  57. ]);
  58. await client.asUserWithCredentials(identifier, password);
  59. });
  60. it('can read', async () => {
  61. await assertRequestAllowed(GET_PRODUCT_LIST);
  62. });
  63. it('cannot uppdate', async () => {
  64. await assertRequestForbidden<UpdateProductMutationArgs>(UPDATE_PRODUCT, {
  65. input: {
  66. id: '1',
  67. translations: [],
  68. },
  69. });
  70. });
  71. it('cannot create', async () => {
  72. await assertRequestForbidden<CreateProductMutationArgs>(CREATE_PRODUCT, {
  73. input: {
  74. translations: [],
  75. },
  76. });
  77. });
  78. });
  79. describe('CRUD on Customers', () => {
  80. beforeAll(async () => {
  81. await client.asSuperAdmin();
  82. const { identifier, password } = await createAdministratorWithPermissions('CRUDCustomer', [
  83. Permission.CreateCustomer,
  84. Permission.ReadCustomer,
  85. Permission.UpdateCustomer,
  86. Permission.DeleteCustomer,
  87. ]);
  88. await client.asUserWithCredentials(identifier, password);
  89. });
  90. it('can create', async () => {
  91. await assertRequestAllowed(
  92. gql`
  93. mutation CreateCustomer($input: CreateCustomerInput!) {
  94. createCustomer(input: $input) {
  95. id
  96. }
  97. }
  98. `,
  99. { input: { emailAddress: '', firstName: '', lastName: '' } },
  100. );
  101. });
  102. it('can read', async () => {
  103. await assertRequestAllowed(gql`
  104. query {
  105. customers {
  106. totalItems
  107. }
  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 errorCode = getErrorCode(e);
  119. if (!errorCode) {
  120. fail(`Unexpected failure: ${e}`);
  121. } else {
  122. fail(`Operation should be allowed, got status ${getErrorCode(e)}`);
  123. }
  124. }
  125. }
  126. async function assertRequestForbidden<V>(operation: DocumentNode, variables: V) {
  127. try {
  128. const status = await client.query(operation, variables);
  129. fail(`Should have thrown`);
  130. } catch (e) {
  131. expect(getErrorCode(e)).toBe('FORBIDDEN');
  132. }
  133. }
  134. function getErrorCode(err: any): string {
  135. return err.response.errors[0].extensions.code;
  136. }
  137. async function createAdministratorWithPermissions(
  138. code: string,
  139. permissions: Permission[],
  140. ): Promise<{ identifier: string; password: string }> {
  141. const roleResult = await client.query<CreateRole.Mutation, CreateRole.Variables>(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.Mutation, CreateAdministrator.Variables>(
  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. });