-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathallowed_bulk.rs
More file actions
211 lines (190 loc) · 6.39 KB
/
allowed_bulk.rs
File metadata and controls
211 lines (190 loc) · 6.39 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use crate::errors::ApiError;
use crate::headers::ClientCacheControl;
use crate::opa_client::cached::{query_allowed_bulk_cached, AllowedQuery, BulkAuthorizationResult};
use crate::openapi::AUTHZ_TAG;
use crate::state::AppState;
use axum::{
extract::{Json, State},
http::HeaderMap,
response::{IntoResponse, Response},
};
use http::{header::CACHE_CONTROL, StatusCode};
#[utoipa::path(
post,
path = "/allowed/bulk",
tag = AUTHZ_TAG,
request_body = Vec<AllowedQuery>,
params(
("Authorization" = String, Header, description = "Authorization header"),
("Cache-Control" = String, Header, description = "Cache control directives"),
),
responses(
(status = 200, description = "Bulk authorization check completed successfully", body = BulkAuthorizationResult),
(status = 422, description = "Invalid request payload"),
(status = 500, description = "Internal server error")
)
)]
pub(super) async fn allowed_bulk_handler(
State(state): State<AppState>,
headers: HeaderMap,
Json(queries): Json<Vec<AllowedQuery>>,
) -> Response {
// Parse client cache control headers
let cache_control = ClientCacheControl::from_header_value(headers.get(CACHE_CONTROL));
match query_allowed_bulk_cached(&state, &queries, &cache_control).await {
Ok(result) => (StatusCode::OK, Json(result)).into_response(),
Err(err) => {
log::error!("Failed to send request to OPA: {err}");
ApiError::from(err).into_response()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::TestFixture;
use http::{Method, StatusCode};
use serde_json::json;
#[tokio::test]
async fn test_handle_allowed_bulk_success() {
// Setup test fixture
let fixture = TestFixture::new().await;
// Create test queries
let test_queries = json!([
{
"user": {
"key": "test-user-1",
"first_name": "Test",
"last_name": "User",
"email": "test1@example.com",
"attributes": {}
},
"action": "read",
"resource": {
"type": "document",
"key": "test-resource-1",
"tenant": "test-tenant",
"attributes": {},
"context": {}
},
"context": {}
},
{
"user": {
"key": "test-user-2",
"first_name": "Test",
"last_name": "User",
"email": "test2@example.com",
"attributes": {}
},
"action": "write",
"resource": {
"type": "document",
"key": "test-resource-2",
"tenant": "test-tenant",
"attributes": {},
"context": {}
},
"context": {}
}
]);
// Setup mock OPA response
fixture
.add_opa_mock(
Method::POST,
"/v1/data/permit/bulk",
json!({
"result": {
"allow": [
{ "allow": true, "result": true },
{ "allow": false, "result": false }
]
}
}),
StatusCode::OK,
1,
)
.await;
// Send request to the API
let response = fixture.post("/allowed/bulk", &test_queries).await;
// Verify response status and body
response.assert_ok();
let result: BulkAuthorizationResult = response.json_as();
assert_eq!(result.allow.len(), 2);
assert!(result.allow[0].allow);
assert!(!result.allow[1].allow);
// Verify mock expectations
fixture.opa_mock.verify().await;
}
#[tokio::test]
async fn test_handle_allowed_bulk_empty_list() {
// Setup test fixture
let fixture = TestFixture::new().await;
// Create empty queries list
let empty_queries = json!([]);
// Setup mock OPA response
fixture
.add_opa_mock(
Method::POST,
"/v1/data/permit/bulk",
json!({
"result": {
"allow": []
}
}),
StatusCode::OK,
1,
)
.await;
// Send request to the API
let response = fixture.post("/allowed/bulk", &empty_queries).await;
// Verify response status and body
response.assert_ok();
let result: BulkAuthorizationResult = response.json_as();
assert_eq!(result.allow.len(), 0);
// Verify mock expectations
fixture.opa_mock.verify().await;
}
#[tokio::test]
async fn test_handle_allowed_bulk_opa_error() {
// Setup test fixture
let fixture = TestFixture::new().await;
// Create test queries
let test_queries = json!([
{
"user": {
"key": "test-user",
"first_name": "Test",
"last_name": "User",
"email": "test@example.com",
"attributes": {}
},
"action": "read",
"resource": {
"type": "document",
"key": "test-resource",
"tenant": "test-tenant",
"attributes": {},
"context": {}
},
"context": {}
}
]);
// Setup mock OPA response with error
fixture
.add_opa_mock(
Method::POST,
"/v1/data/permit/bulk",
"Internal Server Error",
StatusCode::INTERNAL_SERVER_ERROR,
1,
)
.await;
// Send request to the API
let response = fixture.post("/allowed/bulk", &test_queries).await;
// Verify response - should be a 502 Bad Gateway when OPA returns 5xx
response.assert_status(StatusCode::BAD_GATEWAY);
// Verify mock expectations
fixture.opa_mock.verify().await;
}
}