-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.test.ts
109 lines (93 loc) · 3.73 KB
/
api.test.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
import { randomUUID } from 'node:crypto';
import {
AuthorityClient,
ClientError,
MemberCreationCommand,
MemberPublicKeyImportCommand,
MemberRole,
OrgCreationCommand,
} from '@relaycorp/veraid-authority';
import { HTTP_STATUS_CODES } from '../utilities/http.js';
import { MEMBER_EMAIL, TEST_SERVICE_OID } from '../testUtils/stubs.js';
import { generateKeyPair } from '../testUtils/webcrypto.js';
import { derSerialisePublicKey } from '../utilities/webcrypto.js';
import { API_URL, makeClient } from './utils/api.js';
import { post } from './utils/http.js';
import { AuthScope } from './utils/authServer.js';
function generateOrgName(): string {
return `${randomUUID()}.example`;
}
describe('API', () => {
describe('Orgs', () => {
describe('Authentication', () => {
test('Anonymous request to should be refused', async () => {
const response = await post(`${API_URL}/orgs`, {});
expect(response.status).toBe(HTTP_STATUS_CODES.UNAUTHORIZED);
});
test('Invalid access token should be refused', async () => {
const client = new AuthorityClient(API_URL, { scheme: 'Foo', parameters: 'Bar' });
const command = new OrgCreationCommand({ name: generateOrgName() });
await expect(client.send(command)).rejects.toThrowWithMessage(
ClientError,
/refused access token/u,
);
});
});
test('Create org as super admin', async () => {
const client = await makeClient(AuthScope.SUPER_ADMIN);
const command = new OrgCreationCommand({ name: generateOrgName() });
await expect(client.send(command)).toResolve();
});
test('Create org admin as super admin', async () => {
const client = await makeClient(AuthScope.SUPER_ADMIN);
const { members: membersEndpoint } = await client.send(
new OrgCreationCommand({ name: generateOrgName() }),
);
const orgAdminCreationCommand = new MemberCreationCommand({
endpoint: membersEndpoint,
role: MemberRole.ORG_ADMIN,
});
await expect(client.send(orgAdminCreationCommand)).toResolve();
});
test('Create member as org admin', async () => {
const superAdminClient = await makeClient(AuthScope.SUPER_ADMIN);
const { members: membersEndpoint } = await superAdminClient.send(
new OrgCreationCommand({ name: generateOrgName() }),
);
await superAdminClient.send(
new MemberCreationCommand({
endpoint: membersEndpoint,
role: MemberRole.ORG_ADMIN,
email: MEMBER_EMAIL,
}),
);
const orgAdminClient = await makeClient(AuthScope.USER);
const memberCreationCommand = new MemberCreationCommand({
endpoint: membersEndpoint,
role: MemberRole.REGULAR,
});
await expect(orgAdminClient.send(memberCreationCommand)).toResolve();
});
test('Import public key as regular org member', async () => {
const superAdminClient = await makeClient(AuthScope.SUPER_ADMIN);
const { members: membersEndpoint } = await superAdminClient.send(
new OrgCreationCommand({ name: generateOrgName() }),
);
const { publicKeys: publicKeysEndpoint } = await superAdminClient.send(
new MemberCreationCommand({
endpoint: membersEndpoint,
role: MemberRole.REGULAR,
email: MEMBER_EMAIL,
}),
);
const memberClient = await makeClient(AuthScope.USER);
const { publicKey } = await generateKeyPair();
const keyImportCommand = new MemberPublicKeyImportCommand({
endpoint: publicKeysEndpoint,
publicKeyDer: await derSerialisePublicKey(publicKey),
serviceOid: TEST_SERVICE_OID,
});
await expect(memberClient.send(keyImportCommand)).toResolve();
});
});
});