18e381ea Andy McCall 1 month ago ↶
1
2
3
4
5
use axum:: body:: Body;
6
use axum:: http:: { header, Request, StatusCode} ;
7
use http_body_util:: BodyExt;
8
use oxy_core:: repo:: Repo;
9
use oxygenhub:: config:: Config;
10
use oxygenhub:: db;
11
use oxygenhub:: repos:: RepoName;
12
use oxygenhub:: Shared;
13
use std:: path:: Path;
14
use tower:: ServiceExt;
15
16
fn hermetic ( ) {
17
static ONCE : std:: sync:: Once = std:: sync:: Once:: new( ) ;
18
static DIR : std:: sync:: OnceLock< tempfile:: TempDir> = std:: sync:: OnceLock:: new( ) ;
19
ONCE .call_once ( | | {
20
let d = tempfile:: tempdir( ) .unwrap ( ) ;
21
std:: env:: set_var( " OXY_HOME" , d.path ( ) ) ;
22
DIR .set ( d) .ok ( ) ;
23
} ) ;
24
}
25
26
fn write ( root : & Path, rel : & str , content : & [u8 ]) {
27
let p = root.join ( rel) ;
28
std:: fs:: create_dir_all( p.parent ( ) .unwrap ( ) ) .unwrap ( ) ;
29
std:: fs:: write( p, content) .unwrap ( ) ;
30
}
31
32
struct Hub {
33
app : Shared,
34
base : String,
35
_dir : tempfile:: TempDir,
36
}
37
38
async fn hub ( ) -> Hub {
39
hermetic ( ) ;
40
let dir = tempfile:: tempdir( ) .unwrap ( ) ;
41
let mut config = Config:: load( dir.path ( ) ) .unwrap ( ) ;
42
config.auth.push_tokens = vec! [ " site" .into ( ) ] ;
43
config.registration.mode = " open" .into ( ) ;
44
let listener = tokio:: net:: TcpListener:: bind( " 127.0.0.1:0" ) .await.unwrap ( ) ;
45
let port = listener.local_addr ( ) .unwrap ( ) .port ( ) ;
46
config.server.base_url = format! ( " http://127.0.0.1:{port} " ) ;
47
let app = oxygenhub:: build( config) .await.unwrap ( ) ;
48
let router = oxygenhub:: router( app.clone ( ) ) ;
49
tokio:: spawn( async move { axum:: serve( listener, router) .await.unwrap ( ) } ) ;
50
Hub { app, base: format! ( " http://127.0.0.1:{port} " ) , _dir: dir }
51
}
52
53
struct Res {
54
status : StatusCode,
55
body : String,
56
cookie : Option< String> ,
57
location : Option< String> ,
58
}
59
60
async fn send ( app : & Shared, method : & str , path : & str , cookie : Option< & str > , form : Option< & str > ) -> Res {
61
let mut req = Request:: builder( ) .method ( method) .uri ( path) ;
62
if let Some ( c) = cookie {
63
req = req.header ( header:: COOKIE , format! ( " oxyhub={c} " ) ) ;
64
}
65
let body = match form {
66
Some ( f) => {
67
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
68
Body:: from( f.to_string ( ) )
69
}
70
None => Body:: empty( ) ,
71
} ;
72
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req.body ( body) .unwrap ( ) ) .await.unwrap ( ) ;
73
let status = res.status ( ) ;
74
let cookie = res
75
.headers ( )
76
.get ( header:: SET_COOKIE )
77
.and_then ( | v | v.to_str ( ) .ok ( ) )
78
.and_then ( | v | v.split ( ' ;' ) .next ( ) )
79
.and_then ( | kv | kv.split_once ( ' =' ) )
80
.map ( | ( _, v ) | v.to_string ( ) ) ;
81
let location = res.headers ( ) .get ( header:: LOCATION ) .and_then ( | v | v.to_str ( ) .ok ( ) ) .map ( String :: from) ;
82
let bytes = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ;
83
Res { status, body: String :: from_utf8_lossy( & bytes) .into_owned ( ) , cookie, location }
84
}
85
86
async fn get ( app : & Shared, path : & str , cookie : Option< & str > ) -> Res {
87
send ( app, " GET" , path, cookie, None ) .await
88
}
89
90
async fn post ( app : & Shared, path : & str , cookie : Option< & str > , form : & str ) -> Res {
91
send ( app, " POST" , path, cookie, Some ( form) ) .await
92
}
93
94
async fn register ( app : & Shared, user : & str ) -> String {
95
let r = post ( app, " /register" , None , & format! ( " username={user} &email={user} %40example.com&password=correct-horse-battery" ) ) .await;
96
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
97
r.cookie.unwrap ( )
98
}
99
100
fn csrf_of ( page : & str ) -> String {
101
let i = page.find ( " name=\" _csrf\" value=\" " ) .expect ( " csrf field" ) + " name=\" _csrf\" value=\" " .len ( ) ;
102
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
103
}
104
105
# [ tokio ::test ( flavor = " multi_thread" ) ]
106
async fn issues_notifications_and_search ( ) {
107
let h = hub ( ) .await;
108
let app = & h.app;
109
let andy = register ( app, " andy" ) .await;
110
let bob = register ( app, " bob" ) .await;
111
let repo = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
112
app.repos.create ( & repo, None ) .unwrap ( ) ;
113
db:: repos:: create( & app.db, " andy" , " demo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
114
115
116
let labels_page = get ( app, " /andy/demo/labels" , Some ( & andy) ) .await;
117
let csrf_a = csrf_of ( & labels_page.body) ;
118
assert! ( post ( app, " /andy/demo/labels" , Some ( & andy) , & format! ( " _csrf={csrf_a} &name=bug&colour=%23ff0000&description=Something+broke" ) )
119
.await
120
.body
121
.contains ( " Saved bug" ) ) ;
122
assert! ( post ( app, " /andy/demo/milestones" , Some ( & andy) , & format! ( " _csrf={csrf_a} &title=v1&description=First&due=2027-01-01" ) )
123
.await
124
.body
125
.contains ( " v1" ) ) ;
126
assert_eq! ( get ( app, " /andy/demo/labels" , Some ( & bob) ) .await.status, StatusCode:: OK , " readers see labels" ) ;
127
assert! ( ! get ( app, " /andy/demo/labels" , Some ( & bob) ) .await.body.contains ( " Add or change" ) , " but cannot edit them" ) ;
128
129
130
let overview = get ( app, " /andy/demo" , Some ( & bob) ) .await;
131
let csrf_b = csrf_of ( & overview.body) ;
132
assert_eq! ( post ( app, " /andy/demo/watch" , Some ( & bob) , & format! ( " _csrf={csrf_b} " ) ) .await.status, StatusCode:: SEE_OTHER ) ;
133
assert! ( get ( app, " /andy/demo" , Some ( & bob) ) .await.body.contains ( " UNWATCH" ) ) ;
134
let new_page = get ( app, " /andy/demo/issues/new" , Some ( & andy) ) .await;
135
let csrf_a = csrf_of ( & new_page.body) ;
136
let r = post (
137
app,
138
" /andy/demo/issues/new" ,
139
Some ( & andy) ,
140
& format! ( " _csrf={csrf_a} &title=Crash+on+start&body=cc+%40bob+see+%23999+and+deadbeefcafe1234" ) ,
141
)
142
.await;
143
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
144
assert_eq! ( r.location.as_deref ( ) , Some ( " /andy/demo/issue/1" ) ) ;
145
let page = get ( app, " /andy/demo/issue/1" , Some ( & bob) ) .await;
146
assert! ( page.body.contains ( " Crash on start" ) ) ;
147
assert! ( page.body.contains ( " href=\" /bob\" " ) , " mention linked: {}" , page.body) ;
148
assert! ( page.body.contains ( " /andy/demo/change/deadbeefcafe1234" ) , " hash linked: {}" , page.body) ;
149
assert! ( page.body.contains ( " #999" ) & & ! page.body.contains ( " issue/999" ) , " unknown numbers are not linked" ) ;
150
let unread = db:: notify:: unread( & app.db, db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id) .await.unwrap ( ) ;
151
assert_eq! ( unread, 1 , " one notification, not one per reason" ) ;
152
let inbox = get ( app, " /notifications" , Some ( & bob) ) .await;
153
assert! ( inbox.body.contains ( " andy/demo#1: Crash on start" ) , " {}" , inbox.body) ;
154
assert! ( get ( app, " /andy/demo" , Some ( & bob) ) .await.body.contains ( " class=\" count\" >1<" ) , " bell shows the count" ) ;
155
156
157
let page = get ( app, " /andy/demo/issue/1" , Some ( & andy) ) .await;
158
let csrf_a = csrf_of ( & page.body) ;
159
let label_id: i64 = sqlx:: query_scalar( " SELECT id FROM labels WHERE name = 'bug'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
160
let r = post ( app, " /andy/demo/issue/1/labels" , Some ( & andy) , & format! ( " _csrf={csrf_a} &label={label_id} " ) ) .await;
161
assert! ( r.body.contains ( " >bug<" ) , " {}" , r.body) ;
162
let r = post ( app, " /andy/demo/issue/1/assign" , Some ( & andy) , & format! ( " _csrf={csrf_a} &username=bob&action=add" ) ) .await;
163
assert! ( r.body.contains ( " href=\" /bob\" >bob</a>" ) , " {}" , r.body) ;
164
let ms_id: i64 = sqlx:: query_scalar( " SELECT id FROM milestones WHERE title = 'v1'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
165
let r = post ( app, " /andy/demo/issue/1/milestone" , Some ( & andy) , & format! ( " _csrf={csrf_a} &milestone={ms_id} " ) ) .await;
166
assert! ( r.body.contains ( " >v1<" ) , " {}" , r.body) ;
167
assert! ( post ( app, " /andy/demo/issue/1/state" , Some ( & andy) , & format! ( " _csrf={csrf_a} &action=pin" ) ) .await.body.contains ( " UNPIN" ) ) ;
168
assert_eq! (
169
post (
170
app,
171
" /andy/demo/issue/1/state" ,
172
Some ( & bob) ,
173
& format! ( " _csrf={} &action=lock" , csrf_of ( & get ( app, " /andy/demo/issue/1" , Some ( & bob) ) .await.body) )
174
)
175
.await
176
.status,
177
StatusCode:: NOT_FOUND ,
178
" bob cannot lock"
179
) ;
180
181
let list = get ( app, " /andy/demo/issues?q=label:bug+assignee:bob+milestone:v1" , None ) .await;
182
assert! ( list.body.contains ( " Crash on start" ) , " {}" , list.body) ;
183
assert! ( ! get ( app, " /andy/demo/issues?q=label:feature" , None ) .await.body.contains ( " Crash on start" ) ) ;
184
assert! ( get ( app, " /andy/demo/milestones" , None ) .await.body.contains ( " 1 open, 0 closed" ) ) ;
185
186
187
let csrf_b = csrf_of ( & get ( app, " /andy/demo/issue/1" , Some ( & bob) ) .await.body) ;
188
let r = post ( app, " /andy/demo/issue/1/comment" , Some ( & bob) , & format! ( " _csrf={csrf_b} &body=Reproduced+with+the+flux+capacitor" ) ) .await;
189
assert! ( r.body.contains ( " flux capacitor" ) , " {}" , r.body) ;
190
assert! ( get ( app, " /notifications" , Some ( & andy) ) .await.body.contains ( " andy/demo#1" ) ) ;
191
let s = get ( app, " /search?q=capacitor" , None ) .await;
192
assert! ( s.body.contains ( " Crash on start" ) & & s.body.contains ( " <mark>capacitor</mark>" ) , " {}" , s.body) ;
193
let s = get ( app, " /search?q=demo" , None ) .await;
194
assert! ( s.body.contains ( " /andy/demo\" " ) , " repository hit: {}" , s.body) ;
195
196
let r = post ( app, " /andy/demo/issue/1/comment" , Some ( & bob) , & format! ( " _csrf={csrf_b} &body=closing&close=completed" ) ) .await;
197
assert! ( r.body.contains ( " >open<" ) | | r.body.contains ( " head\" >open" ) , " still open: {}" , r.body) ;
198
let r = post ( app, " /andy/demo/issue/1/comment" , Some ( & andy) , & format! ( " _csrf={csrf_a} &body=fixed&close=completed" ) ) .await;
199
assert! ( r.body.contains ( " closed" ) , " {}" , r.body) ;
200
assert! ( get ( app, " /andy/demo/issues?state=closed" , None ) .await.body.contains ( " Crash on start" ) ) ;
201
202
203
let csrf_a = csrf_of ( & get ( app, " /andy/demo/issues/new" , Some ( & andy) ) .await.body) ;
204
assert_eq! (
205
post ( app, " /andy/demo/issues/new" , Some ( & andy) , & format! ( " _csrf={csrf_a} &title=Typo&body=" ) ) .await.location.as_deref ( ) ,
206
Some ( " /andy/demo/issue/2" )
207
) ;
208
let work = tempfile:: tempdir( ) .unwrap ( ) ;
209
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
210
client.config.set ( " remote.origin.url" , & format! ( " {} /andy/demo" , h.base) ) .unwrap ( ) ;
211
client.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
212
client.save_config ( ) .unwrap ( ) ;
213
write ( work.path ( ) , " a.txt" , b " a\n " ) ;
214
client.snapshot ( " t" ) .unwrap ( ) ;
215
client.commit ( " Fix the typo\n \n Closes #2" , & [ ] ) .unwrap ( ) ;
216
assert! ( client.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
217
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
218
let issue2 = db:: issues:: get( & app.db, db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) .id, 2 ) .await.unwrap ( ) .unwrap ( ) ;
219
assert_eq! ( issue2.state, " closed" , " closed by the landed change" ) ;
220
assert! ( get ( app, " /andy/demo/issue/2" , None ) .await.body.contains ( " closed by change" ) ) ;
221
222
223
client.bookmark_create ( " feat" , None ) .unwrap ( ) ;
224
client.bookmark_switch ( " feat" , false ) .unwrap ( ) ;
225
write ( work.path ( ) , " b.txt" , b " b\n " ) ;
226
client.snapshot ( " t" ) .unwrap ( ) ;
227
client.commit ( " feat" , & [ ] ) .unwrap ( ) ;
228
assert! ( client.push_bookmark ( " origin" , " feat" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
229
let csrf_a = csrf_of ( & get ( app, " /andy/demo/proposals/new?source=feat&target=main" , Some ( & andy) ) .await.body) ;
230
let r = post ( app, " /andy/demo/proposals/new" , Some ( & andy) , & format! ( " _csrf={csrf_a} &title=Feat&target=main&source=feat" ) ) .await;
231
assert_eq! ( r.location.as_deref ( ) , Some ( " /andy/demo/proposal/3" ) , " {}" , r.body) ;
232
assert_eq! ( get ( app, " /andy/demo/issue/3" , None ) .await.location.as_deref ( ) , Some ( " /andy/demo/proposal/3" ) ) ;
233
234
let r = post ( app, " /andy/demo/issue/2/comment" , Some ( & andy) , & format! ( " _csrf={csrf_a} &body=see+%233" ) ) .await;
235
assert! ( r.body.contains ( " href=\" /andy/demo/proposal/3\" " ) , " {}" , r.body) ;
236
237
assert! ( get ( app, " /notifications" , Some ( & bob) ) .await.body.contains ( " andy/demo#3: Feat" ) ) ;
238
let csrf_b = csrf_of ( & get ( app, " /notifications" , Some ( & bob) ) .await.body) ;
239
let r = post ( app, " /notifications/read" , Some ( & bob) , & format! ( " _csrf={csrf_b} " ) ) .await;
240
assert! ( r.body.contains ( " 0 unread" ) , " {}" , r.body) ;
241
242
let r = post ( app, " /settings/notifications" , Some ( & bob) , & format! ( " _csrf={csrf_b} &email=1" ) ) .await;
243
assert! ( r.body.contains ( " Email notifications are on" ) ) ;
244
let key: String = sqlx:: query_scalar( " SELECT unsubscribe_key FROM accounts WHERE username = 'bob'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
245
assert_eq! ( get ( app, & format! ( " /settings/notifications?unsubscribe={key} " ) , None ) .await.status, StatusCode:: OK ) ;
246
let on: bool = sqlx:: query_scalar( " SELECT email_notifications FROM accounts WHERE username = 'bob'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
247
assert! ( ! on) ;
248
}
25ab8c73 Andy McCall 1 month ago ↶
249
250
# [ tokio ::test ( flavor = " multi_thread" ) ]
251
async fn dashboard_shows_what_needs_the_viewer ( ) {
252
let h = hub ( ) .await;
253
let app = & h.app;
254
let andy = register ( app, " andy" ) .await;
255
let bob = register ( app, " bob" ) .await;
256
for ( name, vis) in [ ( " demo" , hub_api:: Visibility:: Public) , ( " secret" , hub_api:: Visibility:: Private) ] {
257
app.repos.create ( & RepoName:: parse( & format! ( " andy/{name} " ) ) .unwrap ( ) , None ) .unwrap ( ) ;
258
db:: repos:: create( & app.db, " andy" , name, " " , vis) .await.unwrap ( ) ;
259
}
260
let demo = db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
261
let secret = db:: repos:: get( & app.db, " andy" , " secret" ) .await.unwrap ( ) .unwrap ( ) ;
262
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
263
for ( repo, title) in [ ( demo.id, " Faster startup" ) , ( secret.id, " Hidden work" ) ] {
264
let p = db:: proposals:: create(
265
& app.db,
266
db:: proposals:: NewProposal {
267
repo_id: repo,
268
title,
269
body: " " ,
270
author_id: Some ( bob_id) ,
271
author: " bob" ,
272
draft: false ,
273
target: " main" ,
274
source_kind: " bookmark" ,
275
source_repo_id: Some ( repo) ,
276
source_bookmark: " fast" ,
277
source_changes: " " ,
278
} ,
279
)
280
.await
281
.unwrap ( ) ;
282
db:: proposals:: add_event( & app.db, p.id, " bob" , " opened" , " " ) .await.unwrap ( ) ;
283
}
284
285
286
let anon = get ( app, " /" , None ) .await;
287
assert! ( anon.body.contains ( " andy /" ) & & ! anon.body.contains ( " Dashboard <small>" ) , " {}" , anon.body) ;
288
289
290
let page = get ( app, " /" , Some ( & andy) ) .await;
291
assert_eq! ( page.status, StatusCode:: OK ) ;
292
assert! ( page.body.contains ( " Waiting for your review <small>2</small>" ) , " {}" , page.body) ;
293
assert! ( page.body.contains ( " /andy/demo/proposal/1" ) & & page.body.contains ( " /andy/secret/proposal/1" ) ) ;
294
assert! ( page.body.contains ( " Your repositories" ) ) ;
295
assert! ( page.body.contains ( " opened proposal" ) , " activity feed: {}" , page.body) ;
296
297
298
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
299
let p = db:: proposals:: get( & app.db, demo.id, 1 ) .await.unwrap ( ) .unwrap ( ) ;
300
db:: proposals:: add_review( & app.db, p.id, andy_id, " andy" , " approve" , " " ) .await.unwrap ( ) ;
301
assert! ( get ( app, " /" , Some ( & andy) ) .await.body.contains ( " Waiting for your review <small>1</small>" ) ) ;
302
303
304
let page = get ( app, " /" , Some ( & bob) ) .await;
305
assert! ( page.body.contains ( " Your proposals <small>1</small>" ) , " {}" , page.body) ;
306
assert! ( ! page.body.contains ( " Hidden work" ) ) ;
307
assert! ( page.body.contains ( " Waiting for your review <small>0</small>" ) ) ;
308
assert! ( page.body.contains ( " Faster startup" ) ) ;
309
let feed = & page.body[ page.body.find ( " Recent activity" ) .unwrap ( ) .. ] ;
310
assert! ( ! feed.contains ( " andy/secret" ) , " private activity leaked: {feed}" ) ;
311
312
313
assert! ( get ( app, " /explore" , Some ( & bob) ) .await.body.contains ( " andy /" ) ) ;
314
}
4edcf240 Andy McCall 1 month ago ↶
315
316
# [ tokio ::test ( flavor = " multi_thread" ) ]
317
async fn empty_repository_shows_a_quickstart ( ) {
318
let h = hub ( ) .await;
319
let app = & h.app;
320
let andy = register ( app, " andy" ) .await;
321
let bob = register ( app, " bob" ) .await;
322
app.repos.create ( & RepoName:: parse( " andy/fresh" ) .unwrap ( ) , None ) .unwrap ( ) ;
323
db:: repos:: create( & app.db, " andy" , " fresh" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
324
325
let page = get ( app, " /andy/fresh" , Some ( & andy) ) .await;
326
assert! ( page.body.contains ( " This repository is empty" ) , " {}" , page.body) ;
327
let clone_url = format! ( " {} /andy/fresh" , h.base) ;
328
assert! ( page.body.contains ( & format! ( " oxy remote add hub {clone_url} " ) ) , " {}" , page.body) ;
329
assert! ( page.body.contains ( " oxy config remote.hub.token YOUR-TOKEN" ) ) ;
330
assert! ( page.body.contains ( " oxy push hub" ) ) ;
331
assert! (
332
page.body.contains ( " mkdir fresh && cd fresh" ) | | page.body.contains ( " mkdir fresh && cd fresh" ) ,
333
" {}" ,
334
page.body
335
) ;
336
assert! ( page.body.contains ( " oxy git import --branch main" ) ) ;
337
assert! ( ! page.body.contains ( " oxyhub-ca.pem" ) , " no certificate step without a self-signed certificate" ) ;
338
339
340
let page = get ( app, " /andy/fresh" , Some ( & bob) ) .await;
341
assert! ( page.body.contains ( " Nothing has been pushed yet" ) & & ! page.body.contains ( " oxy push hub" ) , " {}" , page.body) ;
342
343
344
let work = tempfile:: tempdir( ) .unwrap ( ) ;
345
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
346
client.config.set ( " remote.hub.url" , & clone_url) .unwrap ( ) ;
347
client.config.set ( " remote.hub.token" , " site" ) .unwrap ( ) ;
348
client.save_config ( ) .unwrap ( ) ;
349
write ( work.path ( ) , " README.md" , b " # fresh\n " ) ;
350
client.commit ( " First change" , & [ ] ) .unwrap ( ) ;
351
assert! ( client.push ( " hub" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
352
let page = get ( app, " /andy/fresh" , Some ( & andy) ) .await;
353
assert! ( ! page.body.contains ( " This repository is empty" ) & & page.body.contains ( " README.md" ) , " {}" , page.body) ;
354
}
c40c2d3e Andy McCall 1 month ago ↶
355
356
# [ tokio ::test ( flavor = " multi_thread" ) ]
357
async fn theme_is_chosen_in_settings_and_remembered_in_a_cookie ( ) {
358
let h = hub ( ) .await;
359
let app = & h.app;
360
let andy = register ( app, " andy" ) .await;
361
let page = get ( app, " /settings" , Some ( & andy) ) .await;
362
assert! ( page.body.contains ( " Appearance" ) ) ;
363
assert! ( page.body.contains ( r # "value="dark" checked"# ) , " dark by default: {}" , page.body) ;
364
assert! ( page.body.contains ( " data-theme-toggle" ) , " the header has the switch" ) ;
365
let csrf = csrf_of ( & page.body) ;
366
let r = post ( app, " /settings/appearance" , Some ( & andy) , & format! ( " _csrf={csrf} &theme=light" ) ) .await;
367
assert_eq! ( r.status, StatusCode:: SEE_OTHER ) ;
368
assert_eq! ( r.cookie.as_deref ( ) , Some ( " light" ) ) ;
369
assert_eq! ( r.location.as_deref ( ) , Some ( " /settings" ) ) ;
370
let page = send_with_theme ( app, & andy, " light" ) .await;
371
assert! ( page.contains ( r # "value="light" checked"# ) , " the form shows the cookie's theme" ) ;
372
assert_eq! ( post ( app, " /settings/appearance" , Some ( & andy) , & format! ( " _csrf={csrf} &theme=purple" ) ) .await.status, StatusCode:: BAD_REQUEST ) ;
373
assert_eq! ( post ( app, " /settings/appearance" , Some ( & andy) , " _csrf=nope&theme=light" ) .await.status, StatusCode:: BAD_REQUEST ) ;
374
}
375
376
async fn send_with_theme ( app : & Shared, session : & str , theme : & str ) -> String {
377
let req =
378
Request:: builder( ) .uri ( " /settings" ) .header ( header:: COOKIE , format! ( " oxyhub={session} ; theme={theme} " ) ) .body ( Body:: empty( ) ) .unwrap ( ) ;
379
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
380
String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( )
381
}
10174045 Andy McCall 1 month ago ↶
382
383
# [ tokio ::test ( flavor = " multi_thread" ) ]
384
async fn profile_and_organisation_pages ( ) {
385
let h = hub ( ) .await;
386
let app = & h.app;
387
let andy = register ( app, " andy" ) .await;
388
let bob = register ( app, " bob" ) .await;
389
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
390
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
391
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
392
db:: repos:: create( & app.db, " andy" , " demo" , " A demo" , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
393
app.repos.create ( & RepoName:: parse( " andy/hidden" ) .unwrap ( ) , None ) .unwrap ( ) ;
394
db:: repos:: create( & app.db, " andy" , " hidden" , " " , hub_api:: Visibility:: Private) .await.unwrap ( ) ;
395
396
397
let csrf = csrf_of ( & get ( app, " /settings" , Some ( & andy) ) .await.body) ;
398
let r = post ( app, " /settings" , Some ( & andy) , & format! ( " _csrf={csrf} &display_name=Andy+McCall&email=a%40example.com&bio=Builds+%3Cb%3Eoxy%3C%2Fb%3E&website=javascript%3Aalert(1)&location=Earth" ) ) .await;
399
assert! ( r.body.contains ( " must be an http" ) , " {}" , r.body) ;
400
let r = post ( app, " /settings" , Some ( & andy) , & format! ( " _csrf={csrf} &display_name=Andy+McCall&email=a%40example.com&bio=Builds+%3Cb%3Eoxy%3C%2Fb%3E&website=https%3A%2F%2Fexample.org&location=Earth" ) ) .await;
401
assert! ( r.body.contains ( " Profile saved" ) , " {}" , r.body) ;
402
403
let org = db:: accounts:: create_org( & app.db, " retro" , " Retro Club" , andy_id) .await.unwrap ( ) ;
404
db:: accounts:: set_org_member( & app.db, org.id, bob_id, " member" ) .await.unwrap ( ) ;
405
let team = db:: accounts:: create_team( & app.db, org.id, " core" ) .await.unwrap ( ) ;
406
db:: accounts:: add_team_member( & app.db, team.id, bob_id) .await.unwrap ( ) ;
407
408
let page = get ( app, " /andy" , Some ( & bob) ) .await;
409
assert_eq! ( page.status, StatusCode:: OK ) ;
410
for want in [ " Andy McCall" , " Earth" , r # "href="https://example.org""# , " /avatar/andy" , " Signing keys" , " /retro" , " demo" ] {
411
assert! ( page.body.contains ( want) , " profile lacks {want}: {}" , page.body) ;
412
}
413
assert! ( ! page.body.contains ( " <b>oxy</b>" ) , " the bio is escaped" ) ;
414
assert! ( ! page.body.contains ( " [email protected] " ) , " the email stays private" ) ;
415
assert! ( ! page.body.contains ( " /andy/hidden" ) , " bob cannot see the private repository" ) ;
416
assert! ( ! page.body.contains ( " edit profile" ) , " only the owner gets the edit link" ) ;
417
assert! ( get ( app, " /andy" , Some ( & andy) ) .await.body.contains ( " /andy/hidden" ) , " andy sees his own private repository" ) ;
418
419
let page = get ( app, " /retro" , Some ( & bob) ) .await;
420
assert_eq! ( page.status, StatusCode:: OK ) ;
421
for want in [ " Retro Club" , " organisation" , " Members <small>2</small>" , " core" , " 1 member" ] {
422
assert! ( page.body.contains ( want) , " org page lacks {want}: {}" , page.body) ;
423
}
424
assert! ( ! page.body.contains ( " /orgs/retro/settings" ) , " bob is not an owner" ) ;
425
assert! ( get ( app, " /retro" , Some ( & andy) ) .await.body.contains ( " /orgs/retro/settings" ) ) ;
426
427
let svg = get ( app, " /avatar/andy" , None ) .await;
428
assert_eq! ( svg.status, StatusCode:: OK ) ;
429
assert! ( svg.body.starts_with ( " <svg" ) ) ;
430
assert_eq! ( svg.body, get ( app, " /avatar/ANDY" , None ) .await.body) ;
431
assert_eq! ( get ( app, " /nobody-here" , None ) .await.status, StatusCode:: NOT_FOUND ) ;
432
}
9df855f2 Andy McCall 1 month ago ↶
433
434
# [ tokio ::test ( flavor = " multi_thread" ) ]
435
async fn diffs_mark_words_hide_whitespace_and_collapse ( ) {
436
let h = hub ( ) .await;
437
let app = & h.app;
438
let andy = register ( app, " andy" ) .await;
439
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
440
db:: repos:: create( & app.db, " andy" , " demo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
441
let work = tempfile:: tempdir( ) .unwrap ( ) ;
442
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
443
client.config.set ( " remote.hub.url" , & format! ( " {} /andy/demo" , h.base) ) .unwrap ( ) ;
444
client.config.set ( " remote.hub.token" , " site" ) .unwrap ( ) ;
445
client.save_config ( ) .unwrap ( ) ;
446
write ( work.path ( ) , " main.rs" , b " fn main() {\n setup();\n let answer = 42;\n }\n " ) ;
447
client.commit ( " first" , & [ ] ) .unwrap ( ) ;
448
write ( work.path ( ) , " main.rs" , b " fn main() {\n setup();\n let answer = 43;\n }\n " ) ;
449
let second = client.commit ( " reindent and change the answer" , & [ ] ) .unwrap ( ) ;
450
assert! ( client.push ( " hub" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
451
452
let url = format! ( " /andy/demo/change/{} " , second.to_hex ( ) ) ;
453
let page = get ( app, & url, Some ( & andy) ) .await;
454
assert_eq! ( page.status, StatusCode:: OK ) ;
455
assert! ( page.body.contains ( r # "<details class="file-diff""# ) , " files collapse: {}" , page.body) ;
456
assert! ( page.body.contains ( r # "<span class="wx">43</span>"# ) , " the changed word is marked: {}" , page.body) ;
457
assert! ( page.body.contains ( " hide whitespace changes" ) ) ;
458
assert! ( page.body.contains ( " data-diff-mode=\" split\" " ) ) ;
459
let adds = | b : & str | b.matches ( r # "<tr class="add">"# ) .count ( ) ;
460
assert_eq! ( adds ( & page.body) , 2 , " re-indent and answer both changed" ) ;
461
462
let quiet = get ( app, & format! ( " {url} ?w=1" ) , Some ( & andy) ) .await;
463
assert_eq! ( adds ( & quiet.body) , 1 , " only the answer changed ignoring whitespace: {}" , quiet.body) ;
464
assert! ( quiet.body.contains ( " show whitespace changes" ) ) ;
465
466
assert! ( quiet.body.contains ( " ?w=0" ) , " the toggle leads back" ) ;
467
}
07b1a5bb Andy McCall 1 month ago ↶
468
469
# [ tokio ::test ( flavor = " multi_thread" ) ]
470
async fn fork_and_propose_upstream ( ) {
471
let h = hub ( ) .await;
472
let app = & h.app;
473
let andy = register ( app, " andy" ) .await;
474
let bob = register ( app, " bob" ) .await;
475
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
476
db:: repos:: create( & app.db, " andy" , " demo" , " The demo" , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
477
let work = tempfile:: tempdir( ) .unwrap ( ) ;
478
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
479
client.config.set ( " remote.hub.url" , & format! ( " {} /andy/demo" , h.base) ) .unwrap ( ) ;
480
client.config.set ( " remote.hub.token" , " site" ) .unwrap ( ) ;
481
client.save_config ( ) .unwrap ( ) ;
482
write ( work.path ( ) , " README.md" , b " # demo\n " ) ;
483
client.commit ( " first" , & [ ] ) .unwrap ( ) ;
484
assert! ( client.push ( " hub" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
485
486
487
let page = get ( app, " /andy/demo" , Some ( & bob) ) .await;
488
assert! ( page.body.contains ( " /andy/demo/fork" ) , " a fork button: {}" , page.body) ;
489
let csrf = csrf_of ( & get ( app, " /andy/demo/fork" , Some ( & bob) ) .await.body) ;
490
let r = post ( app, " /andy/demo/fork" , Some ( & bob) , & format! ( " _csrf={csrf} &owner=andy&name=other" ) ) .await;
491
assert! ( r.body.contains ( " cannot create repositories under that owner" ) , " {}" , r.body) ;
492
let r = post ( app, " /andy/demo/fork" , Some ( & bob) , & format! ( " _csrf={csrf} &owner=bob&name=demo" ) ) .await;
493
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
494
assert_eq! ( r.location.as_deref ( ) , Some ( " /bob/demo" ) ) ;
495
let forked = get ( app, " /bob/demo" , Some ( & bob) ) .await;
496
assert! ( forked.body.contains ( " README.md" ) & & forked.body.contains ( " forked from" ) , " {}" , forked.body) ;
497
assert! ( forked.body.contains ( " propose to andy/demo" ) ) ;
498
assert! ( get ( app, " /andy/demo" , None ) .await.body.contains ( " 1 fork" ) ) ;
499
assert! ( get ( app, " /andy/demo/forks" , None ) .await.body.contains ( " /bob/demo" ) ) ;
500
let fork_row = db:: repos:: get( & app.db, " bob" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
501
let up_row = db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
502
assert_eq! ( fork_row.forked_from, Some ( up_row.id) ) ;
503
assert_eq! ( fork_row.description, " The demo" ) ;
504
let fork_repo = app.repos.open ( & RepoName:: parse( " bob/demo" ) .unwrap ( ) ) .unwrap ( ) ;
505
let up_repo = app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) ;
506
assert_ne! ( fork_repo.id ( ) .unwrap ( ) , up_repo.id ( ) .unwrap ( ) , " a fork is a repository of its own" ) ;
507
assert_eq! ( fork_repo.bookmark_heads ( ) .unwrap ( ) , up_repo.bookmark_heads ( ) .unwrap ( ) ) ;
508
assert_eq! ( fork_repo.fsck ( ) .unwrap ( ) .errors ( ) , 0 ) ;
509
510
511
let bw = tempfile:: tempdir( ) .unwrap ( ) ;
512
let mut mine = Repo:: clone( & format! ( " {} /bob/demo" , h.base) , & bw.path ( ) .join ( " c" ) ) .unwrap ( ) ;
513
mine.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
514
mine.save_config ( ) .unwrap ( ) ;
515
mine.bookmark_create ( " feat" , None ) .unwrap ( ) ;
516
mine.bookmark_switch ( " feat" , false ) .unwrap ( ) ;
517
write ( & bw.path ( ) .join ( " c" ) , " feature.txt" , b " new\n " ) ;
518
mine.commit ( " Add a feature" , & [ ] ) .unwrap ( ) ;
519
assert! ( mine.push_bookmark ( " origin" , " feat" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
520
let csrf = csrf_of ( & get ( app, " /andy/demo/proposals/new?source=bob/demo:feat" , Some ( & bob) ) .await.body) ;
521
let r =
522
post ( app, " /andy/demo/proposals/new" , Some ( & bob) , & format! ( " _csrf={csrf} &title=From+my+fork&target=main&source=bob%2Fdemo%3Afeat" ) )
523
.await;
524
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
525
let p = get ( app, & r.location.unwrap ( ) , Some ( & andy) ) .await;
526
assert! ( p.body.contains ( " From my fork" ) & & p.body.contains ( " fork bob/demo" ) , " {}" , p.body) ;
527
let files = get ( app, " /andy/demo/proposal/1?tab=files" , Some ( & andy) ) .await;
528
assert! ( files.body.contains ( " feature.txt" ) , " {}" , files.body) ;
529
}
1553bfc9 Andy McCall 1 month ago ↶
530
531
# [ tokio ::test ( flavor = " multi_thread" ) ]
532
async fn stars_topics_and_explore ( ) {
533
let h = hub ( ) .await;
534
let app = & h.app;
535
let andy = register ( app, " andy" ) .await;
536
let bob = register ( app, " bob" ) .await;
537
for ( name, desc) in [ ( " alpha" , " An emulator" ) , ( " beta" , " A compiler" ) , ( " gamma" , " Retro games" ) ] {
538
app.repos.create ( & RepoName:: parse( & format! ( " andy/{name} " ) ) .unwrap ( ) , None ) .unwrap ( ) ;
539
db:: repos:: create( & app.db, " andy" , name, desc, hub_api:: Visibility:: Public) .await.unwrap ( ) ;
540
}
541
542
543
let csrf = csrf_of ( & get ( app, " /andy/gamma/settings" , Some ( & andy) ) .await.body) ;
544
let r = post (
545
app,
546
" /andy/gamma/settings" ,
547
Some ( & andy) ,
548
& format! ( " _csrf={csrf} &description=Retro+games&default_bookmark=main&visibility=public&topics=c%2B%2B" ) ,
549
)
550
.await;
551
assert! ( r.body.contains ( " is not a topic" ) , " {}" , r.body) ;
552
let r = post (
553
app,
554
" /andy/gamma/settings" ,
555
Some ( & andy) ,
556
& format! ( " _csrf={csrf} &description=Retro+games&default_bookmark=main&visibility=public&topics=Retro%2C+6502" ) ,
557
)
558
.await;
559
assert! ( r.body.contains ( " Saved" ) , " {}" , r.body) ;
560
assert_eq! ( db:: repos:: get( & app.db, " andy" , " gamma" ) .await.unwrap ( ) .unwrap ( ) .topics, " retro 6502" ) ;
561
let page = get ( app, " /andy/gamma" , None ) .await;
562
assert! ( page.body.contains ( r # "href="/explore?topic=retro""# ) , " {}" , page.body) ;
563
564
565
for ( who, repo) in [ ( & bob, " beta" ) , ( & bob, " gamma" ) , ( & andy, " gamma" ) , ( & andy, " alpha" ) ] {
566
let csrf = csrf_of ( & get ( app, & format! ( " /andy/{repo} " ) , Some ( who) ) .await.body) ;
567
assert_eq! ( post ( app, & format! ( " /andy/{repo} /star" ) , Some ( who) , & format! ( " _csrf={csrf} " ) ) .await.status, StatusCode:: SEE_OTHER ) ;
568
}
569
let csrf = csrf_of ( & get ( app, " /andy/alpha" , Some ( & andy) ) .await.body) ;
570
post ( app, " /andy/alpha/star" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
571
assert! ( get ( app, " /andy/gamma" , Some ( & bob) ) .await.body.contains ( " ★ UNSTAR" ) ) ;
572
assert! ( get ( app, " /andy/gamma" , None ) .await.body.contains ( " ★ 2" ) ) ;
573
assert! ( get ( app, " /andy/alpha" , None ) .await.body.contains ( " ★ 0" ) , " unstarred again" ) ;
574
575
576
let order = | b : & str | {
577
[ " alpha" , " beta" , " gamma" ]
578
.into_iter ( )
579
.map ( | n | ( b.find ( & format! ( " /andy/{n} \" " ) ) .unwrap_or ( usize :: MAX ) , n) )
580
.collect:: < std:: collections:: BTreeMap< _ , _ > > ( )
581
.into_values ( )
582
.collect:: < Vec< _ > > ( )
583
} ;
584
assert_eq! ( order ( & get ( app, " /explore?sort=stars" , None ) .await.body) [ .. 2 ] , [ " gamma" , " beta" ] ) ;
585
assert_eq! ( order ( & get ( app, " /explore?sort=name" , None ) .await.body) , [ " alpha" , " beta" , " gamma" ] ) ;
586
let filtered = get ( app, " /explore?q=compiler" , None ) .await.body;
587
assert! ( filtered.contains ( " /andy/beta\" " ) & & ! filtered.contains ( " /andy/alpha\" " ) , " {filtered}" ) ;
588
let by_topic = get ( app, " /explore?topic=6502" , None ) .await.body;
589
assert! ( by_topic.contains ( " /andy/gamma\" " ) & & ! by_topic.contains ( " /andy/beta\" " ) , " {by_topic}" ) ;
590
}
1ac84c62 Andy McCall 1 month ago ↶
591
592
# [ tokio ::test ( flavor = " multi_thread" ) ]
593
async fn lists_filter_sort_page_and_count ( ) {
594
let h = hub ( ) .await;
595
let app = & h.app;
596
let andy = register ( app, " andy" ) .await;
597
let _bob = register ( app, " bob" ) .await;
598
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
599
let repo = db:: repos:: create( & app.db, " andy" , " demo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
600
for n in 0 .. 30 {
601
let author = if n % 3 = = 0 { " bob" } else { " andy" } ;
602
db:: issues:: create(
603
& app.db,
604
db:: issues:: NewIssue { repo_id: repo.id, title: & format! ( " Issue number {n} " ) , body: " " , author_id: None , author } ,
605
)
606
.await
607
.unwrap ( ) ;
608
}
609
let ui = db:: issues:: upsert_label( & app.db, repo.id, " ui" , " #00f0ff" , " " ) .await.unwrap ( ) ;
610
for ( title, author, target) in [ ( " Faster menus" , " bob" , " main" ) , ( " New theme" , " andy" , " main" ) , ( " Release prep" , " andy" , " stable" ) ] {
611
let p = db:: proposals:: create(
612
& app.db,
613
db:: proposals:: NewProposal {
614
repo_id: repo.id,
615
title,
616
body: " " ,
617
author_id: None ,
618
author,
619
draft: false ,
620
target,
621
source_kind: " changes" ,
622
source_repo_id: None ,
623
source_bookmark: " " ,
624
source_changes: " " ,
625
} ,
626
)
627
.await
628
.unwrap ( ) ;
629
if title = = " New theme" {
630
db:: issues:: set_proposal_label( & app.db, p.id, ui.id, true ) .await.unwrap ( ) ;
631
}
632
}
633
634
635
let page = get ( app, " /andy/demo/issues" , Some ( & andy) ) .await;
636
assert! ( page.body.contains ( r # "Issues <span class="count-chip">30</span>"# ) , " {}" , page.body) ;
637
assert! ( page.body.contains ( r # "Proposals <span class="count-chip">3</span>"# ) ) ;
638
639
640
let rows = | b : & str | b.matches ( " /andy/demo/issue/" ) .count ( ) ;
641
assert_eq! ( rows ( & page.body) , 25 ) ;
642
assert! ( page.body.contains ( " page 1 of 2" ) & & page.body.contains ( " page=2" ) , " a pager" ) ;
643
let second = get ( app, " /andy/demo/issues?page=2" , Some ( & andy) ) .await;
644
assert_eq! ( rows ( & second.body) , 5 ) ;
645
assert! ( second.body.contains ( " Issue number 0<" ) , " the oldest are last" ) ;
646
let oldest = get ( app, " /andy/demo/issues?sort=oldest" , Some ( & andy) ) .await;
647
assert! ( oldest.body.find ( " Issue number 0<" ) .unwrap ( ) < oldest.body.find ( " Issue number 1<" ) .unwrap ( ) ) ;
648
let bobs = get ( app, " /andy/demo/issues?q=author:bob" , Some ( & andy) ) .await;
649
assert_eq! ( rows ( & bobs.body) , 10 , " {}" , bobs.body) ;
650
651
652
let by_bob = get ( app, " /andy/demo/proposals?q=author:bob" , Some ( & andy) ) .await.body;
653
assert! ( by_bob.contains ( " Faster menus" ) & & ! by_bob.contains ( " New theme" ) ) ;
654
let ui_only = get ( app, " /andy/demo/proposals?q=label:ui" , Some ( & andy) ) .await.body;
655
assert! ( ui_only.contains ( " New theme" ) & & ! ui_only.contains ( " Faster menus" ) & & ui_only.contains ( r # "class="label""# ) ) ;
656
let stable = get ( app, " /andy/demo/proposals?q=target:stable" , Some ( & andy) ) .await.body;
657
assert! ( stable.contains ( " Release prep" ) & & ! stable.contains ( " New theme" ) ) ;
658
}