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 20 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 20 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 ) ) ;
254
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
255
let issue2 = db:: issues:: get( & app.db, db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) .id, 2 ) .await.unwrap ( ) .unwrap ( ) ;
256
assert_eq! ( issue2.state, " closed" , " closed by the landed change" ) ;
257
assert! ( get ( app, " /andy/demo/issue/2" , None ) .await.body.contains ( " closed by change" ) ) ;
258
259
260
client.bookmark_create ( " feat" , None ) .unwrap ( ) ;
261
client.bookmark_switch ( " feat" , false ) .unwrap ( ) ;
262
write ( work.path ( ) , " b.txt" , b " b\n " ) ;
263
client.snapshot ( " t" ) .unwrap ( ) ;
264
client.commit ( " feat" , & [ ] ) .unwrap ( ) ;
265
assert! ( client.push_bookmark ( " origin" , " feat" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
266
let csrf_a = csrf_of ( & get ( app, " /andy/demo/proposals/new?source=feat&target=main" , Some ( & andy) ) .await.body) ;
267
let r = post ( app, " /andy/demo/proposals/new" , Some ( & andy) , & format! ( " _csrf={csrf_a} &title=Feat&target=main&source=feat" ) ) .await;
268
assert_eq! ( r.location.as_deref ( ) , Some ( " /andy/demo/proposal/3" ) , " {}" , r.body) ;
269
assert_eq! ( get ( app, " /andy/demo/issue/3" , None ) .await.location.as_deref ( ) , Some ( " /andy/demo/proposal/3" ) ) ;
270
271
let r = post ( app, " /andy/demo/issue/2/comment" , Some ( & andy) , & format! ( " _csrf={csrf_a} &body=see+%233" ) ) .await;
272
assert! ( r.body.contains ( " href=\" /andy/demo/proposal/3\" " ) , " {}" , r.body) ;
78001759 Andy McCall 1 month ago ↶
273
274
client.bookmark_switch ( " main" , false ) .unwrap ( ) ;
275
write ( work.path ( ) , " c.txt" , b " c\n " ) ;
276
client.snapshot ( " t" ) .unwrap ( ) ;
277
let r = client.commit ( " Tidy up (refs #1, see #3)" , & [ ] ) .unwrap ( ) ;
278
assert! ( client.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
279
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
280
let short = & r.to_hex ( ) [ .. 12 ] ;
281
let one = get ( app, " /andy/demo/issue/1" , None ) .await.body;
282
assert! (
283
one.contains ( " referenced by change" ) & & one.contains ( & format! ( " href=\" /andy/demo/change/{short} \" " ) ) & & one.contains ( " on main" ) ,
284
" {one}"
285
) ;
286
let three = get ( app, " /andy/demo/proposal/3" , Some ( & andy) ) .await.body;
287
assert! ( three.contains ( " referenced by change" ) & & three.contains ( short) , " proposals too" ) ;
288
assert! ( ! get ( app, " /andy/demo/issue/2" , None ) .await.body.contains ( " referenced by change" ) , " a closing change records only that" ) ;
289
4f840409 Andy McCall 12 days ago ↶
290
291
292
let issue1 = db:: issues:: get( & app.db, repo_id, 1 ) .await.unwrap ( ) .unwrap ( ) ;
293
let day = 1_792_540_800_000 i64 ;
294
for ( i, at) in [ day + 1_000 , day + 2_000 , day + 3_000 , day - 86_400_000 ] .into_iter ( ) .enumerate ( ) {
295
sqlx:: query( " INSERT INTO issue_events (issue_id, at_ms, actor, event, detail) VALUES ($1, $2, 'andy', 'referenced by change', $3)" )
296
.bind ( issue1.id)
297
.bind ( at)
298
.bind ( format! ( " {:012x} on main" , 0xabc000 + i) )
299
.execute ( & app.db)
300
.await
301
.unwrap ( ) ;
302
}
303
let one = get ( app, " /andy/demo/issue/1" , None ) .await.body;
304
assert! ( one.contains ( " referenced by 3 changes on 21 Oct 2026" ) , " {one}" ) ;
305
assert! ( one.contains ( " refs-day\" open><summary>referenced by 3 changes" ) , " the newest box is open: {one}" ) ;
306
assert! ( ! one.contains ( " changes on 20 Oct 2026" ) , " a lone reference is a line" ) ;
307
assert! ( one.contains ( & format! ( " href=\" /andy/demo/change/{short} \" " ) ) , " today's own stays" ) ;
308
eb0c44a9 Andy McCall 1 month ago ↶
309
310
let part = get ( app, " /notifications?reason=participating" , Some ( & bob) ) .await.body;
311
assert! ( part.contains ( " andy/demo#1" ) & & ! part.contains ( " andy/demo#3: Feat" ) , " {part}" ) ;
312
let watch = get ( app, " /notifications?reason=watching" , Some ( & bob) ) .await.body;
313
assert! ( watch.contains ( " andy/demo#3: Feat" ) & & ! watch.contains ( " Crash on start" ) , " {watch}" ) ;
314
let props = get ( app, " /notifications?kind=proposal" , Some ( & bob) ) .await.body;
315
assert! ( props.contains ( " andy/demo#3: Feat" ) & & ! props.contains ( " andy/demo#1" ) , " kind filter" ) ;
316
assert! ( get ( app, " /notifications?repo=andy/demo" , Some ( & bob) ) .await.body.contains ( " andy/demo#3: Feat" ) ) ;
317
assert! ( get ( app, " /notifications?repo=nobody/none" , Some ( & bob) ) .await.body.contains ( " Nothing matches these filters" ) ) ;
318
319
let inbox = get ( app, " /notifications" , Some ( & bob) ) .await.body;
320
assert_eq! ( inbox.matches ( " data-row-thread=\" /andy/demo/issue/1\" " ) .count ( ) , 1 , " {inbox}" ) ;
321
assert! ( inbox.contains ( " updates" ) , " the count of a busy thread" ) ;
322
323
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
324
let newest: i64 = sqlx:: query_scalar(
3b1bc27f Andy McCall 20 days ago
325
" 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 ↶
326
)
327
.bind ( bob_id)
328
.fetch_one ( & app.db)
329
.await
330
.unwrap ( ) ;
331
let before = db:: notify:: unread( & app.db, bob_id) .await.unwrap ( ) ;
332
let r = get ( app, & format! ( " /notifications/open/{newest} " ) , Some ( & bob) ) .await;
333
assert_eq! ( r.location.as_deref ( ) , Some ( " /andy/demo/proposal/3" ) ) ;
334
assert! ( db:: notify:: unread( & app.db, bob_id) .await.unwrap ( ) < before) ;
335
assert_eq! ( get ( app, & format! ( " /notifications/open/{newest} " ) , Some ( & andy) ) .await.status, StatusCode:: NOT_FOUND , " only your own" ) ;
336
337
let csrf_b = csrf_of ( & get ( app, " /notifications" , Some ( & bob) ) .await.body) ;
338
let r = post (
339
app,
340
" /notifications/thread" ,
341
Some ( & bob) ,
342
& format! ( " _csrf={csrf_b} &thread=%2Fandy%2Fdemo%2Fissue%2F1&action=done&back=%2Fnotifications" ) ,
343
)
344
.await;
345
assert_eq! ( r.location.as_deref ( ) , Some ( " /notifications" ) ) ;
346
assert! ( ! get ( app, " /notifications" , Some ( & bob) ) .await.body.contains ( " Crash on start" ) ) ;
347
assert! ( get ( app, " /notifications?view=done" , Some ( & bob) ) .await.body.contains ( " Crash on start" ) ) ;
348
post ( app, " /andy/demo/issue/1/comment" , Some ( & andy) , & format! ( " _csrf={csrf_a} &body=one+more+thing" ) ) .await;
349
assert! ( get ( app, " /notifications" , Some ( & bob) ) .await.body.contains ( " Crash on start" ) , " back with the new comment" ) ;
350
let r = post (
351
app,
352
" /notifications/thread" ,
353
Some ( & bob) ,
354
& format! ( " _csrf={csrf_b} &thread=%2Fandy%2Fdemo%2Fissue%2F1&action=read&back=https%3A%2F%2Fevil.example" ) ,
355
)
356
.await;
357
assert_eq! ( r.location.as_deref ( ) , Some ( " /notifications" ) , " only back to the inbox" ) ;
358
18e381ea Andy McCall 1 month ago ↶
359
360
assert! ( get ( app, " /notifications" , Some ( & bob) ) .await.body.contains ( " andy/demo#3: Feat" ) ) ;
361
let csrf_b = csrf_of ( & get ( app, " /notifications" , Some ( & bob) ) .await.body) ;
362
let r = post ( app, " /notifications/read" , Some ( & bob) , & format! ( " _csrf={csrf_b} " ) ) .await;
363
assert! ( r.body.contains ( " 0 unread" ) , " {}" , r.body) ;
364
365
let r = post ( app, " /settings/notifications" , Some ( & bob) , & format! ( " _csrf={csrf_b} &email=1" ) ) .await;
366
assert! ( r.body.contains ( " Email notifications are on" ) ) ;
367
let key: String = sqlx:: query_scalar( " SELECT unsubscribe_key FROM accounts WHERE username = 'bob'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
368
assert_eq! ( get ( app, & format! ( " /settings/notifications?unsubscribe={key} " ) , None ) .await.status, StatusCode:: OK ) ;
369
let on: bool = sqlx:: query_scalar( " SELECT email_notifications FROM accounts WHERE username = 'bob'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
370
assert! ( ! on) ;
371
}
25ab8c73 Andy McCall 1 month ago ↶
372
373
# [ tokio ::test ( flavor = " multi_thread" ) ]
374
async fn dashboard_shows_what_needs_the_viewer ( ) {
375
let h = hub ( ) .await;
376
let app = & h.app;
377
let andy = register ( app, " andy" ) .await;
378
let bob = register ( app, " bob" ) .await;
379
for ( name, vis) in [ ( " demo" , hub_api:: Visibility:: Public) , ( " secret" , hub_api:: Visibility:: Private) ] {
380
app.repos.create ( & RepoName:: parse( & format! ( " andy/{name} " ) ) .unwrap ( ) , None ) .unwrap ( ) ;
381
db:: repos:: create( & app.db, " andy" , name, " " , vis) .await.unwrap ( ) ;
382
}
383
let demo = db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
384
let secret = db:: repos:: get( & app.db, " andy" , " secret" ) .await.unwrap ( ) .unwrap ( ) ;
385
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
386
for ( repo, title) in [ ( demo.id, " Faster startup" ) , ( secret.id, " Hidden work" ) ] {
387
let p = db:: proposals:: create(
388
& app.db,
389
db:: proposals:: NewProposal {
390
repo_id: repo,
391
title,
392
body: " " ,
393
author_id: Some ( bob_id) ,
394
author: " bob" ,
395
draft: false ,
396
target: " main" ,
397
source_kind: " bookmark" ,
398
source_repo_id: Some ( repo) ,
399
source_bookmark: " fast" ,
400
source_changes: " " ,
401
} ,
402
)
403
.await
404
.unwrap ( ) ;
405
db:: proposals:: add_event( & app.db, p.id, " bob" , " opened" , " " ) .await.unwrap ( ) ;
406
}
407
408
409
let anon = get ( app, " /" , None ) .await;
410
assert! ( anon.body.contains ( " andy /" ) & & ! anon.body.contains ( " Dashboard <small>" ) , " {}" , anon.body) ;
411
412
413
let page = get ( app, " /" , Some ( & andy) ) .await;
414
assert_eq! ( page.status, StatusCode:: OK ) ;
f8cf54e9 Andy McCall 23 days ago ↶
415
assert! ( page.body.contains ( " Waiting for your review</h2><span class=\" count-chip\" >2</span>" ) , " {}" , page.body) ;
25ab8c73 Andy McCall 1 month ago ↶
416
assert! ( page.body.contains ( " /andy/demo/proposal/1" ) & & page.body.contains ( " /andy/secret/proposal/1" ) ) ;
417
assert! ( page.body.contains ( " Your repositories" ) ) ;
418
assert! ( page.body.contains ( " opened proposal" ) , " activity feed: {}" , page.body) ;
419
17ca6cdd Andy McCall 23 days ago ↶
420
421
422
assert! ( page.body.contains ( r # "<canvas id="backdrop" aria-hidden="true">"# ) ) ;
423
let start = page.body.find ( r # "id="backdrop-data">"# ) .unwrap ( ) + r # "id="backdrop-data">"# .len ( ) ;
424
let data: serde_json:: Value = serde_json:: from_str( & page.body[ start.. start + page.body[ start.. ] .find ( " </script>" ) .unwrap ( ) ] ) .unwrap ( ) ;
425
let repos: Vec< & str > = data[ " repos" ] .as_array ( ) .unwrap ( ) .iter ( ) .filter_map ( | r | r.as_str ( ) ) .collect ( ) ;
426
assert! ( repos.contains ( & " andy/demo" ) & & repos.contains ( & " andy/secret" ) , " {data}" ) ;
427
let events = data[ " events" ] .as_array ( ) .unwrap ( ) ;
428
assert! ( events.iter ( ) .any ( | e | e[ 1 ] = = 1 ) , " the proposals opened are marks: {data}" ) ;
429
assert! ( events.iter ( ) .all ( | e | e[ 0 ] .as_u64 ( ) .unwrap ( ) < repos.len ( ) as u64 ) ) ;
33bd0bd4 Andy McCall 23 days ago ↶
430
431
let res = oxygenhub:: router( app.clone ( ) )
432
.oneshot ( Request:: builder( ) .uri ( " /" ) .header ( header:: COOKIE , format! ( " oxyhub={andy} " ) ) .body ( Body:: empty( ) ) .unwrap ( ) )
433
.await
434
.unwrap ( ) ;
435
assert_eq! ( res.headers ( ) [ header:: CACHE_CONTROL ] , " private, no-store" ) ;
6246d1e5 Andy McCall 23 days ago ↶
436
437
assert! ( get ( app, " /login" , None )
438
.await
439
.body
440
.contains ( r # "<script type="application/json" id="backdrop-data">{"scene":"mark"}</script>"# ) ) ;
441
let lost = get ( app, " /no/such/page/here" , None ) .await;
442
assert_eq! ( lost.status, StatusCode:: NOT_FOUND ) ;
443
assert! ( lost.body.contains ( r # "{"scene":"adrift"}"# ) ) ;
0a3b244b Andy McCall 23 days ago ↶
444
445
let sky_of = | body : & str | {
446
let start = body.find ( r # "id="backdrop-data">"# ) .unwrap ( ) + r # "id="backdrop-data">"# .len ( ) ;
447
body[ start.. start + body[ start.. ] .find ( " </script>" ) .unwrap ( ) ] .to_string ( )
448
} ;
449
let hub_sky = sky_of ( & anon.body) ;
450
assert! ( hub_sky.contains ( " andy/demo" ) & & ! hub_sky.contains ( " andy/secret" ) , " {hub_sky}" ) ;
451
452
453
let explore = get ( app, " /explore" , Some ( & andy) ) .await;
454
assert! ( explore.body.contains ( " andy/secret" ) , " the list shows him his private repository" ) ;
455
let hub_sky = sky_of ( & explore.body) ;
456
assert! ( hub_sky.contains ( " andy/demo" ) & & ! hub_sky.contains ( " andy/secret" ) , " {hub_sky}" ) ;
457
assert! ( ! get ( app, " /andy" , None ) .await.body.contains ( " backdrop-data" ) ) ;
17ca6cdd Andy McCall 23 days ago ↶
458
459
let res = oxygenhub:: router( app.clone ( ) )
460
.oneshot ( Request:: builder( ) .uri ( " /static/backdrop/backdrop_bg.wasm" ) .body ( Body:: empty( ) ) .unwrap ( ) )
461
.await
462
.unwrap ( ) ;
463
assert_eq! ( res.headers ( ) [ header:: CONTENT_TYPE ] , " application/wasm" ) ;
464
assert! ( res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .starts_with ( b " \0 asm" ) ) ;
da8f7826 Andy McCall 23 days ago ↶
465
466
assert! ( sky_of ( & page.body) .contains ( r # ""live":"mine""# ) & & hub_sky.contains ( r # ""live":"hub""# ) ) ;
467
let since = db:: now_ms( ) - 3_600_000 ;
468
let arrivals = | res : Res| -> Vec< String> {
469
assert_eq! ( res.status, StatusCode:: OK , " {}" , res.body) ;
470
let v: serde_json:: Value = serde_json:: from_str( & res.body) .unwrap ( ) ;
471
v[ " events" ] .as_array ( ) .unwrap ( ) .iter ( ) .map ( | e | e[ 0 ] .as_str ( ) .unwrap ( ) .to_string ( ) ) .collect ( )
472
} ;
473
let mine = arrivals ( get ( app, & format! ( " /sky/arrivals?scene=mine&since={since} " ) , Some ( & andy) ) .await) ;
474
assert! ( mine.iter ( ) .any ( | r | r = = " andy/demo" ) & & mine.iter ( ) .any ( | r | r = = " andy/secret" ) , " {mine:?}" ) ;
475
let hub = arrivals ( get ( app, & format! ( " /sky/arrivals?scene=hub&since={since} " ) , Some ( & andy) ) .await) ;
476
assert! ( hub.iter ( ) .any ( | r | r = = " andy/demo" ) & & ! hub.iter ( ) .any ( | r | r = = " andy/secret" ) , " the hub's sky is public only: {hub:?}" ) ;
477
let anon_hub = arrivals ( get ( app, & format! ( " /sky/arrivals?scene=hub&since={since} " ) , None ) .await) ;
478
assert! ( ! anon_hub.iter ( ) .any ( | r | r = = " andy/secret" ) , " {anon_hub:?}" ) ;
479
assert! ( arrivals ( get ( app, & format! ( " /sky/arrivals?scene=mine&since={since} " ) , None ) .await) .is_empty ( ) , " signed out has no `mine`" ) ;
480
assert! (
481
arrivals ( get ( app, & format! ( " /sky/arrivals?scene=mine&since={since} " ) , Some ( & bob) ) .await) .is_empty ( ) ,
482
" Bob's sky has none of Andy's"
483
) ;
484
assert! (
485
arrivals ( get ( app, & format! ( " /sky/arrivals?scene=mine&since={} " , db:: now_ms( ) ) , Some ( & andy) ) .await) .is_empty ( ) ,
486
" nothing since now"
487
) ;
17ca6cdd Andy McCall 23 days ago ↶
488
25ab8c73 Andy McCall 1 month ago ↶
489
490
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
491
let p = db:: proposals:: get( & app.db, demo.id, 1 ) .await.unwrap ( ) .unwrap ( ) ;
492
db:: proposals:: add_review( & app.db, p.id, andy_id, " andy" , " approve" , " " ) .await.unwrap ( ) ;
f8cf54e9 Andy McCall 23 days ago ↶
493
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 ↶
494
495
496
let page = get ( app, " /" , Some ( & bob) ) .await;
33bd0bd4 Andy McCall 23 days ago ↶
497
498
499
let start = page.body.find ( r # "id="backdrop-data">"# ) .unwrap ( ) + r # "id="backdrop-data">"# .len ( ) ;
500
let sky = & page.body[ start.. start + page.body[ start.. ] .find ( " </script>" ) .unwrap ( ) ] ;
501
assert! ( ! sky.contains ( " andy/secret" ) & & ! sky.contains ( " andy/demo" ) , " {sky}" ) ;
f8cf54e9 Andy McCall 23 days ago ↶
502
assert! ( page.body.contains ( " Your proposals</h2><span class=\" count-chip\" >1</span>" ) , " {}" , page.body) ;
25ab8c73 Andy McCall 1 month ago ↶
503
assert! ( ! page.body.contains ( " Hidden work" ) ) ;
f8cf54e9 Andy McCall 23 days ago ↶
504
assert! ( page.body.contains ( " Waiting for your review</h2><span class=\" count-chip\" >0</span>" ) ) ;
25ab8c73 Andy McCall 1 month ago ↶
505
assert! ( page.body.contains ( " Faster startup" ) ) ;
506
let feed = & page.body[ page.body.find ( " Recent activity" ) .unwrap ( ) .. ] ;
507
assert! ( ! feed.contains ( " andy/secret" ) , " private activity leaked: {feed}" ) ;
508
509
510
assert! ( get ( app, " /explore" , Some ( & bob) ) .await.body.contains ( " andy /" ) ) ;
511
}
4edcf240 Andy McCall 1 month ago ↶
512
513
# [ tokio ::test ( flavor = " multi_thread" ) ]
514
async fn empty_repository_shows_a_quickstart ( ) {
515
let h = hub ( ) .await;
516
let app = & h.app;
517
let andy = register ( app, " andy" ) .await;
518
let bob = register ( app, " bob" ) .await;
519
app.repos.create ( & RepoName:: parse( " andy/fresh" ) .unwrap ( ) , None ) .unwrap ( ) ;
520
db:: repos:: create( & app.db, " andy" , " fresh" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
521
522
let page = get ( app, " /andy/fresh" , Some ( & andy) ) .await;
523
assert! ( page.body.contains ( " This repository is empty" ) , " {}" , page.body) ;
524
let clone_url = format! ( " {} /andy/fresh" , h.base) ;
525
assert! ( page.body.contains ( & format! ( " oxy remote add hub {clone_url} " ) ) , " {}" , page.body) ;
526
assert! ( page.body.contains ( " oxy config remote.hub.token YOUR-TOKEN" ) ) ;
527
assert! ( page.body.contains ( " oxy push hub" ) ) ;
528
assert! (
529
page.body.contains ( " mkdir fresh && cd fresh" ) | | page.body.contains ( " mkdir fresh && cd fresh" ) ,
530
" {}" ,
531
page.body
532
) ;
533
assert! ( page.body.contains ( " oxy git import --branch main" ) ) ;
534
assert! ( ! page.body.contains ( " oxyhub-ca.pem" ) , " no certificate step without a self-signed certificate" ) ;
535
536
537
let page = get ( app, " /andy/fresh" , Some ( & bob) ) .await;
538
assert! ( page.body.contains ( " Nothing has been pushed yet" ) & & ! page.body.contains ( " oxy push hub" ) , " {}" , page.body) ;
539
540
541
let work = tempfile:: tempdir( ) .unwrap ( ) ;
542
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
543
client.config.set ( " remote.hub.url" , & clone_url) .unwrap ( ) ;
544
client.config.set ( " remote.hub.token" , " site" ) .unwrap ( ) ;
545
client.save_config ( ) .unwrap ( ) ;
546
write ( work.path ( ) , " README.md" , b " # fresh\n " ) ;
547
client.commit ( " First change" , & [ ] ) .unwrap ( ) ;
548
assert! ( client.push ( " hub" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
549
let page = get ( app, " /andy/fresh" , Some ( & andy) ) .await;
550
assert! ( ! page.body.contains ( " This repository is empty" ) & & page.body.contains ( " README.md" ) , " {}" , page.body) ;
551
}
c40c2d3e Andy McCall 1 month ago ↶
552
553
# [ tokio ::test ( flavor = " multi_thread" ) ]
554
async fn theme_is_chosen_in_settings_and_remembered_in_a_cookie ( ) {
555
let h = hub ( ) .await;
556
let app = & h.app;
557
let andy = register ( app, " andy" ) .await;
558
let page = get ( app, " /settings" , Some ( & andy) ) .await;
559
assert! ( page.body.contains ( " Appearance" ) ) ;
d98a93c1 Andy McCall 1 month ago ↶
560
assert! ( page.body.contains ( r # "value="light" checked"# ) , " light by default: {}" , page.body) ;
c40c2d3e Andy McCall 1 month ago ↶
561
assert! ( page.body.contains ( " data-theme-toggle" ) , " the header has the switch" ) ;
562
let csrf = csrf_of ( & page.body) ;
d98a93c1 Andy McCall 1 month ago ↶
563
let r = post ( app, " /settings/appearance" , Some ( & andy) , & format! ( " _csrf={csrf} &theme=dark" ) ) .await;
c40c2d3e Andy McCall 1 month ago ↶
564
assert_eq! ( r.status, StatusCode:: SEE_OTHER ) ;
d98a93c1 Andy McCall 1 month ago ↶
565
assert_eq! ( r.cookie.as_deref ( ) , Some ( " dark" ) ) ;
c40c2d3e Andy McCall 1 month ago ↶
566
assert_eq! ( r.location.as_deref ( ) , Some ( " /settings" ) ) ;
d98a93c1 Andy McCall 1 month ago ↶
567
let page = send_with_theme ( app, & andy, " dark" ) .await;
568
assert! ( page.contains ( r # "value="dark" checked"# ) , " the form shows the cookie's theme" ) ;
c40c2d3e Andy McCall 1 month ago ↶
569
assert_eq! ( post ( app, " /settings/appearance" , Some ( & andy) , & format! ( " _csrf={csrf} &theme=purple" ) ) .await.status, StatusCode:: BAD_REQUEST ) ;
17ca6cdd Andy McCall 23 days ago ↶
570
571
572
573
assert! ( get ( app, " /settings" , Some ( & andy) ) .await.body.contains ( r # "name="backdrop" value="moving" checked"# ) ) ;
574
let res = oxygenhub:: router( app.clone ( ) )
575
.oneshot (
576
Request:: builder( )
577
.method ( " POST" )
578
.uri ( " /settings/appearance" )
579
.header ( header:: COOKIE , format! ( " oxyhub={andy} " ) )
580
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
581
.body ( Body:: from( format! ( " _csrf={csrf} &theme=light&backdrop=still" ) ) )
582
.unwrap ( ) ,
583
)
584
.await
585
.unwrap ( ) ;
586
let cookies: Vec< & str > = res.headers ( ) .get_all ( header:: SET_COOKIE ) .iter ( ) .filter_map ( | v | v.to_str ( ) .ok ( ) ) .collect ( ) ;
587
assert! (
588
cookies.iter ( ) .any ( | c | c.starts_with ( " theme=light;" ) ) & & cookies.iter ( ) .any ( | c | c.starts_with ( " backdrop=still;" ) ) ,
589
" {cookies:?}"
590
) ;
591
assert_eq! (
592
post ( app, " /settings/appearance" , Some ( & andy) , & format! ( " _csrf={csrf} &theme=light&backdrop=wild" ) ) .await.status,
593
StatusCode:: BAD_REQUEST
594
) ;
c40c2d3e Andy McCall 1 month ago ↶
595
assert_eq! ( post ( app, " /settings/appearance" , Some ( & andy) , " _csrf=nope&theme=light" ) .await.status, StatusCode:: BAD_REQUEST ) ;
596
}
597
598
async fn send_with_theme ( app : & Shared, session : & str , theme : & str ) -> String {
599
let req =
600
Request:: builder( ) .uri ( " /settings" ) .header ( header:: COOKIE , format! ( " oxyhub={session} ; theme={theme} " ) ) .body ( Body:: empty( ) ) .unwrap ( ) ;
601
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
602
String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( )
603
}
10174045 Andy McCall 1 month ago ↶
604
605
# [ tokio ::test ( flavor = " multi_thread" ) ]
606
async fn profile_and_organisation_pages ( ) {
607
let h = hub ( ) .await;
608
let app = & h.app;
609
let andy = register ( app, " andy" ) .await;
610
let bob = register ( app, " bob" ) .await;
611
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
612
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
613
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
614
db:: repos:: create( & app.db, " andy" , " demo" , " A demo" , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
615
app.repos.create ( & RepoName:: parse( " andy/hidden" ) .unwrap ( ) , None ) .unwrap ( ) ;
616
db:: repos:: create( & app.db, " andy" , " hidden" , " " , hub_api:: Visibility:: Private) .await.unwrap ( ) ;
617
618
619
let csrf = csrf_of ( & get ( app, " /settings" , Some ( & andy) ) .await.body) ;
620
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;
621
assert! ( r.body.contains ( " must be an http" ) , " {}" , r.body) ;
622
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;
623
assert! ( r.body.contains ( " Profile saved" ) , " {}" , r.body) ;
624
625
let org = db:: accounts:: create_org( & app.db, " retro" , " Retro Club" , andy_id) .await.unwrap ( ) ;
626
db:: accounts:: set_org_member( & app.db, org.id, bob_id, " member" ) .await.unwrap ( ) ;
627
let team = db:: accounts:: create_team( & app.db, org.id, " core" ) .await.unwrap ( ) ;
628
db:: accounts:: add_team_member( & app.db, team.id, bob_id) .await.unwrap ( ) ;
629
630
let page = get ( app, " /andy" , Some ( & bob) ) .await;
631
assert_eq! ( page.status, StatusCode:: OK ) ;
632
for want in [ " Andy McCall" , " Earth" , r # "href="https://example.org""# , " /avatar/andy" , " Signing keys" , " /retro" , " demo" ] {
633
assert! ( page.body.contains ( want) , " profile lacks {want}: {}" , page.body) ;
634
}
635
assert! ( ! page.body.contains ( " <b>oxy</b>" ) , " the bio is escaped" ) ;
636
assert! ( ! page.body.contains ( " [email protected] " ) , " the email stays private" ) ;
637
assert! ( ! page.body.contains ( " /andy/hidden" ) , " bob cannot see the private repository" ) ;
638
assert! ( ! page.body.contains ( " edit profile" ) , " only the owner gets the edit link" ) ;
639
assert! ( get ( app, " /andy" , Some ( & andy) ) .await.body.contains ( " /andy/hidden" ) , " andy sees his own private repository" ) ;
640
641
let page = get ( app, " /retro" , Some ( & bob) ) .await;
642
assert_eq! ( page.status, StatusCode:: OK ) ;
61ecbd29 Andy McCall 23 days ago ↶
643
for want in [ " Retro Club" , " organisation" , r # "Members <span class="count-chip">2</span>"# , " core" , " 1 member" ] {
10174045 Andy McCall 1 month ago ↶
644
assert! ( page.body.contains ( want) , " org page lacks {want}: {}" , page.body) ;
645
}
646
assert! ( ! page.body.contains ( " /orgs/retro/settings" ) , " bob is not an owner" ) ;
647
assert! ( get ( app, " /retro" , Some ( & andy) ) .await.body.contains ( " /orgs/retro/settings" ) ) ;
648
649
let svg = get ( app, " /avatar/andy" , None ) .await;
650
assert_eq! ( svg.status, StatusCode:: OK ) ;
651
assert! ( svg.body.starts_with ( " <svg" ) ) ;
652
assert_eq! ( svg.body, get ( app, " /avatar/ANDY" , None ) .await.body) ;
653
assert_eq! ( get ( app, " /nobody-here" , None ) .await.status, StatusCode:: NOT_FOUND ) ;
654
}
9df855f2 Andy McCall 1 month ago ↶
655
656
# [ tokio ::test ( flavor = " multi_thread" ) ]
657
async fn diffs_mark_words_hide_whitespace_and_collapse ( ) {
658
let h = hub ( ) .await;
659
let app = & h.app;
660
let andy = register ( app, " andy" ) .await;
661
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
662
db:: repos:: create( & app.db, " andy" , " demo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
663
let work = tempfile:: tempdir( ) .unwrap ( ) ;
664
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
665
client.config.set ( " remote.hub.url" , & format! ( " {} /andy/demo" , h.base) ) .unwrap ( ) ;
666
client.config.set ( " remote.hub.token" , " site" ) .unwrap ( ) ;
667
client.save_config ( ) .unwrap ( ) ;
668
write ( work.path ( ) , " main.rs" , b " fn main() {\n setup();\n let answer = 42;\n }\n " ) ;
669
client.commit ( " first" , & [ ] ) .unwrap ( ) ;
670
write ( work.path ( ) , " main.rs" , b " fn main() {\n setup();\n let answer = 43;\n }\n " ) ;
671
let second = client.commit ( " reindent and change the answer" , & [ ] ) .unwrap ( ) ;
672
assert! ( client.push ( " hub" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
673
674
let url = format! ( " /andy/demo/change/{} " , second.to_hex ( ) ) ;
675
let page = get ( app, & url, Some ( & andy) ) .await;
676
assert_eq! ( page.status, StatusCode:: OK ) ;
677
assert! ( page.body.contains ( r # "<details class="file-diff""# ) , " files collapse: {}" , page.body) ;
678
assert! ( page.body.contains ( r # "<span class="wx">43</span>"# ) , " the changed word is marked: {}" , page.body) ;
679
assert! ( page.body.contains ( " hide whitespace changes" ) ) ;
680
assert! ( page.body.contains ( " data-diff-mode=\" split\" " ) ) ;
681
let adds = | b : & str | b.matches ( r # "<tr class="add">"# ) .count ( ) ;
682
assert_eq! ( adds ( & page.body) , 2 , " re-indent and answer both changed" ) ;
683
684
let quiet = get ( app, & format! ( " {url} ?w=1" ) , Some ( & andy) ) .await;
685
assert_eq! ( adds ( & quiet.body) , 1 , " only the answer changed ignoring whitespace: {}" , quiet.body) ;
686
assert! ( quiet.body.contains ( " show whitespace changes" ) ) ;
687
688
assert! ( quiet.body.contains ( " ?w=0" ) , " the toggle leads back" ) ;
689
}
07b1a5bb Andy McCall 1 month ago ↶
690
691
# [ tokio ::test ( flavor = " multi_thread" ) ]
692
async fn fork_and_propose_upstream ( ) {
693
let h = hub ( ) .await;
694
let app = & h.app;
695
let andy = register ( app, " andy" ) .await;
696
let bob = register ( app, " bob" ) .await;
697
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
698
db:: repos:: create( & app.db, " andy" , " demo" , " The demo" , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
699
let work = tempfile:: tempdir( ) .unwrap ( ) ;
700
let mut client = Repo:: init( work.path ( ) ) .unwrap ( ) ;
701
client.config.set ( " remote.hub.url" , & format! ( " {} /andy/demo" , h.base) ) .unwrap ( ) ;
702
client.config.set ( " remote.hub.token" , " site" ) .unwrap ( ) ;
703
client.save_config ( ) .unwrap ( ) ;
704
write ( work.path ( ) , " README.md" , b " # demo\n " ) ;
705
client.commit ( " first" , & [ ] ) .unwrap ( ) ;
706
assert! ( client.push ( " hub" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
707
708
709
let page = get ( app, " /andy/demo" , Some ( & bob) ) .await;
710
assert! ( page.body.contains ( " /andy/demo/fork" ) , " a fork button: {}" , page.body) ;
711
let csrf = csrf_of ( & get ( app, " /andy/demo/fork" , Some ( & bob) ) .await.body) ;
712
let r = post ( app, " /andy/demo/fork" , Some ( & bob) , & format! ( " _csrf={csrf} &owner=andy&name=other" ) ) .await;
713
assert! ( r.body.contains ( " cannot create repositories under that owner" ) , " {}" , r.body) ;
714
let r = post ( app, " /andy/demo/fork" , Some ( & bob) , & format! ( " _csrf={csrf} &owner=bob&name=demo" ) ) .await;
715
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
716
assert_eq! ( r.location.as_deref ( ) , Some ( " /bob/demo" ) ) ;
717
let forked = get ( app, " /bob/demo" , Some ( & bob) ) .await;
718
assert! ( forked.body.contains ( " README.md" ) & & forked.body.contains ( " forked from" ) , " {}" , forked.body) ;
719
assert! ( forked.body.contains ( " propose to andy/demo" ) ) ;
720
assert! ( get ( app, " /andy/demo" , None ) .await.body.contains ( " 1 fork" ) ) ;
721
assert! ( get ( app, " /andy/demo/forks" , None ) .await.body.contains ( " /bob/demo" ) ) ;
722
let fork_row = db:: repos:: get( & app.db, " bob" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
723
let up_row = db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
724
assert_eq! ( fork_row.forked_from, Some ( up_row.id) ) ;
725
assert_eq! ( fork_row.description, " The demo" ) ;
726
let fork_repo = app.repos.open ( & RepoName:: parse( " bob/demo" ) .unwrap ( ) ) .unwrap ( ) ;
727
let up_repo = app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) ;
728
assert_ne! ( fork_repo.id ( ) .unwrap ( ) , up_repo.id ( ) .unwrap ( ) , " a fork is a repository of its own" ) ;
729
assert_eq! ( fork_repo.bookmark_heads ( ) .unwrap ( ) , up_repo.bookmark_heads ( ) .unwrap ( ) ) ;
730
assert_eq! ( fork_repo.fsck ( ) .unwrap ( ) .errors ( ) , 0 ) ;
731
732
733
let bw = tempfile:: tempdir( ) .unwrap ( ) ;
734
let mut mine = Repo:: clone( & format! ( " {} /bob/demo" , h.base) , & bw.path ( ) .join ( " c" ) ) .unwrap ( ) ;
735
mine.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
736
mine.save_config ( ) .unwrap ( ) ;
737
mine.bookmark_create ( " feat" , None ) .unwrap ( ) ;
738
mine.bookmark_switch ( " feat" , false ) .unwrap ( ) ;
739
write ( & bw.path ( ) .join ( " c" ) , " feature.txt" , b " new\n " ) ;
740
mine.commit ( " Add a feature" , & [ ] ) .unwrap ( ) ;
741
assert! ( mine.push_bookmark ( " origin" , " feat" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
742
let csrf = csrf_of ( & get ( app, " /andy/demo/proposals/new?source=bob/demo:feat" , Some ( & bob) ) .await.body) ;
743
let r =
744
post ( app, " /andy/demo/proposals/new" , Some ( & bob) , & format! ( " _csrf={csrf} &title=From+my+fork&target=main&source=bob%2Fdemo%3Afeat" ) )
745
.await;
746
assert_eq! ( r.status, StatusCode:: SEE_OTHER , " {}" , r.body) ;
747
let p = get ( app, & r.location.unwrap ( ) , Some ( & andy) ) .await;
748
assert! ( p.body.contains ( " From my fork" ) & & p.body.contains ( " fork bob/demo" ) , " {}" , p.body) ;
749
let files = get ( app, " /andy/demo/proposal/1?tab=files" , Some ( & andy) ) .await;
750
assert! ( files.body.contains ( " feature.txt" ) , " {}" , files.body) ;
751
}
1553bfc9 Andy McCall 1 month ago ↶
752
753
# [ tokio ::test ( flavor = " multi_thread" ) ]
754
async fn stars_topics_and_explore ( ) {
755
let h = hub ( ) .await;
756
let app = & h.app;
757
let andy = register ( app, " andy" ) .await;
758
let bob = register ( app, " bob" ) .await;
759
for ( name, desc) in [ ( " alpha" , " An emulator" ) , ( " beta" , " A compiler" ) , ( " gamma" , " Retro games" ) ] {
760
app.repos.create ( & RepoName:: parse( & format! ( " andy/{name} " ) ) .unwrap ( ) , None ) .unwrap ( ) ;
761
db:: repos:: create( & app.db, " andy" , name, desc, hub_api:: Visibility:: Public) .await.unwrap ( ) ;
762
}
763
764
765
let csrf = csrf_of ( & get ( app, " /andy/gamma/settings" , Some ( & andy) ) .await.body) ;
766
let r = post (
767
app,
768
" /andy/gamma/settings" ,
769
Some ( & andy) ,
770
& format! ( " _csrf={csrf} &description=Retro+games&default_bookmark=main&visibility=public&topics=c%2B%2B" ) ,
771
)
772
.await;
c7067c7b Andy McCall 16 days ago
773
let r = settled ( app, r, Some ( & andy) , " /andy/gamma/settings/general" ) .await;
1553bfc9 Andy McCall 1 month ago ↶
774
assert! ( r.body.contains ( " is not a topic" ) , " {}" , r.body) ;
775
let r = post (
776
app,
777
" /andy/gamma/settings" ,
778
Some ( & andy) ,
779
& format! ( " _csrf={csrf} &description=Retro+games&default_bookmark=main&visibility=public&topics=Retro%2C+6502" ) ,
780
)
781
.await;
c7067c7b Andy McCall 16 days ago
782
let r = settled ( app, r, Some ( & andy) , " /andy/gamma/settings/general" ) .await;
1553bfc9 Andy McCall 1 month ago ↶
783
assert! ( r.body.contains ( " Saved" ) , " {}" , r.body) ;
784
assert_eq! ( db:: repos:: get( & app.db, " andy" , " gamma" ) .await.unwrap ( ) .unwrap ( ) .topics, " retro 6502" ) ;
785
let page = get ( app, " /andy/gamma" , None ) .await;
786
assert! ( page.body.contains ( r # "href="/explore?topic=retro""# ) , " {}" , page.body) ;
787
788
789
for ( who, repo) in [ ( & bob, " beta" ) , ( & bob, " gamma" ) , ( & andy, " gamma" ) , ( & andy, " alpha" ) ] {
790
let csrf = csrf_of ( & get ( app, & format! ( " /andy/{repo} " ) , Some ( who) ) .await.body) ;
791
assert_eq! ( post ( app, & format! ( " /andy/{repo} /star" ) , Some ( who) , & format! ( " _csrf={csrf} " ) ) .await.status, StatusCode:: SEE_OTHER ) ;
792
}
793
let csrf = csrf_of ( & get ( app, " /andy/alpha" , Some ( & andy) ) .await.body) ;
794
post ( app, " /andy/alpha/star" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
795
assert! ( get ( app, " /andy/gamma" , Some ( & bob) ) .await.body.contains ( " ★ UNSTAR" ) ) ;
7f283f8e Andy McCall 23 days ago ↶
796
assert! ( get ( app, " /andy/gamma" , None ) .await.body.contains ( r # "title="2 stars">"# ) ) ;
797
assert! ( get ( app, " /andy/alpha" , None ) .await.body.contains ( r # "title="0 stars">"# ) , " unstarred again" ) ;
1553bfc9 Andy McCall 1 month ago ↶
798
799
800
let order = | b : & str | {
801
[ " alpha" , " beta" , " gamma" ]
802
.into_iter ( )
803
.map ( | n | ( b.find ( & format! ( " /andy/{n} \" " ) ) .unwrap_or ( usize :: MAX ) , n) )
804
.collect:: < std:: collections:: BTreeMap< _ , _ > > ( )
805
.into_values ( )
806
.collect:: < Vec< _ > > ( )
807
} ;
808
assert_eq! ( order ( & get ( app, " /explore?sort=stars" , None ) .await.body) [ .. 2 ] , [ " gamma" , " beta" ] ) ;
809
assert_eq! ( order ( & get ( app, " /explore?sort=name" , None ) .await.body) , [ " alpha" , " beta" , " gamma" ] ) ;
810
let filtered = get ( app, " /explore?q=compiler" , None ) .await.body;
811
assert! ( filtered.contains ( " /andy/beta\" " ) & & ! filtered.contains ( " /andy/alpha\" " ) , " {filtered}" ) ;
812
let by_topic = get ( app, " /explore?topic=6502" , None ) .await.body;
813
assert! ( by_topic.contains ( " /andy/gamma\" " ) & & ! by_topic.contains ( " /andy/beta\" " ) , " {by_topic}" ) ;
814
}
1ac84c62 Andy McCall 1 month ago ↶
815
816
# [ tokio ::test ( flavor = " multi_thread" ) ]
817
async fn lists_filter_sort_page_and_count ( ) {
818
let h = hub ( ) .await;
819
let app = & h.app;
820
let andy = register ( app, " andy" ) .await;
821
let _bob = register ( app, " bob" ) .await;
822
app.repos.create ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) , None ) .unwrap ( ) ;
823
let repo = db:: repos:: create( & app.db, " andy" , " demo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
824
for n in 0 .. 30 {
825
let author = if n % 3 = = 0 { " bob" } else { " andy" } ;
826
db:: issues:: create(
827
& app.db,
828
db:: issues:: NewIssue { repo_id: repo.id, title: & format! ( " Issue number {n} " ) , body: " " , author_id: None , author } ,
829
)
830
.await
831
.unwrap ( ) ;
832
}
833
let ui = db:: issues:: upsert_label( & app.db, repo.id, " ui" , " #00f0ff" , " " ) .await.unwrap ( ) ;
834
for ( title, author, target) in [ ( " Faster menus" , " bob" , " main" ) , ( " New theme" , " andy" , " main" ) , ( " Release prep" , " andy" , " stable" ) ] {
835
let p = db:: proposals:: create(
836
& app.db,
837
db:: proposals:: NewProposal {
838
repo_id: repo.id,
839
title,
840
body: " " ,
841
author_id: None ,
842
author,
843
draft: false ,
844
target,
845
source_kind: " changes" ,
846
source_repo_id: None ,
847
source_bookmark: " " ,
848
source_changes: " " ,
849
} ,
850
)
851
.await
852
.unwrap ( ) ;
853
if title = = " New theme" {
854
db:: issues:: set_proposal_label( & app.db, p.id, ui.id, true ) .await.unwrap ( ) ;
855
}
856
}
857
858
859
let page = get ( app, " /andy/demo/issues" , Some ( & andy) ) .await;
860
assert! ( page.body.contains ( r # "Issues <span class="count-chip">30</span>"# ) , " {}" , page.body) ;
861
assert! ( page.body.contains ( r # "Proposals <span class="count-chip">3</span>"# ) ) ;
862
863
864
let rows = | b : & str | b.matches ( " /andy/demo/issue/" ) .count ( ) ;
865
assert_eq! ( rows ( & page.body) , 25 ) ;
866
assert! ( page.body.contains ( " page 1 of 2" ) & & page.body.contains ( " page=2" ) , " a pager" ) ;
867
let second = get ( app, " /andy/demo/issues?page=2" , Some ( & andy) ) .await;
868
assert_eq! ( rows ( & second.body) , 5 ) ;
869
assert! ( second.body.contains ( " Issue number 0<" ) , " the oldest are last" ) ;
870
let oldest = get ( app, " /andy/demo/issues?sort=oldest" , Some ( & andy) ) .await;
871
assert! ( oldest.body.find ( " Issue number 0<" ) .unwrap ( ) < oldest.body.find ( " Issue number 1<" ) .unwrap ( ) ) ;
872
let bobs = get ( app, " /andy/demo/issues?q=author:bob" , Some ( & andy) ) .await;
873
assert_eq! ( rows ( & bobs.body) , 10 , " {}" , bobs.body) ;
874
875
876
let by_bob = get ( app, " /andy/demo/proposals?q=author:bob" , Some ( & andy) ) .await.body;
877
assert! ( by_bob.contains ( " Faster menus" ) & & ! by_bob.contains ( " New theme" ) ) ;
878
let ui_only = get ( app, " /andy/demo/proposals?q=label:ui" , Some ( & andy) ) .await.body;
879
assert! ( ui_only.contains ( " New theme" ) & & ! ui_only.contains ( " Faster menus" ) & & ui_only.contains ( r # "class="label""# ) ) ;
880
let stable = get ( app, " /andy/demo/proposals?q=target:stable" , Some ( & andy) ) .await.body;
881
assert! ( stable.contains ( " Release prep" ) & & ! stable.contains ( " New theme" ) ) ;
882
}
e1ec9fbf Andy McCall 1 month ago ↶
883
884
# [ tokio ::test ( flavor = " multi_thread" ) ]
885
async fn project_boards ( ) {
886
let h = hub ( ) .await;
887
let app = & h.app;
888
let andy = register ( app, " andy" ) .await;
889
let bob = register ( app, " bob" ) .await;
890
for ( name, vis) in [ ( " demo" , hub_api:: Visibility:: Public) , ( " secret" , hub_api:: Visibility:: Private) ] {
891
app.repos.create ( & RepoName:: parse( & format! ( " andy/{name} " ) ) .unwrap ( ) , None ) .unwrap ( ) ;
892
let row = db:: repos:: create( & app.db, " andy" , name, " " , vis) .await.unwrap ( ) ;
893
db:: issues:: create(
894
& app.db,
895
db:: issues:: NewIssue { repo_id: row.id, title: & format! ( " {name} issue" ) , body: " " , author_id: None , author: " andy" } ,
896
)
897
.await
898
.unwrap ( ) ;
899
}
16ee5d19 Andy McCall 18 days ago ↶
900
901
902
a024e800 Andy McCall 27 days ago ↶
903
let home = get ( app, " /andy/demo" , Some ( & andy) ) .await.body;
16ee5d19 Andy McCall 18 days ago ↶
904
assert! ( home.contains ( " href=\" /projects/andy\" ><svg class=\" ic\" " ) , " site bar" ) ;
905
assert! ( ! home.contains ( " project boards\" >Projects</a>" ) , " not a repository tab" ) ;
906
assert! ( get ( app, " /andy" , Some ( & bob) ) .await.body.contains ( " href=\" /projects/andy\" >Projects</a>" ) , " the owner's profile" ) ;
a024e800 Andy McCall 27 days ago ↶
907
let list = get ( app, " /projects/andy" , Some ( & andy) ) .await.body;
908
assert! ( list.contains ( " href=\" #new\" >+ NEW PROJECT</a>" ) & & list.contains ( " id=\" new\" " ) , " the button and the form" ) ;
909
assert! ( get ( app, " /projects/andy" , Some ( & bob) ) .await.body.contains ( " Only andy" ) , " who can make one" ) ;
910
let csrf = csrf_of ( & list) ;
e1ec9fbf Andy McCall 1 month ago ↶
911
let r = post ( app, " /projects/andy" , Some ( & andy) , & format! ( " _csrf={csrf} &title=Roadmap&description=Next+steps" ) ) .await;
912
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/andy/1" ) , " {}" , r.body) ;
7b26e611 Andy McCall 26 days ago ↶
913
914
let list = get ( app, " /projects/andy" , Some ( & andy) ) .await.body;
915
assert! (
916
list.contains ( " >Roadmap</a>" ) & & list.contains ( " Next steps" ) & & list.contains ( " Open <span class=\" count-chip\" >1</span>" ) ,
917
" {list}"
918
) ;
919
let filtered = get ( app, " /projects/andy?q=roadm" , Some ( & andy) ) .await.body;
920
assert! ( filtered.contains ( " >Roadmap</a>" ) , " a filter matches part of the title, any case" ) ;
921
let none = get ( app, " /projects/andy?q=nothing+like+it" , Some ( & andy) ) .await.body;
922
assert! ( none.contains ( " No open project matches" ) & & ! none.contains ( " >Roadmap</a>" ) , " {none}" ) ;
923
let closed = get ( app, " /projects/andy?state=closed" , Some ( & andy) ) .await.body;
924
assert! ( closed.contains ( " No closed projects." ) & & closed.contains ( " Closed <span class=\" count-chip\" >0</span>" ) , " {closed}" ) ;
e1ec9fbf Andy McCall 1 month ago ↶
925
let board = get ( app, " /projects/andy/1" , Some ( & andy) ) .await.body;
926
for col in [ " Backlog" , " Doing" , " Done" ] {
927
assert! ( board.contains ( & format! ( " >{col} <span" ) ) , " {col}" ) ;
928
}
929
let cols: Vec< i64 > = sqlx:: query_scalar( " SELECT id FROM project_columns ORDER BY position" ) .fetch_all ( & app.db) .await.unwrap ( ) ;
930
let act = | form : String| {
931
let andy = andy.clone ( ) ;
932
async move { post ( app, " /projects/andy/1" , Some ( & andy) , & form) .await }
933
} ;
934
935
assert_eq! (
936
act ( format! ( " _csrf={csrf} &action=add&column={} &reference=demo%231" , cols[ 0 ] ) ) .await.location.as_deref ( ) ,
937
Some ( " /projects/andy/1" )
938
) ;
939
act ( format! ( " _csrf={csrf} &action=add&column={} &repo=secret&reference=%231" , cols[ 0 ] ) ) .await;
940
act ( format! ( " _csrf={csrf} &action=add&column={} ¬e=Write+the+docs" , cols[ 1 ] ) ) .await;
941
assert! ( act ( format! ( " _csrf={csrf} &action=add&column={} &reference=demo%231" , cols[ 1 ] ) ) .await.body.contains ( " already on this project" ) ) ;
942
assert! ( act ( format! ( " _csrf={csrf} &action=add&column={} &reference=demo%2399" , cols[ 0 ] ) ) .await.body.contains ( " no issue or proposal #99" ) ) ;
943
assert! ( act ( format! ( " _csrf={csrf} &action=add&column={} &reference=nothere%231" , cols[ 0 ] ) ) .await.body.contains ( " no repository nothere" ) ) ;
944
945
let demo_card: i64 =
946
sqlx:: query_scalar( " SELECT c.id FROM project_cards c JOIN repositories r ON r.id = c.repo_id WHERE r.name = 'demo'" )
947
.fetch_one ( & app.db)
948
.await
949
.unwrap ( ) ;
950
act ( format! ( " _csrf={csrf} &action=move&card={demo_card} &column={} " , cols[ 2 ] ) ) .await;
951
let issue = get ( app, " /andy/demo/issue/1" , None ) .await.body;
952
assert! ( issue.contains ( " href=\" /projects/andy/1\" >Roadmap</a> <span class=\" mute\" >· Done" ) , " the issue shows where it is" ) ;
953
954
955
let anon = get ( app, " /projects/andy/1" , None ) .await.body;
956
assert! ( anon.contains ( " demo issue" ) & & anon.contains ( " Write the docs" ) & & ! anon.contains ( " secret issue" ) , " {anon}" ) ;
957
assert! ( ! anon.contains ( " name=\" action\" " ) , " and cannot change anything" ) ;
958
assert! ( get ( app, " /projects/andy/1" , Some ( & andy) ) .await.body.contains ( " secret issue" ) ) ;
959
960
let csrf_b = csrf_of ( & get ( app, " /settings" , Some ( & bob) ) .await.body) ;
961
assert_eq! (
962
post ( app, " /projects/andy/1" , Some ( & bob) , & format! ( " _csrf={csrf_b} &action=remove&card={demo_card} " ) ) .await.status,
963
StatusCode:: NOT_FOUND
964
) ;
965
act ( format! ( " _csrf={csrf} &action=settings&title=Roadmap&description=&private=1" ) ) .await;
966
assert_eq! ( get ( app, " /projects/andy/1" , Some ( & bob) ) .await.status, StatusCode:: NOT_FOUND ) ;
967
assert! ( ! get ( app, " /projects/andy" , None ) .await.body.contains ( " Roadmap" ) , " not even listed" ) ;
968
assert! ( ! get ( app, " /andy/demo/issue/1" , None ) .await.body.contains ( " Roadmap" ) , " nor on the issue" ) ;
969
970
971
act ( format! ( " _csrf={csrf} &action=remove_column&column={} " , cols[ 1 ] ) ) .await;
972
let note_col: i64 = sqlx:: query_scalar( " SELECT column_id FROM project_cards WHERE kind = 'note'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
973
assert_eq! ( note_col, cols[ 0 ] ) ;
974
act ( format! ( " _csrf={csrf} &action=remove_column&column={} " , cols[ 2 ] ) ) .await;
975
assert! ( act ( format! ( " _csrf={csrf} &action=remove_column&column={} " , cols[ 0 ] ) ) .await.body.contains ( " at least one column" ) ) ;
976
977
act ( format! ( " _csrf={csrf} &action=settings&title=Roadmap&closed=1" ) ) .await;
978
assert! ( act ( format! ( " _csrf={csrf} &action=add&column={} ¬e=late" , cols[ 0 ] ) ) .await.body.contains ( " This project is closed" ) ) ;
979
let r = act ( format! ( " _csrf={csrf} &action=delete" ) ) .await;
980
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/andy" ) ) ;
981
assert_eq! ( get ( app, " /projects/andy/1" , Some ( & andy) ) .await.status, StatusCode:: NOT_FOUND ) ;
982
assert_eq! ( get ( app, " /andy/demo/issue/1" , None ) .await.status, StatusCode:: OK ) ;
983
assert_eq! ( get ( app, " /projects/nobody" , None ) .await.status, StatusCode:: NOT_FOUND ) ;
984
}
c79a1111 Andy McCall 1 month ago ↶
985
657e7d5c Andy McCall 12 days ago ↶
986
987
c79a1111 Andy McCall 1 month ago ↶
988
# [ tokio ::test ( flavor = " multi_thread" ) ]
657e7d5c Andy McCall 12 days ago ↶
989
async fn a_long_column_shows_its_newest_cards ( ) {
990
let h = hub ( ) .await;
991
let app = & h.app;
992
let andy = register ( app, " andy" ) .await;
993
let csrf = csrf_of ( & get ( app, " /projects/andy" , Some ( & andy) ) .await.body) ;
994
post ( app, " /projects/andy" , Some ( & andy) , & format! ( " _csrf={csrf} &title=Roadmap" ) ) .await;
995
let ( project, done) : ( i64 , i64 ) =
996
sqlx:: query_as( " SELECT p.id, c.id FROM projects p JOIN project_columns c ON c.project_id = p.id WHERE c.name = 'Done'" )
997
.fetch_one ( & app.db)
998
.await
999
.unwrap ( ) ;
1000
for i in 1 .. = 35 {
1001
sqlx:: query(
1002
" INSERT INTO project_cards (project_id, column_id, position, kind, note, created_ms) VALUES ($1, $2, $3, 'note', $4, 0)" ,
1003
)
1004
.bind ( project)
1005
.bind ( done)
1006
.bind ( i)
1007
.bind ( format! ( " card-{i:03} " ) )
1008
.execute ( & app.db)
1009
.await
1010
.unwrap ( ) ;
1011
}
1012
let board = get ( app, " /projects/andy/1" , Some ( & andy) ) .await.body;
1013
assert! ( board.contains ( " 5 older cards not shown" ) & & board.contains ( & format! ( " ?all={done} #col-{done} \" >show all 35</a>" ) ) , " {board}" ) ;
1014
assert! ( board.contains ( " card-006" ) & & board.contains ( " card-035" ) & & ! board.contains ( " card-005" ) , " the newest 30" ) ;
1015
assert! ( board.contains ( " >35</span></h2>" ) , " the count is the column's" ) ;
1016
let all = get ( app, & format! ( " /projects/andy/1?all={done} " ) , Some ( & andy) ) .await.body;
1017
assert! ( all.contains ( " card-001" ) & & ! all.contains ( " older cards not shown" ) , " show all" ) ;
1018
let found = get ( app, " /projects/andy/1?q=CARD-002" , Some ( & andy) ) .await.body;
1019
assert! ( found.contains ( " card-002" ) & & ! found.contains ( " card-030" ) & & found.contains ( " 1 of 35" ) , " {found}" ) ;
1020
assert! ( found.contains ( " >clear</a>" ) ) ;
1021
let open = get ( app, " /projects/andy/1?state=open" , Some ( & andy) ) .await.body;
1022
assert! ( ! open.contains ( " card-0" ) , " notes have no state" ) ;
1023
}
1024
6a306cd0 Andy McCall 12 days ago ↶
1025
1026
1027
657e7d5c Andy McCall 12 days ago ↶
1028
# [ tokio ::test ( flavor = " multi_thread" ) ]
6a306cd0 Andy McCall 12 days ago ↶
1029
async fn a_board_filters_by_label_and_assignee ( ) {
1030
let h = hub ( ) .await;
1031
let app = & h.app;
1032
let andy = register ( app, " andy" ) .await;
1033
let csrf = csrf_of ( & get ( app, " /projects/andy" , Some ( & andy) ) .await.body) ;
1034
post ( app, " /projects/andy" , Some ( & andy) , & format! ( " _csrf={csrf} &title=Roadmap" ) ) .await;
1035
app.repos.create ( & RepoName:: parse( " andy/game" ) .unwrap ( ) , None ) .unwrap ( ) ;
1036
let row = db:: repos:: create( & app.db, " andy" , " game" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
1037
let mut ids = Vec :: new( ) ;
1038
for title in [ " Sound crackles" , " Jump is floaty" , " Title screen" ] {
1039
let i = db:: issues:: create( & app.db, db:: issues:: NewIssue { repo_id: row.id, title, body: " " , author_id: None , author: " andy" } )
1040
.await
1041
.unwrap ( ) ;
1042
ids.push ( i.id) ;
1043
}
1044
let bug = db:: issues:: upsert_label( & app.db, row.id, " bug" , " #d73a4a" , " Something is wrong" ) .await.unwrap ( ) ;
1045
let feel = db:: issues:: upsert_label( & app.db, row.id, " feel" , " #0e8a16" , " " ) .await.unwrap ( ) ;
1046
db:: issues:: set_issue_label( & app.db, ids[ 0 ] , bug.id, true ) .await.unwrap ( ) ;
1047
db:: issues:: set_issue_label( & app.db, ids[ 1 ] , bug.id, true ) .await.unwrap ( ) ;
1048
db:: issues:: set_issue_label( & app.db, ids[ 1 ] , feel.id, true ) .await.unwrap ( ) ;
1049
let ( andy_id, ) : ( i64 , ) = sqlx:: query_as( " SELECT id FROM accounts WHERE username = 'andy'" ) .fetch_one ( & app.db) .await.unwrap ( ) ;
1050
db:: issues:: set_assignee( & app.db, ids[ 1 ] , andy_id, true ) .await.unwrap ( ) ;
1051
let ( project, todo) : ( i64 , i64 ) =
1052
sqlx:: query_as( " SELECT p.id, c.id FROM projects p JOIN project_columns c ON c.project_id = p.id ORDER BY c.position LIMIT 1" )
1053
.fetch_one ( & app.db)
1054
.await
1055
.unwrap ( ) ;
1056
for n in 1 .. = 3 i64 {
1057
sqlx:: query(
1058
" INSERT INTO project_cards (project_id, column_id, position, kind, repo_id, number, note, created_ms) VALUES ($1, $2, $3, 'issue', $4, $5, '', 0)" ,
1059
)
1060
.bind ( project)
1061
.bind ( todo)
1062
.bind ( n)
1063
.bind ( row.id)
1064
.bind ( n)
1065
.execute ( & app.db)
1066
.await
1067
.unwrap ( ) ;
1068
}
1069
let board = get ( app, " /projects/andy/1" , Some ( & andy) ) .await.body;
1070
assert! ( board.contains ( " href=\" /projects/andy/1?q=label:bug\" " ) , " {board}" ) ;
1071
assert! ( board.contains ( " title=\" Something is wrong\" >bug</a>" ) , " a label's description is its title" ) ;
1072
assert! ( board.contains ( " >@andy</a>" ) , " {board}" ) ;
1073
let bugs = get ( app, " /projects/andy/1?q=label:bug" , Some ( & andy) ) .await.body;
1074
assert! ( bugs.contains ( " Sound crackles" ) & & bugs.contains ( " Jump is floaty" ) & & ! bugs.contains ( " Title screen" ) , " {bugs}" ) ;
1075
assert! ( bugs.contains ( " 2 of 3" ) ) ;
1076
let both = get ( app, " /projects/andy/1?q=label:BUG+label:feel" , Some ( & andy) ) .await.body;
1077
assert! ( ! both.contains ( " Sound crackles" ) & & both.contains ( " Jump is floaty" ) , " every label must hold" ) ;
1078
let mine = get ( app, " /projects/andy/1?q=assignee:%40andy" , Some ( & andy) ) .await.body;
1079
assert! ( mine.contains ( " Jump is floaty" ) & & ! mine.contains ( " Sound crackles" ) , " {mine}" ) ;
1080
let words = get ( app, " /projects/andy/1?q=label:bug+sound" , Some ( & andy) ) .await.body;
1081
assert! ( words.contains ( " Sound crackles" ) & & ! words.contains ( " Jump is floaty" ) ) ;
1082
let closed = get ( app, " /projects/andy/1?q=state:closed" , Some ( & andy) ) .await.body;
1083
assert! ( closed.contains ( " 0 of 3" ) , " {closed}" ) ;
1084
}
1085
439ecd87 Andy McCall 12 days ago ↶
1086
1087
1088
1089
6a306cd0 Andy McCall 12 days ago ↶
1090
# [ tokio ::test ( flavor = " multi_thread" ) ]
439ecd87 Andy McCall 12 days ago ↶
1091
async fn board_cards_are_archived_and_restored ( ) {
1092
let h = hub ( ) .await;
1093
let app = & h.app;
1094
let andy = register ( app, " andy" ) .await;
1095
let csrf = csrf_of ( & get ( app, " /projects/andy" , Some ( & andy) ) .await.body) ;
1096
post ( app, " /projects/andy" , Some ( & andy) , & format! ( " _csrf={csrf} &title=Roadmap" ) ) .await;
1097
let ( project, done) : ( i64 , i64 ) =
1098
sqlx:: query_as( " SELECT p.id, c.id FROM projects p JOIN project_columns c ON c.project_id = p.id WHERE c.name = 'Done'" )
1099
.fetch_one ( & app.db)
1100
.await
1101
.unwrap ( ) ;
1102
let day = 86_400_000 i64 ;
1103
let now = db:: now_ms( ) ;
1104
for ( i, age) in [ 40 , 20 , 10 , 1 ] .iter ( ) .enumerate ( ) {
1105
sqlx:: query(
1106
" INSERT INTO project_cards (project_id, column_id, position, kind, note, created_ms, moved_ms) VALUES ($1, $2, $3, 'note', $4, 0, $5)" ,
1107
)
1108
.bind ( project)
1109
.bind ( done)
1110
.bind ( i as i64 )
1111
.bind ( format! ( " aged-{age} " ) )
1112
.bind ( now - age * day)
1113
.execute ( & app.db)
1114
.await
1115
.unwrap ( ) ;
1116
}
1117
let id_of = | note : & 'static str | async move {
1118
sqlx:: query_scalar:: < _ , i64 > ( " SELECT id FROM project_cards WHERE note = $1" ) .bind ( note) .fetch_one ( & app.db) .await.unwrap ( )
1119
} ;
1120
let board = " /projects/andy/1" ;
1121
let ( who, token) = ( & andy, & csrf) ;
1122
let act = | body : String| async move { post ( app, board, Some ( who) , & format! ( " _csrf={token} &{body} " ) ) .await } ;
1123
1124
1125
let forty = id_of ( " aged-40" ) .await;
1126
act ( format! ( " action=archive&card={forty} " ) ) .await;
1127
let page = get ( app, board, Some ( & andy) ) .await.body;
1128
assert! ( ! page.contains ( " aged-40" ) & & page.contains ( " aged-20" ) , " {page}" ) ;
1129
assert! ( page.contains ( " ?archived=1\" >1 archived card</a>" ) , " {page}" ) ;
1130
let archived = get ( app, & format! ( " {board} ?archived=1" ) , Some ( & andy) ) .await.body;
1131
assert! ( archived.contains ( " aged-40" ) & & ! archived.contains ( " aged-20" ) , " {archived}" ) ;
1132
assert! ( archived.contains ( " value=\" restore\" " ) & & archived.contains ( " Back to the board" ) ) ;
1133
assert! ( ! archived.contains ( " add a card" ) , " the archive view adds nothing" ) ;
1134
let found = get ( app, & format! ( " {board} ?archived=1&q=aged-40" ) , Some ( & andy) ) .await.body;
1135
assert! ( found.contains ( " 1 of 1" ) & & found.contains ( " name=\" archived\" value=\" 1\" " ) , " {found}" ) ;
1136
act ( format! ( " action=restore&card={forty} " ) ) .await;
1137
assert! ( get ( app, board, Some ( & andy) ) .await.body.contains ( " aged-40" ) ) ;
1138
1139
1140
1141
let r = act ( format! ( " action=archive_older&column={done} &days=15" ) ) .await;
1142
assert! ( r.body.contains ( " Archived 1 card from Done." ) , " {}" , r.body) ;
1143
let page = get ( app, board, Some ( & andy) ) .await.body;
1144
assert! ( page.contains ( " aged-40" ) & & ! page.contains ( " aged-20" ) & & page.contains ( " aged-10" ) ) ;
1145
let r = act ( format! ( " action=archive_older&column={done} &days=soon" ) ) .await;
1146
assert! ( r.body.contains ( " Give a number of days" ) , " {}" , r.body) ;
1147
1148
1149
let r = act ( format! ( " action=archive_after&column={done} &days=5" ) ) .await;
1150
assert! ( r.body.contains ( " Done archives its cards 5 days after they reach it." ) , " {}" , r.body) ;
1151
assert! ( get ( app, board, Some ( & andy) ) .await.body.contains ( " archives cards 5 days after they reach it" ) ) ;
1152
assert_eq! ( db:: projects:: archive_due( & app.db, db:: now_ms( ) ) .await.unwrap ( ) , 1 ) ;
1153
let page = get ( app, board, Some ( & andy) ) .await.body;
1154
assert! ( page.contains ( " aged-40" ) & & ! page.contains ( " aged-10" ) & & page.contains ( " aged-1<" ) , " {page}" ) ;
1155
assert! ( page.contains ( " 2 archived cards" ) ) ;
1156
let r = act ( format! ( " action=archive_after&column={done} &days=" ) ) .await;
1157
assert! ( r.body.contains ( " Done keeps its cards until you archive them." ) ) ;
1158
assert_eq! ( db:: projects:: archive_due( & app.db, db:: now_ms( ) + 100 * day) .await.unwrap ( ) , 0 , " never, now" ) ;
1159
}
1160
1161
# [ tokio ::test ( flavor = " multi_thread" ) ]
c79a1111 Andy McCall 1 month ago ↶
1162
async fn issue_markdown_follows_the_default_bookmark ( ) {
1163
let h = hub ( ) .await;
1164
let app = & h.app;
1165
register ( app, " andy" ) .await;
1166
app.repos.create ( & RepoName:: parse( " andy/trunky" ) .unwrap ( ) , None ) .unwrap ( ) ;
1167
let row = db:: repos:: create( & app.db, " andy" , " trunky" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
3b1bc27f Andy McCall 20 days ago
1168
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 ↶
1169
db:: issues:: create(
1170
& app.db,
1171
db:: issues:: NewIssue {
1172
repo_id: row.id,
1173
title: " Docs" ,
1174
body: " See [the guide](docs/guide.md) and ." ,
1175
author_id: None ,
1176
author: " andy" ,
1177
} ,
1178
)
1179
.await
1180
.unwrap ( ) ;
1181
let page = get ( app, " /andy/trunky/issue/1" , None ) .await.body;
1182
assert! ( page.contains ( " href=\" /andy/trunky/file/trunk/docs/guide.md\" " ) , " {page}" ) ;
1183
assert! ( page.contains ( " src=\" /andy/trunky/raw/trunk/img/a.png\" " ) ) ;
1184
assert! ( ! page.contains ( " /file/main/" ) ) ;
1185
}
b1e92a62 Andy McCall 26 days ago ↶
1186
1187
1188
1189
1190
# [ tokio ::test ( flavor = " multi_thread" ) ]
1191
async fn a_project_transfers_to_an_organisation ( ) {
1192
let h = hub ( ) .await;
1193
let app = & h.app;
1194
1195
1196
let _first = register ( app, " ada" ) .await;
1197
let andy = register ( app, " andy" ) .await;
1198
let _bob = register ( app, " bob" ) .await;
1199
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
1200
let bob_id = db:: accounts:: account_by_name( & app.db, " bob" ) .await.unwrap ( ) .unwrap ( ) .id;
1201
db:: accounts:: create_org( & app.db, " retro" , " Retro Club" , andy_id) .await.unwrap ( ) ;
1202
db:: accounts:: create_org( & app.db, " other" , " Someone else's" , bob_id) .await.unwrap ( ) ;
1203
let list = get ( app, " /projects/andy" , Some ( & andy) ) .await.body;
1204
let csrf = csrf_of ( & list) ;
1205
let r = post ( app, " /projects/andy" , Some ( & andy) , & format! ( " _csrf={csrf} &title=Roadmap" ) ) .await;
1206
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/andy/1" ) ) ;
1207
post ( app, " /projects/andy/1" , Some ( & andy) , & format! ( " _csrf={csrf} &action=add¬e=Keep+me" ) ) .await;
1208
1209
1210
let board = get ( app, " /projects/andy/1" , Some ( & andy) ) .await.body;
1211
assert! ( board.contains ( " <option value=\" retro\" >retro</option>" ) & & ! board.contains ( " value=\" other\" " ) , " {board}" ) ;
1212
let refused = post ( app, " /projects/andy/1" , Some ( & andy) , & format! ( " _csrf={csrf} &action=transfer&to=other" ) ) .await;
1213
assert! ( refused.body.contains ( " You cannot make projects under other" ) , " {}" , refused.body) ;
1214
1215
let r = post ( app, " /projects/andy/1" , Some ( & andy) , & format! ( " _csrf={csrf} &action=transfer&to=retro" ) ) .await;
1216
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/retro/1" ) , " {}" , r.body) ;
1217
let moved = get ( app, " /projects/retro/1" , Some ( & andy) ) .await.body;
1218
assert! ( moved.contains ( " Roadmap" ) & & moved.contains ( " Keep me" ) , " the cards come too" ) ;
1219
let old = get ( app, " /projects/andy/1" , Some ( & andy) ) .await;
1220
assert_eq! ( old.status, StatusCode:: PERMANENT_REDIRECT ) ;
1221
assert_eq! ( old.location.as_deref ( ) , Some ( " /projects/retro/1" ) ) ;
1222
1223
1224
let r = post ( app, " /projects/andy" , Some ( & andy) , & format! ( " _csrf={csrf} &title=Fresh" ) ) .await;
1225
assert_eq! ( r.location.as_deref ( ) , Some ( " /projects/andy/1" ) ) ;
1226
let fresh = get ( app, " /projects/andy/1" , Some ( & andy) ) .await;
1227
assert_eq! ( fresh.status, StatusCode:: OK ) ;
1228
assert! ( fresh.body.contains ( " Fresh" ) ) ;
1229
}
d7d18246 Andy McCall 23 days ago ↶
1230
1231
1232
1233
# [ tokio ::test ( flavor = " multi_thread" ) ]
1234
async fn backups_are_set_up_in_settings ( ) {
1235
let h = hub ( ) .await;
1236
let app = & h.app;
1237
let andy = register ( app, " andy" ) .await;
1238
let page = get ( app, " /settings/backups" , Some ( & andy) ) .await;
1239
assert_eq! ( page.status, StatusCode:: OK ) ;
1240
assert! ( page.body.contains ( " href=\" /settings/backups\" class=\" on\" " ) & & page.body.contains ( " No runs yet." ) , " {}" , page.body) ;
1241
let csrf = csrf_of ( & page.body) ;
1242
1243
let bad = post (
1244
app,
1245
" /settings/backups" ,
1246
Some ( & andy) ,
1247
& format! ( " _csrf={csrf} &every=hourly&destination=gcs&bucket=andy-backup&provider=nope&keep_days=30" ) ,
1248
)
1249
.await;
1250
assert! ( bad.body.contains ( " workload identity provider" ) , " {}" , bad.body) ;
1251
let dir =
1252
post ( app, " /settings/backups" , Some ( & andy) , & format! ( " _csrf={csrf} &every=hourly&destination=dir&dir=%2Ftmp&keep_days=30" ) ) .await;
1253
assert! ( dir.body.contains ( " Pick one of the directories this hub allows." ) ) ;
1254
let provider =
1255
" %2F%2Fiam.googleapis.com%2Fprojects%2F1%2Flocations%2Fglobal%2FworkloadIdentityPools%2Foxygenhub%2Fproviders%2Foxygenhub-io" ;
1256
let ok = post (
1257
app,
1258
" /settings/backups" ,
1259
Some ( & andy) ,
1260
& format! ( " _csrf={csrf} &every=hourly&destination=gcs&bucket=gs%3A%2F%2Fandy-backup&provider={provider} &keep_days=7" ) ,
1261
)
1262
.await;
1263
assert! ( ok.body.contains ( " Saved." ) , " {}" , ok.body) ;
1264
let s = oxygenhub:: backups:: setting( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
1265
assert_eq! ( ( s.target.as_str ( ) , s.keep_days, s.every.as_str ( ) ) , ( " andy-backup" , 7 , " hourly" ) ) ;
7b9f4570 Andy McCall 23 days ago ↶
1266
1267
let bad = post (
1268
app,
1269
" /settings/backups" ,
1270
Some ( & andy) ,
1271
& format! ( " _csrf={csrf} &every=hourly&destination=s3&s3_bucket=andy-backup&s3_region=eu-west-2&s3_role=admin&keep_days=30" ) ,
1272
)
1273
.await;
1274
assert! ( bad.body.contains ( " the IAM role" ) , " {}" , bad.body) ;
1275
let role = " arn%3Aaws%3Aiam%3A%3A123456789012%3Arole%2Foxygenhub-backup" ;
1276
let ok = post (
1277
app,
1278
" /settings/backups" ,
1279
Some ( & andy) ,
1280
& format! (
1281
" _csrf={csrf} &every=daily&destination=s3&s3_bucket=s3%3A%2F%2Fandy-backup&s3_region=eu-west-2&s3_role={role} &keep_days=30"
1282
) ,
1283
)
1284
.await;
1285
assert! ( ok.body.contains ( " Saved." ) & & ok.body.contains ( r # "value="s3" checked"# ) , " {}" , ok.body) ;
1286
let s = oxygenhub:: backups:: setting( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
1287
assert_eq! ( ( s.destination.as_str ( ) , s.target.as_str ( ) , s.region.as_str ( ) ) , ( " s3" , " andy-backup" , " eu-west-2" ) ) ;
1288
assert_eq! ( s.provider, " arn:aws:iam::123456789012:role/oxygenhub-backup" ) ;
1289
1290
let ok = post (
1291
app,
1292
" /settings/backups" ,
1293
Some ( & andy) ,
1294
& format! ( " _csrf={csrf} &every=hourly&destination=gcs&bucket=gs%3A%2F%2Fandy-backup&provider={provider} &keep_days=7" ) ,
1295
)
1296
.await;
1297
assert! ( ok.body.contains ( " Saved." ) ) ;
d7d18246 Andy McCall 23 days ago ↶
1298
1299
let bob = register ( app, " bob" ) .await;
1300
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
1301
db:: accounts:: create_org( & app.db, " club" , " Club" , andy_id) .await.unwrap ( ) ;
1302
assert_eq! ( get ( app, " /orgs/club/settings/backups" , Some ( & bob) ) .await.status, StatusCode:: NOT_FOUND ) ;
1303
let club = get ( app, " /orgs/club/settings/backups" , Some ( & andy) ) .await;
1304
assert! ( club.body.contains ( " club</a>'s repositories" ) , " {}" , club.body) ;
1305
assert! ( get ( app, " /orgs/club/settings" , Some ( & andy) ) .await.body.contains ( " /orgs/club/settings/backups" ) ) ;
1e8a6e8b Andy McCall 23 days ago ↶
1306
1307
1308
1309
sqlx:: query( " UPDATE accounts SET is_admin = (username = 'andy')" ) .execute ( & app.db) .await.unwrap ( ) ;
1310
app.repos.create ( & oxygenhub:: repos:: RepoName:: parse( " bob/game" ) .unwrap ( ) , None ) .unwrap ( ) ;
1311
db:: repos:: create( & app.db, " bob" , " game" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
1312
assert_eq! ( get ( app, " /admin/backups" , Some ( & bob) ) .await.status, StatusCode:: NOT_FOUND ) ;
1313
let admin = get ( app, " /admin/backups" , Some ( & andy) ) .await;
1314
assert_eq! ( admin.status, StatusCode:: OK ) ;
1315
assert! ( admin.body.contains ( " gs://andy-backup" ) & & admin.body.contains ( " hourly" ) & & admin.body.contains ( " never" ) , " {}" , admin.body) ;
1316
let missing = & admin.body[ admin.body.find ( " Not backed up" ) .unwrap ( ) .. ] ;
1317
assert! ( missing.contains ( " href=\" /bob\" " ) & & missing.contains ( " 1 repository" ) , " {missing}" ) ;
1318
assert! ( get ( app, " /admin" , Some ( & andy) ) .await.body.contains ( " /admin/backups" ) ) ;
d3b048b2 Andy McCall 23 days ago ↶
1319
c7067c7b Andy McCall 16 days ago
1320
let game = get ( app, " /bob/game/settings/backup" , Some ( & bob) ) .await;
d3b048b2 Andy McCall 23 days ago ↶
1321
assert! ( game.body.contains ( " bob has no backup set up yet" ) & & game.body.contains ( r # "name="backup" value="" checked"# ) , " {}" , game.body) ;
1322
let csrf = csrf_of ( & game.body) ;
1323
let saved = post ( app, " /bob/game/settings/backup" , Some ( & bob) , & format! ( " _csrf={csrf} &backup=off" ) ) .await;
c7067c7b Andy McCall 16 days ago
1324
let saved = settled ( app, saved, Some ( & bob) , " /bob/game/settings/backup" ) .await;
d3b048b2 Andy McCall 23 days ago ↶
1325
assert! ( saved.body.contains ( " left out of its owner" ) , " {}" , saved.body) ;
1326
assert_eq! ( db:: repos:: get( & app.db, " bob" , " game" ) .await.unwrap ( ) .unwrap ( ) .backup, " off" ) ;
1327
let bad = post ( app, " /bob/game/settings/backup" , Some ( & bob) , & format! ( " _csrf={csrf} &backup=weekly" ) ) .await;
c7067c7b Andy McCall 16 days ago
1328
let bad = settled ( app, bad, Some ( & bob) , " /bob/game/settings/backup" ) .await;
d3b048b2 Andy McCall 23 days ago ↶
1329
assert! ( bad.body.contains ( " Pick how this repository is backed up." ) ) ;
d7d18246 Andy McCall 23 days ago ↶
1330
}
bff9106e Andy McCall 22 days ago ↶
1331
1332
1333
1334
1335
# [ tokio ::test ( flavor = " multi_thread" ) ]
1336
async fn owners_hear_by_default_and_agents_tell_their_owner ( ) {
1337
let h = hub ( ) .await;
1338
let app = & h.app;
1339
let andy = register ( app, " andy" ) .await;
1340
let bob = register ( app, " bob" ) .await;
1341
let andy_id = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
1342
let kinds = | id : i64 | {
1343
let db = app.db.clone ( ) ;
1344
async move { db:: notify:: inbox( & db, id, 50 ) .await.unwrap ( ) .into_iter ( ) .map ( | n | ( n.kind, n.title, n.actor) ) .collect:: < Vec< _ > > ( ) }
1345
} ;
1346
assert! ( kinds ( andy_id) .await.iter ( ) .any ( | ( k , t , _) | k = = " account.registered" & & t.contains ( " bob" ) ) , " the administrator hears of bob" ) ;
1347
let repo = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
1348
app.repos.create ( & repo, None ) .unwrap ( ) ;
1349
db:: repos:: create( & app.db, " andy" , " demo" , " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
1350
let open = | who : & 'static str , title : & 'static str | {
1351
let ( app, cookie) = ( app.clone ( ) , if who = = " bob" { bob.clone ( ) } else { andy.clone ( ) } ) ;
1352
async move {
1353
let csrf = csrf_of ( & get ( & app, " /andy/demo/issues/new" , Some ( & cookie) ) .await.body) ;
1354
post ( & app, " /andy/demo/issues/new" , Some ( & cookie) , & format! ( " _csrf={csrf} &title={title} &body=x" ) ) .await
1355
}
1356
} ;
1357
1358
assert! ( get ( app, " /andy/demo" , Some ( & andy) ) .await.body.contains ( " UNWATCH" ) , " an owner watches by default" ) ;
1359
open ( " bob" , " First" ) .await;
1360
assert! ( kinds ( andy_id) .await.iter ( ) .any ( | ( k , t , _) | k = = " issue.opened" & & t.contains ( " First" ) ) ) ;
1361
1362
let csrf = csrf_of ( & get ( app, " /andy/demo" , Some ( & andy) ) .await.body) ;
1363
post ( app, " /andy/demo/watch" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
1364
assert! ( get ( app, " /andy/demo" , Some ( & andy) ) .await.body.contains ( " >WATCH<" ) , " muted" ) ;
1365
open ( " bob" , " Second" ) .await;
1366
assert! ( ! kinds ( andy_id) .await.iter ( ) .any ( | ( _, t , _ ) | t.contains ( " Second" ) ) ) ;
1367
post ( app, " /andy/demo/watch" , Some ( & andy) , & format! ( " _csrf={csrf} " ) ) .await;
1368
1369
1370
let token = | agent : bool | {
1371
let db = app.db.clone ( ) ;
1372
async move {
1373
let ( t, hash) = oxygenhub:: auth:: new_token( ) ;
1374
db:: accounts:: create_token( & db, andy_id, if agent { " Claude" } else { " laptop" } , & hash, " repo:read,repo:write" , " " , None )
1375
.await
1376
.unwrap ( ) ;
1377
if agent {
1378
let id = db:: accounts:: token_by_hash( & db, & hash) .await.unwrap ( ) .unwrap ( ) .id;
1379
db:: accounts:: set_token_agent( & db, andy_id, id, true ) .await.unwrap ( ) ;
1380
}
1381
t
1382
}
1383
} ;
1384
let api_issue = | t : String, title : & 'static str | {
1385
let app = app.clone ( ) ;
1386
async move {
1387
use tower:: ServiceExt;
1388
let req = axum:: http:: Request:: post( " /api/v1/repos/andy/demo/issues" )
1389
.header ( " authorization" , format! ( " Bearer {t} " ) )
1390
.header ( " content-type" , " application/json" )
1391
.body ( axum:: body:: Body:: from( serde_json:: json! ( { " title" : title, " body" : " " } ) .to_string ( ) ) )
1392
.unwrap ( ) ;
1393
oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) .status ( )
1394
}
1395
} ;
1396
assert_eq! ( api_issue ( token ( false ) .await, " By hand" ) .await, StatusCode:: CREATED ) ;
1397
assert! ( ! kinds ( andy_id) .await.iter ( ) .any ( | ( _, t , _ ) | t.contains ( " By hand" ) ) , " his own doing" ) ;
1398
assert_eq! ( api_issue ( token ( true ) .await, " By the agent" ) .await, StatusCode:: CREATED ) ;
1399
let told = kinds ( andy_id) .await;
1400
let agent = told.iter ( ) .find ( | ( _, t , _ ) | t.contains ( " By the agent" ) ) .expect ( " told of his agent's issue" ) ;
1401
assert_eq! ( agent.2 , " andy (agent token Claude)" ) ;
1402
1403
let page = get ( app, " /settings/tokens" , Some ( & andy) ) .await;
00ef7b83 Andy McCall 22 days ago ↶
1404
assert! ( page.body.contains ( " Not an agent" ) & & page.body.contains ( " Mark as agent" ) , " {}" , page.body) ;
1405
assert! ( page.body.contains ( r # "<span class="pill agent-pill""# ) , " the agent's token says so" ) ;
bff9106e Andy McCall 22 days ago ↶
1406
}