19bb31ff Andy McCall 1 month ago ↶
1
2
3
4
use axum:: body:: Body;
5
use axum:: http:: { header, Request, StatusCode} ;
6
use http_body_util:: BodyExt;
7
use oxy_core:: repo:: Repo;
8
use oxygenhub:: config:: Config;
9
use oxygenhub:: db;
10
use oxygenhub:: Shared;
11
use std:: path:: Path;
12
use tower:: ServiceExt;
13
14
fn hermetic ( ) {
15
static ONCE : std:: sync:: Once = std:: sync:: Once:: new( ) ;
16
static DIR : std:: sync:: OnceLock< tempfile:: TempDir> = std:: sync:: OnceLock:: new( ) ;
17
ONCE .call_once ( | | {
18
let d = tempfile:: tempdir( ) .unwrap ( ) ;
19
std:: env:: set_var( " OXY_HOME" , d.path ( ) ) ;
20
DIR .set ( d) .ok ( ) ;
21
} ) ;
22
}
23
24
fn write ( root : & Path, rel : & str , content : & [u8 ]) {
25
let p = root.join ( rel) ;
26
std:: fs:: create_dir_all( p.parent ( ) .unwrap ( ) ) .unwrap ( ) ;
27
std:: fs:: write( p, content) .unwrap ( ) ;
28
}
29
30
struct Hub {
31
app : Shared,
32
base : String,
33
_dir : tempfile:: TempDir,
34
}
35
36
async fn hub ( ) -> Hub {
37
hermetic ( ) ;
38
let dir = tempfile:: tempdir( ) .unwrap ( ) ;
39
let mut config = Config:: load( dir.path ( ) ) .unwrap ( ) ;
40
let listener = tokio:: net:: TcpListener:: bind( " 127.0.0.1:0" ) .await.unwrap ( ) ;
41
let port = listener.local_addr ( ) .unwrap ( ) .port ( ) ;
42
config.server.base_url = format! ( " http://127.0.0.1:{port} " ) ;
43
config.registration.mode = " invite" .into ( ) ;
44
let app = oxygenhub:: build( config) .await.unwrap ( ) ;
45
let router = oxygenhub:: router( app.clone ( ) ) ;
46
tokio:: spawn( async move { axum:: serve( listener, router) .await.unwrap ( ) } ) ;
47
Hub { app, base: format! ( " http://127.0.0.1:{port} " ) , _dir: dir }
48
}
49
50
struct Res {
51
status : StatusCode,
52
body : String,
83940d53 Andy McCall 1 month ago ↶
53
19bb31ff Andy McCall 1 month ago ↶
54
cookie : Option< String> ,
55
location : Option< String> ,
83940d53 Andy McCall 1 month ago ↶
56
57
raw_cookies : Vec< String> ,
19bb31ff Andy McCall 1 month ago ↶
58
}
59
60
async fn send ( app : & Shared, method : & str , path : & str , cookie : Option< & str > , form : Option< & str > ) -> Res {
83940d53 Andy McCall 1 month ago ↶
61
let header_value = cookie.map ( | c | format! ( " oxyhub={c} " ) ) ;
62
send_with ( app, method, path, header_value.as_deref ( ) , form) .await
63
}
64
65
66
async fn send_with ( app : & Shared, method : & str , path : & str , cookie_header : Option< & str > , form : Option< & str > ) -> Res {
19bb31ff Andy McCall 1 month ago ↶
67
let mut req = Request:: builder( ) .method ( method) .uri ( path) ;
83940d53 Andy McCall 1 month ago ↶
68
if let Some ( c) = cookie_header {
69
req = req.header ( header:: COOKIE , c.to_string ( ) ) ;
19bb31ff Andy McCall 1 month ago ↶
70
}
71
let body = match form {
72
Some ( f) => {
73
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
74
Body:: from( f.to_string ( ) )
75
}
76
None => Body:: empty( ) ,
77
} ;
78
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req.body ( body) .unwrap ( ) ) .await.unwrap ( ) ;
79
let status = res.status ( ) ;
83940d53 Andy McCall 1 month ago ↶
80
let raw_cookies: Vec< String> =
81
res.headers ( ) .get_all ( header:: SET_COOKIE ) .iter ( ) .filter_map ( | v | v.to_str ( ) .ok ( ) ) .map ( String :: from) .collect ( ) ;
82
let cookie =
83
raw_cookies.iter ( ) .filter_map ( | c | c.split ( ' ;' ) .next ( ) ) .filter_map ( | kv | kv.strip_prefix ( " oxyhub=" ) ) .map ( String :: from) .next ( ) ;
19bb31ff Andy McCall 1 month ago ↶
84
let location = res.headers ( ) .get ( header:: LOCATION ) .and_then ( | v | v.to_str ( ) .ok ( ) ) .map ( String :: from) ;
85
let bytes = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ;
83940d53 Andy McCall 1 month ago ↶
86
Res { status, body: String :: from_utf8_lossy( & bytes) .into_owned ( ) , cookie, location, raw_cookies }
19bb31ff Andy McCall 1 month ago ↶
87
}
88
89
async fn get ( app : & Shared, path : & str , cookie : Option< & str > ) -> Res {
90
send ( app, " GET" , path, cookie, None ) .await
91
}
92
93
async fn post ( app : & Shared, path : & str , cookie : Option< & str > , form : & str ) -> Res {
94
send ( app, " POST" , path, cookie, Some ( form) ) .await
95
}
96
97
98
async fn register ( app : & Shared, user : & str , invite : & str ) -> String {
99
let r =
100
post ( app, " /register" , None , & format! ( " username={user} &email={user} %40example.com&password=correct-horse-battery&invite={invite} " ) )
101
.await;
102
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
103
r.cookie.expect ( " session cookie" )
104
}
105
106
fn csrf_of ( page : & str ) -> String {
107
let i = page.find ( " name=\" _csrf\" value=\" " ) .expect ( " csrf field" ) + " name=\" _csrf\" value=\" " .len ( ) ;
108
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
109
}
110
111
# [ tokio ::test ( flavor = " multi_thread" ) ]
112
async fn accounts_roles_tokens_and_attribution ( ) {
113
let h = hub ( ) .await;
114
let app = & h.app;
115
116
117
let andy = register ( app, " andy" , " " ) .await;
118
let me = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
119
assert! ( me.is_admin) ;
120
121
let r = post ( app, " /register" , None , " username=mallory&email=m%40example.com&password=correct-horse-battery&invite=" ) .await;
122
assert_eq! ( r.status, StatusCode:: OK ) ;
123
assert! ( r.body.contains ( " invitation" ) , " {}" , r.body) ;
124
let admin_page = get ( app, " /admin" , Some ( & andy) ) .await;
125
assert_eq! ( admin_page.status, StatusCode:: OK ) ;
126
let csrf = csrf_of ( & admin_page.body) ;
127
let r = post ( app, " /admin/invite" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
128
let code = r.body.split ( " register?invite=" ) .nth ( 1 ) .unwrap ( ) .split ( ' <' ) .next ( ) .unwrap ( ) .trim ( ) .to_string ( ) ;
129
let bob = register ( app, " bob" , & code) .await;
130
assert! ( ! db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .is_admin) ;
131
132
assert_eq! ( post ( app, " /login" , None , " username=andy&password=nope" ) .await.status, StatusCode:: OK ) ;
133
assert_eq! ( post ( app, " /settings" , Some ( & andy) , " _csrf=bogus&display_name=x&email=y" ) .await.status, StatusCode:: BAD_REQUEST ) ;
134
assert_eq! ( get ( app, " /settings" , None ) .await.status, StatusCode:: SEE_OTHER , " anonymous settings redirects to sign in" ) ;
135
136
137
let new_page = get ( app, " /new" , Some ( & andy) ) .await;
138
let csrf = csrf_of ( & new_page.body) ;
139
let r = post ( app, " /new" , Some ( & andy) , & format! ( " _csrf={csrf} &owner=andy&name=secret&description=hush&visibility=private" ) ) .await;
140
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
141
assert_eq! ( get ( app, " /andy/secret" , Some ( & andy) ) .await.status, StatusCode:: OK ) ;
142
assert_eq! ( get ( app, " /andy/secret" , Some ( & bob) ) .await.status, StatusCode:: NOT_FOUND ) ;
143
assert_eq! ( get ( app, " /andy/secret" , None ) .await.status, StatusCode:: NOT_FOUND ) ;
144
assert! ( ! get ( app, " /" , Some ( & bob) ) .await.body.contains ( " secret" ) ) ;
145
assert! ( get ( app, " /" , Some ( & andy) ) .await.body.contains ( " secret" ) ) ;
146
147
148
let tokens_page = get ( app, " /settings/tokens" , Some ( & andy) ) .await;
149
let csrf = csrf_of ( & tokens_page.body) ;
150
let r = post ( app, " /settings/tokens" , Some ( & andy) , & format! ( " _csrf={csrf} &name=ro&scopes=repo%3Aread" ) ) .await;
151
let ro = r.body.split ( " oxyhub_" ) .nth ( 1 ) .map ( | s | format! ( " oxyhub_{} " , s.split ( ' <' ) .next ( ) .unwrap ( ) .trim ( ) ) ) .unwrap ( ) ;
152
let r = post ( app, " /settings/tokens" , Some ( & andy) , & format! ( " _csrf={csrf} &name=rw&scopes=repo%3Aread&scopes=repo%3Awrite" ) ) .await;
153
let rw = r.body.split ( " oxyhub_" ) .nth ( 1 ) .map ( | s | format! ( " oxyhub_{} " , s.split ( ' <' ) .next ( ) .unwrap ( ) .trim ( ) ) ) .unwrap ( ) ;
154
let work = tempfile:: tempdir( ) .unwrap ( ) ;
155
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
156
client.create_identity ( " Andy McCall" , " [email protected] " , oxy_core:: repo:: IdentityScope:: User) .unwrap ( ) ;
157
client.config.set ( " remote.origin.url" , & format! ( " {} /andy/secret" , h.base) ) .unwrap ( ) ;
158
client.config.set ( " core.sign" , " change" ) .unwrap ( ) ;
159
client.save_config ( ) .unwrap ( ) ;
160
write ( work.path ( ) , " a.txt" , b " a\n " ) ;
161
client.snapshot ( " t" ) .unwrap ( ) ;
162
let c1 = client.commit ( " first" , & [ ] ) .unwrap ( ) ;
163
let err = client.push ( " origin" , false ) .err ( ) .unwrap ( ) .to_string ( ) ;
164
assert! ( err.contains ( " 404" ) , " anonymous cannot even see it: {err}" ) ;
165
client.config.set ( " remote.origin.token" , & ro) .unwrap ( ) ;
166
let err = client.push ( " origin" , false ) .err ( ) .unwrap ( ) .to_string ( ) ;
167
assert! ( err.contains ( " 403" ) , " read-only token cannot push: {err}" ) ;
168
client.config.set ( " remote.origin.token" , & rw) .unwrap ( ) ;
169
assert! ( client.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
170
171
let bob_tokens = get ( app, " /settings/tokens" , Some ( & bob) ) .await;
172
let csrf_b = csrf_of ( & bob_tokens.body) ;
173
let r = post ( app, " /settings/tokens" , Some ( & bob) , & format! ( " _csrf={csrf_b} &name=b&scopes=repo%3Aread&scopes=repo%3Awrite" ) ) .await;
174
let bob_tok = r.body.split ( " oxyhub_" ) .nth ( 1 ) .map ( | s | format! ( " oxyhub_{} " , s.split ( ' <' ) .next ( ) .unwrap ( ) .trim ( ) ) ) .unwrap ( ) ;
175
std:: env:: set_var( " OXY_TOKEN" , & bob_tok) ;
176
let err = Repo:: clone( & format! ( " {} /andy/secret" , h.base) , & work.path ( ) .join ( " bobclone" ) ) .err ( ) .unwrap ( ) .to_string ( ) ;
177
std:: env:: remove_var( " OXY_TOKEN" ) ;
178
assert! ( err.contains ( " 404" ) , " {err}" ) ;
179
180
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 150 ) ) .await;
181
let pushes = get ( app, " /andy/secret/pushes" , Some ( & andy) ) .await.body;
182
assert! ( pushes.contains ( " andy" ) , " {pushes}" ) ;
183
184
185
let settings = get ( app, " /andy/secret/settings" , Some ( & andy) ) .await;
186
assert_eq! ( settings.status, StatusCode:: OK ) ;
187
let csrf = csrf_of ( & settings.body) ;
188
let r = post ( app, " /andy/secret/settings/collaborators" , Some ( & andy) , & format! ( " _csrf={csrf} &username=bob&role=write" ) ) .await;
189
assert_eq! ( r.status, StatusCode:: OK , " {}" , r.body) ;
190
assert_eq! ( get ( app, " /andy/secret" , Some ( & bob) ) .await.status, StatusCode:: OK ) ;
191
assert_eq! ( get ( app, " /andy/secret/settings" , Some ( & bob) ) .await.status, StatusCode:: NOT_FOUND , " write is not maintain" ) ;
192
std:: env:: set_var( " OXY_TOKEN" , & bob_tok) ;
193
let ok = Repo:: clone( & format! ( " {} /andy/secret" , h.base) , & work.path ( ) .join ( " bobclone2" ) ) .is_ok ( ) ;
194
std:: env:: remove_var( " OXY_TOKEN" ) ;
195
assert! ( ok) ;
196
2bbe88a8 Andy McCall 1 month ago ↶
197
198
199
let store = oxy_core:: credentials:: Credentials:: user( ) ;
200
let hub = oxy_core:: credentials:: hub_of( & h.base) .unwrap ( ) ;
201
store.save ( & hub, & rw, Some ( " andy" ) , None ) .unwrap ( ) ;
202
let signed_in = Repo:: clone( & format! ( " {} /andy/secret" , h.base) , & work.path ( ) .join ( " signedin" ) ) ;
203
store.remove ( & hub) .unwrap ( ) ;
204
let signed_in = signed_in.unwrap ( ) ;
205
assert! ( signed_in.config.remote_tokens.is_empty ( ) ) ;
206
assert_eq! ( signed_in.bookmark_heads ( ) .unwrap ( ) , vec! [ c1] ) ;
207
19bb31ff Andy McCall 1 month ago ↶
208
209
let ( key, identity) = {
210
let k = client.config.author_key.unwrap ( ) ;
211
( k, client.identity ( & k) .unwrap ( ) )
212
} ;
213
let keys_page = get ( app, " /settings/keys" , Some ( & andy) ) .await;
214
let csrf = csrf_of ( & keys_page.body) ;
215
let boundary = " XYZ" ;
216
let mut multipart = Vec :: new( ) ;
217
multipart.extend_from_slice ( format! ( " --{boundary} \r \n Content-Disposition: form-data; name=\" _csrf\" \r \n \r \n {csrf} \r \n " ) .as_bytes ( ) ) ;
218
multipart.extend_from_slice ( format! ( " --{boundary} \r \n Content-Disposition: form-data; name=\" identity\" ; filename=\" me.identity\" \r \n Content-Type: application/octet-stream\r \n \r \n " ) .as_bytes ( ) ) ;
219
multipart.extend_from_slice ( & identity.encode ( ) ) ;
220
multipart.extend_from_slice ( format! ( " \r \n --{boundary} --\r \n " ) .as_bytes ( ) ) ;
221
let req = Request:: builder( )
222
.method ( " POST" )
223
.uri ( " /settings/keys/upload" )
224
.header ( header:: COOKIE , format! ( " oxyhub={andy} " ) )
225
.header ( header:: CONTENT_TYPE , format! ( " multipart/form-data; boundary={boundary} " ) )
226
.body ( Body:: from( multipart) )
227
.unwrap ( ) ;
228
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
229
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
230
let body = String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
231
let challenge_hex = body.split ( " sign-challenge " ) .nth ( 1 ) .unwrap ( ) .split ( ' <' ) .next ( ) .unwrap ( ) .trim ( ) .to_string ( ) ;
232
let challenge_id = body.split ( " name=\" challenge\" value=\" " ) .nth ( 1 ) .unwrap ( ) .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
233
let ( _ , sk) = client.secret_key ( ) .unwrap ( ) .unwrap ( ) ;
234
let sig = sk.sign_challenge ( & key, & hex:: decode( & challenge_hex) .unwrap ( ) ) .unwrap ( ) ;
235
let sig_hex = hex:: encode( oxy_core:: identity:: Signature:: encode_list( & [ sig] ) ) ;
236
let r = post ( app, " /settings/keys/verify" , Some ( & andy) , & format! ( " _csrf={csrf} &challenge={challenge_id} &signature={sig_hex} " ) ) .await;
237
assert! ( r.body.contains ( " Key linked" ) , " {}" , r.body) ;
238
let hist = get ( app, " /andy/secret/changes/main" , Some ( & andy) ) .await.body;
239
assert! ( hist.contains ( " href=\" /andy\" >Andy McCall</a>" ) , " author linked to the account: {hist}" ) ;
240
let change = get ( app, & format! ( " /andy/secret/change/{} " , c1.to_hex ( ) ) , Some ( & andy) ) .await.body;
241
assert! ( change.contains ( " @andy" ) , " {change}" ) ;
242
243
let r = post ( app, " /settings/keys/verify" , Some ( & andy) , & format! ( " _csrf={csrf} &challenge=999&signature={sig_hex} " ) ) .await;
244
assert! ( r.body.contains ( " expired" ) , " {}" , r.body) ;
245
246
247
let org_page = get ( app, " /orgs/new" , Some ( & andy) ) .await;
248
let csrf = csrf_of ( & org_page.body) ;
249
let r = post ( app, " /orgs/new" , Some ( & andy) , & format! ( " _csrf={csrf} &name=acme&display_name=Acme" ) ) .await;
250
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
251
let new_page = get ( app, " /new" , Some ( & andy) ) .await;
252
let csrf = csrf_of ( & new_page.body) ;
253
assert_eq! (
254
post ( app, " /new" , Some ( & andy) , & format! ( " _csrf={csrf} &owner=acme&name=plans&visibility=private" ) ) .await.status,
255
StatusCode:: SEE_OTHER
256
) ;
257
assert_eq! ( get ( app, " /acme/plans" , Some ( & bob) ) .await.status, StatusCode:: NOT_FOUND ) ;
258
let org_settings = get ( app, " /orgs/acme/settings" , Some ( & andy) ) .await;
259
let csrf = csrf_of ( & org_settings.body) ;
260
assert_eq! (
261
post ( app, " /orgs/acme/settings/members" , Some ( & andy) , & format! ( " _csrf={csrf} &username=bob&role=member" ) ) .await.status,
262
StatusCode:: OK
263
) ;
264
assert_eq! ( get ( app, " /acme/plans" , Some ( & bob) ) .await.status, StatusCode:: OK , " members read by default" ) ;
265
assert_eq! ( get ( app, " /orgs/acme/settings" , Some ( & bob) ) .await.status, StatusCode:: NOT_FOUND , " members do not administer" ) ;
266
267
let r = post (
268
app,
269
" /new" ,
270
Some ( & bob) ,
271
& format! ( " _csrf={} &owner=acme&name=bobs&visibility=public" , csrf_of ( & get ( app, " /new" , Some ( & bob) ) .await.body) ) ,
272
)
273
.await;
274
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
275
276
277
let sec = get ( app, " /settings/security" , Some ( & andy) ) .await;
278
let csrf = csrf_of ( & sec.body) ;
279
let r = post ( app, " /settings/security/sessions" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
280
assert_eq! ( r.status, StatusCode:: SEE_OTHER ) ;
281
assert_eq! ( get ( app, " /settings" , Some ( & andy) ) .await.status, StatusCode:: SEE_OTHER , " old session is gone" ) ;
282
let fresh = r.cookie.unwrap ( ) ;
283
assert_eq! ( get ( app, " /settings" , Some ( & fresh) ) .await.status, StatusCode:: OK ) ;
284
285
286
let sec = get ( app, " /settings/security" , Some ( & fresh) ) .await;
287
let csrf = csrf_of ( & sec.body) ;
288
let r = post ( app, " /settings/security/totp/begin" , Some ( & fresh) , & format! ( " _csrf={csrf} " ) ) .await;
289
let secret = r.body.split ( " name=\" secret\" value=\" " ) .nth ( 1 ) .unwrap ( ) .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
290
let now = ( db:: now_ms( ) / 1000 ) as u64 ;
291
let code = oxygenhub:: auth:: totp:: current( & secret, now) .unwrap ( ) ;
292
let r = post ( app, " /settings/security/totp/enable" , Some ( & fresh) , & format! ( " _csrf={csrf} &secret={secret} &code={code} " ) ) .await;
293
assert! ( r.body.contains ( " Recovery codes" ) , " {}" , r.body) ;
294
let r = post ( app, " /login" , None , " username=andy&password=correct-horse-battery" ) .await;
295
assert! ( r.body.contains ( " authenticator" ) , " second step asked: {}" , r.body) ;
1b3a0098 Andy McCall 1 month ago ↶
296
297
298
let form = & r.body[ r.body.find ( " <form class=\" card\" method=\" post\" action=\" /login\" >" ) .unwrap ( ) .. ] ;
299
let form = & form[ .. form.find ( " </form>" ) .unwrap ( ) ] ;
300
let names: Vec< & str > = form.split ( " name=\" " ) .skip ( 1 ) .map ( | t | t.split ( ' "' ) .next ( ) .unwrap ( ) ) .collect ( ) ;
301
let mut unique = names.clone ( ) ;
302
unique.sort ( ) ;
303
unique.dedup ( ) ;
304
assert_eq! ( unique.len ( ) , names.len ( ) , " a field named twice: {names:?}" ) ;
bff9106e Andy McCall 22 days ago ↶
305
306
307
assert! ( ! names.contains ( & " password" ) , " {names:?}" ) ;
308
let pending = form.split ( " name=\" pending\" value=\" " ) .nth ( 1 ) .unwrap ( ) .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
19bb31ff Andy McCall 1 month ago ↶
309
let code = oxygenhub:: auth:: totp:: current( & secret, ( db:: now_ms( ) / 1000 ) as u64 ) .unwrap ( ) ;
bff9106e Andy McCall 22 days ago ↶
310
let fill = | pending : & str | -> String {
311
names
312
.iter ( )
313
.map ( | n | match * n {
314
" username" => " username=andy" .to_string ( ) ,
315
" pending" => format! ( " pending={pending} " ) ,
316
" code" => format! ( " code={code} " ) ,
317
other => format! ( " {other} =" ) ,
318
} )
319
.collect:: < Vec< _ > > ( )
320
.join ( " &" )
321
} ;
322
323
let r = post ( app, " /login" , None , & fill ( " 1.forged" ) ) .await;
324
assert! ( r.body.contains ( " sign in again" ) & & r.cookie.is_none ( ) , " {}" , r.body) ;
325
let r = post ( app, " /login" , None , & fill ( & pending) ) .await;
19bb31ff Andy McCall 1 month ago ↶
326
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
327
assert! ( r.cookie.is_some ( ) ) ;
328
assert! ( r.location.as_deref ( ) = = Some ( " /" ) ) ;
329
}
a160b98f Andy McCall 1 month ago ↶
330
331
332
fn picture_form ( csrf : & str , action : & str , file : & [u8 ]) -> ( String, Vec< u8 > ) {
333
let boundary = " picture-boundary-1234" ;
334
let mut body = Vec :: new( ) ;
335
body.extend_from_slice ( format! ( " --{boundary} \r \n Content-Disposition: form-data; name=\" _csrf\" \r \n \r \n {csrf} \r \n " ) .as_bytes ( ) ) ;
336
body.extend_from_slice ( format! ( " --{boundary} \r \n Content-Disposition: form-data; name=\" action\" \r \n \r \n {action} \r \n " ) .as_bytes ( ) ) ;
337
body.extend_from_slice (
338
format! ( " --{boundary} \r \n Content-Disposition: form-data; name=\" picture\" ; filename=\" me.png\" \r \n Content-Type: image/png\r \n \r \n " )
339
.as_bytes ( ) ,
340
) ;
341
body.extend_from_slice ( file) ;
342
body.extend_from_slice ( format! ( " \r \n --{boundary} --\r \n " ) .as_bytes ( ) ) ;
343
( format! ( " multipart/form-data; boundary={boundary} " ) , body)
344
}
345
346
async fn upload ( app : & Shared, path : & str , cookie : & str , csrf : & str , action : & str , file : & [u8 ]) -> Res {
347
let ( ct, body) = picture_form ( csrf, action, file) ;
348
let req = Request:: post( path)
349
.header ( header:: COOKIE , format! ( " oxyhub={cookie} " ) )
350
.header ( header:: CONTENT_TYPE , ct)
351
.body ( Body:: from( body) )
352
.unwrap ( ) ;
353
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
354
let status = res.status ( ) ;
355
let bytes = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ;
83940d53 Andy McCall 1 month ago ↶
356
Res { status, body: String :: from_utf8_lossy( & bytes) .into_owned ( ) , cookie: None , location: None , raw_cookies: vec! [ ] }
a160b98f Andy McCall 1 month ago ↶
357
}
358
359
360
async fn picture ( app : & Shared, name : & str ) -> ( StatusCode, String, Vec< u8 > ) {
361
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( Request:: get( format! ( " /avatar/{name} " ) ) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
362
let ct = res.headers ( ) .get ( header:: CONTENT_TYPE ) .and_then ( | v | v.to_str ( ) .ok ( ) ) .unwrap_or ( " " ) .to_string ( ) ;
363
let status = res.status ( ) ;
364
( status, ct, res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) )
365
}
366
367
fn png ( w : u32 , h : u32 , alpha : bool ) -> Vec< u8 > {
368
let img = if alpha {
369
image:: DynamicImage:: ImageRgba8( image:: RgbaImage:: from_fn( w, h, | x , _| {
370
image:: Rgba( [ 0 , 114 , 255 , if x % 2 = = 0 { 0 } else { 255 } ] )
371
} ) )
372
} else {
373
image:: DynamicImage:: ImageRgb8( image:: RgbImage:: from_fn( w, h, | x , y | image:: Rgb( [ x as u8 , y as u8 , 128 ] ) ) )
374
} ;
375
let mut out = Vec :: new( ) ;
376
img.write_to ( & mut std:: io:: Cursor:: new( & mut out) , image:: ImageFormat:: Png) .unwrap ( ) ;
377
out
378
}
379
380
# [ tokio ::test ( flavor = " multi_thread" ) ]
381
async fn profile_pictures_and_logos ( ) {
382
let h = hub ( ) .await;
383
let app = & h.app;
384
let andy = register ( app, " andy" , " " ) .await;
385
assert_eq! ( picture ( app, " andy" ) .await.1 , " image/svg+xml" , " the identicon first" ) ;
386
let csrf = csrf_of ( & get ( app, " /settings" , Some ( & andy) ) .await.body) ;
387
assert! ( get ( app, " /settings" , Some ( & andy) ) .await.body.contains ( " id=\" picture\" " ) ) ;
388
389
390
let r = upload ( app, " /settings/avatar" , & andy, & csrf, " upload" , & png ( 640 , 480 , false ) ) .await;
391
assert! ( r.body.contains ( " Picture saved" ) , " {}" , r.body) ;
392
let ( st, ct, bytes) = picture ( app, " andy" ) .await;
393
assert_eq! ( ( st, ct.as_str ( ) ) , ( StatusCode:: OK , " image/jpeg" ) ) ;
394
let img = image:: load_from_memory( & bytes) .unwrap ( ) ;
395
assert_eq! ( ( img.width ( ) , img.height ( ) ) , ( 400 , 400 ) ) ;
396
let stored = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .avatar;
397
assert! ( stored.starts_with ( " jpeg:" ) , " {stored}" ) ;
398
399
400
let r = upload (
401
app,
402
" /settings/avatar" ,
403
& andy,
404
& csrf,
405
" upload" ,
406
b " <svg xmlns=\" http://www.w3.org/2000/svg\" ><script>alert(1)</script></svg>" ,
407
)
408
.await;
409
assert! ( r.body.contains ( " not a PNG, JPEG or WebP" ) , " {}" , r.body) ;
410
let r = upload ( app, " /settings/avatar" , & andy, " wrong" , " upload" , & png ( 10 , 10 , false ) ) .await;
411
assert_ne! ( r.status, StatusCode:: OK ) ;
412
assert_eq! ( picture ( app, " andy" ) .await.1 , " image/jpeg" ) ;
413
414
let r = upload ( app, " /settings/avatar" , & andy, & csrf, " remove" , b " " ) .await;
415
assert! ( r.body.contains ( " Picture removed" ) ) ;
416
assert_eq! ( picture ( app, " andy" ) .await.1 , " image/svg+xml" ) ;
417
418
419
let me = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
420
db:: accounts:: create_org( & app.db, " acme" , " Acme" , me.id) .await.unwrap ( ) ;
421
let org_csrf = csrf_of ( & get ( app, " /orgs/acme/settings" , Some ( & andy) ) .await.body) ;
422
let r = upload ( app, " /orgs/acme/settings/avatar" , & andy, & org_csrf, " upload" , & png ( 120 , 120 , true ) ) .await;
423
assert! ( r.body.contains ( " Picture saved" ) , " {}" , r.body) ;
424
let ( _ , ct, bytes) = picture ( app, " acme" ) .await;
425
assert_eq! ( ct, " image/png" ) ;
426
assert! ( image:: load_from_memory( & bytes) .unwrap ( ) .color ( ) .has_alpha ( ) ) ;
427
let invite = {
428
let admin = get ( app, " /admin" , Some ( & andy) ) .await;
429
let r = post ( app, " /admin/invite" , Some ( & andy) , & format! ( " _csrf={} " , csrf_of ( & admin.body) ) ) .await;
430
let i = r.body.find ( " invite=" ) .map ( | i | i + 7 ) .expect ( " an invitation" ) ;
431
r.body[ i.. ] .chars ( ) .take_while ( | c | c.is_ascii_alphanumeric ( ) ) .collect:: < String> ( )
432
} ;
433
let bob = register ( app, " bob" , & invite) .await;
434
let bob_csrf = csrf_of ( & get ( app, " /settings" , Some ( & bob) ) .await.body) ;
435
let r = upload ( app, " /orgs/acme/settings/avatar" , & bob, & bob_csrf, " remove" , b " " ) .await;
436
assert_eq! ( r.status, StatusCode:: NOT_FOUND , " not an owner" ) ;
437
assert_eq! ( picture ( app, " acme" ) .await.1 , " image/png" ) ;
438
}
8fb35df7 Andy McCall 1 month ago ↶
439
440
# [ tokio ::test ( flavor = " multi_thread" ) ]
441
async fn organisation_webhooks_and_storage ( ) {
442
let h = hub ( ) .await;
443
let app = & h.app;
444
let andy = register ( app, " andy" , " " ) .await;
445
let me = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
446
let org = db:: accounts:: create_org( & app.db, " acme" , " Acme" , me.id) .await.unwrap ( ) ;
447
ff656953 Andy McCall 1 month ago ↶
448
449
450
451
let alone = db:: repos:: create( & app.db, " acme" , " alone" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
452
let solo = db:: repos:: create( & app.db, " andy" , " solo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
453
assert_eq! ( db:: proposals:: requirements( & app.db, alone.id) .await.unwrap ( ) .required_approvals, 0 ) ;
454
assert_eq! ( db:: proposals:: requirements( & app.db, solo.id) .await.unwrap ( ) .required_approvals, 0 ) ;
455
let bobby = db:: accounts:: create_account( & app.db, " bobby" , " [email protected] " , " x" , " salt" , false ) .await.unwrap ( ) ;
456
db:: accounts:: set_org_member( & app.db, org.id, bobby.id, " member" ) .await.unwrap ( ) ;
462f88d1 Andy McCall 1 month ago ↶
457
let tools = db:: repos:: create( & app.db, " acme" , " tools" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
458
assert_eq! ( db:: proposals:: requirements( & app.db, tools.id) .await.unwrap ( ) .required_approvals, 1 ) ;
459
8fb35df7 Andy McCall 1 month ago ↶
460
461
let page = get ( app, " /orgs/acme/settings" , Some ( & andy) ) .await;
462
assert! ( page.body.contains ( " id=\" webhooks\" " ) & & page.body.contains ( " href=\" #webhooks\" " ) ) ;
463
let csrf = csrf_of ( & page.body) ;
464
let r = post ( app, " /orgs/acme/settings/hooks" , Some ( & andy) , & format! ( " _csrf={csrf} &url=ftp%3A%2F%2Fx&events=*" ) ) .await;
465
assert! ( r.body.contains ( " starts with http" ) , " {}" , r.body) ;
466
let r = post (
467
app,
468
" /orgs/acme/settings/hooks" ,
469
Some ( & andy) ,
470
& format! ( " _csrf={csrf} &url=https%3A%2F%2Fhooks.invalid%2Facme&secret=s&events=push" ) ,
471
)
472
.await;
473
assert! ( r.body.contains ( " Webhook added" ) ) ;
474
let hooks = db:: hooks:: hooks_for_org( & app.db, org.id) .await.unwrap ( ) ;
475
assert_eq! ( hooks.len ( ) , 1 ) ;
476
let id = hooks[ 0 ] .id;
477
assert! ( post ( app, " /orgs/acme/settings/hooks" , Some ( & andy) , & format! ( " _csrf={csrf} &id={id} &action=ping" ) )
478
.await
479
.body
480
.contains ( " Ping queued" ) ) ;
481
let deliveries = get ( app, & format! ( " /orgs/acme/settings/hooks/{id} /deliveries" ) , Some ( & andy) ) .await;
482
assert_eq! ( deliveries.status, StatusCode:: OK ) ;
483
assert! ( deliveries.body.contains ( " >ping<" ) & & deliveries.body.contains ( " /orgs/acme/settings#webhooks" ) ) ;
484
485
let repo = oxygenhub:: repos:: RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
486
app.repos.create ( & repo, None ) .unwrap ( ) ;
487
let row = db:: repos:: create( & app.db, " andy" , " demo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
488
let repo_hook = db:: hooks:: create_hook( & app.db, Some ( row.id) , None , " https://hooks.invalid/repo" , " " , " *" ) .await.unwrap ( ) .id;
489
post ( app, " /orgs/acme/settings/hooks" , Some ( & andy) , & format! ( " _csrf={csrf} &id={repo_hook} &action=delete" ) ) .await;
490
assert! ( db:: hooks:: hook( & app.db, repo_hook) .await.unwrap ( ) .is_some ( ) , " not the organisation's" ) ;
491
assert_eq! ( get ( app, & format! ( " /orgs/acme/settings/hooks/{repo_hook} /deliveries" ) , Some ( & andy) ) .await.status, StatusCode:: NOT_FOUND ) ;
492
493
let rs = get ( app, " /andy/demo/settings" , Some ( & andy) ) .await;
494
assert! ( rs.body.contains ( " https://hooks.invalid/repo" ) & & rs.body.contains ( " action=\" /andy/demo/settings/hooks\" " ) ) ;
495
496
497
let work = tempfile:: tempdir( ) .unwrap ( ) ;
498
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
499
client.config.set ( " remote.origin.url" , & format! ( " {} /andy/demo" , h.base) ) .unwrap ( ) ;
500
let ( token, hash) = oxygenhub:: auth:: new_token( ) ;
501
db:: accounts:: create_token( & app.db, me.id, " push" , & hash, " repo:write" , " " , None ) .await.unwrap ( ) ;
502
client.config.set ( " remote.origin.token" , & token) .unwrap ( ) ;
503
client.save_config ( ) .unwrap ( ) ;
504
write ( work.path ( ) , " a.txt" , b " a\n " ) ;
505
client.snapshot ( " t" ) .unwrap ( ) ;
506
client.commit ( " one" , & [ ] ) .unwrap ( ) ;
507
assert! ( client.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
508
let storage = get ( app, " /admin/storage" , Some ( & andy) ) .await;
509
assert_eq! ( storage.status, StatusCode:: OK ) ;
510
assert! ( storage.body.contains ( " href=\" /andy/demo\" " ) ) ;
511
let loose_before = std:: fs:: read_dir( app.repos.dir ( & repo) .join ( " .oxy/objects/loose" ) ) .map ( | d | d.count ( ) ) .unwrap_or ( 0 ) ;
512
assert! ( loose_before > 0 , " the push left loose objects" ) ;
513
let r = post ( app, " /admin/storage" , Some ( & andy) , & format! ( " _csrf={} &repo=andy%2Fdemo" , csrf_of ( & storage.body) ) ) .await;
514
assert! ( r.body.contains ( " Maintained 1 repository" ) & & r.body.contains ( " loose objects packed" ) , " {}" , r.body) ;
515
let loose_after: usize = walk_files ( & app.repos.dir ( & repo) .join ( " .oxy/objects/loose" ) ) ;
516
assert_eq! ( loose_after, 0 , " packed" ) ;
517
let audit = db:: accounts:: audit_rows( & app.db, 20 ) .await.unwrap ( ) ;
518
assert! ( audit.iter ( ) .any ( | a | a.action = = " admin.gc" & & a.target = = " andy/demo" ) ) ;
519
let r = post ( app, " /admin/storage" , Some ( & andy) , & format! ( " _csrf={} &repo=nobody%2Fnone" , csrf_of ( & storage.body) ) ) .await;
520
assert! ( r.body.contains ( " No repository nobody/none" ) ) ;
521
}
522
523
fn walk_files ( dir : & Path) -> usize {
524
let Ok ( rd) = std:: fs:: read_dir( dir) else { return 0 } ;
525
rd.flatten ( ) .map ( | e | if e.path ( ) .is_dir ( ) { walk_files ( & e.path ( ) ) } else { 1 } ) .sum ( )
526
}
6a178055 Andy McCall 1 month ago ↶
527
528
529
530
531
# [ tokio ::test ( flavor = " multi_thread" ) ]
532
async fn admins_disable_enable_and_delete_accounts ( ) {
533
let h = hub ( ) .await;
534
let app = & h.app;
535
let andy = register ( app, " andy" , " " ) .await;
536
let invite = | app : Shared, andy : String| async move {
537
let csrf = csrf_of ( & get ( & app, " /admin" , Some ( & andy) ) .await.body) ;
538
let r = post ( & app, " /admin/invite" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
539
r.body.split ( " register?invite=" ) .nth ( 1 ) .unwrap ( ) .split ( ' <' ) .next ( ) .unwrap ( ) .trim ( ) .to_string ( )
540
} ;
541
let bob = register ( app, " bob" , & invite ( app.clone ( ) , andy.clone ( ) ) .await) .await;
542
let act = | who : & 'static str , action : & 'static str , confirm : & 'static str | {
543
let ( app, andy) = ( app.clone ( ) , andy.clone ( ) ) ;
544
async move {
545
let csrf = csrf_of ( & get ( & app, " /admin" , Some ( & andy) ) .await.body) ;
546
post ( & app, " /admin/account" , Some ( & andy) , & format! ( " _csrf={csrf} &username={who} &action={action} &confirm={confirm} " ) ) .await.body
547
}
548
} ;
549
let login = | user : & 'static str | {
550
let app = app.clone ( ) ;
551
async move { post ( & app, " /login" , None , & format! ( " username={user} &password=correct-horse-battery" ) ) .await }
552
} ;
553
554
555
act ( " bob" , " disable" , " " ) .await;
556
assert! ( db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .disabled) ;
557
let r = login ( " bob" ) .await;
558
assert! ( r.body.contains ( " Your account has been disabled" ) , " {}" , r.body) ;
559
assert! ( r.cookie.is_none ( ) ) ;
560
assert_eq! ( get ( app, " /settings" , Some ( & bob) ) .await.status, StatusCode:: SEE_OTHER , " the session is refused" ) ;
561
assert! ( act ( " andy" , " disable" , " " ) .await.contains ( " Not on yourself" ) ) ;
562
563
act ( " bob" , " enable" , " " ) .await;
564
assert_eq! ( login ( " bob" ) .await.status, StatusCode:: SEE_OTHER ) ;
565
566
567
let bob = login ( " bob" ) .await.cookie.unwrap ( ) ;
568
let csrf = csrf_of ( & get ( app, " /new" , Some ( & bob) ) .await.body) ;
569
assert_eq! (
570
post ( app, " /new" , Some ( & bob) , & format! ( " _csrf={csrf} &owner=bob&name=keep&visibility=public" ) ) .await.status,
571
StatusCode:: SEE_OTHER
572
) ;
573
assert! ( act ( " bob" , " delete" , " bo" ) .await.contains ( " type bob to confirm" ) ) ;
574
let page = act ( " bob" , " delete" , " bob" ) .await;
575
assert! ( page.contains ( " still owns bob/keep" ) , " {page}" ) ;
576
assert! ( db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .is_some ( ) ) ;
577
let row = db:: repos:: get( & app.db, " bob" , " keep" ) .await.unwrap ( ) .unwrap ( ) ;
578
db:: repos:: delete( & app.db, row.id) .await.unwrap ( ) ;
579
let page = act ( " bob" , " delete" , " bob" ) .await;
580
assert! ( page.contains ( " bob deleted" ) , " {page}" ) ;
581
assert! ( db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .is_none ( ) ) ;
582
assert_eq! ( get ( app, " /settings" , Some ( & bob) ) .await.status, StatusCode:: SEE_OTHER ) ;
583
584
let code = invite ( app.clone ( ) , andy.clone ( ) ) .await;
585
let r = post ( app, " /register" , None , & format! ( " username=bob&email=b%40example.com&password=correct-horse-battery&invite={code} " ) ) .await;
586
assert! ( r.body.contains ( " That name is taken" ) , " {}" , r.body) ;
587
588
assert! ( act ( " andy" , " delete" , " andy" ) .await.contains ( " Not on yourself" ) ) ;
589
}
5dead1f4 Andy McCall 1 month ago ↶
590
591
592
593
# [ tokio ::test ( flavor = " multi_thread" ) ]
594
async fn admins_set_the_job_time_limits ( ) {
595
let h = hub ( ) .await;
596
let app = & h.app;
597
let andy = register ( app, " andy" , " " ) .await;
598
let page = get ( app, " /admin/runners/settings" , Some ( & andy) ) .await;
599
assert_eq! ( page.status, StatusCode:: OK ) ;
600
assert! ( page.body.contains ( " Job time limits" ) & & page.body.contains ( " value=\" 60\" " ) , " the hub.toml default shows" ) ;
601
let csrf = csrf_of ( & page.body) ;
602
let r = post ( app, " /admin/runners/settings" , Some ( & andy) , & format! ( " _csrf={csrf} &default_minutes=200&max_minutes=120&grace_minutes=5" ) )
603
.await;
604
assert! ( r.body.contains ( " cannot be longer than the maximum" ) , " {}" , r.body) ;
605
let r = post ( app, " /admin/runners/settings" , Some ( & andy) , & format! ( " _csrf={csrf} &default_minutes=90&max_minutes=240&grace_minutes=10" ) )
606
.await;
607
assert! ( r.body.contains ( " Saved." ) , " {}" , r.body) ;
608
let l = oxygenhub:: flow:: limits( app) .await;
609
assert_eq! ( ( l.default_s, l.max_s, l.grace_ms) , ( 5400 , 14400 , 600_000 ) ) ;
610
assert! ( get ( app, " /admin/runners" , Some ( & andy) ) .await.body.contains ( " href=\" /admin/runners/settings\" " ) ) ;
611
let code = {
612
let csrf = csrf_of ( & get ( app, " /admin" , Some ( & andy) ) .await.body) ;
613
let r = post ( app, " /admin/invite" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
614
r.body.split ( " register?invite=" ) .nth ( 1 ) .unwrap ( ) .split ( ' <' ) .next ( ) .unwrap ( ) .trim ( ) .to_string ( )
615
} ;
616
let bob = register ( app, " bob" , & code) .await;
617
assert_ne! ( get ( app, " /admin/runners/settings" , Some ( & bob) ) .await.status, StatusCode:: OK , " not for others" ) ;
618
}
3556d08e Andy McCall 1 month ago ↶
619
620
621
622
# [ tokio ::test ( flavor = " multi_thread" ) ]
623
async fn mail_settings_are_saved_masked_and_tested_from_the_admin_page ( ) {
624
let h = hub ( ) .await;
625
let app = & h.app;
626
let andy = register ( app, " andy" , " " ) .await;
627
let page = get ( app, " /admin" , Some ( & andy) ) .await.body;
628
assert! ( page.contains ( " Mail is off" ) , " {page}" ) ;
629
let csrf = csrf_of ( & page) ;
630
631
let outbox = tempfile:: tempdir( ) .unwrap ( ) ;
632
let r = post (
633
app,
634
" /admin/mail" ,
635
Some ( & andy) ,
636
& format! ( " _csrf={csrf} &smtp=file%3A%2F%2F{} &from=Hub+%3Chub%40example.org%3E" , outbox.path ( ) .display ( ) ) ,
637
)
638
.await;
639
assert! ( r.body.contains ( " Mail settings saved" ) & & r.body.contains ( " Mail is on" ) , " {}" , r.body) ;
640
assert! ( oxygenhub:: mail:: enabled( app) ) ;
641
let r = post ( app, " /admin/mail/test" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
642
assert! ( r.body.contains ( " Sent a test message to" ) , " {}" , r.body) ;
643
let sent: Vec< _ > = std:: fs:: read_dir( outbox.path ( ) ) .unwrap ( ) .flatten ( ) .collect ( ) ;
644
assert_eq! ( sent.len ( ) , 1 ) ;
645
let text = std:: fs:: read_to_string( sent[ 0 ] .path ( ) ) .unwrap ( ) ;
646
assert! ( text.contains ( " Subject: [" ) & & text.contains ( " Test message" ) & & text.contains ( " From: Hub <[email protected] >" ) , " {text}" ) ;
647
648
let r = post (
649
app,
650
" /admin/mail" ,
651
Some ( & andy) ,
652
& format! ( " _csrf={csrf} &smtp=smtps%3A%2F%2Fhub%3Asecret%40mail.example.org%3A465&from=Hub+%3Chub%40example.org%3E" ) ,
653
)
654
.await;
2a83f530 Andy McCall 1 month ago ↶
655
assert! ( r.body.contains ( " smtps://hub:***@mail.example.org:465" ) & & ! r.body.contains ( " hub:secret" ) , " {}" , r.body) ;
3556d08e Andy McCall 1 month ago ↶
656
let r = post (
657
app,
658
" /admin/mail" ,
659
Some ( & andy) ,
660
& format! ( " _csrf={csrf} &smtp=smtps%3A%2F%2Fhub%3A***%40mail.example.org%3A465&from=Hub+%3Chub%40example.org%3E" ) ,
661
)
662
.await;
663
assert! ( r.body.contains ( " Mail settings saved" ) , " {}" , r.body) ;
664
assert_eq! ( oxygenhub:: mail:: settings( app) .smtp, " smtps://hub:[email protected] :465" ) ;
665
666
let r = post ( app, " /admin/mail" , Some ( & andy) , & format! ( " _csrf={csrf} &smtp=mail.example.org&from=x" ) ) .await;
667
assert! ( r.body.contains ( " starts with smtp://" ) , " {}" , r.body) ;
668
let r = post ( app, " /admin/mail" , Some ( & andy) , & format! ( " _csrf={csrf} &smtp=&from=" ) ) .await;
669
assert! ( r.body.contains ( " Mail is off" ) , " {}" , r.body) ;
670
assert! ( ! oxygenhub:: mail:: enabled( app) ) ;
671
672
let r = post ( app, " /admin/mail" , None , & format! ( " _csrf={csrf} &smtp=&from=" ) ) .await;
673
assert_ne! ( r.status, StatusCode:: OK ) ;
cd46e56b Andy McCall 22 days ago ↶
674
675
676
677
let from = " from=OxygenHub+%3Cnoreply%40example.org%3E" ;
678
let r = post ( app, " /admin/mail" , Some ( & andy) , & format! ( " _csrf={csrf} &service=resend&secret=&{from} " ) ) .await;
679
assert! ( r.body.contains ( " Give the Resend API key" ) , " {}" , r.body) ;
680
let r = post ( app, " /admin/mail" , Some ( & andy) , & format! ( " _csrf={csrf} &service=resend&secret=re_test_key_1&{from} " ) ) .await;
681
assert! ( r.body.contains ( " Mail settings saved" ) & & ! r.body.contains ( " re_test_key_1" ) , " {}" , r.body) ;
682
assert! ( r.body.contains ( r # "<option value="resend" selected>"# ) & & r.body.contains ( " saved: leave empty to keep it" ) , " {}" , r.body) ;
683
assert_eq! ( oxygenhub:: mail:: settings( app) .smtp, " smtps://resend:[email protected] :465" ) ;
684
let r = post ( app, " /admin/mail" , Some ( & andy) , & format! ( " _csrf={csrf} &service=resend&secret=&{from} " ) ) .await;
685
assert! ( r.body.contains ( " Mail settings saved" ) , " {}" , r.body) ;
686
assert_eq! ( oxygenhub:: mail:: settings( app) .smtp, " smtps://resend:[email protected] :465" , " kept" ) ;
687
688
let r =
689
post ( app, " /admin/mail" , Some ( & andy) , & format! ( " _csrf={csrf} &service=ses®ion=eu-west-2&user=AKIAEXAMPLE&secret=s3cret&{from} " ) )
690
.await;
691
assert! ( r.body.contains ( r # "<option value="ses" selected>"# ) , " {}" , r.body) ;
692
assert_eq! ( oxygenhub:: mail:: settings( app) .smtp, " smtps://AKIAEXAMPLE:[email protected] :465" ) ;
693
let r = post ( app, " /admin/mail" , Some ( & andy) , & format! ( " _csrf={csrf} &service=off&{from} " ) ) .await;
694
assert! ( r.body.contains ( " Mail is off" ) , " {}" , r.body) ;
3556d08e Andy McCall 1 month ago ↶
695
}
e71846aa Andy McCall 1 month ago ↶
696
697
698
699
async fn mock_assistant ( ) -> ( String, std:: sync:: Arc< std:: sync:: Mutex< Vec< String> > > ) {
700
use std:: sync:: { Arc, Mutex} ;
701
let seen: Arc< Mutex< Vec< String> > > = Arc:: new( Mutex:: new( vec! [ ] ) ) ;
702
let s2 = seen.clone ( ) ;
703
let router = axum:: Router:: new( ) .route (
704
" /v1/chat/completions" ,
705
axum:: routing:: post( move | headers: axum:: http:: HeaderMap, body: String | {
706
let s2 = s2.clone ( ) ;
707
async move {
708
let auth = headers.get ( " authorization" ) .and_then ( | v | v.to_str ( ) .ok ( ) ) .unwrap_or ( " " ) .to_string ( ) ;
709
s2.lock ( ) .unwrap ( ) .push ( format! ( " {auth} {body} " ) ) ;
710
axum:: Json( serde_json:: json! ( { " choices" : [ { " message" : { " role" : " assistant" , " content" : " OK from mock" } } ] } ) )
711
}
712
} ) ,
713
) ;
714
let listener = tokio:: net:: TcpListener:: bind( " 127.0.0.1:0" ) .await.unwrap ( ) ;
715
let port = listener.local_addr ( ) .unwrap ( ) .port ( ) ;
716
tokio:: spawn( async move { axum:: serve( listener, router) .await.unwrap ( ) } ) ;
717
( format! ( " http://127.0.0.1:{port} /v1/chat/completions" ) , seen)
718
}
719
720
fn enc ( s : & str ) -> String {
721
s.replace ( ' %' , " %25" ) .replace ( ' :' , " %3A" ) .replace ( ' /' , " %2F" ) .replace ( ' ' , " +" ) .replace ( ' \n ' , " %0A" ) .replace ( ' &' , " %26" )
722
}
723
724
# [ tokio ::test ( flavor = " multi_thread" ) ]
725
async fn ai_assistants_are_kept_per_level_with_sealed_tokens_and_combined_downwards ( ) {
726
use oxygenhub:: assistants:: { self , Scope} ;
727
let h = hub ( ) .await;
728
let app = & h.app;
729
let andy = register ( app, " andy" , " " ) .await;
730
let ( endpoint, seen) = mock_assistant ( ) .await;
731
732
733
let page = get ( app, " /admin" , Some ( & andy) ) .await.body;
734
assert! ( page.contains ( " AI assistants" ) & & page.contains ( " Add an assistant" ) , " {page}" ) ;
735
let csrf = csrf_of ( & page) ;
736
let add = | name : & str , extra : & str | {
737
format! (
738
" _csrf={csrf} &action=add&name={name} &endpoint={} &api=chat-completions&model=claude-opus-5-5&token=sk-test-secret&enabled=1&budget=50000{extra} " ,
739
enc ( & endpoint)
740
)
741
} ;
742
let r = post ( app, " /admin/assistants" , Some ( & andy) , & add ( " Claude" , " " ) ) .await;
206c1fc1 Andy McCall 1 month ago ↶
743
assert! ( r.body.contains ( " Claude added and answered: OK from mock" ) & & r.body.contains ( " token set" ) , " {}" , r.body) ;
e71846aa Andy McCall 1 month ago ↶
744
assert! ( ! r.body.contains ( " sk-test-secret" ) , " the token never comes back" ) ;
745
let site = assistants:: list( & app.db, Scope:: Site) .await.unwrap ( ) ;
746
assert_eq! ( site.len ( ) , 1 ) ;
747
let claude = & site[ 0 ] ;
748
assert! ( ! String :: from_utf8_lossy( & claude.token) .contains ( " sk-test" ) , " sealed at rest" ) ;
749
assert_eq! ( app.assistant_key.open ( & claude.token) .as_deref ( ) , Some ( " sk-test-secret" ) ) ;
750
751
let r = post ( app, " /admin/assistants" , Some ( & andy) , & add ( " claude" , " " ) ) .await;
752
assert! ( r.body.contains ( " already an assistant called" ) , " {}" , r.body) ;
753
let r = post (
754
app,
755
" /admin/assistants" ,
756
Some ( & andy) ,
757
& format! ( " _csrf={csrf} &action=add&name=Bad&endpoint=ftp%3A%2F%2Fx&api=chat-completions&model=m" ) ,
758
)
759
.await;
760
assert! ( r.body.contains ( " starting with https://" ) , " {}" , r.body) ;
761
762
763
let r = post ( app, " /admin/assistants" , Some ( & andy) , & format! ( " _csrf={csrf} &action=test&id={} " , claude.id) ) .await;
764
assert! ( r.body.contains ( " Claude answered: OK from mock" ) , " {}" , r.body) ;
765
{
766
let calls = seen.lock ( ) .unwrap ( ) ;
206c1fc1 Andy McCall 1 month ago ↶
767
assert_eq! ( calls.len ( ) , 2 , " once on add, once on test" ) ;
e71846aa Andy McCall 1 month ago ↶
768
assert! ( calls[ 0 ] .starts_with ( " Bearer sk-test-secret " ) , " {}" , calls[ 0 ] ) ;
769
assert! ( calls[ 0 ] .contains ( " \" model\" :\" claude-opus-5-5\" " ) & & calls[ 0 ] .contains ( " Reply with the single word OK" ) , " {}" , calls[ 0 ] ) ;
770
}
771
772
let r = post (
773
app,
774
" /admin/assistants" ,
775
Some ( & andy) ,
776
& format! ( " _csrf={csrf} &action=update&id={} &name=Claude&endpoint={} &api=chat-completions&model=claude-opus-5-5&token=&enabled=1&budget=60000" , claude.id, enc ( & endpoint) ) ,
777
)
778
.await;
779
assert! ( r.body.contains ( " Claude updated." ) & & r.body.contains ( " context 60000 characters" ) , " {}" , r.body) ;
780
assert_eq! (
781
app.assistant_key.open ( & assistants:: get( & app.db, claude.id) .await.unwrap ( ) .unwrap ( ) .token) .as_deref ( ) ,
782
Some ( " sk-test-secret" )
783
) ;
784
785
let r =
786
post ( app, " /admin/assistants" , Some ( & andy) , & format! ( " _csrf={csrf} &action=prompt&prompt={} " , enc ( " Review for correctness first." ) ) )
787
.await;
788
assert! ( r.body.contains ( " Prompt saved." ) & & r.body.contains ( " Review for correctness first." ) , " {}" , r.body) ;
789
790
791
let r = post ( app, " /orgs/new" , Some ( & andy) , & format! ( " _csrf={csrf} &name=acme&display_name=Acme" ) ) .await;
792
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
793
let r = post ( app, " /new" , Some ( & andy) , & format! ( " _csrf={csrf} &owner=acme&name=plans&visibility=public" ) ) .await;
794
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
795
let org_page = get ( app, " /orgs/acme/settings" , Some ( & andy) ) .await.body;
796
assert! ( org_page.contains ( " Inherited" ) & & org_page.contains ( " from the site" ) & & org_page.contains ( " None of its own" ) , " {org_page}" ) ;
797
assert! ( org_page.contains ( " From the site" ) & & org_page.contains ( " Review for correctness first." ) , " {org_page}" ) ;
798
let repo = db:: repos:: get( & app.db, " acme" , " plans" ) .await.unwrap ( ) .unwrap ( ) ;
799
let on = assistants:: enabled_for_repo( & app.db, & repo) .await.unwrap ( ) ;
800
assert_eq! ( on.iter ( ) .map ( | a | a.name.as_str ( ) ) .collect:: < Vec< _ > > ( ) , [ " Claude" ] ) ;
801
802
803
804
let r = post (
805
app,
806
" /orgs/acme/settings/assistants" ,
807
Some ( & andy) ,
808
& format! ( " _csrf={csrf} &action=prompt&prompt={} " , enc ( " We write 6502 assembly." ) ) ,
809
)
810
.await;
811
assert! ( r.body.contains ( " Prompt saved." ) , " {}" , r.body) ;
812
let r = post ( app, " /orgs/acme/settings/assistants" , Some ( & andy) , & add ( " DeepSeek" , " " ) ) .await;
206c1fc1 Andy McCall 1 month ago ↶
813
assert! ( r.body.contains ( " DeepSeek added and answered" ) , " {}" , r.body) ;
e71846aa Andy McCall 1 month ago ↶
814
let prompt = assistants:: prompt_for_repo( & app.db, & repo) .await.unwrap ( ) ;
815
assert! ( prompt.starts_with ( " ## Instructions from the site\n \n Review for correctness first.\n \n ## Instructions from the acme organisation\n \n We write 6502 assembly." ) , " {prompt}" ) ;
816
let repo_page = get ( app, " /acme/plans/settings" , Some ( & andy) ) .await.body;
817
assert! ( repo_page.contains ( " from the acme organisation" ) & & repo_page.contains ( " From the acme organisation" ) , " {repo_page}" ) ;
818
let on = assistants:: enabled_for_repo( & app.db, & repo) .await.unwrap ( ) ;
819
assert_eq! ( on.iter ( ) .map ( | a | a.name.as_str ( ) ) .collect:: < Vec< _ > > ( ) , [ " Claude" , " DeepSeek" ] ) ;
820
821
822
823
824
let r = post ( app, " /admin/assistants" , Some ( & andy) , & format! ( " _csrf={csrf} &action=toggle&id={} " , claude.id) ) .await;
825
assert! ( r.body.contains ( " Claude is off." ) , " {}" , r.body) ;
826
let r = post ( app, " /acme/plans/settings/assistants" , Some ( & andy) , & add ( " Claude" , " " ) ) .await;
206c1fc1 Andy McCall 1 month ago ↶
827
assert! ( r.body.contains ( " Claude added and answered" ) , " {}" , r.body) ;
e71846aa Andy McCall 1 month ago ↶
828
let on = assistants:: enabled_for_repo( & app.db, & repo) .await.unwrap ( ) ;
829
assert_eq! ( on.iter ( ) .map ( | a | a.name.as_str ( ) ) .collect:: < Vec< _ > > ( ) , [ " DeepSeek" ] ) ;
830
let repo_page = get ( app, " /acme/plans/settings" , Some ( & andy) ) .await.body;
831
assert! ( repo_page.contains ( " off above" ) , " {repo_page}" ) ;
832
post ( app, " /admin/assistants" , Some ( & andy) , & format! ( " _csrf={csrf} &action=toggle&id={} " , claude.id) ) .await;
833
let on = assistants:: enabled_for_repo( & app.db, & repo) .await.unwrap ( ) ;
834
assert_eq! ( on.iter ( ) .map ( | a | ( a.name.as_str ( ) , a.scope.as_str ( ) ) ) .collect:: < Vec< _ > > ( ) , [ ( " DeepSeek" , " org" ) , ( " Claude" , " repo" ) ] ) ;
835
836
837
let invite = {
838
let r = post ( app, " /admin/invite" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
839
let i = r.body.find ( " invite=" ) .map ( | i | i + 7 ) .expect ( " an invitation" ) ;
840
r.body[ i.. ] .chars ( ) .take_while ( | c | c.is_ascii_alphanumeric ( ) ) .collect:: < String> ( )
841
} ;
842
let bob = register ( app, " bob" , & invite) .await;
843
assert_eq! (
844
post ( app, " /admin/assistants" , Some ( & bob) , & format! ( " _csrf={csrf} &action=prompt&prompt=x" ) ) .await.status,
845
StatusCode:: NOT_FOUND
846
) ;
847
assert_eq! (
848
post ( app, " /orgs/acme/settings/assistants" , Some ( & bob) , & format! ( " _csrf={csrf} &action=prompt&prompt=x" ) ) .await.status,
849
StatusCode:: NOT_FOUND
850
) ;
851
assert_eq! (
852
post ( app, " /acme/plans/settings/assistants" , Some ( & bob) , & format! ( " _csrf={csrf} &action=prompt&prompt=x" ) ) .await.status,
853
StatusCode:: NOT_FOUND
854
) ;
855
856
let r = post ( app, " /acme/plans/settings/delete" , Some ( & andy) , & format! ( " _csrf={csrf} &confirm=acme%2Fplans" ) ) .await;
857
assert! ( r.status = = StatusCode:: SEE_OTHER | | r.status = = StatusCode:: OK , " {} {}" , r.status, r.body) ;
858
assert! ( assistants:: list( & app.db, Scope:: Repo( repo.id) ) .await.unwrap ( ) .is_empty ( ) ) ;
859
}
89e89836 Andy McCall 1 month ago ↶
860
861
# [ tokio ::test ( flavor = " multi_thread" ) ]
862
async fn accounts_elsewhere_are_checked_and_shown_on_the_profile ( ) {
863
let h = hub ( ) .await;
864
let app = & h.app;
865
let andy = register ( app, " andy" , " " ) .await;
866
let page = get ( app, " /settings" , Some ( & andy) ) .await.body;
867
let csrf = csrf_of ( & page) ;
868
let base = format! ( " _csrf={csrf} &display_name=Andy&email=andy%40example.com&bio=&website=&location=" ) ;
869
let r = post ( app, " /settings" , Some ( & andy) , & format! ( " {base} &bluesky=%40Andy.bsky.social&mastodon=https%3A%2F%2FFosstodon.org%2F%40andy&github=https%3A%2F%2Fgithub.com%2Fandymccall%2F" ) ) .await;
870
assert! (
871
r.body.contains ( " value=\" andy.bsky.social\" " )
872
& & r.body.contains ( " value=\" @[email protected] \" " )
873
& & r.body.contains ( " value=\" andymccall\" " ) ,
874
" {}" ,
875
r.body
876
) ;
877
let profile = get ( app, " /andy" , None ) .await.body;
878
assert! ( profile.contains ( " href=\" https://bsky.app/profile/andy.bsky.social\" " ) & & profile.contains ( " @andy.bsky.social" ) , " {profile}" ) ;
879
assert! ( profile.contains ( " href=\" https://fosstodon.org/@andy\" " ) & & profile.contains ( " rel=\" nofollow noopener me\" " ) , " {profile}" ) ;
880
assert! ( profile.contains ( " href=\" https://github.com/andymccall\" " ) & & profile.contains ( " title=\" GitHub\" " ) , " {profile}" ) ;
881
882
let r = post ( app, " /settings" , Some ( & andy) , & format! ( " {base} &bluesky=not+a+handle&mastodon=&github=" ) ) .await;
883
assert! ( r.body.contains ( " not a Bluesky handle" ) , " {}" , r.body) ;
884
assert! ( get ( app, " /andy" , None ) .await.body.contains ( " andy.bsky.social" ) ) ;
885
886
post ( app, " /settings" , Some ( & andy) , & format! ( " {base} &bluesky=&mastodon=&github=" ) ) .await;
887
assert! ( ! get ( app, " /andy" , None ) .await.body.contains ( " class=\" social\" " ) ) ;
83940d53 Andy McCall 1 month ago ↶
888
}
889
890
891
async fn mock_provider ( ) -> ( String, std:: sync:: Arc< std:: sync:: Mutex< Vec< String> > > ) {
892
use std:: sync:: { Arc, Mutex} ;
893
let seen: Arc< Mutex< Vec< String> > > = Arc:: new( Mutex:: new( vec! [ ] ) ) ;
894
let s1 = seen.clone ( ) ;
895
let s2 = seen.clone ( ) ;
896
let router = axum:: Router:: new( )
897
.route (
898
" /github/token" ,
899
axum:: routing:: post( move | body: String | {
900
let s1 = s1.clone ( ) ;
901
async move {
902
s1.lock ( ) .unwrap ( ) .push ( format! ( " github token {body} " ) ) ;
903
axum:: Json( serde_json:: json! ( { " access_token" : " gh-token" , " token_type" : " bearer" } ) )
904
}
905
} ) ,
906
)
907
.route (
908
" /github/userinfo" ,
909
axum:: routing:: get( | headers : axum:: http:: HeaderMap| async move {
910
assert_eq! ( headers.get ( " authorization" ) .and_then ( | v | v.to_str ( ) .ok ( ) ) , Some ( " Bearer gh-token" ) ) ;
911
axum:: Json( serde_json:: json! ( { " id" : 583231 , " login" : " octocat" , " name" : " The Octocat" , " email" : null } ) )
912
} ) ,
913
)
914
.route (
915
" /github/userinfo/emails" ,
916
axum:: routing:: get( | | async {
917
axum:: Json( serde_json:: json! ( [
918
{ " email" : " [email protected] " , " primary" : false , " verified" : false } ,
919
{ " email" : " [email protected] " , " primary" : true , " verified" : true }
920
] ) )
921
} ) ,
922
)
923
.route (
924
" /google/token" ,
925
axum:: routing:: post( move | body: String | {
926
let s2 = s2.clone ( ) ;
927
async move {
928
s2.lock ( ) .unwrap ( ) .push ( format! ( " google token {body} " ) ) ;
929
axum:: Json( serde_json:: json! ( { " access_token" : " g-token" , " id_token" : " x" } ) )
930
}
931
} ) ,
932
)
933
.route (
810896d4 Andy McCall 21 days ago ↶
934
" /gitlab/token" ,
935
axum:: routing:: post( | | async { axum:: Json( serde_json:: json! ( { " access_token" : " gl-token" , " token_type" : " Bearer" } ) ) } ) ,
936
)
937
.route (
938
" /gitlab/userinfo" ,
939
axum:: routing:: get( | headers : axum:: http:: HeaderMap| async move {
940
assert_eq! ( headers.get ( " authorization" ) .and_then ( | v | v.to_str ( ) .ok ( ) ) , Some ( " Bearer gl-token" ) ) ;
941
axum:: Json( serde_json:: json! ( { " sub" : " 1234567" , " nickname" : " tanuki" , " name" : " The Tanuki" , " email" : " [email protected] " , " email_verified" : true } ) )
942
} ) ,
943
)
944
.route (
83940d53 Andy McCall 1 month ago ↶
945
" /google/userinfo" ,
946
axum:: routing:: get( | | async {
947
axum:: Json( serde_json:: json! ( { " sub" : " 1076915035000615071" , " email" : " [email protected] " , " email_verified" : true , " name" : " Andy McCall" } ) )
948
} ) ,
949
) ;
950
let listener = tokio:: net:: TcpListener:: bind( " 127.0.0.1:0" ) .await.unwrap ( ) ;
951
let port = listener.local_addr ( ) .unwrap ( ) .port ( ) ;
952
tokio:: spawn( async move { axum:: serve( listener, router) .await.unwrap ( ) } ) ;
953
( format! ( " http://127.0.0.1:{port} " ) , seen)
954
}
955
956
fn state_of ( res : & Res) -> ( String, String) {
957
let cookie = res.raw_cookies.iter ( ) .find ( | c | c.starts_with ( " oxyhub_signin=" ) ) .expect ( " state cookie" ) .clone ( ) ;
958
let value = cookie.trim_start_matches ( " oxyhub_signin=" ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
959
( cookie, value)
960
}
961
962
# [ tokio ::test ( flavor = " multi_thread" ) ]
963
async fn people_sign_in_with_github_or_google_once_an_admin_sets_it_up ( ) {
964
hermetic ( ) ;
965
let ( mock, seen) = mock_provider ( ) .await;
966
let dir = tempfile:: tempdir( ) .unwrap ( ) ;
967
let mut config = Config:: load( dir.path ( ) ) .unwrap ( ) ;
968
let listener = tokio:: net:: TcpListener:: bind( " 127.0.0.1:0" ) .await.unwrap ( ) ;
969
let port = listener.local_addr ( ) .unwrap ( ) .port ( ) ;
970
config.server.base_url = format! ( " http://127.0.0.1:{port} " ) ;
971
config.registration.mode = " open" .into ( ) ;
972
config.auth.signin_mock = Some ( mock.clone ( ) ) ;
973
let app = oxygenhub:: build( config) .await.unwrap ( ) ;
974
let router = oxygenhub:: router( app.clone ( ) ) ;
975
tokio:: spawn( async move { axum:: serve( listener, router) .await.unwrap ( ) } ) ;
976
let app = & app;
977
let andy = register ( app, " andy" , " " ) .await;
978
let andy_row = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
979
db:: accounts:: set_email_verified( & app.db, andy_row.id, true ) .await.unwrap ( ) ;
980
981
982
assert! ( ! get ( app, " /login" , None ) .await.body.contains ( " /login/github" ) ) ;
983
let admin = get ( app, " /admin" , Some ( & andy) ) .await.body;
984
assert! ( admin.contains ( " Sign in with" ) & & admin.contains ( & format! ( " http://127.0.0.1:{port} /login/github/callback" ) ) , " {admin}" ) ;
985
let csrf = csrf_of ( & admin) ;
986
let r =
987
post ( app, " /admin/signin" , Some ( & andy) , & format! ( " _csrf={csrf} &provider=github&client_id=gh-cid&secret=gh-sek&enabled=1" ) ) .await;
988
assert! ( r.body.contains ( " Signing in with GitHub is on." ) & & ! r.body.contains ( " gh-sek" ) , " {}" , r.body) ;
989
let r = post ( app, " /admin/signin" , Some ( & andy) , & format! ( " _csrf={csrf} &provider=google&client_id=g-cid&secret=g-sek&enabled=1" ) ) .await;
990
assert! ( r.body.contains ( " Signing in with Google is on." ) , " {}" , r.body) ;
991
let login = get ( app, " /login" , None ) .await.body;
992
assert! ( login.contains ( " href=\" /login/github\" " ) & & login.contains ( " href=\" /login/google\" " ) , " {login}" ) ;
993
994
995
let r = get ( app, " /login/github" , None ) .await;
996
assert_eq! ( r.status, StatusCode:: SEE_OTHER ) ;
997
let to = r.location.clone ( ) .unwrap ( ) ;
998
assert! ( to.starts_with ( & format! ( " {mock} /github/authorize?client_id=gh-cid&redirect_uri=" ) ) & & to.contains ( " &state=" ) , " {to}" ) ;
999
let ( cookie, state) = state_of ( & r) ;
1000
let bad = send_with ( app, " GET" , " /login/github/callback?code=c1&state=wrong" , Some ( & cookie) , None ) .await;
1001
assert! ( bad.location.as_deref ( ) .unwrap_or ( " " ) .starts_with ( " /login?error=" ) , " {:?}" , bad.location) ;
1002
let ok = send_with ( app, " GET" , & format! ( " /login/github/callback?code=c1&state={state} " ) , Some ( & cookie) , None ) .await;
1003
assert_eq! ( ok.status, StatusCode:: SEE_OTHER , " {}" , ok.body) ;
1004
assert_eq! ( ok.location.as_deref ( ) , Some ( " /" ) ) ;
1005
let octo_cookie = ok.cookie.clone ( ) .expect ( " signed in" ) ;
1006
let octo = db:: accounts:: account_by_name( & app.db, " octocat" ) .await.unwrap ( ) .expect ( " account made from the GitHub login" ) ;
1007
assert! (
1008
( octo.email.as_str ( ) , octo.email_verified, octo.display_name.as_str ( ) ) = = ( " [email protected] " , true , " The Octocat" ) ,
1009
" {octo:?}"
1010
) ;
1011
assert! ( seen.lock ( ) .unwrap ( ) .iter ( ) .any ( | s | s.contains ( " github token" ) & & s.contains ( " code=c1" ) & & s.contains ( " client_secret=gh-sek" ) ) ) ;
1012
assert! ( get ( app, " /settings" , Some ( & octo_cookie) ) .await.body.contains ( " octocat" ) ) ;
1013
1014
let r = get ( app, " /login/github" , None ) .await;
1015
let ( cookie, state) = state_of ( & r) ;
1016
let ok = send_with ( app, " GET" , & format! ( " /login/github/callback?code=c2&state={state} " ) , Some ( & cookie) , None ) .await;
1017
assert_eq! ( ok.location.as_deref ( ) , Some ( " /" ) ) ;
1018
assert_eq! ( db:: accounts:: account_count( & app.db) .await.unwrap ( ) , 2 ) ;
810896d4 Andy McCall 21 days ago ↶
1019
1020
1021
1022
let r = post (
1023
app,
1024
" /admin/signin" ,
1025
Some ( & andy) ,
1026
& format! ( " _csrf={csrf} &provider=gitlab&client_id=gl-cid&secret=gl-sek&enabled=1&base=http%3A%2F%2Fgitlab.example.org" ) ,
1027
)
1028
.await;
1029
assert! ( r.body.contains ( " is an https:// address" ) , " {}" , r.body) ;
1030
let r = post (
1031
app,
1032
" /admin/signin" ,
1033
Some ( & andy) ,
1034
& format! ( " _csrf={csrf} &provider=gitlab&client_id=gl-cid&secret=gl-sek&enabled=1&base=https%3A%2F%2Fgitlab.example.org%2F" ) ,
1035
)
1036
.await;
1037
assert! ( r.body.contains ( " Signing in with GitLab is on." ) & & r.body.contains ( " value=\" https://gitlab.example.org\" " ) , " {}" , r.body) ;
1038
let r = post (
1039
app,
1040
" /admin/signin" ,
1041
Some ( & andy) ,
1042
& format! ( " _csrf={csrf} &provider=github&client_id=gh-cid&enabled=1&base=https%3A%2F%2Fx.org" ) ,
1043
)
1044
.await;
1045
assert! ( r.body.contains ( " GitHub has no address to set." ) , " {}" , r.body) ;
1046
let login = get ( app, " /login" , None ) .await.body;
1047
assert! ( login.contains ( " signin-btn gitlab" ) & & login.contains ( " Sign in with GitLab" ) , " {login}" ) ;
1048
let r = get ( app, " /login/gitlab" , None ) .await;
1049
assert! ( r.location.clone ( ) .unwrap ( ) .contains ( " scope=openid%20profile%20email" ) , " {:?}" , r.location) ;
1050
let ( cookie, state) = state_of ( & r) ;
1051
let ok = send_with ( app, " GET" , & format! ( " /login/gitlab/callback?code=c9&state={state} " ) , Some ( & cookie) , None ) .await;
1052
assert_eq! ( ok.location.as_deref ( ) , Some ( " /" ) , " {}" , ok.body) ;
1053
let tanuki = db:: accounts:: account_by_name( & app.db, " tanuki" ) .await.unwrap ( ) .expect ( " account made from the GitLab username" ) ;
1054
assert! ( ( tanuki.email.as_str ( ) , tanuki.email_verified) = = ( " [email protected] " , true ) , " {tanuki:?}" ) ;
1055
assert_eq! ( db:: accounts:: account_count( & app.db) .await.unwrap ( ) , 3 ) ;
83940d53 Andy McCall 1 month ago ↶
1056
1057
1058
let r = get ( app, " /login/google" , None ) .await;
1059
assert! ( r.location.clone ( ) .unwrap ( ) .contains ( " prompt=select_account" ) ) ;
1060
let ( cookie, state) = state_of ( & r) ;
1061
let ok = send_with ( app, " GET" , & format! ( " /login/google/callback?code=c3&state={state} " ) , Some ( & cookie) , None ) .await;
1062
assert_eq! ( ok.location.as_deref ( ) , Some ( " /" ) , " {}" , ok.body) ;
1063
let as_andy = ok.cookie.clone ( ) .unwrap ( ) ;
1064
let sec = get ( app, " /settings/security" , Some ( & as_andy) ) .await.body;
1065
assert! (
1066
sec.contains ( " Connected accounts" )
1067
& & sec.contains ( " Google: <strong>andy</strong>" )
1068
& & sec.contains ( " GitHub: <a href=\" /settings/security/connect/github\" >connect</a>" ) ,
1069
" {sec}"
1070
) ;
810896d4 Andy McCall 21 days ago ↶
1071
assert_eq! ( db:: accounts:: account_count( & app.db) .await.unwrap ( ) , 3 , " no fourth account" ) ;
83940d53 Andy McCall 1 month ago ↶
1072
1073
let csrf_s = csrf_of ( & sec) ;
1074
let r = post ( app, " /settings/security/disconnect/google" , Some ( & as_andy) , & format! ( " _csrf={csrf_s} " ) ) .await;
1075
assert! ( r.location.as_deref ( ) .unwrap_or ( " " ) .contains ( " notice=Google%20disconnected" ) , " {:?}" , r.location) ;
1076
assert! ( ! get ( app, " /settings/security" , Some ( & as_andy) ) .await.body.contains ( " Google: <strong>andy</strong>" ) ) ;
1077
let r = get ( app, " /settings/security/connect/google" , Some ( & as_andy) ) .await;
1078
assert_eq! ( r.status, StatusCode:: SEE_OTHER ) ;
1079
let ( cookie, state) = state_of ( & r) ;
1080
let both = format! ( " {cookie} ; oxyhub={as_andy} " ) ;
1081
let ok = send_with ( app, " GET" , & format! ( " /login/google/callback?code=c4&state={state} " ) , Some ( & both) , None ) .await;
1082
assert! ( ok.location.as_deref ( ) .unwrap_or ( " " ) .contains ( " notice=Google%20connected%20as%20andy" ) , " {:?} {}" , ok.location, ok.body) ;
1083
1084
1085
db:: accounts:: set_setting( & app.db, " registration_mode" , " closed" ) .await.unwrap ( ) ;
1086
sqlx:: query( " DELETE FROM account_logins WHERE provider = 'github'" ) .execute ( & app.db) .await.unwrap ( ) ;
1087
sqlx:: query( " UPDATE accounts SET email_verified = 0 WHERE username = 'octocat'" ) .execute ( & app.db) .await.unwrap ( ) ;
1088
let r = get ( app, " /login/github" , None ) .await;
1089
let ( cookie, state) = state_of ( & r) ;
1090
let no = send_with ( app, " GET" , & format! ( " /login/github/callback?code=c5&state={state} " ) , Some ( & cookie) , None ) .await;
1091
assert! ( no.location.as_deref ( ) .unwrap_or ( " " ) .contains ( " registration%20is%20closed" ) , " {:?}" , no.location) ;
1092
assert! ( no.cookie.is_none ( ) ) ;
89e89836 Andy McCall 1 month ago ↶
1093
}
e8e7158b Andy McCall 1 month ago ↶
1094
1095
# [ tokio ::test ( flavor = " multi_thread" ) ]
1096
async fn administration_is_sectioned_and_its_audit_log_pages ( ) {
1097
let h = hub ( ) .await;
1098
let app = & h.app;
1099
let andy = register ( app, " andy" , " " ) .await;
1100
let andy_row = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
1101
for i in 0 .. 60 {
1102
db:: accounts:: audit( & app.db, Some ( & andy_row) , " test.event" , & format! ( " t{i} " ) , " " ) .await.unwrap ( ) ;
1103
}
1104
let page = get ( app, " /admin" , Some ( & andy) ) .await.body;
1105
for id in [ " accounts" , " registration" , " organisations" , " signin" , " mail" , " variables" , " secrets" , " assistants" , " audit" ] {
1106
assert! ( page.contains ( & format! ( " href=\" #{id} \" " ) ) & & page.contains ( & format! ( " id=\" {id} \" " ) ) , " section {id}: {page}" ) ;
1107
}
1108
assert! ( page.contains ( " class=\" section-nav\" " ) ) ;
1109
assert! ( page.contains ( " >t59<" ) & & ! page.contains ( " >t5<" ) , " the newest page only" ) ;
1110
let i = page.find ( " audit_before=" ) .expect ( " an older link" ) + " audit_before=" .len ( ) ;
1111
let before: String = page[ i.. ] .chars ( ) .take_while ( | c | c.is_ascii_digit ( ) ) .collect ( ) ;
1112
let older = get ( app, & format! ( " /admin?audit_before={before} " ) , Some ( & andy) ) .await.body;
1113
assert! ( older.contains ( " >t5<" ) & & ! older.contains ( " >t59<" ) , " the next page: {older}" ) ;
1114
}
155cab14 Andy McCall 1 month ago ↶
1115
1116
# [ tokio ::test ( flavor = " multi_thread" ) ]
1117
async fn adding_an_assistant_offers_providers_and_only_models_the_endpoint_lists ( ) {
1118
let h = hub ( ) .await;
1119
let app = & h.app;
1120
let andy = register ( app, " andy" , " " ) .await;
1121
let router = axum:: Router:: new( )
1122
.route (
1123
" /v1/models" ,
1124
axum:: routing:: get( | headers : axum:: http:: HeaderMap| async move {
1125
assert_eq! ( headers.get ( " authorization" ) .and_then ( | v | v.to_str ( ) .ok ( ) ) , Some ( " Bearer sk-m" ) ) ;
1126
axum:: Json( serde_json:: json! ( { " data" : [
1127
{ " id" : " models/gemini-3-pro" } , { " id" : " gemini-3-flash" } , { " id" : " text-embedding-004" }
1128
] } ) )
1129
} ) ,
1130
)
1131
.route (
1132
" /v1/chat/completions" ,
1133
axum:: routing:: post( | | async { axum:: Json( serde_json:: json! ( { " choices" : [ { " message" : { " content" : " OK" } } ] } ) ) } ) ,
1134
) ;
1135
let listener = tokio:: net:: TcpListener:: bind( " 127.0.0.1:0" ) .await.unwrap ( ) ;
1136
let port = listener.local_addr ( ) .unwrap ( ) .port ( ) ;
1137
tokio:: spawn( async move { axum:: serve( listener, router) .await.unwrap ( ) } ) ;
1138
let ep = format! ( " http%3A%2F%2F127.0.0.1%3A{port} %2Fv1%2Fchat%2Fcompletions" ) ;
1139
1140
let page = get ( app, " /admin" , Some ( & andy) ) .await.body;
1141
assert! ( page.contains ( " data-preset-select" ) & & page.contains ( " Google Gemini" ) & & page.contains ( " Other (enter the endpoint)" ) , " {page}" ) ;
1142
assert! ( page.contains ( " LIST MODELS" ) ) ;
1143
let csrf = csrf_of ( & page) ;
1144
1145
let r =
1146
post ( app, " /admin/assistants/models" , Some ( & andy) , & format! ( " _csrf={csrf} &endpoint={ep} &api=chat-completions&token=sk-m" ) ) .await;
1147
let v: serde_json:: Value = serde_json:: from_str( & r.body) .unwrap ( ) ;
1148
assert_eq! ( v[ " models" ] , serde_json:: json! ( [ " gemini-3-flash" , " gemini-3-pro" ] ) , " {}" , r.body) ;
1149
1150
let r = post (
1151
app,
1152
" /admin/assistants" ,
1153
Some ( & andy) ,
1154
& format! ( " _csrf={csrf} &action=add&preset=gemini&endpoint={ep} &model=gemini-2.5-pro&token=sk-m&enabled=1" ) ,
1155
)
1156
.await;
1157
assert! ( r.body.contains ( " gemini-2.5-pro is not a model this endpoint offers" ) & & r.body.contains ( " gemini-3-pro" ) , " {}" , r.body) ;
1158
1159
let r = post (
1160
app,
1161
" /admin/assistants" ,
1162
Some ( & andy) ,
1163
& format! ( " _csrf={csrf} &action=add&preset=gemini&endpoint={ep} &model=gemini-3-pro&token=sk-m&enabled=1" ) ,
1164
)
1165
.await;
1166
assert! ( r.body.contains ( " Gemini added and answered: OK" ) , " {}" , r.body) ;
1167
let a = & oxygenhub:: assistants:: list( & app.db, oxygenhub:: levels:: Scope:: Site) .await.unwrap ( ) [ 0 ] ;
1168
assert_eq! ( ( a.name.as_str ( ) , a.api.as_str ( ) , a.model.as_str ( ) ) , ( " Gemini" , " chat-completions" , " gemini-3-pro" ) ) ;
1169
1170
let r =
1171
post ( app, " /admin/assistants/models" , Some ( & andy) , & format! ( " _csrf={csrf} &id={} &endpoint={ep} &api=chat-completions" , a.id) ) .await;
1172
assert! ( r.body.contains ( " gemini-3-flash" ) , " {}" , r.body) ;
1173
}