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 ) ;
f8cf54e9 Andy McCall 22 days ago ↶
387
assert! ( page.body.contains ( " Waiting for your review</h2><span class=\" count-chip\" >2</span>" ) , " {}" , page.body) ;
25ab8c73 Andy McCall 1 month ago ↶
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
17ca6cdd Andy McCall 22 days ago ↶
392
393
394
assert! ( page.body.contains ( r # "<canvas id="backdrop" aria-hidden="true">"# ) ) ;
395
let start = page.body.find ( r # "id="backdrop-data">"# ) .unwrap ( ) + r # "id="backdrop-data">"# .len ( ) ;
396
let data: serde_json:: Value = serde_json:: from_str( & page.body[ start.. start + page.body[ start.. ] .find ( " </script>" ) .unwrap ( ) ] ) .unwrap ( ) ;
397
let repos: Vec< & str > = data[ " repos" ] .as_array ( ) .unwrap ( ) .iter ( ) .filter_map ( | r | r.as_str ( ) ) .collect ( ) ;
398
assert! ( repos.contains ( & " andy/demo" ) & & repos.contains ( & " andy/secret" ) , " {data}" ) ;
399
let events = data[ " events" ] .as_array ( ) .unwrap ( ) ;
400
assert! ( events.iter ( ) .any ( | e | e[ 1 ] = = 1 ) , " the proposals opened are marks: {data}" ) ;
401
assert! ( events.iter ( ) .all ( | e | e[ 0 ] .as_u64 ( ) .unwrap ( ) < repos.len ( ) as u64 ) ) ;
33bd0bd4 Andy McCall 22 days ago ↶
402
403
let res = oxygenhub:: router( app.clone ( ) )
404
.oneshot ( Request:: builder( ) .uri ( " /" ) .header ( header:: COOKIE , format! ( " oxyhub={andy} " ) ) .body ( Body:: empty( ) ) .unwrap ( ) )
405
.await
406
.unwrap ( ) ;
407
assert_eq! ( res.headers ( ) [ header:: CACHE_CONTROL ] , " private, no-store" ) ;
6246d1e5 Andy McCall 22 days ago ↶
408
409
assert! ( get ( app, " /login" , None )
410
.await
411
.body
412
.contains ( r # "<script type="application/json" id="backdrop-data">{"scene":"mark"}</script>"# ) ) ;
413
let lost = get ( app, " /no/such/page/here" , None ) .await;
414
assert_eq! ( lost.status, StatusCode:: NOT_FOUND ) ;
415
assert! ( lost.body.contains ( r # "{"scene":"adrift"}"# ) ) ;
0a3b244b Andy McCall 22 days ago ↶
416
417
let sky_of = | body : & str | {
418
let start = body.find ( r # "id="backdrop-data">"# ) .unwrap ( ) + r # "id="backdrop-data">"# .len ( ) ;
419
body[ start.. start + body[ start.. ] .find ( " </script>" ) .unwrap ( ) ] .to_string ( )
420
} ;
421
let hub_sky = sky_of ( & anon.body) ;
422
assert! ( hub_sky.contains ( " andy/demo" ) & & ! hub_sky.contains ( " andy/secret" ) , " {hub_sky}" ) ;
423
424
425
let explore = get ( app, " /explore" , Some ( & andy) ) .await;
426
assert! ( explore.body.contains ( " andy/secret" ) , " the list shows him his private repository" ) ;
427
let hub_sky = sky_of ( & explore.body) ;
428
assert! ( hub_sky.contains ( " andy/demo" ) & & ! hub_sky.contains ( " andy/secret" ) , " {hub_sky}" ) ;
429
assert! ( ! get ( app, " /andy" , None ) .await.body.contains ( " backdrop-data" ) ) ;
17ca6cdd Andy McCall 22 days ago ↶
430
431
let res = oxygenhub:: router( app.clone ( ) )
432
.oneshot ( Request:: builder( ) .uri ( " /static/backdrop/backdrop_bg.wasm" ) .body ( Body:: empty( ) ) .unwrap ( ) )
433
.await
434
.unwrap ( ) ;
435
assert_eq! ( res.headers ( ) [ header:: CONTENT_TYPE ] , " application/wasm" ) ;
436
assert! ( res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .starts_with ( b " \0 asm" ) ) ;
da8f7826 Andy McCall 22 days ago ↶
437
438
assert! ( sky_of ( & page.body) .contains ( r # ""live":"mine""# ) & & hub_sky.contains ( r # ""live":"hub""# ) ) ;
439
let since = db:: now_ms( ) - 3_600_000 ;
440
let arrivals = | res : Res| -> Vec< String> {
441
assert_eq! ( res.status, StatusCode:: OK , " {}" , res.body) ;
442
let v: serde_json:: Value = serde_json:: from_str( & res.body) .unwrap ( ) ;
443
v[ " events" ] .as_array ( ) .unwrap ( ) .iter ( ) .map ( | e | e[ 0 ] .as_str ( ) .unwrap ( ) .to_string ( ) ) .collect ( )
444
} ;
445
let mine = arrivals ( get ( app, & format! ( " /sky/arrivals?scene=mine&since={since} " ) , Some ( & andy) ) .await) ;
446
assert! ( mine.iter ( ) .any ( | r | r = = " andy/demo" ) & & mine.iter ( ) .any ( | r | r = = " andy/secret" ) , " {mine:?}" ) ;
447
let hub = arrivals ( get ( app, & format! ( " /sky/arrivals?scene=hub&since={since} " ) , Some ( & andy) ) .await) ;
448
assert! ( hub.iter ( ) .any ( | r | r = = " andy/demo" ) & & ! hub.iter ( ) .any ( | r | r = = " andy/secret" ) , " the hub's sky is public only: {hub:?}" ) ;
449
let anon_hub = arrivals ( get ( app, & format! ( " /sky/arrivals?scene=hub&since={since} " ) , None ) .await) ;
450
assert! ( ! anon_hub.iter ( ) .any ( | r | r = = " andy/secret" ) , " {anon_hub:?}" ) ;
451
assert! ( arrivals ( get ( app, & format! ( " /sky/arrivals?scene=mine&since={since} " ) , None ) .await) .is_empty ( ) , " signed out has no `mine`" ) ;
452
assert! (
453
arrivals ( get ( app, & format! ( " /sky/arrivals?scene=mine&since={since} " ) , Some ( & bob) ) .await) .is_empty ( ) ,
454
" Bob's sky has none of Andy's"
455
) ;
456
assert! (
457
arrivals ( get ( app, & format! ( " /sky/arrivals?scene=mine&since={} " , db:: now_ms( ) ) , Some ( & andy) ) .await) .is_empty ( ) ,
458
" nothing since now"
459
) ;
17ca6cdd Andy McCall 22 days ago ↶
460
25ab8c73 Andy McCall 1 month ago ↶
461
462
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
463
let p = db:: proposals:: get( & app.db, demo.id, 1 ) .await.unwrap ( ) .unwrap ( ) ;
464
db:: proposals:: add_review( & app.db, p.id, andy_id, " andy" , " approve" , " " ) .await.unwrap ( ) ;
f8cf54e9 Andy McCall 22 days ago ↶
465
assert! ( get ( app, " /" , Some ( & andy) ) .await.body.contains ( " Waiting for your review</h2><span class=\" count-chip\" >1</span>" ) ) ;
25ab8c73 Andy McCall 1 month ago ↶
466
467
468
let page = get ( app, " /" , Some ( & bob) ) .await;
33bd0bd4 Andy McCall 22 days ago ↶
469
470
471
let start = page.body.find ( r # "id="backdrop-data">"# ) .unwrap ( ) + r # "id="backdrop-data">"# .len ( ) ;
472
let sky = & page.body[ start.. start + page.body[ start.. ] .find ( " </script>" ) .unwrap ( ) ] ;
473
assert! ( ! sky.contains ( " andy/secret" ) & & ! sky.contains ( " andy/demo" ) , " {sky}" ) ;
f8cf54e9 Andy McCall 22 days ago ↶
474
assert! ( page.body.contains ( " Your proposals</h2><span class=\" count-chip\" >1</span>" ) , " {}" , page.body) ;
25ab8c73 Andy McCall 1 month ago ↶
475
assert! ( ! page.body.contains ( " Hidden work" ) ) ;
f8cf54e9 Andy McCall 22 days ago ↶
476
assert! ( page.body.contains ( " Waiting for your review</h2><span class=\" count-chip\" >0</span>" ) ) ;
25ab8c73 Andy McCall 1 month ago ↶
477
assert! ( page.body.contains ( " Faster startup" ) ) ;
478
let feed = & page.body[ page.body.find ( " Recent activity" ) .unwrap ( ) .. ] ;
479
assert! ( ! feed.contains ( " andy/secret" ) , " private activity leaked: {feed}" ) ;
480
481
482
assert! ( get ( app, " /explore" , Some ( & bob) ) .await.body.contains ( " andy /" ) ) ;
483
}
4edcf240 Andy McCall 1 month ago ↶
484
485
# [ tokio ::test ( flavor = " multi_thread" ) ]
486
async fn empty_repository_shows_a_quickstart ( ) {
487
let h = hub ( ) .await;
488
let app = & h.app;
489
let andy = register ( app, " andy" ) .await;
490
let bob = register ( app, " bob" ) .await;
491
app.repos.create ( & RepoName:: parse( " andy/fresh" ) .unwrap ( ) , None ) .unwrap ( ) ;
492
db:: repos:: create( & app.db, " andy" , " fresh" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
493
494
let page = get ( app, " /andy/fresh" , Some ( & andy) ) .await;
495
assert! ( page.body.contains ( " This repository is empty" ) , " {}" , page.body) ;
496
let clone_url = format! ( " {} /andy/fresh" , h.base) ;
497
assert! ( page.body.contains ( & format! ( " oxy remote add hub {clone_url} " ) ) , " {}" , page.body) ;
498
assert! ( page.body.contains ( " oxy config remote.hub.token YOUR-TOKEN" ) ) ;
499
assert! ( page.body.contains ( " oxy push hub" ) ) ;
500
assert! (
501
page.body.contains ( " mkdir fresh && cd fresh" ) | | page.body.contains ( " mkdir fresh && cd fresh" ) ,
502
" {}" ,
503
page.body
504
) ;
505
assert! ( page.body.contains ( " oxy git import --branch main" ) ) ;
506
assert! ( ! page.body.contains ( " oxyhub-ca.pem" ) , " no certificate step without a self-signed certificate" ) ;
507
508
509
let page = get ( app, " /andy/fresh" , Some ( & bob) ) .await;
510
assert! ( page.body.contains ( " Nothing has been pushed yet" ) & & ! page.body.contains ( " oxy push hub" ) , " {}" , page.body) ;
511
512
513
let work = tempfile:: tempdir( ) .unwrap ( ) ;
514
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
515
client.config.set ( " remote.hub.url" , & clone_url) .unwrap ( ) ;
516
client.config.set ( " remote.hub.token" , " site" ) .unwrap ( ) ;
517
client.save_config ( ) .unwrap ( ) ;
518
write ( work.path ( ) , " README.md" , b " # fresh\n " ) ;
519
client.commit ( " First change" , & [ ] ) .unwrap ( ) ;
520
assert! ( client.push ( " hub" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
521
let page = get ( app, " /andy/fresh" , Some ( & andy) ) .await;
522
assert! ( ! page.body.contains ( " This repository is empty" ) & & page.body.contains ( " README.md" ) , " {}" , page.body) ;
523
}
c40c2d3e Andy McCall 1 month ago ↶
524
525
# [ tokio ::test ( flavor = " multi_thread" ) ]
526
async fn theme_is_chosen_in_settings_and_remembered_in_a_cookie ( ) {
527
let h = hub ( ) .await;
528
let app = & h.app;
529
let andy = register ( app, " andy" ) .await;
530
let page = get ( app, " /settings" , Some ( & andy) ) .await;
531
assert! ( page.body.contains ( " Appearance" ) ) ;
d98a93c1 Andy McCall 1 month ago ↶
532
assert! ( page.body.contains ( r # "value="light" checked"# ) , " light by default: {}" , page.body) ;
c40c2d3e Andy McCall 1 month ago ↶
533
assert! ( page.body.contains ( " data-theme-toggle" ) , " the header has the switch" ) ;
534
let csrf = csrf_of ( & page.body) ;
d98a93c1 Andy McCall 1 month ago ↶
535
let r = post ( app, " /settings/appearance" , Some ( & andy) , & format! ( " _csrf={csrf} &theme=dark" ) ) .await;
c40c2d3e Andy McCall 1 month ago ↶
536
assert_eq! ( r.status, StatusCode:: SEE_OTHER ) ;
d98a93c1 Andy McCall 1 month ago ↶
537
assert_eq! ( r.cookie.as_deref ( ) , Some ( " dark" ) ) ;
c40c2d3e Andy McCall 1 month ago ↶
538
assert_eq! ( r.location.as_deref ( ) , Some ( " /settings" ) ) ;
d98a93c1 Andy McCall 1 month ago ↶
539
let page = send_with_theme ( app, & andy, " dark" ) .await;
540
assert! ( page.contains ( r # "value="dark" checked"# ) , " the form shows the cookie's theme" ) ;
c40c2d3e Andy McCall 1 month ago ↶
541
assert_eq! ( post ( app, " /settings/appearance" , Some ( & andy) , & format! ( " _csrf={csrf} &theme=purple" ) ) .await.status, StatusCode:: BAD_REQUEST ) ;
17ca6cdd Andy McCall 22 days ago ↶
542
543
544
545
assert! ( get ( app, " /settings" , Some ( & andy) ) .await.body.contains ( r # "name="backdrop" value="moving" checked"# ) ) ;
546
let res = oxygenhub:: router( app.clone ( ) )
547
.oneshot (
548
Request:: builder( )
549
.method ( " POST" )
550
.uri ( " /settings/appearance" )
551
.header ( header:: COOKIE , format! ( " oxyhub={andy} " ) )
552
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
553
.body ( Body:: from( format! ( " _csrf={csrf} &theme=light&backdrop=still" ) ) )
554
.unwrap ( ) ,
555
)
556
.await
557
.unwrap ( ) ;
558
let cookies: Vec< & str > = res.headers ( ) .get_all ( header:: SET_COOKIE ) .iter ( ) .filter_map ( | v | v.to_str ( ) .ok ( ) ) .collect ( ) ;
559
assert! (
560
cookies.iter ( ) .any ( | c | c.starts_with ( " theme=light;" ) ) & & cookies.iter ( ) .any ( | c | c.starts_with ( " backdrop=still;" ) ) ,
561
" {cookies:?}"
562
) ;
563
assert_eq! (
564
post ( app, " /settings/appearance" , Some ( & andy) , & format! ( " _csrf={csrf} &theme=light&backdrop=wild" ) ) .await.status,
565
StatusCode:: BAD_REQUEST
566
) ;
c40c2d3e Andy McCall 1 month ago ↶
567
assert_eq! ( post ( app, " /settings/appearance" , Some ( & andy) , " _csrf=nope&theme=light" ) .await.status, StatusCode:: BAD_REQUEST ) ;
568
}
569
570
async fn send_with_theme ( app : & Shared, session : & str , theme : & str ) -> String {
571
let req =
572
Request:: builder( ) .uri ( " /settings" ) .header ( header:: COOKIE , format! ( " oxyhub={session} ; theme={theme} " ) ) .body ( Body:: empty( ) ) .unwrap ( ) ;
573
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
574
String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( )
575
}
10174045 Andy McCall 1 month ago ↶
576
577
# [ tokio ::test ( flavor = " multi_thread" ) ]
578
async fn profile_and_organisation_pages ( ) {
579
let h = hub ( ) .await;
580
let app = & h.app;
581
let andy = register ( app, " andy" ) .await;
582
let bob = register ( app, " bob" ) .await;
583
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
584
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
585
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
586
db:: repos:: create( & app.db, " andy" , " demo" , " A demo" , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
587
app.repos.create ( & RepoName:: parse( " andy/hidden" ) .unwrap ( ) , None ) .unwrap ( ) ;
588
db:: repos:: create( & app.db, " andy" , " hidden" , " " , hub_api:: Visibility:: Private) .await.unwrap ( ) ;
589
590
591
let csrf = csrf_of ( & get ( app, " /settings" , Some ( & andy) ) .await.body) ;
592
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;
593
assert! ( r.body.contains ( " must be an http" ) , " {}" , r.body) ;
594
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;
595
assert! ( r.body.contains ( " Profile saved" ) , " {}" , r.body) ;
596
597
let org = db:: accounts:: create_org( & app.db, " retro" , " Retro Club" , andy_id) .await.unwrap ( ) ;
598
db:: accounts:: set_org_member( & app.db, org.id, bob_id, " member" ) .await.unwrap ( ) ;
599
let team = db:: accounts:: create_team( & app.db, org.id, " core" ) .await.unwrap ( ) ;
600
db:: accounts:: add_team_member( & app.db, team.id, bob_id) .await.unwrap ( ) ;
601
602
let page = get ( app, " /andy" , Some ( & bob) ) .await;
603
assert_eq! ( page.status, StatusCode:: OK ) ;
604
for want in [ " Andy McCall" , " Earth" , r # "href="https://example.org""# , " /avatar/andy" , " Signing keys" , " /retro" , " demo" ] {
605
assert! ( page.body.contains ( want) , " profile lacks {want}: {}" , page.body) ;
606
}
607
assert! ( ! page.body.contains ( " <b>oxy</b>" ) , " the bio is escaped" ) ;
608
assert! ( ! page.body.contains ( " [email protected] " ) , " the email stays private" ) ;
609
assert! ( ! page.body.contains ( " /andy/hidden" ) , " bob cannot see the private repository" ) ;
610
assert! ( ! page.body.contains ( " edit profile" ) , " only the owner gets the edit link" ) ;
611
assert! ( get ( app, " /andy" , Some ( & andy) ) .await.body.contains ( " /andy/hidden" ) , " andy sees his own private repository" ) ;
612
613
let page = get ( app, " /retro" , Some ( & bob) ) .await;
614
assert_eq! ( page.status, StatusCode:: OK ) ;
61ecbd29 Andy McCall 22 days ago ↶
615
for want in [ " Retro Club" , " organisation" , r # "Members <span class="count-chip">2</span>"# , " core" , " 1 member" ] {
10174045 Andy McCall 1 month ago ↶
616
assert! ( page.body.contains ( want) , " org page lacks {want}: {}" , page.body) ;
617
}
618
assert! ( ! page.body.contains ( " /orgs/retro/settings" ) , " bob is not an owner" ) ;
619
assert! ( get ( app, " /retro" , Some ( & andy) ) .await.body.contains ( " /orgs/retro/settings" ) ) ;
620
621
let svg = get ( app, " /avatar/andy" , None ) .await;
622
assert_eq! ( svg.status, StatusCode:: OK ) ;
623
assert! ( svg.body.starts_with ( " <svg" ) ) ;
624
assert_eq! ( svg.body, get ( app, " /avatar/ANDY" , None ) .await.body) ;
625
assert_eq! ( get ( app, " /nobody-here" , None ) .await.status, StatusCode:: NOT_FOUND ) ;
626
}
9df855f2 Andy McCall 1 month ago ↶
627
628
# [ tokio ::test ( flavor = " multi_thread" ) ]
629
async fn diffs_mark_words_hide_whitespace_and_collapse ( ) {
630
let h = hub ( ) .await;
631
let app = & h.app;
632
let andy = register ( app, " andy" ) .await;
633
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
634
db:: repos:: create( & app.db, " andy" , " demo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
635
let work = tempfile:: tempdir( ) .unwrap ( ) ;
636
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
637
client.config.set ( " remote.hub.url" , & format! ( " {} /andy/demo" , h.base) ) .unwrap ( ) ;
638
client.config.set ( " remote.hub.token" , " site" ) .unwrap ( ) ;
639
client.save_config ( ) .unwrap ( ) ;
640
write ( work.path ( ) , " main.rs" , b " fn main() {\n setup();\n let answer = 42;\n }\n " ) ;
641
client.commit ( " first" , & [ ] ) .unwrap ( ) ;
642
write ( work.path ( ) , " main.rs" , b " fn main() {\n setup();\n let answer = 43;\n }\n " ) ;
643
let second = client.commit ( " reindent and change the answer" , & [ ] ) .unwrap ( ) ;
644
assert! ( client.push ( " hub" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
645
646
let url = format! ( " /andy/demo/change/{} " , second.to_hex ( ) ) ;
647
let page = get ( app, & url, Some ( & andy) ) .await;
648
assert_eq! ( page.status, StatusCode:: OK ) ;
649
assert! ( page.body.contains ( r # "<details class="file-diff""# ) , " files collapse: {}" , page.body) ;
650
assert! ( page.body.contains ( r # "<span class="wx">43</span>"# ) , " the changed word is marked: {}" , page.body) ;
651
assert! ( page.body.contains ( " hide whitespace changes" ) ) ;
652
assert! ( page.body.contains ( " data-diff-mode=\" split\" " ) ) ;
653
let adds = | b : & str | b.matches ( r # "<tr class="add">"# ) .count ( ) ;
654
assert_eq! ( adds ( & page.body) , 2 , " re-indent and answer both changed" ) ;
655
656
let quiet = get ( app, & format! ( " {url} ?w=1" ) , Some ( & andy) ) .await;
657
assert_eq! ( adds ( & quiet.body) , 1 , " only the answer changed ignoring whitespace: {}" , quiet.body) ;
658
assert! ( quiet.body.contains ( " show whitespace changes" ) ) ;
659
660
assert! ( quiet.body.contains ( " ?w=0" ) , " the toggle leads back" ) ;
661
}
07b1a5bb Andy McCall 1 month ago ↶
662
663
# [ tokio ::test ( flavor = " multi_thread" ) ]
664
async fn fork_and_propose_upstream ( ) {
665
let h = hub ( ) .await;
666
let app = & h.app;
667
let andy = register ( app, " andy" ) .await;
668
let bob = register ( app, " bob" ) .await;
669
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
670
db:: repos:: create( & app.db, " andy" , " demo" , " The demo" , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
671
let work = tempfile:: tempdir( ) .unwrap ( ) ;
672
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
673
client.config.set ( " remote.hub.url" , & format! ( " {} /andy/demo" , h.base) ) .unwrap ( ) ;
674
client.config.set ( " remote.hub.token" , " site" ) .unwrap ( ) ;
675
client.save_config ( ) .unwrap ( ) ;
676
write ( work.path ( ) , " README.md" , b " # demo\n " ) ;
677
client.commit ( " first" , & [ ] ) .unwrap ( ) ;
678
assert! ( client.push ( " hub" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
679
680
681
let page = get ( app, " /andy/demo" , Some ( & bob) ) .await;
682
assert! ( page.body.contains ( " /andy/demo/fork" ) , " a fork button: {}" , page.body) ;
683
let csrf = csrf_of ( & get ( app, " /andy/demo/fork" , Some ( & bob) ) .await.body) ;
684
let r = post ( app, " /andy/demo/fork" , Some ( & bob) , & format! ( " _csrf={csrf} &owner=andy&name=other" ) ) .await;
685
assert! ( r.body.contains ( " cannot create repositories under that owner" ) , " {}" , r.body) ;
686
let r = post ( app, " /andy/demo/fork" , Some ( & bob) , & format! ( " _csrf={csrf} &owner=bob&name=demo" ) ) .await;
687
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
688
assert_eq! ( r.location.as_deref ( ) , Some ( " /bob/demo" ) ) ;
689
let forked = get ( app, " /bob/demo" , Some ( & bob) ) .await;
690
assert! ( forked.body.contains ( " README.md" ) & & forked.body.contains ( " forked from" ) , " {}" , forked.body) ;
691
assert! ( forked.body.contains ( " propose to andy/demo" ) ) ;
692
assert! ( get ( app, " /andy/demo" , None ) .await.body.contains ( " 1 fork" ) ) ;
693
assert! ( get ( app, " /andy/demo/forks" , None ) .await.body.contains ( " /bob/demo" ) ) ;
694
let fork_row = db:: repos:: get( & app.db, " bob" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
695
let up_row = db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
696
assert_eq! ( fork_row.forked_from, Some ( up_row.id) ) ;
697
assert_eq! ( fork_row.description, " The demo" ) ;
698
let fork_repo = app.repos.open ( & RepoName:: parse( " bob/demo" ) .unwrap ( ) ) .unwrap ( ) ;
699
let up_repo = app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) ;
700
assert_ne! ( fork_repo.id ( ) .unwrap ( ) , up_repo.id ( ) .unwrap ( ) , " a fork is a repository of its own" ) ;
701
assert_eq! ( fork_repo.bookmark_heads ( ) .unwrap ( ) , up_repo.bookmark_heads ( ) .unwrap ( ) ) ;
702
assert_eq! ( fork_repo.fsck ( ) .unwrap ( ) .errors ( ) , 0 ) ;
703
704
705
let bw = tempfile:: tempdir( ) .unwrap ( ) ;
706
let mut mine = Repo:: clone( & format! ( " {} /bob/demo" , h.base) , & bw.path ( ) .join ( " c" ) ) .unwrap ( ) ;
707
mine.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
708
mine.save_config ( ) .unwrap ( ) ;
709
mine.bookmark_create ( " feat" , None ) .unwrap ( ) ;
710
mine.bookmark_switch ( " feat" , false ) .unwrap ( ) ;
711
write ( & bw.path ( ) .join ( " c" ) , " feature.txt" , b " new\n " ) ;
712
mine.commit ( " Add a feature" , & [ ] ) .unwrap ( ) ;
713
assert! ( mine.push_bookmark ( " origin" , " feat" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
714
let csrf = csrf_of ( & get ( app, " /andy/demo/proposals/new?source=bob/demo:feat" , Some ( & bob) ) .await.body) ;
715
let r =
716
post ( app, " /andy/demo/proposals/new" , Some ( & bob) , & format! ( " _csrf={csrf} &title=From+my+fork&target=main&source=bob%2Fdemo%3Afeat" ) )
717
.await;
718
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
719
let p = get ( app, & r.location.unwrap ( ) , Some ( & andy) ) .await;
720
assert! ( p.body.contains ( " From my fork" ) & & p.body.contains ( " fork bob/demo" ) , " {}" , p.body) ;
721
let files = get ( app, " /andy/demo/proposal/1?tab=files" , Some ( & andy) ) .await;
722
assert! ( files.body.contains ( " feature.txt" ) , " {}" , files.body) ;
723
}
1553bfc9 Andy McCall 1 month ago ↶
724
725
# [ tokio ::test ( flavor = " multi_thread" ) ]
726
async fn stars_topics_and_explore ( ) {
727
let h = hub ( ) .await;
728
let app = & h.app;
729
let andy = register ( app, " andy" ) .await;
730
let bob = register ( app, " bob" ) .await;
731
for ( name, desc) in [ ( " alpha" , " An emulator" ) , ( " beta" , " A compiler" ) , ( " gamma" , " Retro games" ) ] {
732
app.repos.create ( & RepoName:: parse( & format! ( " andy/{name} " ) ) .unwrap ( ) , None ) .unwrap ( ) ;
733
db:: repos:: create( & app.db, " andy" , name, desc, hub_api:: Visibility:: Public) .await.unwrap ( ) ;
734
}
735
736
737
let csrf = csrf_of ( & get ( app, " /andy/gamma/settings" , Some ( & andy) ) .await.body) ;
738
let r = post (
739
app,
740
" /andy/gamma/settings" ,
741
Some ( & andy) ,
742
& format! ( " _csrf={csrf} &description=Retro+games&default_bookmark=main&visibility=public&topics=c%2B%2B" ) ,
743
)
744
.await;
745
assert! ( r.body.contains ( " is not a topic" ) , " {}" , r.body) ;
746
let r = post (
747
app,
748
" /andy/gamma/settings" ,
749
Some ( & andy) ,
750
& format! ( " _csrf={csrf} &description=Retro+games&default_bookmark=main&visibility=public&topics=Retro%2C+6502" ) ,
751
)
752
.await;
753
assert! ( r.body.contains ( " Saved" ) , " {}" , r.body) ;
754
assert_eq! ( db:: repos:: get( & app.db, " andy" , " gamma" ) .await.unwrap ( ) .unwrap ( ) .topics, " retro 6502" ) ;
755
let page = get ( app, " /andy/gamma" , None ) .await;
756
assert! ( page.body.contains ( r # "href="/explore?topic=retro""# ) , " {}" , page.body) ;
757
758
759
for ( who, repo) in [ ( & bob, " beta" ) , ( & bob, " gamma" ) , ( & andy, " gamma" ) , ( & andy, " alpha" ) ] {
760
let csrf = csrf_of ( & get ( app, & format! ( " /andy/{repo} " ) , Some ( who) ) .await.body) ;
761
assert_eq! ( post ( app, & format! ( " /andy/{repo} /star" ) , Some ( who) , & format! ( " _csrf={csrf} " ) ) .await.status, StatusCode:: SEE_OTHER ) ;
762
}
763
let csrf = csrf_of ( & get ( app, " /andy/alpha" , Some ( & andy) ) .await.body) ;
764
post ( app, " /andy/alpha/star" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
765
assert! ( get ( app, " /andy/gamma" , Some ( & bob) ) .await.body.contains ( " ★ UNSTAR" ) ) ;
7f283f8e Andy McCall 22 days ago ↶
766
assert! ( get ( app, " /andy/gamma" , None ) .await.body.contains ( r # "title="2 stars">"# ) ) ;
767
assert! ( get ( app, " /andy/alpha" , None ) .await.body.contains ( r # "title="0 stars">"# ) , " unstarred again" ) ;
1553bfc9 Andy McCall 1 month ago ↶
768
769
770
let order = | b : & str | {
771
[ " alpha" , " beta" , " gamma" ]
772
.into_iter ( )
773
.map ( | n | ( b.find ( & format! ( " /andy/{n} \" " ) ) .unwrap_or ( usize :: MAX ) , n) )
774
.collect:: < std:: collections:: BTreeMap< _ , _ > > ( )
775
.into_values ( )
776
.collect:: < Vec< _ > > ( )
777
} ;
778
assert_eq! ( order ( & get ( app, " /explore?sort=stars" , None ) .await.body) [ .. 2 ] , [ " gamma" , " beta" ] ) ;
779
assert_eq! ( order ( & get ( app, " /explore?sort=name" , None ) .await.body) , [ " alpha" , " beta" , " gamma" ] ) ;
780
let filtered = get ( app, " /explore?q=compiler" , None ) .await.body;
781
assert! ( filtered.contains ( " /andy/beta\" " ) & & ! filtered.contains ( " /andy/alpha\" " ) , " {filtered}" ) ;
782
let by_topic = get ( app, " /explore?topic=6502" , None ) .await.body;
783
assert! ( by_topic.contains ( " /andy/gamma\" " ) & & ! by_topic.contains ( " /andy/beta\" " ) , " {by_topic}" ) ;
784
}
1ac84c62 Andy McCall 1 month ago ↶
785
786
# [ tokio ::test ( flavor = " multi_thread" ) ]
787
async fn lists_filter_sort_page_and_count ( ) {
788
let h = hub ( ) .await;
789
let app = & h.app;
790
let andy = register ( app, " andy" ) .await;
791
let _bob = register ( app, " bob" ) .await;
792
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
793
let repo = db:: repos:: create( & app.db, " andy" , " demo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
794
for n in 0 .. 30 {
795
let author = if n % 3 = = 0 { " bob" } else { " andy" } ;
796
db:: issues:: create(
797
& app.db,
798
db:: issues:: NewIssue { repo_id: repo.id, title: & format! ( " Issue number {n} " ) , body: " " , author_id: None , author } ,
799
)
800
.await
801
.unwrap ( ) ;
802
}
803
let ui = db:: issues:: upsert_label( & app.db, repo.id, " ui" , " #00f0ff" , " " ) .await.unwrap ( ) ;
804
for ( title, author, target) in [ ( " Faster menus" , " bob" , " main" ) , ( " New theme" , " andy" , " main" ) , ( " Release prep" , " andy" , " stable" ) ] {
805
let p = db:: proposals:: create(
806
& app.db,
807
db:: proposals:: NewProposal {
808
repo_id: repo.id,
809
title,
810
body: " " ,
811
author_id: None ,
812
author,
813
draft: false ,
814
target,
815
source_kind: " changes" ,
816
source_repo_id: None ,
817
source_bookmark: " " ,
818
source_changes: " " ,
819
} ,
820
)
821
.await
822
.unwrap ( ) ;
823
if title = = " New theme" {
824
db:: issues:: set_proposal_label( & app.db, p.id, ui.id, true ) .await.unwrap ( ) ;
825
}
826
}
827
828
829
let page = get ( app, " /andy/demo/issues" , Some ( & andy) ) .await;
830
assert! ( page.body.contains ( r # "Issues <span class="count-chip">30</span>"# ) , " {}" , page.body) ;
831
assert! ( page.body.contains ( r # "Proposals <span class="count-chip">3</span>"# ) ) ;
832
833
834
let rows = | b : & str | b.matches ( " /andy/demo/issue/" ) .count ( ) ;
835
assert_eq! ( rows ( & page.body) , 25 ) ;
836
assert! ( page.body.contains ( " page 1 of 2" ) & & page.body.contains ( " page=2" ) , " a pager" ) ;
837
let second = get ( app, " /andy/demo/issues?page=2" , Some ( & andy) ) .await;
838
assert_eq! ( rows ( & second.body) , 5 ) ;
839
assert! ( second.body.contains ( " Issue number 0<" ) , " the oldest are last" ) ;
840
let oldest = get ( app, " /andy/demo/issues?sort=oldest" , Some ( & andy) ) .await;
841
assert! ( oldest.body.find ( " Issue number 0<" ) .unwrap ( ) < oldest.body.find ( " Issue number 1<" ) .unwrap ( ) ) ;
842
let bobs = get ( app, " /andy/demo/issues?q=author:bob" , Some ( & andy) ) .await;
843
assert_eq! ( rows ( & bobs.body) , 10 , " {}" , bobs.body) ;
844
845
846
let by_bob = get ( app, " /andy/demo/proposals?q=author:bob" , Some ( & andy) ) .await.body;
847
assert! ( by_bob.contains ( " Faster menus" ) & & ! by_bob.contains ( " New theme" ) ) ;
848
let ui_only = get ( app, " /andy/demo/proposals?q=label:ui" , Some ( & andy) ) .await.body;
849
assert! ( ui_only.contains ( " New theme" ) & & ! ui_only.contains ( " Faster menus" ) & & ui_only.contains ( r # "class="label""# ) ) ;
850
let stable = get ( app, " /andy/demo/proposals?q=target:stable" , Some ( & andy) ) .await.body;
851
assert! ( stable.contains ( " Release prep" ) & & ! stable.contains ( " New theme" ) ) ;
852
}
e1ec9fbf Andy McCall 1 month ago ↶
853
854
# [ tokio ::test ( flavor = " multi_thread" ) ]
855
async fn project_boards ( ) {
856
let h = hub ( ) .await;
857
let app = & h.app;
858
let andy = register ( app, " andy" ) .await;
859
let bob = register ( app, " bob" ) .await;
860
for ( name, vis) in [ ( " demo" , hub_api:: Visibility:: Public) , ( " secret" , hub_api:: Visibility:: Private) ] {
861
app.repos.create ( & RepoName:: parse( & format! ( " andy/{name} " ) ) .unwrap ( ) , None ) .unwrap ( ) ;
862
let row = db:: repos:: create( & app.db, " andy" , name, " " , vis) .await.unwrap ( ) ;
863
db:: issues:: create(
864
& app.db,
865
db:: issues:: NewIssue { repo_id: row.id, title: & format! ( " {name} issue" ) , body: " " , author_id: None , author: " andy" } ,
866
)
867
.await
868
.unwrap ( ) ;
869
}
a024e800 Andy McCall 26 days ago ↶
870
871
872
let home = get ( app, " /andy/demo" , Some ( & andy) ) .await.body;
873
assert! (
c9aacc0e Andy McCall 23 days ago ↶
874
home.contains ( " href=\" /projects/andy\" ><svg class=\" ic\" " ) & & home.contains ( " title=\" andy's project boards\" >Projects</a>" ) ,
a024e800 Andy McCall 26 days ago ↶
875
" site bar and tab"
876
) ;
877
let list = get ( app, " /projects/andy" , Some ( & andy) ) .await.body;
878
assert! ( list.contains ( " href=\" #new\" >+ NEW PROJECT</a>" ) & & list.contains ( " id=\" new\" " ) , " the button and the form" ) ;
879
assert! ( get ( app, " /projects/andy" , Some ( & bob) ) .await.body.contains ( " Only andy" ) , " who can make one" ) ;
880
let csrf = csrf_of ( & list) ;
e1ec9fbf Andy McCall 1 month ago ↶
881
let r = post ( app, " /projects/andy" , Some ( & andy) , & format! ( " _csrf={csrf} &title=Roadmap&description=Next+steps" ) ) .await;
882
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/andy/1" ) , " {}" , r.body) ;
7b26e611 Andy McCall 25 days ago ↶
883
884
let list = get ( app, " /projects/andy" , Some ( & andy) ) .await.body;
885
assert! (
886
list.contains ( " >Roadmap</a>" ) & & list.contains ( " Next steps" ) & & list.contains ( " Open <span class=\" count-chip\" >1</span>" ) ,
887
" {list}"
888
) ;
889
let filtered = get ( app, " /projects/andy?q=roadm" , Some ( & andy) ) .await.body;
890
assert! ( filtered.contains ( " >Roadmap</a>" ) , " a filter matches part of the title, any case" ) ;
891
let none = get ( app, " /projects/andy?q=nothing+like+it" , Some ( & andy) ) .await.body;
892
assert! ( none.contains ( " No open project matches" ) & & ! none.contains ( " >Roadmap</a>" ) , " {none}" ) ;
893
let closed = get ( app, " /projects/andy?state=closed" , Some ( & andy) ) .await.body;
894
assert! ( closed.contains ( " No closed projects." ) & & closed.contains ( " Closed <span class=\" count-chip\" >0</span>" ) , " {closed}" ) ;
e1ec9fbf Andy McCall 1 month ago ↶
895
let board = get ( app, " /projects/andy/1" , Some ( & andy) ) .await.body;
896
for col in [ " Backlog" , " Doing" , " Done" ] {
897
assert! ( board.contains ( & format! ( " >{col} <span" ) ) , " {col}" ) ;
898
}
899
let cols: Vec< i64 > = sqlx:: query_scalar( " SELECT id FROM project_columns ORDER BY position" ) .fetch_all ( & app.db) .await.unwrap ( ) ;
900
let act = | form : String| {
901
let andy = andy.clone ( ) ;
902
async move { post ( app, " /projects/andy/1" , Some ( & andy) , & form) .await }
903
} ;
904
905
assert_eq! (
906
act ( format! ( " _csrf={csrf} &action=add&column={} &reference=demo%231" , cols[ 0 ] ) ) .await.location.as_deref ( ) ,
907
Some ( " /projects/andy/1" )
908
) ;
909
act ( format! ( " _csrf={csrf} &action=add&column={} &repo=secret&reference=%231" , cols[ 0 ] ) ) .await;
910
act ( format! ( " _csrf={csrf} &action=add&column={} ¬e=Write+the+docs" , cols[ 1 ] ) ) .await;
911
assert! ( act ( format! ( " _csrf={csrf} &action=add&column={} &reference=demo%231" , cols[ 1 ] ) ) .await.body.contains ( " already on this project" ) ) ;
912
assert! ( act ( format! ( " _csrf={csrf} &action=add&column={} &reference=demo%2399" , cols[ 0 ] ) ) .await.body.contains ( " no issue or proposal #99" ) ) ;
913
assert! ( act ( format! ( " _csrf={csrf} &action=add&column={} &reference=nothere%231" , cols[ 0 ] ) ) .await.body.contains ( " no repository nothere" ) ) ;
914
915
let demo_card: i64 =
916
sqlx:: query_scalar( " SELECT c.id FROM project_cards c JOIN repositories r ON r.id = c.repo_id WHERE r.name = 'demo'" )
917
.fetch_one ( & app.db)
918
.await
919
.unwrap ( ) ;
920
act ( format! ( " _csrf={csrf} &action=move&card={demo_card} &column={} " , cols[ 2 ] ) ) .await;
921
let issue = get ( app, " /andy/demo/issue/1" , None ) .await.body;
922
assert! ( issue.contains ( " href=\" /projects/andy/1\" >Roadmap</a> <span class=\" mute\" >· Done" ) , " the issue shows where it is" ) ;
923
924
925
let anon = get ( app, " /projects/andy/1" , None ) .await.body;
926
assert! ( anon.contains ( " demo issue" ) & & anon.contains ( " Write the docs" ) & & ! anon.contains ( " secret issue" ) , " {anon}" ) ;
927
assert! ( ! anon.contains ( " name=\" action\" " ) , " and cannot change anything" ) ;
928
assert! ( get ( app, " /projects/andy/1" , Some ( & andy) ) .await.body.contains ( " secret issue" ) ) ;
929
930
let csrf_b = csrf_of ( & get ( app, " /settings" , Some ( & bob) ) .await.body) ;
931
assert_eq! (
932
post ( app, " /projects/andy/1" , Some ( & bob) , & format! ( " _csrf={csrf_b} &action=remove&card={demo_card} " ) ) .await.status,
933
StatusCode:: NOT_FOUND
934
) ;
935
act ( format! ( " _csrf={csrf} &action=settings&title=Roadmap&description=&private=1" ) ) .await;
936
assert_eq! ( get ( app, " /projects/andy/1" , Some ( & bob) ) .await.status, StatusCode:: NOT_FOUND ) ;
937
assert! ( ! get ( app, " /projects/andy" , None ) .await.body.contains ( " Roadmap" ) , " not even listed" ) ;
938
assert! ( ! get ( app, " /andy/demo/issue/1" , None ) .await.body.contains ( " Roadmap" ) , " nor on the issue" ) ;
939
940
941
act ( format! ( " _csrf={csrf} &action=remove_column&column={} " , cols[ 1 ] ) ) .await;
942
let note_col: i64 = sqlx:: query_scalar( " SELECT column_id FROM project_cards WHERE kind = 'note'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
943
assert_eq! ( note_col, cols[ 0 ] ) ;
944
act ( format! ( " _csrf={csrf} &action=remove_column&column={} " , cols[ 2 ] ) ) .await;
945
assert! ( act ( format! ( " _csrf={csrf} &action=remove_column&column={} " , cols[ 0 ] ) ) .await.body.contains ( " at least one column" ) ) ;
946
947
act ( format! ( " _csrf={csrf} &action=settings&title=Roadmap&closed=1" ) ) .await;
948
assert! ( act ( format! ( " _csrf={csrf} &action=add&column={} ¬e=late" , cols[ 0 ] ) ) .await.body.contains ( " This project is closed" ) ) ;
949
let r = act ( format! ( " _csrf={csrf} &action=delete" ) ) .await;
950
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/andy" ) ) ;
951
assert_eq! ( get ( app, " /projects/andy/1" , Some ( & andy) ) .await.status, StatusCode:: NOT_FOUND ) ;
952
assert_eq! ( get ( app, " /andy/demo/issue/1" , None ) .await.status, StatusCode:: OK ) ;
953
assert_eq! ( get ( app, " /projects/nobody" , None ) .await.status, StatusCode:: NOT_FOUND ) ;
954
}
c79a1111 Andy McCall 1 month ago ↶
955
956
# [ tokio ::test ( flavor = " multi_thread" ) ]
957
async fn issue_markdown_follows_the_default_bookmark ( ) {
958
let h = hub ( ) .await;
959
let app = & h.app;
960
register ( app, " andy" ) .await;
961
app.repos.create ( & RepoName:: parse( " andy/trunky" ) .unwrap ( ) , None ) .unwrap ( ) ;
962
let row = db:: repos:: create( & app.db, " andy" , " trunky" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
963
sqlx:: query( " UPDATE repositories SET default_bookmark = 'trunk' WHERE id = ?" ) .bind ( row.id) .execute ( & app.db) .await.unwrap ( ) ;
964
db:: issues:: create(
965
& app.db,
966
db:: issues:: NewIssue {
967
repo_id: row.id,
968
title: " Docs" ,
969
body: " See [the guide](docs/guide.md) and ." ,
970
author_id: None ,
971
author: " andy" ,
972
} ,
973
)
974
.await
975
.unwrap ( ) ;
976
let page = get ( app, " /andy/trunky/issue/1" , None ) .await.body;
977
assert! ( page.contains ( " href=\" /andy/trunky/file/trunk/docs/guide.md\" " ) , " {page}" ) ;
978
assert! ( page.contains ( " src=\" /andy/trunky/raw/trunk/img/a.png\" " ) ) ;
979
assert! ( ! page.contains ( " /file/main/" ) ) ;
980
}
b1e92a62 Andy McCall 25 days ago ↶
981
982
983
984
985
# [ tokio ::test ( flavor = " multi_thread" ) ]
986
async fn a_project_transfers_to_an_organisation ( ) {
987
let h = hub ( ) .await;
988
let app = & h.app;
989
990
991
let _first = register ( app, " ada" ) .await;
992
let andy = register ( app, " andy" ) .await;
993
let _bob = register ( app, " bob" ) .await;
994
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
995
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
996
db:: accounts:: create_org( & app.db, " retro" , " Retro Club" , andy_id) .await.unwrap ( ) ;
997
db:: accounts:: create_org( & app.db, " other" , " Someone else's" , bob_id) .await.unwrap ( ) ;
998
let list = get ( app, " /projects/andy" , Some ( & andy) ) .await.body;
999
let csrf = csrf_of ( & list) ;
1000
let r = post ( app, " /projects/andy" , Some ( & andy) , & format! ( " _csrf={csrf} &title=Roadmap" ) ) .await;
1001
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/andy/1" ) ) ;
1002
post ( app, " /projects/andy/1" , Some ( & andy) , & format! ( " _csrf={csrf} &action=add¬e=Keep+me" ) ) .await;
1003
1004
1005
let board = get ( app, " /projects/andy/1" , Some ( & andy) ) .await.body;
1006
assert! ( board.contains ( " <option value=\" retro\" >retro</option>" ) & & ! board.contains ( " value=\" other\" " ) , " {board}" ) ;
1007
let refused = post ( app, " /projects/andy/1" , Some ( & andy) , & format! ( " _csrf={csrf} &action=transfer&to=other" ) ) .await;
1008
assert! ( refused.body.contains ( " You cannot make projects under other" ) , " {}" , refused.body) ;
1009
1010
let r = post ( app, " /projects/andy/1" , Some ( & andy) , & format! ( " _csrf={csrf} &action=transfer&to=retro" ) ) .await;
1011
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/retro/1" ) , " {}" , r.body) ;
1012
let moved = get ( app, " /projects/retro/1" , Some ( & andy) ) .await.body;
1013
assert! ( moved.contains ( " Roadmap" ) & & moved.contains ( " Keep me" ) , " the cards come too" ) ;
1014
let old = get ( app, " /projects/andy/1" , Some ( & andy) ) .await;
1015
assert_eq! ( old.status, StatusCode:: PERMANENT_REDIRECT ) ;
1016
assert_eq! ( old.location.as_deref ( ) , Some ( " /projects/retro/1" ) ) ;
1017
1018
1019
let r = post ( app, " /projects/andy" , Some ( & andy) , & format! ( " _csrf={csrf} &title=Fresh" ) ) .await;
1020
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/andy/1" ) ) ;
1021
let fresh = get ( app, " /projects/andy/1" , Some ( & andy) ) .await;
1022
assert_eq! ( fresh.status, StatusCode:: OK ) ;
1023
assert! ( fresh.body.contains ( " Fresh" ) ) ;
1024
}
d7d18246 Andy McCall 22 days ago ↶
1025
1026
1027
1028
# [ tokio ::test ( flavor = " multi_thread" ) ]
1029
async fn backups_are_set_up_in_settings ( ) {
1030
let h = hub ( ) .await;
1031
let app = & h.app;
1032
let andy = register ( app, " andy" ) .await;
1033
let page = get ( app, " /settings/backups" , Some ( & andy) ) .await;
1034
assert_eq! ( page.status, StatusCode:: OK ) ;
1035
assert! ( page.body.contains ( " href=\" /settings/backups\" class=\" on\" " ) & & page.body.contains ( " No runs yet." ) , " {}" , page.body) ;
1036
let csrf = csrf_of ( & page.body) ;
1037
1038
let bad = post (
1039
app,
1040
" /settings/backups" ,
1041
Some ( & andy) ,
1042
& format! ( " _csrf={csrf} &every=hourly&destination=gcs&bucket=andy-backup&provider=nope&keep_days=30" ) ,
1043
)
1044
.await;
1045
assert! ( bad.body.contains ( " workload identity provider" ) , " {}" , bad.body) ;
1046
let dir =
1047
post ( app, " /settings/backups" , Some ( & andy) , & format! ( " _csrf={csrf} &every=hourly&destination=dir&dir=%2Ftmp&keep_days=30" ) ) .await;
1048
assert! ( dir.body.contains ( " Pick one of the directories this hub allows." ) ) ;
1049
let provider =
1050
" %2F%2Fiam.googleapis.com%2Fprojects%2F1%2Flocations%2Fglobal%2FworkloadIdentityPools%2Foxygenhub%2Fproviders%2Foxygenhub-io" ;
1051
let ok = post (
1052
app,
1053
" /settings/backups" ,
1054
Some ( & andy) ,
1055
& format! ( " _csrf={csrf} &every=hourly&destination=gcs&bucket=gs%3A%2F%2Fandy-backup&provider={provider} &keep_days=7" ) ,
1056
)
1057
.await;
1058
assert! ( ok.body.contains ( " Saved." ) , " {}" , ok.body) ;
1059
let s = oxygenhub:: backups:: setting( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
1060
assert_eq! ( ( s.target.as_str ( ) , s.keep_days, s.every.as_str ( ) ) , ( " andy-backup" , 7 , " hourly" ) ) ;
7b9f4570 Andy McCall 22 days ago ↶
1061
1062
let bad = post (
1063
app,
1064
" /settings/backups" ,
1065
Some ( & andy) ,
1066
& format! ( " _csrf={csrf} &every=hourly&destination=s3&s3_bucket=andy-backup&s3_region=eu-west-2&s3_role=admin&keep_days=30" ) ,
1067
)
1068
.await;
1069
assert! ( bad.body.contains ( " the IAM role" ) , " {}" , bad.body) ;
1070
let role = " arn%3Aaws%3Aiam%3A%3A123456789012%3Arole%2Foxygenhub-backup" ;
1071
let ok = post (
1072
app,
1073
" /settings/backups" ,
1074
Some ( & andy) ,
1075
& format! (
1076
" _csrf={csrf} &every=daily&destination=s3&s3_bucket=s3%3A%2F%2Fandy-backup&s3_region=eu-west-2&s3_role={role} &keep_days=30"
1077
) ,
1078
)
1079
.await;
1080
assert! ( ok.body.contains ( " Saved." ) & & ok.body.contains ( r # "value="s3" checked"# ) , " {}" , ok.body) ;
1081
let s = oxygenhub:: backups:: setting( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
1082
assert_eq! ( ( s.destination.as_str ( ) , s.target.as_str ( ) , s.region.as_str ( ) ) , ( " s3" , " andy-backup" , " eu-west-2" ) ) ;
1083
assert_eq! ( s.provider, " arn:aws:iam::123456789012:role/oxygenhub-backup" ) ;
1084
1085
let ok = post (
1086
app,
1087
" /settings/backups" ,
1088
Some ( & andy) ,
1089
& format! ( " _csrf={csrf} &every=hourly&destination=gcs&bucket=gs%3A%2F%2Fandy-backup&provider={provider} &keep_days=7" ) ,
1090
)
1091
.await;
1092
assert! ( ok.body.contains ( " Saved." ) ) ;
d7d18246 Andy McCall 22 days ago ↶
1093
1094
let bob = register ( app, " bob" ) .await;
1095
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
1096
db:: accounts:: create_org( & app.db, " club" , " Club" , andy_id) .await.unwrap ( ) ;
1097
assert_eq! ( get ( app, " /orgs/club/settings/backups" , Some ( & bob) ) .await.status, StatusCode:: NOT_FOUND ) ;
1098
let club = get ( app, " /orgs/club/settings/backups" , Some ( & andy) ) .await;
1099
assert! ( club.body.contains ( " club</a>'s repositories" ) , " {}" , club.body) ;
1100
assert! ( get ( app, " /orgs/club/settings" , Some ( & andy) ) .await.body.contains ( " /orgs/club/settings/backups" ) ) ;
1e8a6e8b Andy McCall 22 days ago ↶
1101
1102
1103
1104
sqlx:: query( " UPDATE accounts SET is_admin = (username = 'andy')" ) .execute ( & app.db) .await.unwrap ( ) ;
1105
app.repos.create ( & oxygenhub:: repos:: RepoName:: parse( " bob/game" ) .unwrap ( ) , None ) .unwrap ( ) ;
1106
db:: repos:: create( & app.db, " bob" , " game" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
1107
assert_eq! ( get ( app, " /admin/backups" , Some ( & bob) ) .await.status, StatusCode:: NOT_FOUND ) ;
1108
let admin = get ( app, " /admin/backups" , Some ( & andy) ) .await;
1109
assert_eq! ( admin.status, StatusCode:: OK ) ;
1110
assert! ( admin.body.contains ( " gs://andy-backup" ) & & admin.body.contains ( " hourly" ) & & admin.body.contains ( " never" ) , " {}" , admin.body) ;
1111
let missing = & admin.body[ admin.body.find ( " Not backed up" ) .unwrap ( ) .. ] ;
1112
assert! ( missing.contains ( " href=\" /bob\" " ) & & missing.contains ( " 1 repository" ) , " {missing}" ) ;
1113
assert! ( get ( app, " /admin" , Some ( & andy) ) .await.body.contains ( " /admin/backups" ) ) ;
d3b048b2 Andy McCall 22 days ago ↶
1114
1115
let game = get ( app, " /bob/game/settings" , Some ( & bob) ) .await;
1116
assert! ( game.body.contains ( " bob has no backup set up yet" ) & & game.body.contains ( r # "name="backup" value="" checked"# ) , " {}" , game.body) ;
1117
let csrf = csrf_of ( & game.body) ;
1118
let saved = post ( app, " /bob/game/settings/backup" , Some ( & bob) , & format! ( " _csrf={csrf} &backup=off" ) ) .await;
1119
assert! ( saved.body.contains ( " left out of its owner" ) , " {}" , saved.body) ;
1120
assert_eq! ( db:: repos:: get( & app.db, " bob" , " game" ) .await.unwrap ( ) .unwrap ( ) .backup, " off" ) ;
1121
let bad = post ( app, " /bob/game/settings/backup" , Some ( & bob) , & format! ( " _csrf={csrf} &backup=weekly" ) ) .await;
1122
assert! ( bad.body.contains ( " Pick how this repository is backed up." ) ) ;
d7d18246 Andy McCall 22 days ago ↶
1123
}
bff9106e Andy McCall 22 days ago ↶
1124
1125
1126
1127
1128
# [ tokio ::test ( flavor = " multi_thread" ) ]
1129
async fn owners_hear_by_default_and_agents_tell_their_owner ( ) {
1130
let h = hub ( ) .await;
1131
let app = & h.app;
1132
let andy = register ( app, " andy" ) .await;
1133
let bob = register ( app, " bob" ) .await;
1134
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
1135
let kinds = | id : i64 | {
1136
let db = app.db.clone ( ) ;
1137
async move { db:: notify:: inbox( & db, id, 50 ) .await.unwrap ( ) .into_iter ( ) .map ( | n | ( n.kind, n.title, n.actor) ) .collect:: < Vec< _ > > ( ) }
1138
} ;
1139
assert! ( kinds ( andy_id) .await.iter ( ) .any ( | ( k , t , _) | k = = " account.registered" & & t.contains ( " bob" ) ) , " the administrator hears of bob" ) ;
1140
let repo = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
1141
app.repos.create ( & repo, None ) .unwrap ( ) ;
1142
db:: repos:: create( & app.db, " andy" , " demo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
1143
let open = | who : & 'static str , title : & 'static str | {
1144
let ( app, cookie) = ( app.clone ( ) , if who = = " bob" { bob.clone ( ) } else { andy.clone ( ) } ) ;
1145
async move {
1146
let csrf = csrf_of ( & get ( & app, " /andy/demo/issues/new" , Some ( & cookie) ) .await.body) ;
1147
post ( & app, " /andy/demo/issues/new" , Some ( & cookie) , & format! ( " _csrf={csrf} &title={title} &body=x" ) ) .await
1148
}
1149
} ;
1150
1151
assert! ( get ( app, " /andy/demo" , Some ( & andy) ) .await.body.contains ( " UNWATCH" ) , " an owner watches by default" ) ;
1152
open ( " bob" , " First" ) .await;
1153
assert! ( kinds ( andy_id) .await.iter ( ) .any ( | ( k , t , _) | k = = " issue.opened" & & t.contains ( " First" ) ) ) ;
1154
1155
let csrf = csrf_of ( & get ( app, " /andy/demo" , Some ( & andy) ) .await.body) ;
1156
post ( app, " /andy/demo/watch" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
1157
assert! ( get ( app, " /andy/demo" , Some ( & andy) ) .await.body.contains ( " >WATCH<" ) , " muted" ) ;
1158
open ( " bob" , " Second" ) .await;
1159
assert! ( ! kinds ( andy_id) .await.iter ( ) .any ( | ( _, t , _ ) | t.contains ( " Second" ) ) ) ;
1160
post ( app, " /andy/demo/watch" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
1161
1162
1163
let token = | agent : bool | {
1164
let db = app.db.clone ( ) ;
1165
async move {
1166
let ( t, hash) = oxygenhub:: auth:: new_token( ) ;
1167
db:: accounts:: create_token( & db, andy_id, if agent { " Claude" } else { " laptop" } , & hash, " repo:read,repo:write" , " " , None )
1168
.await
1169
.unwrap ( ) ;
1170
if agent {
1171
let id = db:: accounts:: token_by_hash( & db, & hash) .await.unwrap ( ) .unwrap ( ) .id;
1172
db:: accounts:: set_token_agent( & db, andy_id, id, true ) .await.unwrap ( ) ;
1173
}
1174
t
1175
}
1176
} ;
1177
let api_issue = | t : String, title : & 'static str | {
1178
let app = app.clone ( ) ;
1179
async move {
1180
use tower:: ServiceExt;
1181
let req = axum:: http:: Request:: post( " /api/v1/repos/andy/demo/issues" )
1182
.header ( " authorization" , format! ( " Bearer {t} " ) )
1183
.header ( " content-type" , " application/json" )
1184
.body ( axum:: body:: Body:: from( serde_json:: json! ( { " title" : title, " body" : " " } ) .to_string ( ) ) )
1185
.unwrap ( ) ;
1186
oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) .status ( )
1187
}
1188
} ;
1189
assert_eq! ( api_issue ( token ( false ) .await, " By hand" ) .await, StatusCode:: CREATED ) ;
1190
assert! ( ! kinds ( andy_id) .await.iter ( ) .any ( | ( _, t , _ ) | t.contains ( " By hand" ) ) , " his own doing" ) ;
1191
assert_eq! ( api_issue ( token ( true ) .await, " By the agent" ) .await, StatusCode:: CREATED ) ;
1192
let told = kinds ( andy_id) .await;
1193
let agent = told.iter ( ) .find ( | ( _, t , _ ) | t.contains ( " By the agent" ) ) .expect ( " told of his agent's issue" ) ;
1194
assert_eq! ( agent.2 , " andy (agent token Claude)" ) ;
1195
1196
let page = get ( app, " /settings/tokens" , Some ( & andy) ) .await;
00ef7b83 Andy McCall 22 days ago ↶
1197
assert! ( page.body.contains ( " Not an agent" ) & & page.body.contains ( " Mark as agent" ) , " {}" , page.body) ;
1198
assert! ( page.body.contains ( r # "<span class="pill agent-pill""# ) , " the agent's token says so" ) ;
bff9106e Andy McCall 22 days ago ↶
1199
}