Newer
Older
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
use crate::{
middleware::AuthenticatedAdminMiddleware, view_resource::ViewResourceManager, AppState,
};
use actix_web::{
get,
web::{Data, Path},
HttpRequest, Responder,
};
/// Returns the content of a ViewResource after retrieving it by name.
/// If the resource is not found (has not been registered), it returns the 404 ViewResource.
/// The regex matches uris with more than 3 characters so we don't match the /fr /es etc urls used for the websites translation directories
#[get("/{resource_name:.{3,}}")]
pub async fn get_view<'a>(
app_state: Data<AppState>,
resource_manager: Data<ViewResourceManager>,
auth_middleware: Data<AuthenticatedAdminMiddleware<'a>>,
req: HttpRequest,
resource_name: Path<String>,
) -> impl Responder {
resource_manager
.get_resource_as_http_response(
&app_state,
&auth_middleware,
&req,
None,
&resource_name.into_inner(),
)
.await
}
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*@@
*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*@@
* _______ ______ ______ _______ *@@
* |__ __@ | ____@ / ____@ |__ __@ *@@
* | @ | @__ \_ @_ | @ *@@
* | @ | __@ \ @_ | @ *@@
* | @ | @___ ____\ @ | @ *@@
* |__@ |______@ \______@ |__@ *@@
* *@@
*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*@@
*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*@*/
#[cfg(test)]
mod test_views {
use super::*;
use crate::{
middleware::get_auth_cookie,
model::{AdminAuthCredentials, Administrator},
view_resource::ViewResourceDescriptor,
};
use actix_web::{
http::StatusCode,
test,
web::{Bytes, Data},
App,
};
fn get_views_manager() -> ViewResourceManager {
ViewResourceManager::with_views(vec![
ViewResourceDescriptor {
path_str: "test-view",
index_file_name: "index.html",
resource_name: "test-view",
apply_auth_middleware: false,
},
ViewResourceDescriptor {
path_str: "test-view-auth",
index_file_name: "index.html",
resource_name: "test-view-auth",
apply_auth_middleware: true,
},
])
}
async fn get_authenticated_admin(app_state: &AppState) -> Administrator {
Administrator::authenticated(
app_state,
AdminAuthCredentials {
username: app_state.env.default_admin_username.to_owned(),
password: app_state.env.default_admin_password.to_owned(),
},
)
.await
.unwrap()
}
#[tokio::test]
async fn test_get_view() {
dotenv::dotenv().ok();
let app_state = AppState::for_test().await;
let mut app = test::init_service(
App::new()
.app_data(Data::new(app_state.clone()))
.app_data(Data::new(AuthenticatedAdminMiddleware::new(
"kuadrado-admin-auth",
)))
.app_data(Data::new(get_views_manager()))
.service(get_view),
)
.await;
let req = test::TestRequest::with_uri("/test-view").to_request();
let resp = test::call_service(&mut app, req).await;
assert_eq!(resp.status(), StatusCode::OK);
let body = test::read_body(resp).await;
assert_eq!(body, Bytes::from("<h1>TEST</h1>"));
}
#[tokio::test]
async fn test_get_view_auth() {
dotenv::dotenv().ok();
let app_state = AppState::for_test().await;
let mut app = test::init_service(
App::new()
.app_data(Data::new(app_state.clone()))
.app_data(Data::new(AuthenticatedAdminMiddleware::new(
"kuadrado-admin-auth",
)))
.app_data(Data::new(get_views_manager()))
.service(get_view),
)
.await;
let admin_user = get_authenticated_admin(&app_state).await;
let req = test::TestRequest::with_uri("/test-view-auth")
.cookie(get_auth_cookie(
"kuadrado-admin-auth",
app_state
.encryption
.decrypt(&admin_user.auth_token.unwrap())
.to_owned(),
))
.to_request();
let resp = test::call_service(&mut app, req).await;
assert_eq!(resp.status(), StatusCode::OK);
let body = test::read_body(resp).await;
assert_eq!(body, Bytes::from("<h1>TEST AUTH</h1>"));
}
#[tokio::test]
async fn test_get_view_auth_unauthorized() {
dotenv::dotenv().ok();
let app_state = AppState::for_test().await;
let mut app = test::init_service(
App::new()
.app_data(Data::new(app_state.clone()))
.app_data(Data::new(AuthenticatedAdminMiddleware::new(
"kuadrado-admin-auth",
)))
.app_data(Data::new(get_views_manager()))
.service(get_view),
)
.await;
let req = test::TestRequest::with_uri("/test-view-auth").to_request();
let resp = test::call_service(&mut app, req).await;
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
}
#[tokio::test]
async fn test_get_view_not_found() {
dotenv::dotenv().ok();
let app_state = AppState::for_test().await;
let mut app = test::init_service(
App::new()
.app_data(Data::new(app_state.clone()))
.app_data(Data::new(AuthenticatedAdminMiddleware::new(
"kuadrado-admin-auth",
)))
.app_data(Data::new(get_views_manager()))
.service(get_view),
)
.await;
let req = test::TestRequest::with_uri("/whatever").to_request();
let resp = test::call_service(&mut app, req).await;
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}
}