-
Notifications
You must be signed in to change notification settings - Fork 370
/
access.test.ts
169 lines (150 loc) · 5.91 KB
/
access.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
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
import { readJson } from '@medplum/definitions';
import { AccessPolicy, Bundle, Communication, Observation, SearchParameter } from '@medplum/fhirtypes';
import {
AccessPolicyInteraction,
canReadResourceType,
canWriteResource,
canWriteResourceType,
matchesAccessPolicy,
satisfiedAccessPolicy,
} from './access';
import { indexSearchParameterBundle } from './types';
import { indexStructureDefinitionBundle } from './typeschema/types';
const nullPolicy: AccessPolicy = {
resourceType: 'AccessPolicy',
};
const wildcardPolicy: AccessPolicy = {
resourceType: 'AccessPolicy',
resource: [
{
resourceType: '*',
},
],
};
const restrictedPolicy: AccessPolicy = {
resourceType: 'AccessPolicy',
resource: [
{
resourceType: 'Patient',
readonly: true,
},
{
resourceType: 'Observation',
readonly: false,
},
{
resourceType: 'Communication',
criteria: 'Communication?status=in-progress',
},
{
resourceType: 'Communication',
readonly: true,
criteria: 'Communication?status=completed',
},
],
};
describe('Access', () => {
beforeAll(() => {
indexStructureDefinitionBundle(readJson('fhir/r4/profiles-types.json') as Bundle);
indexStructureDefinitionBundle(readJson('fhir/r4/profiles-resources.json') as Bundle);
indexStructureDefinitionBundle(readJson('fhir/r4/profiles-medplum.json') as Bundle);
indexSearchParameterBundle(readJson('fhir/r4/search-parameters.json') as Bundle<SearchParameter>);
});
test('canReadResourceType', () => {
expect(canReadResourceType(nullPolicy, 'Patient')).toBe(false);
expect(canReadResourceType(wildcardPolicy, 'Patient')).toBe(true);
expect(canReadResourceType(restrictedPolicy, 'Patient')).toBe(true);
expect(canReadResourceType(restrictedPolicy, 'Observation')).toBe(true);
expect(canReadResourceType(restrictedPolicy, 'Practitioner')).toBe(false);
});
test('canWriteResourceType', () => {
expect(canWriteResourceType(nullPolicy, 'Patient')).toBe(false);
expect(canWriteResourceType(wildcardPolicy, 'CapabilityStatement')).toBe(true);
expect(canWriteResourceType(wildcardPolicy, 'Login')).toBe(false);
expect(canWriteResourceType(wildcardPolicy, 'Patient')).toBe(true);
expect(canWriteResourceType(restrictedPolicy, 'CapabilityStatement')).toBe(false);
expect(canWriteResourceType(restrictedPolicy, 'Login')).toBe(false);
expect(canWriteResourceType(restrictedPolicy, 'Patient')).toBe(false);
expect(canWriteResourceType(restrictedPolicy, 'Observation')).toBe(true);
expect(canWriteResourceType(restrictedPolicy, 'Practitioner')).toBe(false);
expect(canWriteResourceType(restrictedPolicy, 'Communication')).toBe(true);
});
test('canWriteResource', () => {
expect(canWriteResource(nullPolicy, { resourceType: 'Patient' })).toBe(false);
expect(canWriteResource(wildcardPolicy, { resourceType: 'Patient' })).toBe(true);
expect(canWriteResource(restrictedPolicy, { resourceType: 'Patient' })).toBe(false);
expect(canWriteResource(restrictedPolicy, { resourceType: 'Observation' } as Observation)).toBe(true);
expect(canWriteResource(restrictedPolicy, { resourceType: 'Communication' } as Communication)).toBe(false);
expect(canWriteResource(restrictedPolicy, { resourceType: 'Communication', status: 'in-progress' })).toBe(true);
expect(canWriteResource(restrictedPolicy, { resourceType: 'Communication', status: 'completed' })).toBe(false);
});
test('satisfiedAccessPolicy()', () => {
expect(
satisfiedAccessPolicy({ resourceType: 'Patient' }, AccessPolicyInteraction.UPDATE, nullPolicy)
).toBeUndefined();
expect(
satisfiedAccessPolicy({ resourceType: 'Patient' }, AccessPolicyInteraction.UPDATE, undefined)?.resourceType
).toEqual('*');
expect(
satisfiedAccessPolicy({ resourceType: 'Patient' }, AccessPolicyInteraction.UPDATE, wildcardPolicy)?.resourceType
).toEqual('*');
expect(
satisfiedAccessPolicy({ resourceType: 'Patient' }, AccessPolicyInteraction.UPDATE, restrictedPolicy)
).toBeUndefined();
expect(
satisfiedAccessPolicy(
{ resourceType: 'Observation' } as Observation,
AccessPolicyInteraction.UPDATE,
restrictedPolicy
)?.resourceType
).toEqual('Observation');
expect(
satisfiedAccessPolicy(
{ resourceType: 'Communication', status: 'in-progress' },
AccessPolicyInteraction.UPDATE,
restrictedPolicy
)?.criteria
).toEqual('Communication?status=in-progress');
expect(
satisfiedAccessPolicy(
{ resourceType: 'Communication', status: 'completed' },
AccessPolicyInteraction.UPDATE,
restrictedPolicy
)
).toBeUndefined();
});
test('Legacy compartment case', () => {
// Once upon a time, the recommended way to restrict access to a resource was AccessPolicy.compartment
// That is now obsolete, becaues you can always use criteria with "_compartment=x"
// We still hold onto this for backwards compatibility.
const ap: AccessPolicy = {
resourceType: 'AccessPolicy',
resource: [
{
resourceType: 'Patient',
compartment: { reference: '1' },
},
],
};
expect(
matchesAccessPolicy(ap, { resourceType: 'Patient', meta: { compartment: [{ reference: '1' }] } }, true)
).toEqual(true);
expect(
matchesAccessPolicy(ap, { resourceType: 'Patient', meta: { compartment: [{ reference: '2' }] } }, false)
).toEqual(false);
});
test('Invalid SearchParameter in criteria in AccessPolicy', () => {
const ap: AccessPolicy = {
resourceType: 'AccessPolicy',
resource: [
{
resourceType: 'Patient',
criteria: 'Patient?invalid=invalid',
},
],
};
expect(() =>
matchesAccessPolicy(ap, { resourceType: 'Patient', name: [{ given: ['John'], family: 'Doe' }] }, false)
).toThrow(/Unknown search parameter: invalid/);
});
});