-
Notifications
You must be signed in to change notification settings - Fork 0
/
ory-authorization.guard.spec.ts
150 lines (135 loc) · 4.41 KB
/
ory-authorization.guard.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
import { OryBaseService } from '@getlarge/base-client-wrapper';
import { ExecutionContext } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { Test, TestingModule } from '@nestjs/testing';
import { AxiosHeaders } from 'axios';
import {
IAuthorizationGuard,
OryAuthorizationGuard,
} from './ory-authorization.guard';
import { EnhancedRelationTupleFactory } from './ory-permission-checks.decorator';
import {
OryPermissionsModuleOptions,
OryPermissionsService,
} from './ory-permissions';
const mockAxiosResponse = <D = object>(data: D) => {
return {
status: 200,
statusText: 'OK',
headers: {},
config: {
url: 'http://localhost',
headers: new AxiosHeaders(),
},
data,
};
};
describe('OryAuthorizationGuard', () => {
let oryPermissionsService: OryPermissionsService;
let reflector: Reflector;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
OryPermissionsService,
{
provide: OryPermissionsModuleOptions,
useValue: { basePath: 'http://localhost' },
},
{
provide: OryBaseService,
useValue: {
axios: {
request: jest.fn(),
},
},
},
],
}).compile();
oryPermissionsService = module.get(OryPermissionsService);
reflector = module.get(Reflector);
});
describe('evaluateConditions', () => {
let oryAuthorizationGuard: IAuthorizationGuard;
let context: ExecutionContext;
beforeEach(() => {
context = {} as ExecutionContext; // Mock execution context
const guardFactory = OryAuthorizationGuard({});
oryAuthorizationGuard = new guardFactory(
reflector,
oryPermissionsService
);
});
it('should allow access for a single permitted relation tuple', async () => {
const factory = () => 'user:123#access@resource:456';
jest
.spyOn(oryPermissionsService, 'checkPermission')
.mockResolvedValue(mockAxiosResponse({ allowed: true }));
const result = await oryAuthorizationGuard.evaluateConditions(
factory,
context
);
expect(result.allowed).toBe(true);
});
it('should deny access if one relation in AND condition is not permitted', async () => {
const factory = {
type: 'AND',
conditions: [
() => 'user:123#access@resource:456',
() => 'user:123#access@resource:789',
],
} satisfies EnhancedRelationTupleFactory;
jest
.spyOn(oryPermissionsService, 'checkPermission')
.mockResolvedValueOnce(mockAxiosResponse({ allowed: true }))
.mockResolvedValueOnce(mockAxiosResponse({ allowed: false }));
const result = await oryAuthorizationGuard.evaluateConditions(
factory,
context
);
expect(result.allowed).toBe(false);
});
it('should allow access if one relation in OR condition is permitted', async () => {
const factory = {
type: 'OR',
conditions: [
() => 'user:123#access@resource:456',
() => 'user:123#access@resource:789',
],
} satisfies EnhancedRelationTupleFactory;
jest
.spyOn(oryPermissionsService, 'checkPermission')
.mockResolvedValueOnce(mockAxiosResponse({ allowed: false }))
.mockResolvedValueOnce(mockAxiosResponse({ allowed: true }));
const result = await oryAuthorizationGuard.evaluateConditions(
factory,
context
);
expect(result.allowed).toBe(true);
});
it('should correctly evaluate nested AND/OR conditions', async () => {
const factory = {
type: 'AND',
conditions: [
{
type: 'OR',
conditions: [
() => 'user:123#access@resource:456',
() => 'user:123#access@resource:789',
],
},
() => 'user:123#access@resource:101',
],
} satisfies EnhancedRelationTupleFactory;
jest
.spyOn(oryPermissionsService, 'checkPermission')
.mockResolvedValueOnce(mockAxiosResponse({ allowed: true }))
.mockResolvedValueOnce(mockAxiosResponse({ allowed: true }))
.mockResolvedValueOnce(mockAxiosResponse({ allowed: true }));
const result = await oryAuthorizationGuard.evaluateConditions(
factory,
context
);
expect(result.allowed).toBe(true);
});
});
});