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 ( ) ;
c8684d44 Andy McCall 19 days ago
42
43
config.registration.setup_code = " test-setup" .into ( ) ;
18e381ea Andy McCall 1 month ago ↶
44
config.auth.push_tokens = vec! [ " site" .into ( ) ] ;
45
config.registration.mode = " open" .into ( ) ;
46
let listener = tokio:: net:: TcpListener:: bind( " 127.0.0.1:0" ) .await.unwrap ( ) ;
47
let port = listener.local_addr ( ) .unwrap ( ) .port ( ) ;
48
config.server.base_url = format! ( " http://127.0.0.1:{port} " ) ;
49
let app = oxygenhub:: build( config) .await.unwrap ( ) ;
50
let router = oxygenhub:: router( app.clone ( ) ) ;
51
tokio:: spawn( async move { axum:: serve( listener, router) .await.unwrap ( ) } ) ;
52
Hub { app, base: format! ( " http://127.0.0.1:{port} " ) , _dir: dir }
53
}
54
55
struct Res {
56
status : StatusCode,
57
body : String,
58
cookie : Option< String> ,
59
location : Option< String> ,
60
}
61
62
async fn send ( app : & Shared, method : & str , path : & str , cookie : Option< & str > , form : Option< & str > ) -> Res {
63
let mut req = Request:: builder( ) .method ( method) .uri ( path) ;
64
if let Some ( c) = cookie {
65
req = req.header ( header:: COOKIE , format! ( " oxyhub={c} " ) ) ;
66
}
67
let body = match form {
68
Some ( f) => {
69
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
70
Body:: from( f.to_string ( ) )
71
}
72
None => Body:: empty( ) ,
73
} ;
74
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req.body ( body) .unwrap ( ) ) .await.unwrap ( ) ;
75
let status = res.status ( ) ;
76
let cookie = res
77
.headers ( )
78
.get ( header:: SET_COOKIE )
79
.and_then ( | v | v.to_str ( ) .ok ( ) )
80
.and_then ( | v | v.split ( ' ;' ) .next ( ) )
81
.and_then ( | kv | kv.split_once ( ' =' ) )
82
.map ( | ( _, v ) | v.to_string ( ) ) ;
83
let location = res.headers ( ) .get ( header:: LOCATION ) .and_then ( | v | v.to_str ( ) .ok ( ) ) .map ( String :: from) ;
84
let bytes = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ;
85
Res { status, body: String :: from_utf8_lossy( & bytes) .into_owned ( ) , cookie, location }
86
}
87
88
async fn get ( app : & Shared, path : & str , cookie : Option< & str > ) -> Res {
89
send ( app, " GET" , path, cookie, None ) .await
90
}
91
92
async fn post ( app : & Shared, path : & str , cookie : Option< & str > , form : & str ) -> Res {
93
send ( app, " POST" , path, cookie, Some ( form) ) .await
94
}
95
96
async fn register ( app : & Shared, user : & str ) -> String {
c8684d44 Andy McCall 19 days ago
97
let r = post (
98
app,
99
" /register" ,
100
None ,
101
& format! ( " username={user} &email={user} %40example.com&password=correct-horse-battery&setup=test-setup" ) ,
102
)
103
.await;
18e381ea Andy McCall 1 month ago ↶
104
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
105
r.cookie.unwrap ( )
106
}
107
c7067c7b Andy McCall 16 days ago
108
109
110
async fn settled ( app : & Shared, r : Res, cookie : Option< & str > , page : & str ) -> Res {
111
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
112
let to = r.location.expect ( " a redirect" ) ;
113
assert! ( to.starts_with ( & format! ( " {page} ?" ) ) , " {to} is not {page}" ) ;
114
get ( app, & to, cookie) .await
115
}
116
18e381ea Andy McCall 1 month ago ↶
117
fn csrf_of ( page : & str ) -> String {
118
let i = page.find ( " name=\" _csrf\" value=\" " ) .expect ( " csrf field" ) + " name=\" _csrf\" value=\" " .len ( ) ;
119
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
120
}
121
122
# [ tokio ::test ( flavor = " multi_thread" ) ]
123
async fn issues_notifications_and_search ( ) {
124
let h = hub ( ) .await;
125
let app = & h.app;
126
let andy = register ( app, " andy" ) .await;
127
let bob = register ( app, " bob" ) .await;
128
let repo = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
129
app.repos.create ( & repo, None ) .unwrap ( ) ;
130
db:: repos:: create( & app.db, " andy" , " demo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
131
132
133
let labels_page = get ( app, " /andy/demo/labels" , Some ( & andy) ) .await;
134
let csrf_a = csrf_of ( & labels_page.body) ;
135
assert! ( post ( app, " /andy/demo/labels" , Some ( & andy) , & format! ( " _csrf={csrf_a} &name=bug&colour=%23ff0000&description=Something+broke" ) )
136
.await
137
.body
138
.contains ( " Saved bug" ) ) ;
139
assert! ( post ( app, " /andy/demo/milestones" , Some ( & andy) , & format! ( " _csrf={csrf_a} &title=v1&description=First&due=2027-01-01" ) )
140
.await
141
.body
142
.contains ( " v1" ) ) ;
143
assert_eq! ( get ( app, " /andy/demo/labels" , Some ( & bob) ) .await.status, StatusCode:: OK , " readers see labels" ) ;
144
assert! ( ! get ( app, " /andy/demo/labels" , Some ( & bob) ) .await.body.contains ( " Add or change" ) , " but cannot edit them" ) ;
145
146
147
let overview = get ( app, " /andy/demo" , Some ( & bob) ) .await;
148
let csrf_b = csrf_of ( & overview.body) ;
149
assert_eq! ( post ( app, " /andy/demo/watch" , Some ( & bob) , & format! ( " _csrf={csrf_b} " ) ) .await.status, StatusCode:: SEE_OTHER ) ;
150
assert! ( get ( app, " /andy/demo" , Some ( & bob) ) .await.body.contains ( " UNWATCH" ) ) ;
151
let new_page = get ( app, " /andy/demo/issues/new" , Some ( & andy) ) .await;
152
let csrf_a = csrf_of ( & new_page.body) ;
153
let r = post (
154
app,
155
" /andy/demo/issues/new" ,
156
Some ( & andy) ,
157
& format! ( " _csrf={csrf_a} &title=Crash+on+start&body=cc+%40bob+see+%23999+and+deadbeefcafe1234" ) ,
158
)
159
.await;
160
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
161
assert_eq! ( r.location.as_deref ( ) , Some ( " /andy/demo/issue/1" ) ) ;
162
let page = get ( app, " /andy/demo/issue/1" , Some ( & bob) ) .await;
163
assert! ( page.body.contains ( " Crash on start" ) ) ;
164
assert! ( page.body.contains ( " href=\" /bob\" " ) , " mention linked: {}" , page.body) ;
165
assert! ( page.body.contains ( " /andy/demo/change/deadbeefcafe1234" ) , " hash linked: {}" , page.body) ;
166
assert! ( page.body.contains ( " #999" ) & & ! page.body.contains ( " issue/999" ) , " unknown numbers are not linked" ) ;
167
let unread = db:: notify:: unread( & app.db, db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id) .await.unwrap ( ) ;
168
assert_eq! ( unread, 1 , " one notification, not one per reason" ) ;
ea14c4ec Andy McCall 20 days ago ↶
169
170
171
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
172
let filter = db:: notify:: Filter { done: false , unread_only: false , reason: None , kind: None , repo_id: None } ;
173
let t = db:: notify:: threads( & app.db, bob_id, & filter, 10 ) .await.unwrap ( ) ;
174
assert_eq! ( ( t[ 0 ] .count, t[ 0 ] .unread) , ( 1 , 1 ) ) ;
175
let unread_only = db:: notify:: Filter { unread_only: true , .. filter.clone ( ) } ;
176
assert_eq! ( db:: notify:: threads( & app.db, bob_id, & unread_only, 10 ) .await.unwrap ( ) .len ( ) , 1 ) ;
18e381ea Andy McCall 1 month ago ↶
177
let inbox = get ( app, " /notifications" , Some ( & bob) ) .await;
178
assert! ( inbox.body.contains ( " andy/demo#1: Crash on start" ) , " {}" , inbox.body) ;
179
assert! ( get ( app, " /andy/demo" , Some ( & bob) ) .await.body.contains ( " class=\" count\" >1<" ) , " bell shows the count" ) ;
180
181
182
let page = get ( app, " /andy/demo/issue/1" , Some ( & andy) ) .await;
183
let csrf_a = csrf_of ( & page.body) ;
184
let label_id: i64 = sqlx:: query_scalar( " SELECT id FROM labels WHERE name = 'bug'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
185
let r = post ( app, " /andy/demo/issue/1/labels" , Some ( & andy) , & format! ( " _csrf={csrf_a} &label={label_id} " ) ) .await;
186
assert! ( r.body.contains ( " >bug<" ) , " {}" , r.body) ;
187
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 ↶
188
assert! ( r.body.contains ( " <li><a href=\" /bob\" ><img class=\" avatar xs\" " ) , " {}" , r.body) ;
189
190
assert! ( r.body.contains ( " Assign people to this issue" ) & & r.body.contains ( " data-menu-filter" ) , " the assignees menu" ) ;
191
let r = post ( app, " /andy/demo/issue/1/assign" , Some ( & andy) , & format! ( " _csrf={csrf_a} &action=set&username=andy&username=bob" ) ) .await;
3b1bc27f Andy McCall 20 days ago
192
let assigned = | db : oxygenhub:: db:: Pool| async move {
cb71e18e Andy McCall 1 month ago ↶
193
let id: i64 = sqlx:: query_scalar( " SELECT id FROM issues WHERE number = 1" ) .fetch_one ( & db) .await.unwrap ( ) ;
194
let mut a = db:: issues:: assignees( & db, id) .await.unwrap ( ) ;
195
a.sort ( ) ;
196
a
197
} ;
198
assert_eq! ( assigned ( app.db.clone ( ) ) .await, [ " andy" , " bob" ] , " {}" , r.status) ;
199
post ( app, " /andy/demo/issue/1/assign" , Some ( & andy) , & format! ( " _csrf={csrf_a} &action=set&username=bob" ) ) .await;
200
assert_eq! ( assigned ( app.db.clone ( ) ) .await, [ " bob" ] , " set takes off who is not listed" ) ;
18e381ea Andy McCall 1 month ago ↶
201
let ms_id: i64 = sqlx:: query_scalar( " SELECT id FROM milestones WHERE title = 'v1'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
202
let r = post ( app, " /andy/demo/issue/1/milestone" , Some ( & andy) , & format! ( " _csrf={csrf_a} &milestone={ms_id} " ) ) .await;
203
assert! ( r.body.contains ( " >v1<" ) , " {}" , r.body) ;
cb71e18e Andy McCall 1 month ago ↶
204
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 ↶
205
assert_eq! (
206
post (
207
app,
208
" /andy/demo/issue/1/state" ,
209
Some ( & bob) ,
210
& format! ( " _csrf={} &action=lock" , csrf_of ( & get ( app, " /andy/demo/issue/1" , Some ( & bob) ) .await.body) )
211
)
212
.await
213
.status,
214
StatusCode:: NOT_FOUND ,
215
" bob cannot lock"
216
) ;
217
218
let list = get ( app, " /andy/demo/issues?q=label:bug+assignee:bob+milestone:v1" , None ) .await;
219
assert! ( list.body.contains ( " Crash on start" ) , " {}" , list.body) ;
220
assert! ( ! get ( app, " /andy/demo/issues?q=label:feature" , None ) .await.body.contains ( " Crash on start" ) ) ;
221
assert! ( get ( app, " /andy/demo/milestones" , None ) .await.body.contains ( " 1 open, 0 closed" ) ) ;
222
223
224
let csrf_b = csrf_of ( & get ( app, " /andy/demo/issue/1" , Some ( & bob) ) .await.body) ;
225
let r = post ( app, " /andy/demo/issue/1/comment" , Some ( & bob) , & format! ( " _csrf={csrf_b} &body=Reproduced+with+the+flux+capacitor" ) ) .await;
226
assert! ( r.body.contains ( " flux capacitor" ) , " {}" , r.body) ;
227
assert! ( get ( app, " /notifications" , Some ( & andy) ) .await.body.contains ( " andy/demo#1" ) ) ;
228
let s = get ( app, " /search?q=capacitor" , None ) .await;
229
assert! ( s.body.contains ( " Crash on start" ) & & s.body.contains ( " <mark>capacitor</mark>" ) , " {}" , s.body) ;
230
let s = get ( app, " /search?q=demo" , None ) .await;
231
assert! ( s.body.contains ( " /andy/demo\" " ) , " repository hit: {}" , s.body) ;
232
233
let r = post ( app, " /andy/demo/issue/1/comment" , Some ( & bob) , & format! ( " _csrf={csrf_b} &body=closing&close=completed" ) ) .await;
234
assert! ( r.body.contains ( " >open<" ) | | r.body.contains ( " head\" >open" ) , " still open: {}" , r.body) ;
235
let r = post ( app, " /andy/demo/issue/1/comment" , Some ( & andy) , & format! ( " _csrf={csrf_a} &body=fixed&close=completed" ) ) .await;
236
assert! ( r.body.contains ( " closed" ) , " {}" , r.body) ;
237
assert! ( get ( app, " /andy/demo/issues?state=closed" , None ) .await.body.contains ( " Crash on start" ) ) ;
238
239
240
let csrf_a = csrf_of ( & get ( app, " /andy/demo/issues/new" , Some ( & andy) ) .await.body) ;
241
assert_eq! (
242
post ( app, " /andy/demo/issues/new" , Some ( & andy) , & format! ( " _csrf={csrf_a} &title=Typo&body=" ) ) .await.location.as_deref ( ) ,
243
Some ( " /andy/demo/issue/2" )
244
) ;
245
let work = tempfile:: tempdir( ) .unwrap ( ) ;
246
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
247
client.config.set ( " remote.origin.url" , & format! ( " {} /andy/demo" , h.base) ) .unwrap ( ) ;
248
client.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
249
client.save_config ( ) .unwrap ( ) ;
250
write ( work.path ( ) , " a.txt" , b " a\n " ) ;
251
client.snapshot ( " t" ) .unwrap ( ) ;
252
client.commit ( " Fix the typo\n \n Closes #2" , & [ ] ) .unwrap ( ) ;
253
assert! ( client.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
92cea629 Andy McCall 25 days ago ↶
254
255
let repo_id = db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) .id;
256
let mut issue2 = db:: issues:: get( & app.db, repo_id, 2 ) .await.unwrap ( ) .unwrap ( ) ;
257
for _ in 0 .. 200 {
258
if issue2.state = = " closed" {
259
break ;
260
}
261
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
262
issue2 = db:: issues:: get( & app.db, repo_id, 2 ) .await.unwrap ( ) .unwrap ( ) ;
263
}
18e381ea Andy McCall 1 month ago ↶
264
assert_eq! ( issue2.state, " closed" , " closed by the landed change" ) ;
265
assert! ( get ( app, " /andy/demo/issue/2" , None ) .await.body.contains ( " closed by change" ) ) ;
266
267
268
client.bookmark_create ( " feat" , None ) .unwrap ( ) ;
269
client.bookmark_switch ( " feat" , false ) .unwrap ( ) ;
270
write ( work.path ( ) , " b.txt" , b " b\n " ) ;
271
client.snapshot ( " t" ) .unwrap ( ) ;
272
client.commit ( " feat" , & [ ] ) .unwrap ( ) ;
273
assert! ( client.push_bookmark ( " origin" , " feat" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
274
let csrf_a = csrf_of ( & get ( app, " /andy/demo/proposals/new?source=feat&target=main" , Some ( & andy) ) .await.body) ;
275
let r = post ( app, " /andy/demo/proposals/new" , Some ( & andy) , & format! ( " _csrf={csrf_a} &title=Feat&target=main&source=feat" ) ) .await;
276
assert_eq! ( r.location.as_deref ( ) , Some ( " /andy/demo/proposal/3" ) , " {}" , r.body) ;
277
assert_eq! ( get ( app, " /andy/demo/issue/3" , None ) .await.location.as_deref ( ) , Some ( " /andy/demo/proposal/3" ) ) ;
278
279
let r = post ( app, " /andy/demo/issue/2/comment" , Some ( & andy) , & format! ( " _csrf={csrf_a} &body=see+%233" ) ) .await;
280
assert! ( r.body.contains ( " href=\" /andy/demo/proposal/3\" " ) , " {}" , r.body) ;
78001759 Andy McCall 1 month ago ↶
281
282
client.bookmark_switch ( " main" , false ) .unwrap ( ) ;
283
write ( work.path ( ) , " c.txt" , b " c\n " ) ;
284
client.snapshot ( " t" ) .unwrap ( ) ;
285
let r = client.commit ( " Tidy up (refs #1, see #3)" , & [ ] ) .unwrap ( ) ;
286
assert! ( client.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
287
let short = & r.to_hex ( ) [ .. 12 ] ;
92cea629 Andy McCall 25 days ago ↶
288
289
290
let mut one = get ( app, " /andy/demo/issue/1" , None ) .await.body;
291
for _ in 0 .. 200 {
292
if one.contains ( " referenced by change" ) {
293
break ;
294
}
295
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
296
one = get ( app, " /andy/demo/issue/1" , None ) .await.body;
297
}
78001759 Andy McCall 1 month ago ↶
298
assert! (
299
one.contains ( " referenced by change" ) & & one.contains ( & format! ( " href=\" /andy/demo/change/{short} \" " ) ) & & one.contains ( " on main" ) ,
300
" {one}"
301
) ;
302
let three = get ( app, " /andy/demo/proposal/3" , Some ( & andy) ) .await.body;
303
assert! ( three.contains ( " referenced by change" ) & & three.contains ( short) , " proposals too" ) ;
304
assert! ( ! get ( app, " /andy/demo/issue/2" , None ) .await.body.contains ( " referenced by change" ) , " a closing change records only that" ) ;
305
eb0c44a9 Andy McCall 1 month ago ↶
306
307
let part = get ( app, " /notifications?reason=participating" , Some ( & bob) ) .await.body;
308
assert! ( part.contains ( " andy/demo#1" ) & & ! part.contains ( " andy/demo#3: Feat" ) , " {part}" ) ;
309
let watch = get ( app, " /notifications?reason=watching" , Some ( & bob) ) .await.body;
310
assert! ( watch.contains ( " andy/demo#3: Feat" ) & & ! watch.contains ( " Crash on start" ) , " {watch}" ) ;
311
let props = get ( app, " /notifications?kind=proposal" , Some ( & bob) ) .await.body;
312
assert! ( props.contains ( " andy/demo#3: Feat" ) & & ! props.contains ( " andy/demo#1" ) , " kind filter" ) ;
313
assert! ( get ( app, " /notifications?repo=andy/demo" , Some ( & bob) ) .await.body.contains ( " andy/demo#3: Feat" ) ) ;
314
assert! ( get ( app, " /notifications?repo=nobody/none" , Some ( & bob) ) .await.body.contains ( " Nothing matches these filters" ) ) ;
315
316
let inbox = get ( app, " /notifications" , Some ( & bob) ) .await.body;
317
assert_eq! ( inbox.matches ( " data-row-thread=\" /andy/demo/issue/1\" " ) .count ( ) , 1 , " {inbox}" ) ;
318
assert! ( inbox.contains ( " updates" ) , " the count of a busy thread" ) ;
319
320
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
321
let newest: i64 = sqlx:: query_scalar(
3b1bc27f Andy McCall 20 days ago
322
" SELECT id FROM notifications WHERE account_id = $1 AND thread = '/andy/demo/proposal/3' ORDER BY id DESC LIMIT 1" ,
eb0c44a9 Andy McCall 1 month ago ↶
323
)
324
.bind ( bob_id)
325
.fetch_one ( & app.db)
326
.await
327
.unwrap ( ) ;
328
let before = db:: notify:: unread( & app.db, bob_id) .await.unwrap ( ) ;
329
let r = get ( app, & format! ( " /notifications/open/{newest} " ) , Some ( & bob) ) .await;
330
assert_eq! ( r.location.as_deref ( ) , Some ( " /andy/demo/proposal/3" ) ) ;
331
assert! ( db:: notify:: unread( & app.db, bob_id) .await.unwrap ( ) < before) ;
332
assert_eq! ( get ( app, & format! ( " /notifications/open/{newest} " ) , Some ( & andy) ) .await.status, StatusCode:: NOT_FOUND , " only your own" ) ;
333
334
let csrf_b = csrf_of ( & get ( app, " /notifications" , Some ( & bob) ) .await.body) ;
335
let r = post (
336
app,
337
" /notifications/thread" ,
338
Some ( & bob) ,
339
& format! ( " _csrf={csrf_b} &thread=%2Fandy%2Fdemo%2Fissue%2F1&action=done&back=%2Fnotifications" ) ,
340
)
341
.await;
342
assert_eq! ( r.location.as_deref ( ) , Some ( " /notifications" ) ) ;
343
assert! ( ! get ( app, " /notifications" , Some ( & bob) ) .await.body.contains ( " Crash on start" ) ) ;
344
assert! ( get ( app, " /notifications?view=done" , Some ( & bob) ) .await.body.contains ( " Crash on start" ) ) ;
345
post ( app, " /andy/demo/issue/1/comment" , Some ( & andy) , & format! ( " _csrf={csrf_a} &body=one+more+thing" ) ) .await;
346
assert! ( get ( app, " /notifications" , Some ( & bob) ) .await.body.contains ( " Crash on start" ) , " back with the new comment" ) ;
347
let r = post (
348
app,
349
" /notifications/thread" ,
350
Some ( & bob) ,
351
& format! ( " _csrf={csrf_b} &thread=%2Fandy%2Fdemo%2Fissue%2F1&action=read&back=https%3A%2F%2Fevil.example" ) ,
352
)
353
.await;
354
assert_eq! ( r.location.as_deref ( ) , Some ( " /notifications" ) , " only back to the inbox" ) ;
355
18e381ea Andy McCall 1 month ago ↶
356
357
assert! ( get ( app, " /notifications" , Some ( & bob) ) .await.body.contains ( " andy/demo#3: Feat" ) ) ;
358
let csrf_b = csrf_of ( & get ( app, " /notifications" , Some ( & bob) ) .await.body) ;
359
let r = post ( app, " /notifications/read" , Some ( & bob) , & format! ( " _csrf={csrf_b} " ) ) .await;
360
assert! ( r.body.contains ( " 0 unread" ) , " {}" , r.body) ;
361
362
let r = post ( app, " /settings/notifications" , Some ( & bob) , & format! ( " _csrf={csrf_b} &email=1" ) ) .await;
363
assert! ( r.body.contains ( " Email notifications are on" ) ) ;
364
let key: String = sqlx:: query_scalar( " SELECT unsubscribe_key FROM accounts WHERE username = 'bob'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
365
assert_eq! ( get ( app, & format! ( " /settings/notifications?unsubscribe={key} " ) , None ) .await.status, StatusCode:: OK ) ;
366
let on: bool = sqlx:: query_scalar( " SELECT email_notifications FROM accounts WHERE username = 'bob'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
367
assert! ( ! on) ;
368
}
25ab8c73 Andy McCall 1 month ago ↶
369
370
# [ tokio ::test ( flavor = " multi_thread" ) ]
371
async fn dashboard_shows_what_needs_the_viewer ( ) {
372
let h = hub ( ) .await;
373
let app = & h.app;
374
let andy = register ( app, " andy" ) .await;
375
let bob = register ( app, " bob" ) .await;
376
for ( name, vis) in [ ( " demo" , hub_api:: Visibility:: Public) , ( " secret" , hub_api:: Visibility:: Private) ] {
377
app.repos.create ( & RepoName:: parse( & format! ( " andy/{name} " ) ) .unwrap ( ) , None ) .unwrap ( ) ;
378
db:: repos:: create( & app.db, " andy" , name, " " , vis) .await.unwrap ( ) ;
379
}
380
let demo = db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
381
let secret = db:: repos:: get( & app.db, " andy" , " secret" ) .await.unwrap ( ) .unwrap ( ) ;
382
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
383
for ( repo, title) in [ ( demo.id, " Faster startup" ) , ( secret.id, " Hidden work" ) ] {
384
let p = db:: proposals:: create(
385
& app.db,
386
db:: proposals:: NewProposal {
387
repo_id: repo,
388
title,
389
body: " " ,
390
author_id: Some ( bob_id) ,
391
author: " bob" ,
392
draft: false ,
393
target: " main" ,
394
source_kind: " bookmark" ,
395
source_repo_id: Some ( repo) ,
396
source_bookmark: " fast" ,
397
source_changes: " " ,
398
} ,
399
)
400
.await
401
.unwrap ( ) ;
402
db:: proposals:: add_event( & app.db, p.id, " bob" , " opened" , " " ) .await.unwrap ( ) ;
403
}
404
405
406
let anon = get ( app, " /" , None ) .await;
407
assert! ( anon.body.contains ( " andy /" ) & & ! anon.body.contains ( " Dashboard <small>" ) , " {}" , anon.body) ;
408
409
410
let page = get ( app, " /" , Some ( & andy) ) .await;
411
assert_eq! ( page.status, StatusCode:: OK ) ;
f8cf54e9 Andy McCall 23 days ago ↶
412
assert! ( page.body.contains ( " Waiting for your review</h2><span class=\" count-chip\" >2</span>" ) , " {}" , page.body) ;
25ab8c73 Andy McCall 1 month ago ↶
413
assert! ( page.body.contains ( " /andy/demo/proposal/1" ) & & page.body.contains ( " /andy/secret/proposal/1" ) ) ;
414
assert! ( page.body.contains ( " Your repositories" ) ) ;
415
assert! ( page.body.contains ( " opened proposal" ) , " activity feed: {}" , page.body) ;
416
17ca6cdd Andy McCall 22 days ago ↶
417
418
419
assert! ( page.body.contains ( r # "<canvas id="backdrop" aria-hidden="true">"# ) ) ;
420
let start = page.body.find ( r # "id="backdrop-data">"# ) .unwrap ( ) + r # "id="backdrop-data">"# .len ( ) ;
421
let data: serde_json:: Value = serde_json:: from_str( & page.body[ start.. start + page.body[ start.. ] .find ( " </script>" ) .unwrap ( ) ] ) .unwrap ( ) ;
422
let repos: Vec< & str > = data[ " repos" ] .as_array ( ) .unwrap ( ) .iter ( ) .filter_map ( | r | r.as_str ( ) ) .collect ( ) ;
423
assert! ( repos.contains ( & " andy/demo" ) & & repos.contains ( & " andy/secret" ) , " {data}" ) ;
424
let events = data[ " events" ] .as_array ( ) .unwrap ( ) ;
425
assert! ( events.iter ( ) .any ( | e | e[ 1 ] = = 1 ) , " the proposals opened are marks: {data}" ) ;
426
assert! ( events.iter ( ) .all ( | e | e[ 0 ] .as_u64 ( ) .unwrap ( ) < repos.len ( ) as u64 ) ) ;
33bd0bd4 Andy McCall 22 days ago ↶
427
428
let res = oxygenhub:: router( app.clone ( ) )
429
.oneshot ( Request:: builder( ) .uri ( " /" ) .header ( header:: COOKIE , format! ( " oxyhub={andy} " ) ) .body ( Body:: empty( ) ) .unwrap ( ) )
430
.await
431
.unwrap ( ) ;
432
assert_eq! ( res.headers ( ) [ header:: CACHE_CONTROL ] , " private, no-store" ) ;
6246d1e5 Andy McCall 22 days ago ↶
433
434
assert! ( get ( app, " /login" , None )
435
.await
436
.body
437
.contains ( r # "<script type="application/json" id="backdrop-data">{"scene":"mark"}</script>"# ) ) ;
438
let lost = get ( app, " /no/such/page/here" , None ) .await;
439
assert_eq! ( lost.status, StatusCode:: NOT_FOUND ) ;
440
assert! ( lost.body.contains ( r # "{"scene":"adrift"}"# ) ) ;
0a3b244b Andy McCall 22 days ago ↶
441
442
let sky_of = | body : & str | {
443
let start = body.find ( r # "id="backdrop-data">"# ) .unwrap ( ) + r # "id="backdrop-data">"# .len ( ) ;
444
body[ start.. start + body[ start.. ] .find ( " </script>" ) .unwrap ( ) ] .to_string ( )
445
} ;
446
let hub_sky = sky_of ( & anon.body) ;
447
assert! ( hub_sky.contains ( " andy/demo" ) & & ! hub_sky.contains ( " andy/secret" ) , " {hub_sky}" ) ;
448
449
450
let explore = get ( app, " /explore" , Some ( & andy) ) .await;
451
assert! ( explore.body.contains ( " andy/secret" ) , " the list shows him his private repository" ) ;
452
let hub_sky = sky_of ( & explore.body) ;
453
assert! ( hub_sky.contains ( " andy/demo" ) & & ! hub_sky.contains ( " andy/secret" ) , " {hub_sky}" ) ;
454
assert! ( ! get ( app, " /andy" , None ) .await.body.contains ( " backdrop-data" ) ) ;
17ca6cdd Andy McCall 22 days ago ↶
455
456
let res = oxygenhub:: router( app.clone ( ) )
457
.oneshot ( Request:: builder( ) .uri ( " /static/backdrop/backdrop_bg.wasm" ) .body ( Body:: empty( ) ) .unwrap ( ) )
458
.await
459
.unwrap ( ) ;
460
assert_eq! ( res.headers ( ) [ header:: CONTENT_TYPE ] , " application/wasm" ) ;
461
assert! ( res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .starts_with ( b " \0 asm" ) ) ;
da8f7826 Andy McCall 22 days ago ↶
462
463
assert! ( sky_of ( & page.body) .contains ( r # ""live":"mine""# ) & & hub_sky.contains ( r # ""live":"hub""# ) ) ;
464
let since = db:: now_ms( ) - 3_600_000 ;
465
let arrivals = | res : Res| -> Vec< String> {
466
assert_eq! ( res.status, StatusCode:: OK , " {}" , res.body) ;
467
let v: serde_json:: Value = serde_json:: from_str( & res.body) .unwrap ( ) ;
468
v[ " events" ] .as_array ( ) .unwrap ( ) .iter ( ) .map ( | e | e[ 0 ] .as_str ( ) .unwrap ( ) .to_string ( ) ) .collect ( )
469
} ;
470
let mine = arrivals ( get ( app, & format! ( " /sky/arrivals?scene=mine&since={since} " ) , Some ( & andy) ) .await) ;
471
assert! ( mine.iter ( ) .any ( | r | r = = " andy/demo" ) & & mine.iter ( ) .any ( | r | r = = " andy/secret" ) , " {mine:?}" ) ;
472
let hub = arrivals ( get ( app, & format! ( " /sky/arrivals?scene=hub&since={since} " ) , Some ( & andy) ) .await) ;
473
assert! ( hub.iter ( ) .any ( | r | r = = " andy/demo" ) & & ! hub.iter ( ) .any ( | r | r = = " andy/secret" ) , " the hub's sky is public only: {hub:?}" ) ;
474
let anon_hub = arrivals ( get ( app, & format! ( " /sky/arrivals?scene=hub&since={since} " ) , None ) .await) ;
475
assert! ( ! anon_hub.iter ( ) .any ( | r | r = = " andy/secret" ) , " {anon_hub:?}" ) ;
476
assert! ( arrivals ( get ( app, & format! ( " /sky/arrivals?scene=mine&since={since} " ) , None ) .await) .is_empty ( ) , " signed out has no `mine`" ) ;
477
assert! (
478
arrivals ( get ( app, & format! ( " /sky/arrivals?scene=mine&since={since} " ) , Some ( & bob) ) .await) .is_empty ( ) ,
479
" Bob's sky has none of Andy's"
480
) ;
481
assert! (
482
arrivals ( get ( app, & format! ( " /sky/arrivals?scene=mine&since={} " , db:: now_ms( ) ) , Some ( & andy) ) .await) .is_empty ( ) ,
483
" nothing since now"
484
) ;
17ca6cdd Andy McCall 22 days ago ↶
485
25ab8c73 Andy McCall 1 month ago ↶
486
487
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
488
let p = db:: proposals:: get( & app.db, demo.id, 1 ) .await.unwrap ( ) .unwrap ( ) ;
489
db:: proposals:: add_review( & app.db, p.id, andy_id, " andy" , " approve" , " " ) .await.unwrap ( ) ;
f8cf54e9 Andy McCall 23 days ago ↶
490
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 ↶
491
492
493
let page = get ( app, " /" , Some ( & bob) ) .await;
33bd0bd4 Andy McCall 22 days ago ↶
494
495
496
let start = page.body.find ( r # "id="backdrop-data">"# ) .unwrap ( ) + r # "id="backdrop-data">"# .len ( ) ;
497
let sky = & page.body[ start.. start + page.body[ start.. ] .find ( " </script>" ) .unwrap ( ) ] ;
498
assert! ( ! sky.contains ( " andy/secret" ) & & ! sky.contains ( " andy/demo" ) , " {sky}" ) ;
f8cf54e9 Andy McCall 23 days ago ↶
499
assert! ( page.body.contains ( " Your proposals</h2><span class=\" count-chip\" >1</span>" ) , " {}" , page.body) ;
25ab8c73 Andy McCall 1 month ago ↶
500
assert! ( ! page.body.contains ( " Hidden work" ) ) ;
f8cf54e9 Andy McCall 23 days ago ↶
501
assert! ( page.body.contains ( " Waiting for your review</h2><span class=\" count-chip\" >0</span>" ) ) ;
25ab8c73 Andy McCall 1 month ago ↶
502
assert! ( page.body.contains ( " Faster startup" ) ) ;
503
let feed = & page.body[ page.body.find ( " Recent activity" ) .unwrap ( ) .. ] ;
504
assert! ( ! feed.contains ( " andy/secret" ) , " private activity leaked: {feed}" ) ;
505
506
507
assert! ( get ( app, " /explore" , Some ( & bob) ) .await.body.contains ( " andy /" ) ) ;
508
}
4edcf240 Andy McCall 1 month ago ↶
509
510
# [ tokio ::test ( flavor = " multi_thread" ) ]
511
async fn empty_repository_shows_a_quickstart ( ) {
512
let h = hub ( ) .await;
513
let app = & h.app;
514
let andy = register ( app, " andy" ) .await;
515
let bob = register ( app, " bob" ) .await;
516
app.repos.create ( & RepoName:: parse( " andy/fresh" ) .unwrap ( ) , None ) .unwrap ( ) ;
517
db:: repos:: create( & app.db, " andy" , " fresh" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
518
519
let page = get ( app, " /andy/fresh" , Some ( & andy) ) .await;
520
assert! ( page.body.contains ( " This repository is empty" ) , " {}" , page.body) ;
521
let clone_url = format! ( " {} /andy/fresh" , h.base) ;
522
assert! ( page.body.contains ( & format! ( " oxy remote add hub {clone_url} " ) ) , " {}" , page.body) ;
523
assert! ( page.body.contains ( " oxy config remote.hub.token YOUR-TOKEN" ) ) ;
524
assert! ( page.body.contains ( " oxy push hub" ) ) ;
525
assert! (
526
page.body.contains ( " mkdir fresh && cd fresh" ) | | page.body.contains ( " mkdir fresh && cd fresh" ) ,
527
" {}" ,
528
page.body
529
) ;
530
assert! ( page.body.contains ( " oxy git import --branch main" ) ) ;
531
assert! ( ! page.body.contains ( " oxyhub-ca.pem" ) , " no certificate step without a self-signed certificate" ) ;
532
533
534
let page = get ( app, " /andy/fresh" , Some ( & bob) ) .await;
535
assert! ( page.body.contains ( " Nothing has been pushed yet" ) & & ! page.body.contains ( " oxy push hub" ) , " {}" , page.body) ;
536
537
538
let work = tempfile:: tempdir( ) .unwrap ( ) ;
539
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
540
client.config.set ( " remote.hub.url" , & clone_url) .unwrap ( ) ;
541
client.config.set ( " remote.hub.token" , " site" ) .unwrap ( ) ;
542
client.save_config ( ) .unwrap ( ) ;
543
write ( work.path ( ) , " README.md" , b " # fresh\n " ) ;
544
client.commit ( " First change" , & [ ] ) .unwrap ( ) ;
545
assert! ( client.push ( " hub" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
546
let page = get ( app, " /andy/fresh" , Some ( & andy) ) .await;
547
assert! ( ! page.body.contains ( " This repository is empty" ) & & page.body.contains ( " README.md" ) , " {}" , page.body) ;
548
}
c40c2d3e Andy McCall 1 month ago ↶
549
550
# [ tokio ::test ( flavor = " multi_thread" ) ]
551
async fn theme_is_chosen_in_settings_and_remembered_in_a_cookie ( ) {
552
let h = hub ( ) .await;
553
let app = & h.app;
554
let andy = register ( app, " andy" ) .await;
555
let page = get ( app, " /settings" , Some ( & andy) ) .await;
556
assert! ( page.body.contains ( " Appearance" ) ) ;
d98a93c1 Andy McCall 1 month ago ↶
557
assert! ( page.body.contains ( r # "value="light" checked"# ) , " light by default: {}" , page.body) ;
c40c2d3e Andy McCall 1 month ago ↶
558
assert! ( page.body.contains ( " data-theme-toggle" ) , " the header has the switch" ) ;
559
let csrf = csrf_of ( & page.body) ;
d98a93c1 Andy McCall 1 month ago ↶
560
let r = post ( app, " /settings/appearance" , Some ( & andy) , & format! ( " _csrf={csrf} &theme=dark" ) ) .await;
c40c2d3e Andy McCall 1 month ago ↶
561
assert_eq! ( r.status, StatusCode:: SEE_OTHER ) ;
d98a93c1 Andy McCall 1 month ago ↶
562
assert_eq! ( r.cookie.as_deref ( ) , Some ( " dark" ) ) ;
c40c2d3e Andy McCall 1 month ago ↶
563
assert_eq! ( r.location.as_deref ( ) , Some ( " /settings" ) ) ;
d98a93c1 Andy McCall 1 month ago ↶
564
let page = send_with_theme ( app, & andy, " dark" ) .await;
565
assert! ( page.contains ( r # "value="dark" checked"# ) , " the form shows the cookie's theme" ) ;
c40c2d3e Andy McCall 1 month ago ↶
566
assert_eq! ( post ( app, " /settings/appearance" , Some ( & andy) , & format! ( " _csrf={csrf} &theme=purple" ) ) .await.status, StatusCode:: BAD_REQUEST ) ;
17ca6cdd Andy McCall 22 days ago ↶
567
568
569
570
assert! ( get ( app, " /settings" , Some ( & andy) ) .await.body.contains ( r # "name="backdrop" value="moving" checked"# ) ) ;
571
let res = oxygenhub:: router( app.clone ( ) )
572
.oneshot (
573
Request:: builder( )
574
.method ( " POST" )
575
.uri ( " /settings/appearance" )
576
.header ( header:: COOKIE , format! ( " oxyhub={andy} " ) )
577
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
578
.body ( Body:: from( format! ( " _csrf={csrf} &theme=light&backdrop=still" ) ) )
579
.unwrap ( ) ,
580
)
581
.await
582
.unwrap ( ) ;
583
let cookies: Vec< & str > = res.headers ( ) .get_all ( header:: SET_COOKIE ) .iter ( ) .filter_map ( | v | v.to_str ( ) .ok ( ) ) .collect ( ) ;
584
assert! (
585
cookies.iter ( ) .any ( | c | c.starts_with ( " theme=light;" ) ) & & cookies.iter ( ) .any ( | c | c.starts_with ( " backdrop=still;" ) ) ,
586
" {cookies:?}"
587
) ;
588
assert_eq! (
589
post ( app, " /settings/appearance" , Some ( & andy) , & format! ( " _csrf={csrf} &theme=light&backdrop=wild" ) ) .await.status,
590
StatusCode:: BAD_REQUEST
591
) ;
c40c2d3e Andy McCall 1 month ago ↶
592
assert_eq! ( post ( app, " /settings/appearance" , Some ( & andy) , " _csrf=nope&theme=light" ) .await.status, StatusCode:: BAD_REQUEST ) ;
593
}
594
595
async fn send_with_theme ( app : & Shared, session : & str , theme : & str ) -> String {
596
let req =
597
Request:: builder( ) .uri ( " /settings" ) .header ( header:: COOKIE , format! ( " oxyhub={session} ; theme={theme} " ) ) .body ( Body:: empty( ) ) .unwrap ( ) ;
598
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
599
String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( )
600
}
10174045 Andy McCall 1 month ago ↶
601
602
# [ tokio ::test ( flavor = " multi_thread" ) ]
603
async fn profile_and_organisation_pages ( ) {
604
let h = hub ( ) .await;
605
let app = & h.app;
606
let andy = register ( app, " andy" ) .await;
607
let bob = register ( app, " bob" ) .await;
608
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
609
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
610
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
611
db:: repos:: create( & app.db, " andy" , " demo" , " A demo" , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
612
app.repos.create ( & RepoName:: parse( " andy/hidden" ) .unwrap ( ) , None ) .unwrap ( ) ;
613
db:: repos:: create( & app.db, " andy" , " hidden" , " " , hub_api:: Visibility:: Private) .await.unwrap ( ) ;
614
615
616
let csrf = csrf_of ( & get ( app, " /settings" , Some ( & andy) ) .await.body) ;
617
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;
618
assert! ( r.body.contains ( " must be an http" ) , " {}" , r.body) ;
619
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;
620
assert! ( r.body.contains ( " Profile saved" ) , " {}" , r.body) ;
621
622
let org = db:: accounts:: create_org( & app.db, " retro" , " Retro Club" , andy_id) .await.unwrap ( ) ;
623
db:: accounts:: set_org_member( & app.db, org.id, bob_id, " member" ) .await.unwrap ( ) ;
624
let team = db:: accounts:: create_team( & app.db, org.id, " core" ) .await.unwrap ( ) ;
625
db:: accounts:: add_team_member( & app.db, team.id, bob_id) .await.unwrap ( ) ;
626
627
let page = get ( app, " /andy" , Some ( & bob) ) .await;
628
assert_eq! ( page.status, StatusCode:: OK ) ;
629
for want in [ " Andy McCall" , " Earth" , r # "href="https://example.org""# , " /avatar/andy" , " Signing keys" , " /retro" , " demo" ] {
630
assert! ( page.body.contains ( want) , " profile lacks {want}: {}" , page.body) ;
631
}
632
assert! ( ! page.body.contains ( " <b>oxy</b>" ) , " the bio is escaped" ) ;
633
assert! ( ! page.body.contains ( " [email protected] " ) , " the email stays private" ) ;
634
assert! ( ! page.body.contains ( " /andy/hidden" ) , " bob cannot see the private repository" ) ;
635
assert! ( ! page.body.contains ( " edit profile" ) , " only the owner gets the edit link" ) ;
636
assert! ( get ( app, " /andy" , Some ( & andy) ) .await.body.contains ( " /andy/hidden" ) , " andy sees his own private repository" ) ;
637
638
let page = get ( app, " /retro" , Some ( & bob) ) .await;
639
assert_eq! ( page.status, StatusCode:: OK ) ;
61ecbd29 Andy McCall 22 days ago ↶
640
for want in [ " Retro Club" , " organisation" , r # "Members <span class="count-chip">2</span>"# , " core" , " 1 member" ] {
10174045 Andy McCall 1 month ago ↶
641
assert! ( page.body.contains ( want) , " org page lacks {want}: {}" , page.body) ;
642
}
643
assert! ( ! page.body.contains ( " /orgs/retro/settings" ) , " bob is not an owner" ) ;
644
assert! ( get ( app, " /retro" , Some ( & andy) ) .await.body.contains ( " /orgs/retro/settings" ) ) ;
645
646
let svg = get ( app, " /avatar/andy" , None ) .await;
647
assert_eq! ( svg.status, StatusCode:: OK ) ;
648
assert! ( svg.body.starts_with ( " <svg" ) ) ;
649
assert_eq! ( svg.body, get ( app, " /avatar/ANDY" , None ) .await.body) ;
650
assert_eq! ( get ( app, " /nobody-here" , None ) .await.status, StatusCode:: NOT_FOUND ) ;
651
}
9df855f2 Andy McCall 1 month ago ↶
652
653
# [ tokio ::test ( flavor = " multi_thread" ) ]
654
async fn diffs_mark_words_hide_whitespace_and_collapse ( ) {
655
let h = hub ( ) .await;
656
let app = & h.app;
657
let andy = register ( app, " andy" ) .await;
658
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
659
db:: repos:: create( & app.db, " andy" , " demo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
660
let work = tempfile:: tempdir( ) .unwrap ( ) ;
661
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
662
client.config.set ( " remote.hub.url" , & format! ( " {} /andy/demo" , h.base) ) .unwrap ( ) ;
663
client.config.set ( " remote.hub.token" , " site" ) .unwrap ( ) ;
664
client.save_config ( ) .unwrap ( ) ;
665
write ( work.path ( ) , " main.rs" , b " fn main() {\n setup();\n let answer = 42;\n }\n " ) ;
666
client.commit ( " first" , & [ ] ) .unwrap ( ) ;
667
write ( work.path ( ) , " main.rs" , b " fn main() {\n setup();\n let answer = 43;\n }\n " ) ;
668
let second = client.commit ( " reindent and change the answer" , & [ ] ) .unwrap ( ) ;
669
assert! ( client.push ( " hub" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
670
671
let url = format! ( " /andy/demo/change/{} " , second.to_hex ( ) ) ;
672
let page = get ( app, & url, Some ( & andy) ) .await;
673
assert_eq! ( page.status, StatusCode:: OK ) ;
674
assert! ( page.body.contains ( r # "<details class="file-diff""# ) , " files collapse: {}" , page.body) ;
675
assert! ( page.body.contains ( r # "<span class="wx">43</span>"# ) , " the changed word is marked: {}" , page.body) ;
676
assert! ( page.body.contains ( " hide whitespace changes" ) ) ;
677
assert! ( page.body.contains ( " data-diff-mode=\" split\" " ) ) ;
678
let adds = | b : & str | b.matches ( r # "<tr class="add">"# ) .count ( ) ;
679
assert_eq! ( adds ( & page.body) , 2 , " re-indent and answer both changed" ) ;
680
681
let quiet = get ( app, & format! ( " {url} ?w=1" ) , Some ( & andy) ) .await;
682
assert_eq! ( adds ( & quiet.body) , 1 , " only the answer changed ignoring whitespace: {}" , quiet.body) ;
683
assert! ( quiet.body.contains ( " show whitespace changes" ) ) ;
684
685
assert! ( quiet.body.contains ( " ?w=0" ) , " the toggle leads back" ) ;
686
}
07b1a5bb Andy McCall 1 month ago ↶
687
688
# [ tokio ::test ( flavor = " multi_thread" ) ]
689
async fn fork_and_propose_upstream ( ) {
690
let h = hub ( ) .await;
691
let app = & h.app;
692
let andy = register ( app, " andy" ) .await;
693
let bob = register ( app, " bob" ) .await;
694
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
695
db:: repos:: create( & app.db, " andy" , " demo" , " The demo" , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
696
let work = tempfile:: tempdir( ) .unwrap ( ) ;
697
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
698
client.config.set ( " remote.hub.url" , & format! ( " {} /andy/demo" , h.base) ) .unwrap ( ) ;
699
client.config.set ( " remote.hub.token" , " site" ) .unwrap ( ) ;
700
client.save_config ( ) .unwrap ( ) ;
701
write ( work.path ( ) , " README.md" , b " # demo\n " ) ;
702
client.commit ( " first" , & [ ] ) .unwrap ( ) ;
703
assert! ( client.push ( " hub" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
704
705
706
let page = get ( app, " /andy/demo" , Some ( & bob) ) .await;
707
assert! ( page.body.contains ( " /andy/demo/fork" ) , " a fork button: {}" , page.body) ;
708
let csrf = csrf_of ( & get ( app, " /andy/demo/fork" , Some ( & bob) ) .await.body) ;
709
let r = post ( app, " /andy/demo/fork" , Some ( & bob) , & format! ( " _csrf={csrf} &owner=andy&name=other" ) ) .await;
710
assert! ( r.body.contains ( " cannot create repositories under that owner" ) , " {}" , r.body) ;
711
let r = post ( app, " /andy/demo/fork" , Some ( & bob) , & format! ( " _csrf={csrf} &owner=bob&name=demo" ) ) .await;
712
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
713
assert_eq! ( r.location.as_deref ( ) , Some ( " /bob/demo" ) ) ;
714
let forked = get ( app, " /bob/demo" , Some ( & bob) ) .await;
715
assert! ( forked.body.contains ( " README.md" ) & & forked.body.contains ( " forked from" ) , " {}" , forked.body) ;
716
assert! ( forked.body.contains ( " propose to andy/demo" ) ) ;
717
assert! ( get ( app, " /andy/demo" , None ) .await.body.contains ( " 1 fork" ) ) ;
718
assert! ( get ( app, " /andy/demo/forks" , None ) .await.body.contains ( " /bob/demo" ) ) ;
719
let fork_row = db:: repos:: get( & app.db, " bob" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
720
let up_row = db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
721
assert_eq! ( fork_row.forked_from, Some ( up_row.id) ) ;
722
assert_eq! ( fork_row.description, " The demo" ) ;
723
let fork_repo = app.repos.open ( & RepoName:: parse( " bob/demo" ) .unwrap ( ) ) .unwrap ( ) ;
724
let up_repo = app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) ;
725
assert_ne! ( fork_repo.id ( ) .unwrap ( ) , up_repo.id ( ) .unwrap ( ) , " a fork is a repository of its own" ) ;
726
assert_eq! ( fork_repo.bookmark_heads ( ) .unwrap ( ) , up_repo.bookmark_heads ( ) .unwrap ( ) ) ;
727
assert_eq! ( fork_repo.fsck ( ) .unwrap ( ) .errors ( ) , 0 ) ;
728
729
730
let bw = tempfile:: tempdir( ) .unwrap ( ) ;
731
let mut mine = Repo:: clone( & format! ( " {} /bob/demo" , h.base) , & bw.path ( ) .join ( " c" ) ) .unwrap ( ) ;
732
mine.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
733
mine.save_config ( ) .unwrap ( ) ;
734
mine.bookmark_create ( " feat" , None ) .unwrap ( ) ;
735
mine.bookmark_switch ( " feat" , false ) .unwrap ( ) ;
736
write ( & bw.path ( ) .join ( " c" ) , " feature.txt" , b " new\n " ) ;
737
mine.commit ( " Add a feature" , & [ ] ) .unwrap ( ) ;
738
assert! ( mine.push_bookmark ( " origin" , " feat" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
739
let csrf = csrf_of ( & get ( app, " /andy/demo/proposals/new?source=bob/demo:feat" , Some ( & bob) ) .await.body) ;
740
let r =
741
post ( app, " /andy/demo/proposals/new" , Some ( & bob) , & format! ( " _csrf={csrf} &title=From+my+fork&target=main&source=bob%2Fdemo%3Afeat" ) )
742
.await;
743
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
744
let p = get ( app, & r.location.unwrap ( ) , Some ( & andy) ) .await;
745
assert! ( p.body.contains ( " From my fork" ) & & p.body.contains ( " fork bob/demo" ) , " {}" , p.body) ;
746
let files = get ( app, " /andy/demo/proposal/1?tab=files" , Some ( & andy) ) .await;
747
assert! ( files.body.contains ( " feature.txt" ) , " {}" , files.body) ;
748
}
1553bfc9 Andy McCall 1 month ago ↶
749
750
# [ tokio ::test ( flavor = " multi_thread" ) ]
751
async fn stars_topics_and_explore ( ) {
752
let h = hub ( ) .await;
753
let app = & h.app;
754
let andy = register ( app, " andy" ) .await;
755
let bob = register ( app, " bob" ) .await;
756
for ( name, desc) in [ ( " alpha" , " An emulator" ) , ( " beta" , " A compiler" ) , ( " gamma" , " Retro games" ) ] {
757
app.repos.create ( & RepoName:: parse( & format! ( " andy/{name} " ) ) .unwrap ( ) , None ) .unwrap ( ) ;
758
db:: repos:: create( & app.db, " andy" , name, desc, hub_api:: Visibility:: Public) .await.unwrap ( ) ;
759
}
760
761
762
let csrf = csrf_of ( & get ( app, " /andy/gamma/settings" , Some ( & andy) ) .await.body) ;
763
let r = post (
764
app,
765
" /andy/gamma/settings" ,
766
Some ( & andy) ,
767
& format! ( " _csrf={csrf} &description=Retro+games&default_bookmark=main&visibility=public&topics=c%2B%2B" ) ,
768
)
769
.await;
c7067c7b Andy McCall 16 days ago
770
let r = settled ( app, r, Some ( & andy) , " /andy/gamma/settings/general" ) .await;
1553bfc9 Andy McCall 1 month ago ↶
771
assert! ( r.body.contains ( " is not a topic" ) , " {}" , r.body) ;
772
let r = post (
773
app,
774
" /andy/gamma/settings" ,
775
Some ( & andy) ,
776
& format! ( " _csrf={csrf} &description=Retro+games&default_bookmark=main&visibility=public&topics=Retro%2C+6502" ) ,
777
)
778
.await;
c7067c7b Andy McCall 16 days ago
779
let r = settled ( app, r, Some ( & andy) , " /andy/gamma/settings/general" ) .await;
1553bfc9 Andy McCall 1 month ago ↶
780
assert! ( r.body.contains ( " Saved" ) , " {}" , r.body) ;
781
assert_eq! ( db:: repos:: get( & app.db, " andy" , " gamma" ) .await.unwrap ( ) .unwrap ( ) .topics, " retro 6502" ) ;
782
let page = get ( app, " /andy/gamma" , None ) .await;
783
assert! ( page.body.contains ( r # "href="/explore?topic=retro""# ) , " {}" , page.body) ;
784
785
786
for ( who, repo) in [ ( & bob, " beta" ) , ( & bob, " gamma" ) , ( & andy, " gamma" ) , ( & andy, " alpha" ) ] {
787
let csrf = csrf_of ( & get ( app, & format! ( " /andy/{repo} " ) , Some ( who) ) .await.body) ;
788
assert_eq! ( post ( app, & format! ( " /andy/{repo} /star" ) , Some ( who) , & format! ( " _csrf={csrf} " ) ) .await.status, StatusCode:: SEE_OTHER ) ;
789
}
790
let csrf = csrf_of ( & get ( app, " /andy/alpha" , Some ( & andy) ) .await.body) ;
791
post ( app, " /andy/alpha/star" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
792
assert! ( get ( app, " /andy/gamma" , Some ( & bob) ) .await.body.contains ( " ★ UNSTAR" ) ) ;
7f283f8e Andy McCall 22 days ago ↶
793
assert! ( get ( app, " /andy/gamma" , None ) .await.body.contains ( r # "title="2 stars">"# ) ) ;
794
assert! ( get ( app, " /andy/alpha" , None ) .await.body.contains ( r # "title="0 stars">"# ) , " unstarred again" ) ;
1553bfc9 Andy McCall 1 month ago ↶
795
796
797
let order = | b : & str | {
798
[ " alpha" , " beta" , " gamma" ]
799
.into_iter ( )
800
.map ( | n | ( b.find ( & format! ( " /andy/{n} \" " ) ) .unwrap_or ( usize :: MAX ) , n) )
801
.collect:: < std:: collections:: BTreeMap< _ , _ > > ( )
802
.into_values ( )
803
.collect:: < Vec< _ > > ( )
804
} ;
805
assert_eq! ( order ( & get ( app, " /explore?sort=stars" , None ) .await.body) [ .. 2 ] , [ " gamma" , " beta" ] ) ;
806
assert_eq! ( order ( & get ( app, " /explore?sort=name" , None ) .await.body) , [ " alpha" , " beta" , " gamma" ] ) ;
807
let filtered = get ( app, " /explore?q=compiler" , None ) .await.body;
808
assert! ( filtered.contains ( " /andy/beta\" " ) & & ! filtered.contains ( " /andy/alpha\" " ) , " {filtered}" ) ;
809
let by_topic = get ( app, " /explore?topic=6502" , None ) .await.body;
810
assert! ( by_topic.contains ( " /andy/gamma\" " ) & & ! by_topic.contains ( " /andy/beta\" " ) , " {by_topic}" ) ;
811
}
1ac84c62 Andy McCall 1 month ago ↶
812
813
# [ tokio ::test ( flavor = " multi_thread" ) ]
814
async fn lists_filter_sort_page_and_count ( ) {
815
let h = hub ( ) .await;
816
let app = & h.app;
817
let andy = register ( app, " andy" ) .await;
818
let _bob = register ( app, " bob" ) .await;
819
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
820
let repo = db:: repos:: create( & app.db, " andy" , " demo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
821
for n in 0 .. 30 {
822
let author = if n % 3 = = 0 { " bob" } else { " andy" } ;
823
db:: issues:: create(
824
& app.db,
825
db:: issues:: NewIssue { repo_id: repo.id, title: & format! ( " Issue number {n} " ) , body: " " , author_id: None , author } ,
826
)
827
.await
828
.unwrap ( ) ;
829
}
830
let ui = db:: issues:: upsert_label( & app.db, repo.id, " ui" , " #00f0ff" , " " ) .await.unwrap ( ) ;
831
for ( title, author, target) in [ ( " Faster menus" , " bob" , " main" ) , ( " New theme" , " andy" , " main" ) , ( " Release prep" , " andy" , " stable" ) ] {
832
let p = db:: proposals:: create(
833
& app.db,
834
db:: proposals:: NewProposal {
835
repo_id: repo.id,
836
title,
837
body: " " ,
838
author_id: None ,
839
author,
840
draft: false ,
841
target,
842
source_kind: " changes" ,
843
source_repo_id: None ,
844
source_bookmark: " " ,
845
source_changes: " " ,
846
} ,
847
)
848
.await
849
.unwrap ( ) ;
850
if title = = " New theme" {
851
db:: issues:: set_proposal_label( & app.db, p.id, ui.id, true ) .await.unwrap ( ) ;
852
}
853
}
854
855
856
let page = get ( app, " /andy/demo/issues" , Some ( & andy) ) .await;
857
assert! ( page.body.contains ( r # "Issues <span class="count-chip">30</span>"# ) , " {}" , page.body) ;
858
assert! ( page.body.contains ( r # "Proposals <span class="count-chip">3</span>"# ) ) ;
859
860
861
let rows = | b : & str | b.matches ( " /andy/demo/issue/" ) .count ( ) ;
862
assert_eq! ( rows ( & page.body) , 25 ) ;
863
assert! ( page.body.contains ( " page 1 of 2" ) & & page.body.contains ( " page=2" ) , " a pager" ) ;
864
let second = get ( app, " /andy/demo/issues?page=2" , Some ( & andy) ) .await;
865
assert_eq! ( rows ( & second.body) , 5 ) ;
866
assert! ( second.body.contains ( " Issue number 0<" ) , " the oldest are last" ) ;
867
let oldest = get ( app, " /andy/demo/issues?sort=oldest" , Some ( & andy) ) .await;
868
assert! ( oldest.body.find ( " Issue number 0<" ) .unwrap ( ) < oldest.body.find ( " Issue number 1<" ) .unwrap ( ) ) ;
869
let bobs = get ( app, " /andy/demo/issues?q=author:bob" , Some ( & andy) ) .await;
870
assert_eq! ( rows ( & bobs.body) , 10 , " {}" , bobs.body) ;
871
872
873
let by_bob = get ( app, " /andy/demo/proposals?q=author:bob" , Some ( & andy) ) .await.body;
874
assert! ( by_bob.contains ( " Faster menus" ) & & ! by_bob.contains ( " New theme" ) ) ;
875
let ui_only = get ( app, " /andy/demo/proposals?q=label:ui" , Some ( & andy) ) .await.body;
876
assert! ( ui_only.contains ( " New theme" ) & & ! ui_only.contains ( " Faster menus" ) & & ui_only.contains ( r # "class="label""# ) ) ;
877
let stable = get ( app, " /andy/demo/proposals?q=target:stable" , Some ( & andy) ) .await.body;
878
assert! ( stable.contains ( " Release prep" ) & & ! stable.contains ( " New theme" ) ) ;
879
}
e1ec9fbf Andy McCall 1 month ago ↶
880
881
# [ tokio ::test ( flavor = " multi_thread" ) ]
882
async fn project_boards ( ) {
883
let h = hub ( ) .await;
884
let app = & h.app;
885
let andy = register ( app, " andy" ) .await;
886
let bob = register ( app, " bob" ) .await;
887
for ( name, vis) in [ ( " demo" , hub_api:: Visibility:: Public) , ( " secret" , hub_api:: Visibility:: Private) ] {
888
app.repos.create ( & RepoName:: parse( & format! ( " andy/{name} " ) ) .unwrap ( ) , None ) .unwrap ( ) ;
889
let row = db:: repos:: create( & app.db, " andy" , name, " " , vis) .await.unwrap ( ) ;
890
db:: issues:: create(
891
& app.db,
892
db:: issues:: NewIssue { repo_id: row.id, title: & format! ( " {name} issue" ) , body: " " , author_id: None , author: " andy" } ,
893
)
894
.await
895
.unwrap ( ) ;
896
}
16ee5d19 Andy McCall 18 days ago ↶
897
898
899
a024e800 Andy McCall 27 days ago ↶
900
let home = get ( app, " /andy/demo" , Some ( & andy) ) .await.body;
16ee5d19 Andy McCall 18 days ago ↶
901
assert! ( home.contains ( " href=\" /projects/andy\" ><svg class=\" ic\" " ) , " site bar" ) ;
902
assert! ( ! home.contains ( " project boards\" >Projects</a>" ) , " not a repository tab" ) ;
903
assert! ( get ( app, " /andy" , Some ( & bob) ) .await.body.contains ( " href=\" /projects/andy\" >Projects</a>" ) , " the owner's profile" ) ;
a024e800 Andy McCall 27 days ago ↶
904
let list = get ( app, " /projects/andy" , Some ( & andy) ) .await.body;
905
assert! ( list.contains ( " href=\" #new\" >+ NEW PROJECT</a>" ) & & list.contains ( " id=\" new\" " ) , " the button and the form" ) ;
906
assert! ( get ( app, " /projects/andy" , Some ( & bob) ) .await.body.contains ( " Only andy" ) , " who can make one" ) ;
907
let csrf = csrf_of ( & list) ;
e1ec9fbf Andy McCall 1 month ago ↶
908
let r = post ( app, " /projects/andy" , Some ( & andy) , & format! ( " _csrf={csrf} &title=Roadmap&description=Next+steps" ) ) .await;
909
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/andy/1" ) , " {}" , r.body) ;
7b26e611 Andy McCall 25 days ago ↶
910
911
let list = get ( app, " /projects/andy" , Some ( & andy) ) .await.body;
912
assert! (
913
list.contains ( " >Roadmap</a>" ) & & list.contains ( " Next steps" ) & & list.contains ( " Open <span class=\" count-chip\" >1</span>" ) ,
914
" {list}"
915
) ;
916
let filtered = get ( app, " /projects/andy?q=roadm" , Some ( & andy) ) .await.body;
917
assert! ( filtered.contains ( " >Roadmap</a>" ) , " a filter matches part of the title, any case" ) ;
918
let none = get ( app, " /projects/andy?q=nothing+like+it" , Some ( & andy) ) .await.body;
919
assert! ( none.contains ( " No open project matches" ) & & ! none.contains ( " >Roadmap</a>" ) , " {none}" ) ;
920
let closed = get ( app, " /projects/andy?state=closed" , Some ( & andy) ) .await.body;
921
assert! ( closed.contains ( " No closed projects." ) & & closed.contains ( " Closed <span class=\" count-chip\" >0</span>" ) , " {closed}" ) ;
e1ec9fbf Andy McCall 1 month ago ↶
922
let board = get ( app, " /projects/andy/1" , Some ( & andy) ) .await.body;
923
for col in [ " Backlog" , " Doing" , " Done" ] {
924
assert! ( board.contains ( & format! ( " >{col} <span" ) ) , " {col}" ) ;
925
}
926
let cols: Vec< i64 > = sqlx:: query_scalar( " SELECT id FROM project_columns ORDER BY position" ) .fetch_all ( & app.db) .await.unwrap ( ) ;
927
let act = | form : String| {
928
let andy = andy.clone ( ) ;
929
async move { post ( app, " /projects/andy/1" , Some ( & andy) , & form) .await }
930
} ;
931
932
assert_eq! (
933
act ( format! ( " _csrf={csrf} &action=add&column={} &reference=demo%231" , cols[ 0 ] ) ) .await.location.as_deref ( ) ,
934
Some ( " /projects/andy/1" )
935
) ;
936
act ( format! ( " _csrf={csrf} &action=add&column={} &repo=secret&reference=%231" , cols[ 0 ] ) ) .await;
937
act ( format! ( " _csrf={csrf} &action=add&column={} ¬e=Write+the+docs" , cols[ 1 ] ) ) .await;
938
assert! ( act ( format! ( " _csrf={csrf} &action=add&column={} &reference=demo%231" , cols[ 1 ] ) ) .await.body.contains ( " already on this project" ) ) ;
939
assert! ( act ( format! ( " _csrf={csrf} &action=add&column={} &reference=demo%2399" , cols[ 0 ] ) ) .await.body.contains ( " no issue or proposal #99" ) ) ;
940
assert! ( act ( format! ( " _csrf={csrf} &action=add&column={} &reference=nothere%231" , cols[ 0 ] ) ) .await.body.contains ( " no repository nothere" ) ) ;
941
942
let demo_card: i64 =
943
sqlx:: query_scalar( " SELECT c.id FROM project_cards c JOIN repositories r ON r.id = c.repo_id WHERE r.name = 'demo'" )
944
.fetch_one ( & app.db)
945
.await
946
.unwrap ( ) ;
947
act ( format! ( " _csrf={csrf} &action=move&card={demo_card} &column={} " , cols[ 2 ] ) ) .await;
948
let issue = get ( app, " /andy/demo/issue/1" , None ) .await.body;
949
assert! ( issue.contains ( " href=\" /projects/andy/1\" >Roadmap</a> <span class=\" mute\" >· Done" ) , " the issue shows where it is" ) ;
950
951
952
let anon = get ( app, " /projects/andy/1" , None ) .await.body;
953
assert! ( anon.contains ( " demo issue" ) & & anon.contains ( " Write the docs" ) & & ! anon.contains ( " secret issue" ) , " {anon}" ) ;
954
assert! ( ! anon.contains ( " name=\" action\" " ) , " and cannot change anything" ) ;
955
assert! ( get ( app, " /projects/andy/1" , Some ( & andy) ) .await.body.contains ( " secret issue" ) ) ;
956
957
let csrf_b = csrf_of ( & get ( app, " /settings" , Some ( & bob) ) .await.body) ;
958
assert_eq! (
959
post ( app, " /projects/andy/1" , Some ( & bob) , & format! ( " _csrf={csrf_b} &action=remove&card={demo_card} " ) ) .await.status,
960
StatusCode:: NOT_FOUND
961
) ;
962
act ( format! ( " _csrf={csrf} &action=settings&title=Roadmap&description=&private=1" ) ) .await;
963
assert_eq! ( get ( app, " /projects/andy/1" , Some ( & bob) ) .await.status, StatusCode:: NOT_FOUND ) ;
964
assert! ( ! get ( app, " /projects/andy" , None ) .await.body.contains ( " Roadmap" ) , " not even listed" ) ;
965
assert! ( ! get ( app, " /andy/demo/issue/1" , None ) .await.body.contains ( " Roadmap" ) , " nor on the issue" ) ;
966
967
968
act ( format! ( " _csrf={csrf} &action=remove_column&column={} " , cols[ 1 ] ) ) .await;
969
let note_col: i64 = sqlx:: query_scalar( " SELECT column_id FROM project_cards WHERE kind = 'note'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
970
assert_eq! ( note_col, cols[ 0 ] ) ;
971
act ( format! ( " _csrf={csrf} &action=remove_column&column={} " , cols[ 2 ] ) ) .await;
972
assert! ( act ( format! ( " _csrf={csrf} &action=remove_column&column={} " , cols[ 0 ] ) ) .await.body.contains ( " at least one column" ) ) ;
973
974
act ( format! ( " _csrf={csrf} &action=settings&title=Roadmap&closed=1" ) ) .await;
975
assert! ( act ( format! ( " _csrf={csrf} &action=add&column={} ¬e=late" , cols[ 0 ] ) ) .await.body.contains ( " This project is closed" ) ) ;
976
let r = act ( format! ( " _csrf={csrf} &action=delete" ) ) .await;
977
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/andy" ) ) ;
978
assert_eq! ( get ( app, " /projects/andy/1" , Some ( & andy) ) .await.status, StatusCode:: NOT_FOUND ) ;
979
assert_eq! ( get ( app, " /andy/demo/issue/1" , None ) .await.status, StatusCode:: OK ) ;
980
assert_eq! ( get ( app, " /projects/nobody" , None ) .await.status, StatusCode:: NOT_FOUND ) ;
981
}
c79a1111 Andy McCall 1 month ago ↶
982
983
# [ tokio ::test ( flavor = " multi_thread" ) ]
984
async fn issue_markdown_follows_the_default_bookmark ( ) {
985
let h = hub ( ) .await;
986
let app = & h.app;
987
register ( app, " andy" ) .await;
988
app.repos.create ( & RepoName:: parse( " andy/trunky" ) .unwrap ( ) , None ) .unwrap ( ) ;
989
let row = db:: repos:: create( & app.db, " andy" , " trunky" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
3b1bc27f Andy McCall 20 days ago
990
sqlx:: query( " UPDATE repositories SET default_bookmark = 'trunk' WHERE id = $1" ) .bind ( row.id) .execute ( & app.db) .await.unwrap ( ) ;
c79a1111 Andy McCall 1 month ago ↶
991
db:: issues:: create(
992
& app.db,
993
db:: issues:: NewIssue {
994
repo_id: row.id,
995
title: " Docs" ,
996
body: " See [the guide](docs/guide.md) and ." ,
997
author_id: None ,
998
author: " andy" ,
999
} ,
1000
)
1001
.await
1002
.unwrap ( ) ;
1003
let page = get ( app, " /andy/trunky/issue/1" , None ) .await.body;
1004
assert! ( page.contains ( " href=\" /andy/trunky/file/trunk/docs/guide.md\" " ) , " {page}" ) ;
1005
assert! ( page.contains ( " src=\" /andy/trunky/raw/trunk/img/a.png\" " ) ) ;
1006
assert! ( ! page.contains ( " /file/main/" ) ) ;
1007
}
b1e92a62 Andy McCall 25 days ago ↶
1008
1009
1010
1011
1012
# [ tokio ::test ( flavor = " multi_thread" ) ]
1013
async fn a_project_transfers_to_an_organisation ( ) {
1014
let h = hub ( ) .await;
1015
let app = & h.app;
1016
1017
1018
let _first = register ( app, " ada" ) .await;
1019
let andy = register ( app, " andy" ) .await;
1020
let _bob = register ( app, " bob" ) .await;
1021
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
1022
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
1023
db:: accounts:: create_org( & app.db, " retro" , " Retro Club" , andy_id) .await.unwrap ( ) ;
1024
db:: accounts:: create_org( & app.db, " other" , " Someone else's" , bob_id) .await.unwrap ( ) ;
1025
let list = get ( app, " /projects/andy" , Some ( & andy) ) .await.body;
1026
let csrf = csrf_of ( & list) ;
1027
let r = post ( app, " /projects/andy" , Some ( & andy) , & format! ( " _csrf={csrf} &title=Roadmap" ) ) .await;
1028
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/andy/1" ) ) ;
1029
post ( app, " /projects/andy/1" , Some ( & andy) , & format! ( " _csrf={csrf} &action=add¬e=Keep+me" ) ) .await;
1030
1031
1032
let board = get ( app, " /projects/andy/1" , Some ( & andy) ) .await.body;
1033
assert! ( board.contains ( " <option value=\" retro\" >retro</option>" ) & & ! board.contains ( " value=\" other\" " ) , " {board}" ) ;
1034
let refused = post ( app, " /projects/andy/1" , Some ( & andy) , & format! ( " _csrf={csrf} &action=transfer&to=other" ) ) .await;
1035
assert! ( refused.body.contains ( " You cannot make projects under other" ) , " {}" , refused.body) ;
1036
1037
let r = post ( app, " /projects/andy/1" , Some ( & andy) , & format! ( " _csrf={csrf} &action=transfer&to=retro" ) ) .await;
1038
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/retro/1" ) , " {}" , r.body) ;
1039
let moved = get ( app, " /projects/retro/1" , Some ( & andy) ) .await.body;
1040
assert! ( moved.contains ( " Roadmap" ) & & moved.contains ( " Keep me" ) , " the cards come too" ) ;
1041
let old = get ( app, " /projects/andy/1" , Some ( & andy) ) .await;
1042
assert_eq! ( old.status, StatusCode:: PERMANENT_REDIRECT ) ;
1043
assert_eq! ( old.location.as_deref ( ) , Some ( " /projects/retro/1" ) ) ;
1044
1045
1046
let r = post ( app, " /projects/andy" , Some ( & andy) , & format! ( " _csrf={csrf} &title=Fresh" ) ) .await;
1047
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/andy/1" ) ) ;
1048
let fresh = get ( app, " /projects/andy/1" , Some ( & andy) ) .await;
1049
assert_eq! ( fresh.status, StatusCode:: OK ) ;
1050
assert! ( fresh.body.contains ( " Fresh" ) ) ;
1051
}
d7d18246 Andy McCall 22 days ago ↶
1052
1053
1054
1055
# [ tokio ::test ( flavor = " multi_thread" ) ]
1056
async fn backups_are_set_up_in_settings ( ) {
1057
let h = hub ( ) .await;
1058
let app = & h.app;
1059
let andy = register ( app, " andy" ) .await;
1060
let page = get ( app, " /settings/backups" , Some ( & andy) ) .await;
1061
assert_eq! ( page.status, StatusCode:: OK ) ;
1062
assert! ( page.body.contains ( " href=\" /settings/backups\" class=\" on\" " ) & & page.body.contains ( " No runs yet." ) , " {}" , page.body) ;
1063
let csrf = csrf_of ( & page.body) ;
1064
1065
let bad = post (
1066
app,
1067
" /settings/backups" ,
1068
Some ( & andy) ,
1069
& format! ( " _csrf={csrf} &every=hourly&destination=gcs&bucket=andy-backup&provider=nope&keep_days=30" ) ,
1070
)
1071
.await;
1072
assert! ( bad.body.contains ( " workload identity provider" ) , " {}" , bad.body) ;
1073
let dir =
1074
post ( app, " /settings/backups" , Some ( & andy) , & format! ( " _csrf={csrf} &every=hourly&destination=dir&dir=%2Ftmp&keep_days=30" ) ) .await;
1075
assert! ( dir.body.contains ( " Pick one of the directories this hub allows." ) ) ;
1076
let provider =
1077
" %2F%2Fiam.googleapis.com%2Fprojects%2F1%2Flocations%2Fglobal%2FworkloadIdentityPools%2Foxygenhub%2Fproviders%2Foxygenhub-io" ;
1078
let ok = post (
1079
app,
1080
" /settings/backups" ,
1081
Some ( & andy) ,
1082
& format! ( " _csrf={csrf} &every=hourly&destination=gcs&bucket=gs%3A%2F%2Fandy-backup&provider={provider} &keep_days=7" ) ,
1083
)
1084
.await;
1085
assert! ( ok.body.contains ( " Saved." ) , " {}" , ok.body) ;
1086
let s = oxygenhub:: backups:: setting( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
1087
assert_eq! ( ( s.target.as_str ( ) , s.keep_days, s.every.as_str ( ) ) , ( " andy-backup" , 7 , " hourly" ) ) ;
7b9f4570 Andy McCall 22 days ago ↶
1088
1089
let bad = post (
1090
app,
1091
" /settings/backups" ,
1092
Some ( & andy) ,
1093
& format! ( " _csrf={csrf} &every=hourly&destination=s3&s3_bucket=andy-backup&s3_region=eu-west-2&s3_role=admin&keep_days=30" ) ,
1094
)
1095
.await;
1096
assert! ( bad.body.contains ( " the IAM role" ) , " {}" , bad.body) ;
1097
let role = " arn%3Aaws%3Aiam%3A%3A123456789012%3Arole%2Foxygenhub-backup" ;
1098
let ok = post (
1099
app,
1100
" /settings/backups" ,
1101
Some ( & andy) ,
1102
& format! (
1103
" _csrf={csrf} &every=daily&destination=s3&s3_bucket=s3%3A%2F%2Fandy-backup&s3_region=eu-west-2&s3_role={role} &keep_days=30"
1104
) ,
1105
)
1106
.await;
1107
assert! ( ok.body.contains ( " Saved." ) & & ok.body.contains ( r # "value="s3" checked"# ) , " {}" , ok.body) ;
1108
let s = oxygenhub:: backups:: setting( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
1109
assert_eq! ( ( s.destination.as_str ( ) , s.target.as_str ( ) , s.region.as_str ( ) ) , ( " s3" , " andy-backup" , " eu-west-2" ) ) ;
1110
assert_eq! ( s.provider, " arn:aws:iam::123456789012:role/oxygenhub-backup" ) ;
1111
1112
let ok = post (
1113
app,
1114
" /settings/backups" ,
1115
Some ( & andy) ,
1116
& format! ( " _csrf={csrf} &every=hourly&destination=gcs&bucket=gs%3A%2F%2Fandy-backup&provider={provider} &keep_days=7" ) ,
1117
)
1118
.await;
1119
assert! ( ok.body.contains ( " Saved." ) ) ;
d7d18246 Andy McCall 22 days ago ↶
1120
1121
let bob = register ( app, " bob" ) .await;
1122
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
1123
db:: accounts:: create_org( & app.db, " club" , " Club" , andy_id) .await.unwrap ( ) ;
1124
assert_eq! ( get ( app, " /orgs/club/settings/backups" , Some ( & bob) ) .await.status, StatusCode:: NOT_FOUND ) ;
1125
let club = get ( app, " /orgs/club/settings/backups" , Some ( & andy) ) .await;
1126
assert! ( club.body.contains ( " club</a>'s repositories" ) , " {}" , club.body) ;
1127
assert! ( get ( app, " /orgs/club/settings" , Some ( & andy) ) .await.body.contains ( " /orgs/club/settings/backups" ) ) ;
1e8a6e8b Andy McCall 22 days ago ↶
1128
1129
1130
1131
sqlx:: query( " UPDATE accounts SET is_admin = (username = 'andy')" ) .execute ( & app.db) .await.unwrap ( ) ;
1132
app.repos.create ( & oxygenhub:: repos:: RepoName:: parse( " bob/game" ) .unwrap ( ) , None ) .unwrap ( ) ;
1133
db:: repos:: create( & app.db, " bob" , " game" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
1134
assert_eq! ( get ( app, " /admin/backups" , Some ( & bob) ) .await.status, StatusCode:: NOT_FOUND ) ;
1135
let admin = get ( app, " /admin/backups" , Some ( & andy) ) .await;
1136
assert_eq! ( admin.status, StatusCode:: OK ) ;
1137
assert! ( admin.body.contains ( " gs://andy-backup" ) & & admin.body.contains ( " hourly" ) & & admin.body.contains ( " never" ) , " {}" , admin.body) ;
1138
let missing = & admin.body[ admin.body.find ( " Not backed up" ) .unwrap ( ) .. ] ;
1139
assert! ( missing.contains ( " href=\" /bob\" " ) & & missing.contains ( " 1 repository" ) , " {missing}" ) ;
1140
assert! ( get ( app, " /admin" , Some ( & andy) ) .await.body.contains ( " /admin/backups" ) ) ;
d3b048b2 Andy McCall 22 days ago ↶
1141
c7067c7b Andy McCall 16 days ago
1142
let game = get ( app, " /bob/game/settings/backup" , Some ( & bob) ) .await;
d3b048b2 Andy McCall 22 days ago ↶
1143
assert! ( game.body.contains ( " bob has no backup set up yet" ) & & game.body.contains ( r # "name="backup" value="" checked"# ) , " {}" , game.body) ;
1144
let csrf = csrf_of ( & game.body) ;
1145
let saved = post ( app, " /bob/game/settings/backup" , Some ( & bob) , & format! ( " _csrf={csrf} &backup=off" ) ) .await;
c7067c7b Andy McCall 16 days ago
1146
let saved = settled ( app, saved, Some ( & bob) , " /bob/game/settings/backup" ) .await;
d3b048b2 Andy McCall 22 days ago ↶
1147
assert! ( saved.body.contains ( " left out of its owner" ) , " {}" , saved.body) ;
1148
assert_eq! ( db:: repos:: get( & app.db, " bob" , " game" ) .await.unwrap ( ) .unwrap ( ) .backup, " off" ) ;
1149
let bad = post ( app, " /bob/game/settings/backup" , Some ( & bob) , & format! ( " _csrf={csrf} &backup=weekly" ) ) .await;
c7067c7b Andy McCall 16 days ago
1150
let bad = settled ( app, bad, Some ( & bob) , " /bob/game/settings/backup" ) .await;
d3b048b2 Andy McCall 22 days ago ↶
1151
assert! ( bad.body.contains ( " Pick how this repository is backed up." ) ) ;
d7d18246 Andy McCall 22 days ago ↶
1152
}
bff9106e Andy McCall 22 days ago ↶
1153
1154
1155
1156
1157
# [ tokio ::test ( flavor = " multi_thread" ) ]
1158
async fn owners_hear_by_default_and_agents_tell_their_owner ( ) {
1159
let h = hub ( ) .await;
1160
let app = & h.app;
1161
let andy = register ( app, " andy" ) .await;
1162
let bob = register ( app, " bob" ) .await;
1163
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
1164
let kinds = | id : i64 | {
1165
let db = app.db.clone ( ) ;
1166
async move { db:: notify:: inbox( & db, id, 50 ) .await.unwrap ( ) .into_iter ( ) .map ( | n | ( n.kind, n.title, n.actor) ) .collect:: < Vec< _ > > ( ) }
1167
} ;
1168
assert! ( kinds ( andy_id) .await.iter ( ) .any ( | ( k , t , _) | k = = " account.registered" & & t.contains ( " bob" ) ) , " the administrator hears of bob" ) ;
1169
let repo = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
1170
app.repos.create ( & repo, None ) .unwrap ( ) ;
1171
db:: repos:: create( & app.db, " andy" , " demo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
1172
let open = | who : & 'static str , title : & 'static str | {
1173
let ( app, cookie) = ( app.clone ( ) , if who = = " bob" { bob.clone ( ) } else { andy.clone ( ) } ) ;
1174
async move {
1175
let csrf = csrf_of ( & get ( & app, " /andy/demo/issues/new" , Some ( & cookie) ) .await.body) ;
1176
post ( & app, " /andy/demo/issues/new" , Some ( & cookie) , & format! ( " _csrf={csrf} &title={title} &body=x" ) ) .await
1177
}
1178
} ;
1179
1180
assert! ( get ( app, " /andy/demo" , Some ( & andy) ) .await.body.contains ( " UNWATCH" ) , " an owner watches by default" ) ;
1181
open ( " bob" , " First" ) .await;
1182
assert! ( kinds ( andy_id) .await.iter ( ) .any ( | ( k , t , _) | k = = " issue.opened" & & t.contains ( " First" ) ) ) ;
1183
1184
let csrf = csrf_of ( & get ( app, " /andy/demo" , Some ( & andy) ) .await.body) ;
1185
post ( app, " /andy/demo/watch" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
1186
assert! ( get ( app, " /andy/demo" , Some ( & andy) ) .await.body.contains ( " >WATCH<" ) , " muted" ) ;
1187
open ( " bob" , " Second" ) .await;
1188
assert! ( ! kinds ( andy_id) .await.iter ( ) .any ( | ( _, t , _ ) | t.contains ( " Second" ) ) ) ;
1189
post ( app, " /andy/demo/watch" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
1190
1191
1192
let token = | agent : bool | {
1193
let db = app.db.clone ( ) ;
1194
async move {
1195
let ( t, hash) = oxygenhub:: auth:: new_token( ) ;
1196
db:: accounts:: create_token( & db, andy_id, if agent { " Claude" } else { " laptop" } , & hash, " repo:read,repo:write" , " " , None )
1197
.await
1198
.unwrap ( ) ;
1199
if agent {
1200
let id = db:: accounts:: token_by_hash( & db, & hash) .await.unwrap ( ) .unwrap ( ) .id;
1201
db:: accounts:: set_token_agent( & db, andy_id, id, true ) .await.unwrap ( ) ;
1202
}
1203
t
1204
}
1205
} ;
1206
let api_issue = | t : String, title : & 'static str | {
1207
let app = app.clone ( ) ;
1208
async move {
1209
use tower:: ServiceExt;
1210
let req = axum:: http:: Request:: post( " /api/v1/repos/andy/demo/issues" )
1211
.header ( " authorization" , format! ( " Bearer {t} " ) )
1212
.header ( " content-type" , " application/json" )
1213
.body ( axum:: body:: Body:: from( serde_json:: json! ( { " title" : title, " body" : " " } ) .to_string ( ) ) )
1214
.unwrap ( ) ;
1215
oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) .status ( )
1216
}
1217
} ;
1218
assert_eq! ( api_issue ( token ( false ) .await, " By hand" ) .await, StatusCode:: CREATED ) ;
1219
assert! ( ! kinds ( andy_id) .await.iter ( ) .any ( | ( _, t , _ ) | t.contains ( " By hand" ) ) , " his own doing" ) ;
1220
assert_eq! ( api_issue ( token ( true ) .await, " By the agent" ) .await, StatusCode:: CREATED ) ;
1221
let told = kinds ( andy_id) .await;
1222
let agent = told.iter ( ) .find ( | ( _, t , _ ) | t.contains ( " By the agent" ) ) .expect ( " told of his agent's issue" ) ;
1223
assert_eq! ( agent.2 , " andy (agent token Claude)" ) ;
1224
1225
let page = get ( app, " /settings/tokens" , Some ( & andy) ) .await;
00ef7b83 Andy McCall 22 days ago ↶
1226
assert! ( page.body.contains ( " Not an agent" ) & & page.body.contains ( " Mark as agent" ) , " {}" , page.body) ;
1227
assert! ( page.body.contains ( r # "<span class="pill agent-pill""# ) , " the agent's token says so" ) ;
bff9106e Andy McCall 22 days ago ↶
1228
}