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