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