-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathdocuments.rs
253 lines (225 loc) · 7 KB
/
documents.rs
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use axum::body::Bytes;
use axum::extract::Path;
use axum::extract::State;
use axum::http::header::{CACHE_CONTROL, CONTENT_TYPE};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::Extension;
use axum::Json;
use editoast_authz::BuiltinRole;
use editoast_derive::EditoastError;
use serde::Serialize;
use thiserror::Error;
use utoipa::ToSchema;
use crate::error::Result;
use crate::models::*;
use editoast_models::DbConnectionPoolV2;
use super::AuthenticationExt;
use super::AuthorizationError;
crate::routes! {
"/documents" => {
post,
"/{document_key}" => {
get,
delete,
},
},
}
editoast_common::schemas! {
NewDocumentResponse,
}
#[derive(Error, Debug, EditoastError)]
#[editoast_error(base_id = "document")]
pub enum DocumentErrors {
#[error("Document '{document_key}' not found")]
#[editoast_error(status = 404)]
NotFound { document_key: i64 },
}
/// Returns a document of any type
#[utoipa::path(
get, path = "",
tag = "documents",
params(
("document_key" = i64, Path, description = "The document's key"),
),
responses(
(status = 200, description = "The document's binary content", body = [u8]),
(status = 404, description = "Document not found", body = InternalError),
)
)]
async fn get(
State(db_pool): State<DbConnectionPoolV2>,
Extension(auth): AuthenticationExt,
Path(document_id): Path<i64>,
) -> Result<impl IntoResponse> {
let authorized = auth
.check_roles([BuiltinRole::DocumentRead].into())
.await
.map_err(AuthorizationError::AuthError)?;
if !authorized {
return Err(AuthorizationError::Forbidden.into());
}
let conn = &mut db_pool.get().await?;
let doc = Document::retrieve_or_fail(conn, document_id, || DocumentErrors::NotFound {
document_key: document_id,
})
.await?;
Ok((
StatusCode::OK,
[
(CONTENT_TYPE, doc.content_type),
(CACHE_CONTROL, "public, max-age=3600".to_string()),
],
doc.data,
))
}
#[derive(Serialize, ToSchema)]
struct NewDocumentResponse {
document_key: i64,
}
/// Post a new document (content_type by header + binary data)
#[utoipa::path(
post, path = "",
tag = "documents",
params(
("content_type" = String, Header, description = "The document's content type"),
),
request_body = [u8],
responses(
(status = 201, description = "The document was created", body = NewDocumentResponse),
)
)]
async fn post(
State(db_pool): State<DbConnectionPoolV2>,
Extension(auth): AuthenticationExt,
axum_extra::TypedHeader(content_type): axum_extra::TypedHeader<headers::ContentType>,
bytes: Bytes,
) -> Result<impl IntoResponse> {
let authorized = auth
.check_roles([BuiltinRole::DocumentWrite].into())
.await
.map_err(AuthorizationError::AuthError)?;
if !authorized {
return Err(AuthorizationError::Forbidden.into());
}
let content_type = content_type.to_string();
// Create document
let conn = &mut db_pool.get().await?;
let doc = Document::changeset()
.content_type(content_type.to_owned())
.data(bytes.to_vec())
.create(conn)
.await?;
// Response
Ok((
StatusCode::CREATED,
Json(NewDocumentResponse {
document_key: doc.id,
}),
))
}
/// Delete an existing document
#[utoipa::path(
delete, path = "",
tag = "documents",
params(
("document_key" = i64, Path, description = "The document's key"),
),
responses(
(status = 204, description = "The document was deleted"),
(status = 404, description = "Document not found", body = InternalError),
)
)]
async fn delete(
State(db_pool): State<DbConnectionPoolV2>,
Extension(auth): AuthenticationExt,
Path(document_id): Path<i64>,
) -> Result<impl IntoResponse> {
let authorized = auth
.check_roles([BuiltinRole::DocumentWrite].into())
.await
.map_err(AuthorizationError::AuthError)?;
if !authorized {
return Err(AuthorizationError::Forbidden.into());
}
let conn = &mut db_pool.get().await?;
Document::delete_static_or_fail(conn, document_id, || DocumentErrors::NotFound {
document_key: document_id,
})
.await?;
Ok(StatusCode::NO_CONTENT)
}
#[cfg(test)]
mod tests {
use axum::http::header;
use axum::http::StatusCode;
use rstest::rstest;
use serde::Deserialize;
use super::*;
use crate::views::test_app::TestAppBuilder;
#[derive(Deserialize, Clone, Debug)]
struct PostDocumentResponse {
document_key: i64,
}
#[rstest]
async fn document_post() {
let app = TestAppBuilder::default_app();
let pool = app.db_pool();
let request = app
.post("/documents")
.add_header(
header::CONTENT_TYPE,
header::HeaderValue::from_str("text/plain").unwrap(),
)
.bytes("Document post test data".into());
// Insert document
let response = request.await;
response.assert_status(StatusCode::CREATED);
let new_doc = response.json::<PostDocumentResponse>().document_key;
// Get create document
let document = Document::retrieve(&mut pool.get_ok(), new_doc)
.await
.expect("Failed to retrieve document")
.expect("Document not found");
assert_eq!(document.data, b"Document post test data".to_vec());
}
#[rstest]
async fn get_document() {
let app = TestAppBuilder::default_app();
let pool = app.db_pool();
// Insert document test
let document = Document::changeset()
.data(b"Document post test data".to_vec())
.content_type(String::from("text/plain"))
.create(&mut pool.get_ok())
.await
.expect("Failed to create document");
// Get document test
let response = app.get(&format!("/documents/{}", document.id)).await;
response.assert_status(StatusCode::OK);
let response = response.as_bytes();
assert_eq!(response.as_ref(), b"Document post test data");
}
#[rstest]
async fn document_delete() {
let app = TestAppBuilder::default_app();
let pool = app.db_pool();
// Insert document test
let document = Document::changeset()
.data(b"Document post test data".to_vec())
.content_type(String::from("text/plain"))
.create(&mut pool.get_ok())
.await
.expect("Failed to create document");
// Delete document request
let response = app
.delete(format!("/documents/{}", document.id).as_str())
.await;
response.assert_status(StatusCode::NO_CONTENT);
// Get create document
let document = Document::exists(&mut pool.get_ok(), document.id)
.await
.expect("Failed to retrieve document");
assert!(!document);
}
}