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;
cb71e18e Andy McCall 1 month ago ↶
163
assert! ( r.body.contains ( " <li><a href=\" /bob\" ><img class=\" avatar xs\" " ) , " {}" , r.body) ;
164
165
assert! ( r.body.contains ( " Assign people to this issue" ) & & r.body.contains ( " data-menu-filter" ) , " the assignees menu" ) ;
166
let r = post ( app, " /andy/demo/issue/1/assign" , Some ( & andy) , & format! ( " _csrf={csrf_a} &action=set&username=andy&username=bob" ) ) .await;
167
let assigned = | db : sqlx:: SqlitePool| async move {
168
let id: i64 = sqlx:: query_scalar( " SELECT id FROM issues WHERE number = 1" ) .fetch_one ( & db) .await.unwrap ( ) ;
169
let mut a = db:: issues:: assignees( & db, id) .await.unwrap ( ) ;
170
a.sort ( ) ;
171
a
172
} ;
173
assert_eq! ( assigned ( app.db.clone ( ) ) .await, [ " andy" , " bob" ] , " {}" , r.status) ;
174
post ( app, " /andy/demo/issue/1/assign" , Some ( & andy) , & format! ( " _csrf={csrf_a} &action=set&username=bob" ) ) .await;
175
assert_eq! ( assigned ( app.db.clone ( ) ) .await, [ " bob" ] , " set takes off who is not listed" ) ;
18e381ea Andy McCall 1 month ago ↶
176
let ms_id: i64 = sqlx:: query_scalar( " SELECT id FROM milestones WHERE title = 'v1'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
177
let r = post ( app, " /andy/demo/issue/1/milestone" , Some ( & andy) , & format! ( " _csrf={csrf_a} &milestone={ms_id} " ) ) .await;
178
assert! ( r.body.contains ( " >v1<" ) , " {}" , r.body) ;
cb71e18e Andy McCall 1 month ago ↶
179
assert! ( post ( app, " /andy/demo/issue/1/state" , Some ( & andy) , & format! ( " _csrf={csrf_a} &action=pin" ) ) .await.body.contains ( " Unpin issue" ) ) ;
18e381ea Andy McCall 1 month ago ↶
180
assert_eq! (
181
post (
182
app,
183
" /andy/demo/issue/1/state" ,
184
Some ( & bob) ,
185
& format! ( " _csrf={} &action=lock" , csrf_of ( & get ( app, " /andy/demo/issue/1" , Some ( & bob) ) .await.body) )
186
)
187
.await
188
.status,
189
StatusCode:: NOT_FOUND ,
190
" bob cannot lock"
191
) ;
192
193
let list = get ( app, " /andy/demo/issues?q=label:bug+assignee:bob+milestone:v1" , None ) .await;
194
assert! ( list.body.contains ( " Crash on start" ) , " {}" , list.body) ;
195
assert! ( ! get ( app, " /andy/demo/issues?q=label:feature" , None ) .await.body.contains ( " Crash on start" ) ) ;
196
assert! ( get ( app, " /andy/demo/milestones" , None ) .await.body.contains ( " 1 open, 0 closed" ) ) ;
197
198
199
let csrf_b = csrf_of ( & get ( app, " /andy/demo/issue/1" , Some ( & bob) ) .await.body) ;
200
let r = post ( app, " /andy/demo/issue/1/comment" , Some ( & bob) , & format! ( " _csrf={csrf_b} &body=Reproduced+with+the+flux+capacitor" ) ) .await;
201
assert! ( r.body.contains ( " flux capacitor" ) , " {}" , r.body) ;
202
assert! ( get ( app, " /notifications" , Some ( & andy) ) .await.body.contains ( " andy/demo#1" ) ) ;
203
let s = get ( app, " /search?q=capacitor" , None ) .await;
204
assert! ( s.body.contains ( " Crash on start" ) & & s.body.contains ( " <mark>capacitor</mark>" ) , " {}" , s.body) ;
205
let s = get ( app, " /search?q=demo" , None ) .await;
206
assert! ( s.body.contains ( " /andy/demo\" " ) , " repository hit: {}" , s.body) ;
207
208
let r = post ( app, " /andy/demo/issue/1/comment" , Some ( & bob) , & format! ( " _csrf={csrf_b} &body=closing&close=completed" ) ) .await;
209
assert! ( r.body.contains ( " >open<" ) | | r.body.contains ( " head\" >open" ) , " still open: {}" , r.body) ;
210
let r = post ( app, " /andy/demo/issue/1/comment" , Some ( & andy) , & format! ( " _csrf={csrf_a} &body=fixed&close=completed" ) ) .await;
211
assert! ( r.body.contains ( " closed" ) , " {}" , r.body) ;
212
assert! ( get ( app, " /andy/demo/issues?state=closed" , None ) .await.body.contains ( " Crash on start" ) ) ;
213
214
215
let csrf_a = csrf_of ( & get ( app, " /andy/demo/issues/new" , Some ( & andy) ) .await.body) ;
216
assert_eq! (
217
post ( app, " /andy/demo/issues/new" , Some ( & andy) , & format! ( " _csrf={csrf_a} &title=Typo&body=" ) ) .await.location.as_deref ( ) ,
218
Some ( " /andy/demo/issue/2" )
219
) ;
220
let work = tempfile:: tempdir( ) .unwrap ( ) ;
221
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
222
client.config.set ( " remote.origin.url" , & format! ( " {} /andy/demo" , h.base) ) .unwrap ( ) ;
223
client.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
224
client.save_config ( ) .unwrap ( ) ;
225
write ( work.path ( ) , " a.txt" , b " a\n " ) ;
226
client.snapshot ( " t" ) .unwrap ( ) ;
227
client.commit ( " Fix the typo\n \n Closes #2" , & [ ] ) .unwrap ( ) ;
228
assert! ( client.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
92cea629 Andy McCall 25 days ago ↶
229
230
let repo_id = db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) .id;
231
let mut issue2 = db:: issues:: get( & app.db, repo_id, 2 ) .await.unwrap ( ) .unwrap ( ) ;
232
for _ in 0 .. 200 {
233
if issue2.state = = " closed" {
234
break ;
235
}
236
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
237
issue2 = db:: issues:: get( & app.db, repo_id, 2 ) .await.unwrap ( ) .unwrap ( ) ;
238
}
18e381ea Andy McCall 1 month ago ↶
239
assert_eq! ( issue2.state, " closed" , " closed by the landed change" ) ;
240
assert! ( get ( app, " /andy/demo/issue/2" , None ) .await.body.contains ( " closed by change" ) ) ;
241
242
243
client.bookmark_create ( " feat" , None ) .unwrap ( ) ;
244
client.bookmark_switch ( " feat" , false ) .unwrap ( ) ;
245
write ( work.path ( ) , " b.txt" , b " b\n " ) ;
246
client.snapshot ( " t" ) .unwrap ( ) ;
247
client.commit ( " feat" , & [ ] ) .unwrap ( ) ;
248
assert! ( client.push_bookmark ( " origin" , " feat" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
249
let csrf_a = csrf_of ( & get ( app, " /andy/demo/proposals/new?source=feat&target=main" , Some ( & andy) ) .await.body) ;
250
let r = post ( app, " /andy/demo/proposals/new" , Some ( & andy) , & format! ( " _csrf={csrf_a} &title=Feat&target=main&source=feat" ) ) .await;
251
assert_eq! ( r.location.as_deref ( ) , Some ( " /andy/demo/proposal/3" ) , " {}" , r.body) ;
252
assert_eq! ( get ( app, " /andy/demo/issue/3" , None ) .await.location.as_deref ( ) , Some ( " /andy/demo/proposal/3" ) ) ;
253
254
let r = post ( app, " /andy/demo/issue/2/comment" , Some ( & andy) , & format! ( " _csrf={csrf_a} &body=see+%233" ) ) .await;
255
assert! ( r.body.contains ( " href=\" /andy/demo/proposal/3\" " ) , " {}" , r.body) ;
78001759 Andy McCall 1 month ago ↶
256
257
client.bookmark_switch ( " main" , false ) .unwrap ( ) ;
258
write ( work.path ( ) , " c.txt" , b " c\n " ) ;
259
client.snapshot ( " t" ) .unwrap ( ) ;
260
let r = client.commit ( " Tidy up (refs #1, see #3)" , & [ ] ) .unwrap ( ) ;
261
assert! ( client.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
262
let short = & r.to_hex ( ) [ .. 12 ] ;
92cea629 Andy McCall 25 days ago ↶
263
264
265
let mut one = get ( app, " /andy/demo/issue/1" , None ) .await.body;
266
for _ in 0 .. 200 {
267
if one.contains ( " referenced by change" ) {
268
break ;
269
}
270
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
271
one = get ( app, " /andy/demo/issue/1" , None ) .await.body;
272
}
78001759 Andy McCall 1 month ago ↶
273
assert! (
274
one.contains ( " referenced by change" ) & & one.contains ( & format! ( " href=\" /andy/demo/change/{short} \" " ) ) & & one.contains ( " on main" ) ,
275
" {one}"
276
) ;
277
let three = get ( app, " /andy/demo/proposal/3" , Some ( & andy) ) .await.body;
278
assert! ( three.contains ( " referenced by change" ) & & three.contains ( short) , " proposals too" ) ;
279
assert! ( ! get ( app, " /andy/demo/issue/2" , None ) .await.body.contains ( " referenced by change" ) , " a closing change records only that" ) ;
280
eb0c44a9 Andy McCall 1 month ago ↶
281
282
let part = get ( app, " /notifications?reason=participating" , Some ( & bob) ) .await.body;
283
assert! ( part.contains ( " andy/demo#1" ) & & ! part.contains ( " andy/demo#3: Feat" ) , " {part}" ) ;
284
let watch = get ( app, " /notifications?reason=watching" , Some ( & bob) ) .await.body;
285
assert! ( watch.contains ( " andy/demo#3: Feat" ) & & ! watch.contains ( " Crash on start" ) , " {watch}" ) ;
286
let props = get ( app, " /notifications?kind=proposal" , Some ( & bob) ) .await.body;
287
assert! ( props.contains ( " andy/demo#3: Feat" ) & & ! props.contains ( " andy/demo#1" ) , " kind filter" ) ;
288
assert! ( get ( app, " /notifications?repo=andy/demo" , Some ( & bob) ) .await.body.contains ( " andy/demo#3: Feat" ) ) ;
289
assert! ( get ( app, " /notifications?repo=nobody/none" , Some ( & bob) ) .await.body.contains ( " Nothing matches these filters" ) ) ;
290
291
let inbox = get ( app, " /notifications" , Some ( & bob) ) .await.body;
292
assert_eq! ( inbox.matches ( " data-row-thread=\" /andy/demo/issue/1\" " ) .count ( ) , 1 , " {inbox}" ) ;
293
assert! ( inbox.contains ( " updates" ) , " the count of a busy thread" ) ;
294
295
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
296
let newest: i64 = sqlx:: query_scalar(
297
" SELECT id FROM notifications WHERE account_id = ? AND thread = '/andy/demo/proposal/3' ORDER BY id DESC LIMIT 1" ,
298
)
299
.bind ( bob_id)
300
.fetch_one ( & app.db)
301
.await
302
.unwrap ( ) ;
303
let before = db:: notify:: unread( & app.db, bob_id) .await.unwrap ( ) ;
304
let r = get ( app, & format! ( " /notifications/open/{newest} " ) , Some ( & bob) ) .await;
305
assert_eq! ( r.location.as_deref ( ) , Some ( " /andy/demo/proposal/3" ) ) ;
306
assert! ( db:: notify:: unread( & app.db, bob_id) .await.unwrap ( ) < before) ;
307
assert_eq! ( get ( app, & format! ( " /notifications/open/{newest} " ) , Some ( & andy) ) .await.status, StatusCode:: NOT_FOUND , " only your own" ) ;
308
309
let csrf_b = csrf_of ( & get ( app, " /notifications" , Some ( & bob) ) .await.body) ;
310
let r = post (
311
app,
312
" /notifications/thread" ,
313
Some ( & bob) ,
314
& format! ( " _csrf={csrf_b} &thread=%2Fandy%2Fdemo%2Fissue%2F1&action=done&back=%2Fnotifications" ) ,
315
)
316
.await;
317
assert_eq! ( r.location.as_deref ( ) , Some ( " /notifications" ) ) ;
318
assert! ( ! get ( app, " /notifications" , Some ( & bob) ) .await.body.contains ( " Crash on start" ) ) ;
319
assert! ( get ( app, " /notifications?view=done" , Some ( & bob) ) .await.body.contains ( " Crash on start" ) ) ;
320
post ( app, " /andy/demo/issue/1/comment" , Some ( & andy) , & format! ( " _csrf={csrf_a} &body=one+more+thing" ) ) .await;
321
assert! ( get ( app, " /notifications" , Some ( & bob) ) .await.body.contains ( " Crash on start" ) , " back with the new comment" ) ;
322
let r = post (
323
app,
324
" /notifications/thread" ,
325
Some ( & bob) ,
326
& format! ( " _csrf={csrf_b} &thread=%2Fandy%2Fdemo%2Fissue%2F1&action=read&back=https%3A%2F%2Fevil.example" ) ,
327
)
328
.await;
329
assert_eq! ( r.location.as_deref ( ) , Some ( " /notifications" ) , " only back to the inbox" ) ;
330
18e381ea Andy McCall 1 month ago ↶
331
332
assert! ( get ( app, " /notifications" , Some ( & bob) ) .await.body.contains ( " andy/demo#3: Feat" ) ) ;
333
let csrf_b = csrf_of ( & get ( app, " /notifications" , Some ( & bob) ) .await.body) ;
334
let r = post ( app, " /notifications/read" , Some ( & bob) , & format! ( " _csrf={csrf_b} " ) ) .await;
335
assert! ( r.body.contains ( " 0 unread" ) , " {}" , r.body) ;
336
337
let r = post ( app, " /settings/notifications" , Some ( & bob) , & format! ( " _csrf={csrf_b} &email=1" ) ) .await;
338
assert! ( r.body.contains ( " Email notifications are on" ) ) ;
339
let key: String = sqlx:: query_scalar( " SELECT unsubscribe_key FROM accounts WHERE username = 'bob'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
340
assert_eq! ( get ( app, & format! ( " /settings/notifications?unsubscribe={key} " ) , None ) .await.status, StatusCode:: OK ) ;
341
let on: bool = sqlx:: query_scalar( " SELECT email_notifications FROM accounts WHERE username = 'bob'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
342
assert! ( ! on) ;
343
}
25ab8c73 Andy McCall 1 month ago ↶
344
345
# [ tokio ::test ( flavor = " multi_thread" ) ]
346
async fn dashboard_shows_what_needs_the_viewer ( ) {
347
let h = hub ( ) .await;
348
let app = & h.app;
349
let andy = register ( app, " andy" ) .await;
350
let bob = register ( app, " bob" ) .await;
351
for ( name, vis) in [ ( " demo" , hub_api:: Visibility:: Public) , ( " secret" , hub_api:: Visibility:: Private) ] {
352
app.repos.create ( & RepoName:: parse( & format! ( " andy/{name} " ) ) .unwrap ( ) , None ) .unwrap ( ) ;
353
db:: repos:: create( & app.db, " andy" , name, " " , vis) .await.unwrap ( ) ;
354
}
355
let demo = db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
356
let secret = db:: repos:: get( & app.db, " andy" , " secret" ) .await.unwrap ( ) .unwrap ( ) ;
357
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
358
for ( repo, title) in [ ( demo.id, " Faster startup" ) , ( secret.id, " Hidden work" ) ] {
359
let p = db:: proposals:: create(
360
& app.db,
361
db:: proposals:: NewProposal {
362
repo_id: repo,
363
title,
364
body: " " ,
365
author_id: Some ( bob_id) ,
366
author: " bob" ,
367
draft: false ,
368
target: " main" ,
369
source_kind: " bookmark" ,
370
source_repo_id: Some ( repo) ,
371
source_bookmark: " fast" ,
372
source_changes: " " ,
373
} ,
374
)
375
.await
376
.unwrap ( ) ;
377
db:: proposals:: add_event( & app.db, p.id, " bob" , " opened" , " " ) .await.unwrap ( ) ;
378
}
379
380
381
let anon = get ( app, " /" , None ) .await;
382
assert! ( anon.body.contains ( " andy /" ) & & ! anon.body.contains ( " Dashboard <small>" ) , " {}" , anon.body) ;
383
384
385
let page = get ( app, " /" , Some ( & andy) ) .await;
386
assert_eq! ( page.status, StatusCode:: OK ) ;
387
assert! ( page.body.contains ( " Waiting for your review <small>2</small>" ) , " {}" , page.body) ;
388
assert! ( page.body.contains ( " /andy/demo/proposal/1" ) & & page.body.contains ( " /andy/secret/proposal/1" ) ) ;
389
assert! ( page.body.contains ( " Your repositories" ) ) ;
390
assert! ( page.body.contains ( " opened proposal" ) , " activity feed: {}" , page.body) ;
391
392
393
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
394
let p = db:: proposals:: get( & app.db, demo.id, 1 ) .await.unwrap ( ) .unwrap ( ) ;
395
db:: proposals:: add_review( & app.db, p.id, andy_id, " andy" , " approve" , " " ) .await.unwrap ( ) ;
396
assert! ( get ( app, " /" , Some ( & andy) ) .await.body.contains ( " Waiting for your review <small>1</small>" ) ) ;
397
398
399
let page = get ( app, " /" , Some ( & bob) ) .await;
400
assert! ( page.body.contains ( " Your proposals <small>1</small>" ) , " {}" , page.body) ;
401
assert! ( ! page.body.contains ( " Hidden work" ) ) ;
402
assert! ( page.body.contains ( " Waiting for your review <small>0</small>" ) ) ;
403
assert! ( page.body.contains ( " Faster startup" ) ) ;
404
let feed = & page.body[ page.body.find ( " Recent activity" ) .unwrap ( ) .. ] ;
405
assert! ( ! feed.contains ( " andy/secret" ) , " private activity leaked: {feed}" ) ;
406
407
408
assert! ( get ( app, " /explore" , Some ( & bob) ) .await.body.contains ( " andy /" ) ) ;
409
}
4edcf240 Andy McCall 1 month ago ↶
410
411
# [ tokio ::test ( flavor = " multi_thread" ) ]
412
async fn empty_repository_shows_a_quickstart ( ) {
413
let h = hub ( ) .await;
414
let app = & h.app;
415
let andy = register ( app, " andy" ) .await;
416
let bob = register ( app, " bob" ) .await;
417
app.repos.create ( & RepoName:: parse( " andy/fresh" ) .unwrap ( ) , None ) .unwrap ( ) ;
418
db:: repos:: create( & app.db, " andy" , " fresh" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
419
420
let page = get ( app, " /andy/fresh" , Some ( & andy) ) .await;
421
assert! ( page.body.contains ( " This repository is empty" ) , " {}" , page.body) ;
422
let clone_url = format! ( " {} /andy/fresh" , h.base) ;
423
assert! ( page.body.contains ( & format! ( " oxy remote add hub {clone_url} " ) ) , " {}" , page.body) ;
424
assert! ( page.body.contains ( " oxy config remote.hub.token YOUR-TOKEN" ) ) ;
425
assert! ( page.body.contains ( " oxy push hub" ) ) ;
426
assert! (
427
page.body.contains ( " mkdir fresh && cd fresh" ) | | page.body.contains ( " mkdir fresh && cd fresh" ) ,
428
" {}" ,
429
page.body
430
) ;
431
assert! ( page.body.contains ( " oxy git import --branch main" ) ) ;
432
assert! ( ! page.body.contains ( " oxyhub-ca.pem" ) , " no certificate step without a self-signed certificate" ) ;
433
434
435
let page = get ( app, " /andy/fresh" , Some ( & bob) ) .await;
436
assert! ( page.body.contains ( " Nothing has been pushed yet" ) & & ! page.body.contains ( " oxy push hub" ) , " {}" , page.body) ;
437
438
439
let work = tempfile:: tempdir( ) .unwrap ( ) ;
440
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
441
client.config.set ( " remote.hub.url" , & clone_url) .unwrap ( ) ;
442
client.config.set ( " remote.hub.token" , " site" ) .unwrap ( ) ;
443
client.save_config ( ) .unwrap ( ) ;
444
write ( work.path ( ) , " README.md" , b " # fresh\n " ) ;
445
client.commit ( " First change" , & [ ] ) .unwrap ( ) ;
446
assert! ( client.push ( " hub" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
447
let page = get ( app, " /andy/fresh" , Some ( & andy) ) .await;
448
assert! ( ! page.body.contains ( " This repository is empty" ) & & page.body.contains ( " README.md" ) , " {}" , page.body) ;
449
}
c40c2d3e Andy McCall 1 month ago ↶
450
451
# [ tokio ::test ( flavor = " multi_thread" ) ]
452
async fn theme_is_chosen_in_settings_and_remembered_in_a_cookie ( ) {
453
let h = hub ( ) .await;
454
let app = & h.app;
455
let andy = register ( app, " andy" ) .await;
456
let page = get ( app, " /settings" , Some ( & andy) ) .await;
457
assert! ( page.body.contains ( " Appearance" ) ) ;
d98a93c1 Andy McCall 1 month ago ↶
458
assert! ( page.body.contains ( r # "value="light" checked"# ) , " light by default: {}" , page.body) ;
c40c2d3e Andy McCall 1 month ago ↶
459
assert! ( page.body.contains ( " data-theme-toggle" ) , " the header has the switch" ) ;
460
let csrf = csrf_of ( & page.body) ;
d98a93c1 Andy McCall 1 month ago ↶
461
let r = post ( app, " /settings/appearance" , Some ( & andy) , & format! ( " _csrf={csrf} &theme=dark" ) ) .await;
c40c2d3e Andy McCall 1 month ago ↶
462
assert_eq! ( r.status, StatusCode:: SEE_OTHER ) ;
d98a93c1 Andy McCall 1 month ago ↶
463
assert_eq! ( r.cookie.as_deref ( ) , Some ( " dark" ) ) ;
c40c2d3e Andy McCall 1 month ago ↶
464
assert_eq! ( r.location.as_deref ( ) , Some ( " /settings" ) ) ;
d98a93c1 Andy McCall 1 month ago ↶
465
let page = send_with_theme ( app, & andy, " dark" ) .await;
466
assert! ( page.contains ( r # "value="dark" checked"# ) , " the form shows the cookie's theme" ) ;
c40c2d3e Andy McCall 1 month ago ↶
467
assert_eq! ( post ( app, " /settings/appearance" , Some ( & andy) , & format! ( " _csrf={csrf} &theme=purple" ) ) .await.status, StatusCode:: BAD_REQUEST ) ;
468
assert_eq! ( post ( app, " /settings/appearance" , Some ( & andy) , " _csrf=nope&theme=light" ) .await.status, StatusCode:: BAD_REQUEST ) ;
469
}
470
471
async fn send_with_theme ( app : & Shared, session : & str , theme : & str ) -> String {
472
let req =
473
Request:: builder( ) .uri ( " /settings" ) .header ( header:: COOKIE , format! ( " oxyhub={session} ; theme={theme} " ) ) .body ( Body:: empty( ) ) .unwrap ( ) ;
474
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
475
String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( )
476
}
10174045 Andy McCall 1 month ago ↶
477
478
# [ tokio ::test ( flavor = " multi_thread" ) ]
479
async fn profile_and_organisation_pages ( ) {
480
let h = hub ( ) .await;
481
let app = & h.app;
482
let andy = register ( app, " andy" ) .await;
483
let bob = register ( app, " bob" ) .await;
484
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
485
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
486
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
487
db:: repos:: create( & app.db, " andy" , " demo" , " A demo" , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
488
app.repos.create ( & RepoName:: parse( " andy/hidden" ) .unwrap ( ) , None ) .unwrap ( ) ;
489
db:: repos:: create( & app.db, " andy" , " hidden" , " " , hub_api:: Visibility:: Private) .await.unwrap ( ) ;
490
491
492
let csrf = csrf_of ( & get ( app, " /settings" , Some ( & andy) ) .await.body) ;
493
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;
494
assert! ( r.body.contains ( " must be an http" ) , " {}" , r.body) ;
495
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;
496
assert! ( r.body.contains ( " Profile saved" ) , " {}" , r.body) ;
497
498
let org = db:: accounts:: create_org( & app.db, " retro" , " Retro Club" , andy_id) .await.unwrap ( ) ;
499
db:: accounts:: set_org_member( & app.db, org.id, bob_id, " member" ) .await.unwrap ( ) ;
500
let team = db:: accounts:: create_team( & app.db, org.id, " core" ) .await.unwrap ( ) ;
501
db:: accounts:: add_team_member( & app.db, team.id, bob_id) .await.unwrap ( ) ;
502
503
let page = get ( app, " /andy" , Some ( & bob) ) .await;
504
assert_eq! ( page.status, StatusCode:: OK ) ;
505
for want in [ " Andy McCall" , " Earth" , r # "href="https://example.org""# , " /avatar/andy" , " Signing keys" , " /retro" , " demo" ] {
506
assert! ( page.body.contains ( want) , " profile lacks {want}: {}" , page.body) ;
507
}
508
assert! ( ! page.body.contains ( " <b>oxy</b>" ) , " the bio is escaped" ) ;
509
assert! ( ! page.body.contains ( " [email protected] " ) , " the email stays private" ) ;
510
assert! ( ! page.body.contains ( " /andy/hidden" ) , " bob cannot see the private repository" ) ;
511
assert! ( ! page.body.contains ( " edit profile" ) , " only the owner gets the edit link" ) ;
512
assert! ( get ( app, " /andy" , Some ( & andy) ) .await.body.contains ( " /andy/hidden" ) , " andy sees his own private repository" ) ;
513
514
let page = get ( app, " /retro" , Some ( & bob) ) .await;
515
assert_eq! ( page.status, StatusCode:: OK ) ;
516
for want in [ " Retro Club" , " organisation" , " Members <small>2</small>" , " core" , " 1 member" ] {
517
assert! ( page.body.contains ( want) , " org page lacks {want}: {}" , page.body) ;
518
}
519
assert! ( ! page.body.contains ( " /orgs/retro/settings" ) , " bob is not an owner" ) ;
520
assert! ( get ( app, " /retro" , Some ( & andy) ) .await.body.contains ( " /orgs/retro/settings" ) ) ;
521
522
let svg = get ( app, " /avatar/andy" , None ) .await;
523
assert_eq! ( svg.status, StatusCode:: OK ) ;
524
assert! ( svg.body.starts_with ( " <svg" ) ) ;
525
assert_eq! ( svg.body, get ( app, " /avatar/ANDY" , None ) .await.body) ;
526
assert_eq! ( get ( app, " /nobody-here" , None ) .await.status, StatusCode:: NOT_FOUND ) ;
527
}
9df855f2 Andy McCall 1 month ago ↶
528
529
# [ tokio ::test ( flavor = " multi_thread" ) ]
530
async fn diffs_mark_words_hide_whitespace_and_collapse ( ) {
531
let h = hub ( ) .await;
532
let app = & h.app;
533
let andy = register ( app, " andy" ) .await;
534
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
535
db:: repos:: create( & app.db, " andy" , " demo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
536
let work = tempfile:: tempdir( ) .unwrap ( ) ;
537
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
538
client.config.set ( " remote.hub.url" , & format! ( " {} /andy/demo" , h.base) ) .unwrap ( ) ;
539
client.config.set ( " remote.hub.token" , " site" ) .unwrap ( ) ;
540
client.save_config ( ) .unwrap ( ) ;
541
write ( work.path ( ) , " main.rs" , b " fn main() {\n setup();\n let answer = 42;\n }\n " ) ;
542
client.commit ( " first" , & [ ] ) .unwrap ( ) ;
543
write ( work.path ( ) , " main.rs" , b " fn main() {\n setup();\n let answer = 43;\n }\n " ) ;
544
let second = client.commit ( " reindent and change the answer" , & [ ] ) .unwrap ( ) ;
545
assert! ( client.push ( " hub" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
546
547
let url = format! ( " /andy/demo/change/{} " , second.to_hex ( ) ) ;
548
let page = get ( app, & url, Some ( & andy) ) .await;
549
assert_eq! ( page.status, StatusCode:: OK ) ;
550
assert! ( page.body.contains ( r # "<details class="file-diff""# ) , " files collapse: {}" , page.body) ;
551
assert! ( page.body.contains ( r # "<span class="wx">43</span>"# ) , " the changed word is marked: {}" , page.body) ;
552
assert! ( page.body.contains ( " hide whitespace changes" ) ) ;
553
assert! ( page.body.contains ( " data-diff-mode=\" split\" " ) ) ;
554
let adds = | b : & str | b.matches ( r # "<tr class="add">"# ) .count ( ) ;
555
assert_eq! ( adds ( & page.body) , 2 , " re-indent and answer both changed" ) ;
556
557
let quiet = get ( app, & format! ( " {url} ?w=1" ) , Some ( & andy) ) .await;
558
assert_eq! ( adds ( & quiet.body) , 1 , " only the answer changed ignoring whitespace: {}" , quiet.body) ;
559
assert! ( quiet.body.contains ( " show whitespace changes" ) ) ;
560
561
assert! ( quiet.body.contains ( " ?w=0" ) , " the toggle leads back" ) ;
562
}
07b1a5bb Andy McCall 1 month ago ↶
563
564
# [ tokio ::test ( flavor = " multi_thread" ) ]
565
async fn fork_and_propose_upstream ( ) {
566
let h = hub ( ) .await;
567
let app = & h.app;
568
let andy = register ( app, " andy" ) .await;
569
let bob = register ( app, " bob" ) .await;
570
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
571
db:: repos:: create( & app.db, " andy" , " demo" , " The demo" , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
572
let work = tempfile:: tempdir( ) .unwrap ( ) ;
573
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
574
client.config.set ( " remote.hub.url" , & format! ( " {} /andy/demo" , h.base) ) .unwrap ( ) ;
575
client.config.set ( " remote.hub.token" , " site" ) .unwrap ( ) ;
576
client.save_config ( ) .unwrap ( ) ;
577
write ( work.path ( ) , " README.md" , b " # demo\n " ) ;
578
client.commit ( " first" , & [ ] ) .unwrap ( ) ;
579
assert! ( client.push ( " hub" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
580
581
582
let page = get ( app, " /andy/demo" , Some ( & bob) ) .await;
583
assert! ( page.body.contains ( " /andy/demo/fork" ) , " a fork button: {}" , page.body) ;
584
let csrf = csrf_of ( & get ( app, " /andy/demo/fork" , Some ( & bob) ) .await.body) ;
585
let r = post ( app, " /andy/demo/fork" , Some ( & bob) , & format! ( " _csrf={csrf} &owner=andy&name=other" ) ) .await;
586
assert! ( r.body.contains ( " cannot create repositories under that owner" ) , " {}" , r.body) ;
587
let r = post ( app, " /andy/demo/fork" , Some ( & bob) , & format! ( " _csrf={csrf} &owner=bob&name=demo" ) ) .await;
588
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
589
assert_eq! ( r.location.as_deref ( ) , Some ( " /bob/demo" ) ) ;
590
let forked = get ( app, " /bob/demo" , Some ( & bob) ) .await;
591
assert! ( forked.body.contains ( " README.md" ) & & forked.body.contains ( " forked from" ) , " {}" , forked.body) ;
592
assert! ( forked.body.contains ( " propose to andy/demo" ) ) ;
593
assert! ( get ( app, " /andy/demo" , None ) .await.body.contains ( " 1 fork" ) ) ;
594
assert! ( get ( app, " /andy/demo/forks" , None ) .await.body.contains ( " /bob/demo" ) ) ;
595
let fork_row = db:: repos:: get( & app.db, " bob" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
596
let up_row = db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
597
assert_eq! ( fork_row.forked_from, Some ( up_row.id) ) ;
598
assert_eq! ( fork_row.description, " The demo" ) ;
599
let fork_repo = app.repos.open ( & RepoName:: parse( " bob/demo" ) .unwrap ( ) ) .unwrap ( ) ;
600
let up_repo = app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) ;
601
assert_ne! ( fork_repo.id ( ) .unwrap ( ) , up_repo.id ( ) .unwrap ( ) , " a fork is a repository of its own" ) ;
602
assert_eq! ( fork_repo.bookmark_heads ( ) .unwrap ( ) , up_repo.bookmark_heads ( ) .unwrap ( ) ) ;
603
assert_eq! ( fork_repo.fsck ( ) .unwrap ( ) .errors ( ) , 0 ) ;
604
605
606
let bw = tempfile:: tempdir( ) .unwrap ( ) ;
607
let mut mine = Repo:: clone( & format! ( " {} /bob/demo" , h.base) , & bw.path ( ) .join ( " c" ) ) .unwrap ( ) ;
608
mine.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
609
mine.save_config ( ) .unwrap ( ) ;
610
mine.bookmark_create ( " feat" , None ) .unwrap ( ) ;
611
mine.bookmark_switch ( " feat" , false ) .unwrap ( ) ;
612
write ( & bw.path ( ) .join ( " c" ) , " feature.txt" , b " new\n " ) ;
613
mine.commit ( " Add a feature" , & [ ] ) .unwrap ( ) ;
614
assert! ( mine.push_bookmark ( " origin" , " feat" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
615
let csrf = csrf_of ( & get ( app, " /andy/demo/proposals/new?source=bob/demo:feat" , Some ( & bob) ) .await.body) ;
616
let r =
617
post ( app, " /andy/demo/proposals/new" , Some ( & bob) , & format! ( " _csrf={csrf} &title=From+my+fork&target=main&source=bob%2Fdemo%3Afeat" ) )
618
.await;
619
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
620
let p = get ( app, & r.location.unwrap ( ) , Some ( & andy) ) .await;
621
assert! ( p.body.contains ( " From my fork" ) & & p.body.contains ( " fork bob/demo" ) , " {}" , p.body) ;
622
let files = get ( app, " /andy/demo/proposal/1?tab=files" , Some ( & andy) ) .await;
623
assert! ( files.body.contains ( " feature.txt" ) , " {}" , files.body) ;
624
}
1553bfc9 Andy McCall 1 month ago ↶
625
626
# [ tokio ::test ( flavor = " multi_thread" ) ]
627
async fn stars_topics_and_explore ( ) {
628
let h = hub ( ) .await;
629
let app = & h.app;
630
let andy = register ( app, " andy" ) .await;
631
let bob = register ( app, " bob" ) .await;
632
for ( name, desc) in [ ( " alpha" , " An emulator" ) , ( " beta" , " A compiler" ) , ( " gamma" , " Retro games" ) ] {
633
app.repos.create ( & RepoName:: parse( & format! ( " andy/{name} " ) ) .unwrap ( ) , None ) .unwrap ( ) ;
634
db:: repos:: create( & app.db, " andy" , name, desc, hub_api:: Visibility:: Public) .await.unwrap ( ) ;
635
}
636
637
638
let csrf = csrf_of ( & get ( app, " /andy/gamma/settings" , Some ( & andy) ) .await.body) ;
639
let r = post (
640
app,
641
" /andy/gamma/settings" ,
642
Some ( & andy) ,
643
& format! ( " _csrf={csrf} &description=Retro+games&default_bookmark=main&visibility=public&topics=c%2B%2B" ) ,
644
)
645
.await;
646
assert! ( r.body.contains ( " is not a topic" ) , " {}" , r.body) ;
647
let r = post (
648
app,
649
" /andy/gamma/settings" ,
650
Some ( & andy) ,
651
& format! ( " _csrf={csrf} &description=Retro+games&default_bookmark=main&visibility=public&topics=Retro%2C+6502" ) ,
652
)
653
.await;
654
assert! ( r.body.contains ( " Saved" ) , " {}" , r.body) ;
655
assert_eq! ( db:: repos:: get( & app.db, " andy" , " gamma" ) .await.unwrap ( ) .unwrap ( ) .topics, " retro 6502" ) ;
656
let page = get ( app, " /andy/gamma" , None ) .await;
657
assert! ( page.body.contains ( r # "href="/explore?topic=retro""# ) , " {}" , page.body) ;
658
659
660
for ( who, repo) in [ ( & bob, " beta" ) , ( & bob, " gamma" ) , ( & andy, " gamma" ) , ( & andy, " alpha" ) ] {
661
let csrf = csrf_of ( & get ( app, & format! ( " /andy/{repo} " ) , Some ( who) ) .await.body) ;
662
assert_eq! ( post ( app, & format! ( " /andy/{repo} /star" ) , Some ( who) , & format! ( " _csrf={csrf} " ) ) .await.status, StatusCode:: SEE_OTHER ) ;
663
}
664
let csrf = csrf_of ( & get ( app, " /andy/alpha" , Some ( & andy) ) .await.body) ;
665
post ( app, " /andy/alpha/star" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
666
assert! ( get ( app, " /andy/gamma" , Some ( & bob) ) .await.body.contains ( " ★ UNSTAR" ) ) ;
667
assert! ( get ( app, " /andy/gamma" , None ) .await.body.contains ( " ★ 2" ) ) ;
668
assert! ( get ( app, " /andy/alpha" , None ) .await.body.contains ( " ★ 0" ) , " unstarred again" ) ;
669
670
671
let order = | b : & str | {
672
[ " alpha" , " beta" , " gamma" ]
673
.into_iter ( )
674
.map ( | n | ( b.find ( & format! ( " /andy/{n} \" " ) ) .unwrap_or ( usize :: MAX ) , n) )
675
.collect:: < std:: collections:: BTreeMap< _ , _ > > ( )
676
.into_values ( )
677
.collect:: < Vec< _ > > ( )
678
} ;
679
assert_eq! ( order ( & get ( app, " /explore?sort=stars" , None ) .await.body) [ .. 2 ] , [ " gamma" , " beta" ] ) ;
680
assert_eq! ( order ( & get ( app, " /explore?sort=name" , None ) .await.body) , [ " alpha" , " beta" , " gamma" ] ) ;
681
let filtered = get ( app, " /explore?q=compiler" , None ) .await.body;
682
assert! ( filtered.contains ( " /andy/beta\" " ) & & ! filtered.contains ( " /andy/alpha\" " ) , " {filtered}" ) ;
683
let by_topic = get ( app, " /explore?topic=6502" , None ) .await.body;
684
assert! ( by_topic.contains ( " /andy/gamma\" " ) & & ! by_topic.contains ( " /andy/beta\" " ) , " {by_topic}" ) ;
685
}
1ac84c62 Andy McCall 1 month ago ↶
686
687
# [ tokio ::test ( flavor = " multi_thread" ) ]
688
async fn lists_filter_sort_page_and_count ( ) {
689
let h = hub ( ) .await;
690
let app = & h.app;
691
let andy = register ( app, " andy" ) .await;
692
let _bob = register ( app, " bob" ) .await;
693
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
694
let repo = db:: repos:: create( & app.db, " andy" , " demo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
695
for n in 0 .. 30 {
696
let author = if n % 3 = = 0 { " bob" } else { " andy" } ;
697
db:: issues:: create(
698
& app.db,
699
db:: issues:: NewIssue { repo_id: repo.id, title: & format! ( " Issue number {n} " ) , body: " " , author_id: None , author } ,
700
)
701
.await
702
.unwrap ( ) ;
703
}
704
let ui = db:: issues:: upsert_label( & app.db, repo.id, " ui" , " #00f0ff" , " " ) .await.unwrap ( ) ;
705
for ( title, author, target) in [ ( " Faster menus" , " bob" , " main" ) , ( " New theme" , " andy" , " main" ) , ( " Release prep" , " andy" , " stable" ) ] {
706
let p = db:: proposals:: create(
707
& app.db,
708
db:: proposals:: NewProposal {
709
repo_id: repo.id,
710
title,
711
body: " " ,
712
author_id: None ,
713
author,
714
draft: false ,
715
target,
716
source_kind: " changes" ,
717
source_repo_id: None ,
718
source_bookmark: " " ,
719
source_changes: " " ,
720
} ,
721
)
722
.await
723
.unwrap ( ) ;
724
if title = = " New theme" {
725
db:: issues:: set_proposal_label( & app.db, p.id, ui.id, true ) .await.unwrap ( ) ;
726
}
727
}
728
729
730
let page = get ( app, " /andy/demo/issues" , Some ( & andy) ) .await;
731
assert! ( page.body.contains ( r # "Issues <span class="count-chip">30</span>"# ) , " {}" , page.body) ;
732
assert! ( page.body.contains ( r # "Proposals <span class="count-chip">3</span>"# ) ) ;
733
734
735
let rows = | b : & str | b.matches ( " /andy/demo/issue/" ) .count ( ) ;
736
assert_eq! ( rows ( & page.body) , 25 ) ;
737
assert! ( page.body.contains ( " page 1 of 2" ) & & page.body.contains ( " page=2" ) , " a pager" ) ;
738
let second = get ( app, " /andy/demo/issues?page=2" , Some ( & andy) ) .await;
739
assert_eq! ( rows ( & second.body) , 5 ) ;
740
assert! ( second.body.contains ( " Issue number 0<" ) , " the oldest are last" ) ;
741
let oldest = get ( app, " /andy/demo/issues?sort=oldest" , Some ( & andy) ) .await;
742
assert! ( oldest.body.find ( " Issue number 0<" ) .unwrap ( ) < oldest.body.find ( " Issue number 1<" ) .unwrap ( ) ) ;
743
let bobs = get ( app, " /andy/demo/issues?q=author:bob" , Some ( & andy) ) .await;
744
assert_eq! ( rows ( & bobs.body) , 10 , " {}" , bobs.body) ;
745
746
747
let by_bob = get ( app, " /andy/demo/proposals?q=author:bob" , Some ( & andy) ) .await.body;
748
assert! ( by_bob.contains ( " Faster menus" ) & & ! by_bob.contains ( " New theme" ) ) ;
749
let ui_only = get ( app, " /andy/demo/proposals?q=label:ui" , Some ( & andy) ) .await.body;
750
assert! ( ui_only.contains ( " New theme" ) & & ! ui_only.contains ( " Faster menus" ) & & ui_only.contains ( r # "class="label""# ) ) ;
751
let stable = get ( app, " /andy/demo/proposals?q=target:stable" , Some ( & andy) ) .await.body;
752
assert! ( stable.contains ( " Release prep" ) & & ! stable.contains ( " New theme" ) ) ;
753
}
e1ec9fbf Andy McCall 1 month ago ↶
754
755
# [ tokio ::test ( flavor = " multi_thread" ) ]
756
async fn project_boards ( ) {
757
let h = hub ( ) .await;
758
let app = & h.app;
759
let andy = register ( app, " andy" ) .await;
760
let bob = register ( app, " bob" ) .await;
761
for ( name, vis) in [ ( " demo" , hub_api:: Visibility:: Public) , ( " secret" , hub_api:: Visibility:: Private) ] {
762
app.repos.create ( & RepoName:: parse( & format! ( " andy/{name} " ) ) .unwrap ( ) , None ) .unwrap ( ) ;
763
let row = db:: repos:: create( & app.db, " andy" , name, " " , vis) .await.unwrap ( ) ;
764
db:: issues:: create(
765
& app.db,
766
db:: issues:: NewIssue { repo_id: row.id, title: & format! ( " {name} issue" ) , body: " " , author_id: None , author: " andy" } ,
767
)
768
.await
769
.unwrap ( ) ;
770
}
a024e800 Andy McCall 26 days ago ↶
771
772
773
let home = get ( app, " /andy/demo" , Some ( & andy) ) .await.body;
774
assert! (
775
home.contains ( " href=\" /projects/andy\" >Projects</a>" ) & & home.contains ( " title=\" andy's project boards\" >Projects</a>" ) ,
776
" site bar and tab"
777
) ;
778
let list = get ( app, " /projects/andy" , Some ( & andy) ) .await.body;
779
assert! ( list.contains ( " href=\" #new\" >+ NEW PROJECT</a>" ) & & list.contains ( " id=\" new\" " ) , " the button and the form" ) ;
780
assert! ( get ( app, " /projects/andy" , Some ( & bob) ) .await.body.contains ( " Only andy" ) , " who can make one" ) ;
781
let csrf = csrf_of ( & list) ;
e1ec9fbf Andy McCall 1 month ago ↶
782
let r = post ( app, " /projects/andy" , Some ( & andy) , & format! ( " _csrf={csrf} &title=Roadmap&description=Next+steps" ) ) .await;
783
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/andy/1" ) , " {}" , r.body) ;
7b26e611 Andy McCall 25 days ago ↶
784
785
let list = get ( app, " /projects/andy" , Some ( & andy) ) .await.body;
786
assert! (
787
list.contains ( " >Roadmap</a>" ) & & list.contains ( " Next steps" ) & & list.contains ( " Open <span class=\" count-chip\" >1</span>" ) ,
788
" {list}"
789
) ;
790
let filtered = get ( app, " /projects/andy?q=roadm" , Some ( & andy) ) .await.body;
791
assert! ( filtered.contains ( " >Roadmap</a>" ) , " a filter matches part of the title, any case" ) ;
792
let none = get ( app, " /projects/andy?q=nothing+like+it" , Some ( & andy) ) .await.body;
793
assert! ( none.contains ( " No open project matches" ) & & ! none.contains ( " >Roadmap</a>" ) , " {none}" ) ;
794
let closed = get ( app, " /projects/andy?state=closed" , Some ( & andy) ) .await.body;
795
assert! ( closed.contains ( " No closed projects." ) & & closed.contains ( " Closed <span class=\" count-chip\" >0</span>" ) , " {closed}" ) ;
e1ec9fbf Andy McCall 1 month ago ↶
796
let board = get ( app, " /projects/andy/1" , Some ( & andy) ) .await.body;
797
for col in [ " Backlog" , " Doing" , " Done" ] {
798
assert! ( board.contains ( & format! ( " >{col} <span" ) ) , " {col}" ) ;
799
}
800
let cols: Vec< i64 > = sqlx:: query_scalar( " SELECT id FROM project_columns ORDER BY position" ) .fetch_all ( & app.db) .await.unwrap ( ) ;
801
let act = | form : String| {
802
let andy = andy.clone ( ) ;
803
async move { post ( app, " /projects/andy/1" , Some ( & andy) , & form) .await }
804
} ;
805
806
assert_eq! (
807
act ( format! ( " _csrf={csrf} &action=add&column={} &reference=demo%231" , cols[ 0 ] ) ) .await.location.as_deref ( ) ,
808
Some ( " /projects/andy/1" )
809
) ;
810
act ( format! ( " _csrf={csrf} &action=add&column={} &repo=secret&reference=%231" , cols[ 0 ] ) ) .await;
811
act ( format! ( " _csrf={csrf} &action=add&column={} ¬e=Write+the+docs" , cols[ 1 ] ) ) .await;
812
assert! ( act ( format! ( " _csrf={csrf} &action=add&column={} &reference=demo%231" , cols[ 1 ] ) ) .await.body.contains ( " already on this project" ) ) ;
813
assert! ( act ( format! ( " _csrf={csrf} &action=add&column={} &reference=demo%2399" , cols[ 0 ] ) ) .await.body.contains ( " no issue or proposal #99" ) ) ;
814
assert! ( act ( format! ( " _csrf={csrf} &action=add&column={} &reference=nothere%231" , cols[ 0 ] ) ) .await.body.contains ( " no repository nothere" ) ) ;
815
816
let demo_card: i64 =
817
sqlx:: query_scalar( " SELECT c.id FROM project_cards c JOIN repositories r ON r.id = c.repo_id WHERE r.name = 'demo'" )
818
.fetch_one ( & app.db)
819
.await
820
.unwrap ( ) ;
821
act ( format! ( " _csrf={csrf} &action=move&card={demo_card} &column={} " , cols[ 2 ] ) ) .await;
822
let issue = get ( app, " /andy/demo/issue/1" , None ) .await.body;
823
assert! ( issue.contains ( " href=\" /projects/andy/1\" >Roadmap</a> <span class=\" mute\" >· Done" ) , " the issue shows where it is" ) ;
824
825
826
let anon = get ( app, " /projects/andy/1" , None ) .await.body;
827
assert! ( anon.contains ( " demo issue" ) & & anon.contains ( " Write the docs" ) & & ! anon.contains ( " secret issue" ) , " {anon}" ) ;
828
assert! ( ! anon.contains ( " name=\" action\" " ) , " and cannot change anything" ) ;
829
assert! ( get ( app, " /projects/andy/1" , Some ( & andy) ) .await.body.contains ( " secret issue" ) ) ;
830
831
let csrf_b = csrf_of ( & get ( app, " /settings" , Some ( & bob) ) .await.body) ;
832
assert_eq! (
833
post ( app, " /projects/andy/1" , Some ( & bob) , & format! ( " _csrf={csrf_b} &action=remove&card={demo_card} " ) ) .await.status,
834
StatusCode:: NOT_FOUND
835
) ;
836
act ( format! ( " _csrf={csrf} &action=settings&title=Roadmap&description=&private=1" ) ) .await;
837
assert_eq! ( get ( app, " /projects/andy/1" , Some ( & bob) ) .await.status, StatusCode:: NOT_FOUND ) ;
838
assert! ( ! get ( app, " /projects/andy" , None ) .await.body.contains ( " Roadmap" ) , " not even listed" ) ;
839
assert! ( ! get ( app, " /andy/demo/issue/1" , None ) .await.body.contains ( " Roadmap" ) , " nor on the issue" ) ;
840
841
842
act ( format! ( " _csrf={csrf} &action=remove_column&column={} " , cols[ 1 ] ) ) .await;
843
let note_col: i64 = sqlx:: query_scalar( " SELECT column_id FROM project_cards WHERE kind = 'note'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
844
assert_eq! ( note_col, cols[ 0 ] ) ;
845
act ( format! ( " _csrf={csrf} &action=remove_column&column={} " , cols[ 2 ] ) ) .await;
846
assert! ( act ( format! ( " _csrf={csrf} &action=remove_column&column={} " , cols[ 0 ] ) ) .await.body.contains ( " at least one column" ) ) ;
847
848
act ( format! ( " _csrf={csrf} &action=settings&title=Roadmap&closed=1" ) ) .await;
849
assert! ( act ( format! ( " _csrf={csrf} &action=add&column={} ¬e=late" , cols[ 0 ] ) ) .await.body.contains ( " This project is closed" ) ) ;
850
let r = act ( format! ( " _csrf={csrf} &action=delete" ) ) .await;
851
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/andy" ) ) ;
852
assert_eq! ( get ( app, " /projects/andy/1" , Some ( & andy) ) .await.status, StatusCode:: NOT_FOUND ) ;
853
assert_eq! ( get ( app, " /andy/demo/issue/1" , None ) .await.status, StatusCode:: OK ) ;
854
assert_eq! ( get ( app, " /projects/nobody" , None ) .await.status, StatusCode:: NOT_FOUND ) ;
855
}
c79a1111 Andy McCall 1 month ago ↶
856
857
# [ tokio ::test ( flavor = " multi_thread" ) ]
858
async fn issue_markdown_follows_the_default_bookmark ( ) {
859
let h = hub ( ) .await;
860
let app = & h.app;
861
register ( app, " andy" ) .await;
862
app.repos.create ( & RepoName:: parse( " andy/trunky" ) .unwrap ( ) , None ) .unwrap ( ) ;
863
let row = db:: repos:: create( & app.db, " andy" , " trunky" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
864
sqlx:: query( " UPDATE repositories SET default_bookmark = 'trunk' WHERE id = ?" ) .bind ( row.id) .execute ( & app.db) .await.unwrap ( ) ;
865
db:: issues:: create(
866
& app.db,
867
db:: issues:: NewIssue {
868
repo_id: row.id,
869
title: " Docs" ,
870
body: " See [the guide](docs/guide.md) and ." ,
871
author_id: None ,
872
author: " andy" ,
873
} ,
874
)
875
.await
876
.unwrap ( ) ;
877
let page = get ( app, " /andy/trunky/issue/1" , None ) .await.body;
878
assert! ( page.contains ( " href=\" /andy/trunky/file/trunk/docs/guide.md\" " ) , " {page}" ) ;
879
assert! ( page.contains ( " src=\" /andy/trunky/raw/trunk/img/a.png\" " ) ) ;
880
assert! ( ! page.contains ( " /file/main/" ) ) ;
881
}
b1e92a62 Andy McCall 25 days ago ↶
882
883
884
885
886
# [ tokio ::test ( flavor = " multi_thread" ) ]
887
async fn a_project_transfers_to_an_organisation ( ) {
888
let h = hub ( ) .await;
889
let app = & h.app;
890
891
892
let _first = register ( app, " ada" ) .await;
893
let andy = register ( app, " andy" ) .await;
894
let _bob = register ( app, " bob" ) .await;
895
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
896
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
897
db:: accounts:: create_org( & app.db, " retro" , " Retro Club" , andy_id) .await.unwrap ( ) ;
898
db:: accounts:: create_org( & app.db, " other" , " Someone else's" , bob_id) .await.unwrap ( ) ;
899
let list = get ( app, " /projects/andy" , Some ( & andy) ) .await.body;
900
let csrf = csrf_of ( & list) ;
901
let r = post ( app, " /projects/andy" , Some ( & andy) , & format! ( " _csrf={csrf} &title=Roadmap" ) ) .await;
902
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/andy/1" ) ) ;
903
post ( app, " /projects/andy/1" , Some ( & andy) , & format! ( " _csrf={csrf} &action=add¬e=Keep+me" ) ) .await;
904
905
906
let board = get ( app, " /projects/andy/1" , Some ( & andy) ) .await.body;
907
assert! ( board.contains ( " <option value=\" retro\" >retro</option>" ) & & ! board.contains ( " value=\" other\" " ) , " {board}" ) ;
908
let refused = post ( app, " /projects/andy/1" , Some ( & andy) , & format! ( " _csrf={csrf} &action=transfer&to=other" ) ) .await;
909
assert! ( refused.body.contains ( " You cannot make projects under other" ) , " {}" , refused.body) ;
910
911
let r = post ( app, " /projects/andy/1" , Some ( & andy) , & format! ( " _csrf={csrf} &action=transfer&to=retro" ) ) .await;
912
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/retro/1" ) , " {}" , r.body) ;
913
let moved = get ( app, " /projects/retro/1" , Some ( & andy) ) .await.body;
914
assert! ( moved.contains ( " Roadmap" ) & & moved.contains ( " Keep me" ) , " the cards come too" ) ;
915
let old = get ( app, " /projects/andy/1" , Some ( & andy) ) .await;
916
assert_eq! ( old.status, StatusCode:: PERMANENT_REDIRECT ) ;
917
assert_eq! ( old.location.as_deref ( ) , Some ( " /projects/retro/1" ) ) ;
918
919
920
let r = post ( app, " /projects/andy" , Some ( & andy) , & format! ( " _csrf={csrf} &title=Fresh" ) ) .await;
921
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/andy/1" ) ) ;
922
let fresh = get ( app, " /projects/andy/1" , Some ( & andy) ) .await;
923
assert_eq! ( fresh.status, StatusCode:: OK ) ;
924
assert! ( fresh.body.contains ( " Fresh" ) ) ;
925
}