forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.010.4 MiB7ef8c05Tahinli3 minutes ago334 commits

admin_live_tests.rs266 lines, 7.2 KiBHistoryRawDownload
1use forge_core::AuditAction;
2use forge_core::store::backup_runs::RunKind;
3use forge_core::store::replication::DB_SUBJECT;
4use serde_json::Value;
5use topcoat::router::request::Request;
6use topcoat::router::{Body, Router};
7
8use crate::audit::{self, Actor, Target};
9use crate::auth::SESSION_COOKIE;
10use crate::live::{Topic, admin};
11use crate::state::AppState;
12use crate::test_support::fixture::{next_event, next_frame};
13use crate::test_support::{app_router, create_session, test_state};
14
15const ADMIN_TOKEN: &str = "admin-live-admin-session";
16const USER_TOKEN: &str = "admin-live-user-session";
17const CSRF: &str = "admin-live-csrf";
18
19fn streams(user_id: &str) -> Vec<String> {
20 vec![
21 "/admin/users/events".to_owned(),
22 format!("/admin/users/{user_id}/events"),
23 "/admin/audit/events".to_owned(),
24 "/admin/backups/events".to_owned(),
25 "/admin/replication/events".to_owned(),
26 "/admin/health/events".to_owned(),
27 "/admin/server/events".to_owned(),
28 ]
29}
30
31struct Fixture {
32 _dir: tempfile::TempDir,
33 app: Router,
34 state: AppState,
35 admin_id: String,
36 user_id: String,
37}
38
39async fn fixture() -> Fixture {
40 let dir = tempfile::tempdir().expect("tempdir");
41 let data_dir = dir.path().join("data");
42 std::fs::create_dir_all(&data_dir).expect("data dir");
43 let state = test_state(&data_dir).await;
44 let admin = state
45 .store
46 .create_user("root", None, "h", true)
47 .await
48 .expect("admin");
49 let user = state
50 .store
51 .create_user("plain", None, "h", false)
52 .await
53 .expect("user");
54 create_session(&state.store, &admin.id, ADMIN_TOKEN, CSRF).await;
55 create_session(&state.store, &user.id, USER_TOKEN, CSRF).await;
56 let app = app_router(state.clone());
57 Fixture {
58 _dir: dir,
59 app,
60 state,
61 admin_id: admin.id,
62 user_id: user.id,
63 }
64}
65
66async fn open(app: &Router, uri: &str, token: Option<&str>) -> topcoat::router::response::Response {
67 let mut builder = Request::builder().method("GET").uri(uri);
68 if let Some(token) = token {
69 builder = builder.header("cookie", format!("__Host-{SESSION_COOKIE}={token}"));
70 }
71 app.handle(builder.body(Body::from(String::new())).expect("request"))
72 .await
73}
74
75fn changed(data: &str) -> Vec<u8> {
76 format!("event: changed\ndata: {data}\n\n").into_bytes()
77}
78
79#[tokio::test]
80async fn admin_streams_answer_only_admins_and_a_missing_account_is_404() {
81 let fx = fixture().await;
82 for uri in streams(&fx.user_id) {
83 let anonymous = open(&fx.app, &uri, None).await;
84 assert_eq!(anonymous.status().as_u16(), 404, "anonymous: {uri}");
85 let plain = open(&fx.app, &uri, Some(USER_TOKEN)).await;
86 assert_eq!(plain.status().as_u16(), 404, "a plain user: {uri}");
87 let admin_reply = open(&fx.app, &uri, Some(ADMIN_TOKEN)).await;
88 assert_eq!(admin_reply.status().as_u16(), 200, "an admin: {uri}");
89 }
90 let reply = open(
91 &fx.app,
92 "/admin/users/no-such-account/events",
93 Some(ADMIN_TOKEN),
94 )
95 .await;
96 assert_eq!(reply.status().as_u16(), 404, "a missing account");
97}
98
99#[tokio::test]
100async fn account_list_account_page_and_audit_send_for_their_changes() {
101 let fx = fixture().await;
102 let mut list = open(&fx.app, "/admin/users/events", Some(ADMIN_TOKEN))
103 .await
104 .into_body()
105 .into_data_stream();
106 let retry = next_frame(&mut list).await.expect("the retry field");
107 assert!(retry.starts_with(b"retry: "), "{retry:?}");
108 let page_uri = format!("/admin/users/{}/events", fx.user_id);
109 let mut page = open(&fx.app, &page_uri, Some(ADMIN_TOKEN))
110 .await
111 .into_body()
112 .into_data_stream();
113 let mut audit = open(
114 &fx.app,
115 "/admin/audit/events?action=login_failed",
116 Some(ADMIN_TOKEN),
117 )
118 .await
119 .into_body()
120 .into_data_stream();
121
122 let newcomer = fx
123 .state
124 .store
125 .create_user("newcomer", None, "h", false)
126 .await
127 .expect("user");
128 audit::record(
129 &fx.state.store,
130 None,
131 Actor::Cli,
132 AuditAction::UserCreated,
133 Target {
134 kind: "user",
135 id: &newcomer.id,
136 name: "newcomer",
137 },
138 Value::Null,
139 )
140 .await;
141 assert_eq!(
142 next_event(&mut list).await,
143 Some(changed("1")),
144 "account list"
145 );
146
147 fx.state
148 .store
149 .suspend_user(&fx.user_id, "spam", Some(&fx.admin_id))
150 .await
151 .expect("suspend");
152 audit::record(
153 &fx.state.store,
154 None,
155 Actor::Cli,
156 AuditAction::UserSuspended,
157 Target {
158 kind: "user",
159 id: &fx.user_id,
160 name: "plain",
161 },
162 Value::Null,
163 )
164 .await;
165 assert_eq!(
166 next_event(&mut page).await,
167 Some(changed("1")),
168 "account page"
169 );
170
171 audit::record(
172 &fx.state.store,
173 None,
174 Actor::Anonymous,
175 AuditAction::LoginFailed,
176 Target::default(),
177 Value::Null,
178 )
179 .await;
180 assert_eq!(
181 next_event(&mut audit).await,
182 Some(changed("1")),
183 "audit log"
184 );
185}
186
187#[tokio::test]
188async fn backups_replication_health_and_server_send_for_their_changes() {
189 let fx = fixture().await;
190 let mut backups = open(&fx.app, "/admin/backups/events", Some(ADMIN_TOKEN))
191 .await
192 .into_body()
193 .into_data_stream();
194 let mut replication = open(&fx.app, "/admin/replication/events", Some(ADMIN_TOKEN))
195 .await
196 .into_body()
197 .into_data_stream();
198 let mut health = open(&fx.app, "/admin/health/events", Some(ADMIN_TOKEN))
199 .await
200 .into_body()
201 .into_data_stream();
202 let mut restarting = open(
203 &fx.app,
204 "/admin/server/events?restarting=an-older-boot",
205 Some(ADMIN_TOKEN),
206 )
207 .await
208 .into_body()
209 .into_data_stream();
210 assert_eq!(
211 next_event(&mut restarting).await,
212 Some(changed("restarted")),
213 "a restart"
214 );
215 let mut settings = open(&fx.app, "/admin/server/events", Some(ADMIN_TOKEN))
216 .await
217 .into_body()
218 .into_data_stream();
219
220 let guard = fx
221 .state
222 .jobs
223 .claim(RunKind::Backup)
224 .expect("the slot is free");
225 assert_eq!(
226 next_event(&mut backups).await,
227 Some(changed("1")),
228 "run start"
229 );
230 drop(guard);
231 assert_eq!(
232 next_event(&mut backups).await,
233 Some(changed("1")),
234 "run end"
235 );
236
237 let outcome = crate::replication::worker::record(
238 &fx.state.store,
239 DB_SUBJECT,
240 Err("standby unreachable".to_owned()),
241 )
242 .await;
243 assert!(outcome.is_err());
244 assert_eq!(
245 next_event(&mut replication).await,
246 Some(changed("1")),
247 "copy"
248 );
249
250 crate::alerts::raise(
251 &fx.state,
252 forge_core::alerts::AlertTopic::RunnerOffline,
253 "runner-offline:tahinli-ci",
254 "tahinli-ci",
255 "silent for an hour",
256 )
257 .await;
258 assert_eq!(next_event(&mut health).await, Some(changed("1")), "alert");
259
260 admin().bump(&Topic::AdminServer);
261 assert_eq!(
262 next_event(&mut settings).await,
263 Some(changed("settings")),
264 "setting"
265 );
266}