@@ -97,6 +97,8 @@ mod auth {
97
97
use crate :: util:: { MockAnonymousUser , MockCookieUser } ;
98
98
use chrono:: { Duration , Utc } ;
99
99
use crates_io:: models:: token:: { CrateScope , EndpointScope } ;
100
+ use crates_io:: schema:: { crates, versions} ;
101
+ use diesel:: prelude:: * ;
100
102
101
103
const CRATE_NAME : & str = "fyk" ;
102
104
const CRATE_VERSION : & str = "1.0.0" ;
@@ -110,74 +112,94 @@ mod auth {
110
112
( app, anon, cookie)
111
113
}
112
114
115
+ fn is_yanked ( app : & TestApp ) -> bool {
116
+ app. db ( |conn| {
117
+ versions:: table
118
+ . inner_join ( crates:: table)
119
+ . select ( versions:: yanked)
120
+ . filter ( crates:: name. eq ( CRATE_NAME ) )
121
+ . filter ( versions:: num. eq ( CRATE_VERSION ) )
122
+ . get_result ( conn)
123
+ . unwrap ( )
124
+ } )
125
+ }
126
+
113
127
#[ test]
114
128
fn unauthenticated ( ) {
115
- let ( _ , client, _) = prepare ( ) ;
129
+ let ( app , client, _) = prepare ( ) ;
116
130
117
131
let response = client. yank ( CRATE_NAME , CRATE_VERSION ) ;
118
132
assert_eq ! ( response. status( ) , StatusCode :: FORBIDDEN ) ;
119
133
assert_eq ! (
120
134
response. into_json( ) ,
121
135
json!( { "errors" : [ { "detail" : "must be logged in to perform that action" } ] } )
122
136
) ;
137
+ assert ! ( !is_yanked( & app) ) ;
123
138
124
139
let response = client. unyank ( CRATE_NAME , CRATE_VERSION ) ;
125
140
assert_eq ! ( response. status( ) , StatusCode :: FORBIDDEN ) ;
126
141
assert_eq ! (
127
142
response. into_json( ) ,
128
143
json!( { "errors" : [ { "detail" : "must be logged in to perform that action" } ] } )
129
144
) ;
145
+ assert ! ( !is_yanked( & app) ) ;
130
146
}
131
147
132
148
#[ test]
133
149
fn cookie_user ( ) {
134
- let ( _ , _, client) = prepare ( ) ;
150
+ let ( app , _, client) = prepare ( ) ;
135
151
136
152
let response = client. yank ( CRATE_NAME , CRATE_VERSION ) ;
137
153
assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
138
154
assert_eq ! ( response. into_json( ) , json!( { "ok" : true } ) ) ;
155
+ assert ! ( is_yanked( & app) ) ;
139
156
140
157
let response = client. unyank ( CRATE_NAME , CRATE_VERSION ) ;
141
158
assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
142
159
assert_eq ! ( response. into_json( ) , json!( { "ok" : true } ) ) ;
160
+ assert ! ( !is_yanked( & app) ) ;
143
161
}
144
162
145
163
#[ test]
146
164
fn token_user ( ) {
147
- let ( _ , _, client) = prepare ( ) ;
165
+ let ( app , _, client) = prepare ( ) ;
148
166
let client = client. db_new_token ( "test-token" ) ;
149
167
150
168
let response = client. yank ( CRATE_NAME , CRATE_VERSION ) ;
151
169
assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
152
170
assert_eq ! ( response. into_json( ) , json!( { "ok" : true } ) ) ;
171
+ assert ! ( is_yanked( & app) ) ;
153
172
154
173
let response = client. unyank ( CRATE_NAME , CRATE_VERSION ) ;
155
174
assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
156
175
assert_eq ! ( response. into_json( ) , json!( { "ok" : true } ) ) ;
176
+ assert ! ( !is_yanked( & app) ) ;
157
177
}
158
178
159
179
#[ test]
160
180
fn token_user_not_expired ( ) {
161
181
let expired_at = Utc :: now ( ) + Duration :: days ( 7 ) ;
162
182
163
- let ( _ , _, client) = prepare ( ) ;
183
+ let ( app , _, client) = prepare ( ) ;
164
184
let client =
165
185
client. db_new_scoped_token ( "test-token" , None , None , Some ( expired_at. naive_utc ( ) ) ) ;
166
186
167
187
let response = client. yank ( CRATE_NAME , CRATE_VERSION ) ;
168
188
assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
169
189
assert_eq ! ( response. into_json( ) , json!( { "ok" : true } ) ) ;
190
+ assert ! ( is_yanked( & app) ) ;
170
191
171
192
let response = client. unyank ( CRATE_NAME , CRATE_VERSION ) ;
172
193
assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
173
194
assert_eq ! ( response. into_json( ) , json!( { "ok" : true } ) ) ;
195
+ assert ! ( !is_yanked( & app) ) ;
174
196
}
175
197
176
198
#[ test]
177
199
fn token_user_expired ( ) {
178
200
let expired_at = Utc :: now ( ) - Duration :: days ( 7 ) ;
179
201
180
- let ( _ , _, client) = prepare ( ) ;
202
+ let ( app , _, client) = prepare ( ) ;
181
203
let client =
182
204
client. db_new_scoped_token ( "test-token" , None , None , Some ( expired_at. naive_utc ( ) ) ) ;
183
205
@@ -187,33 +209,37 @@ mod auth {
187
209
response. into_json( ) ,
188
210
json!( { "errors" : [ { "detail" : "must be logged in to perform that action" } ] } )
189
211
) ;
212
+ assert ! ( !is_yanked( & app) ) ;
190
213
191
214
let response = client. unyank ( CRATE_NAME , CRATE_VERSION ) ;
192
215
assert_eq ! ( response. status( ) , StatusCode :: FORBIDDEN ) ;
193
216
assert_eq ! (
194
217
response. into_json( ) ,
195
218
json!( { "errors" : [ { "detail" : "must be logged in to perform that action" } ] } )
196
219
) ;
220
+ assert ! ( !is_yanked( & app) ) ;
197
221
}
198
222
199
223
#[ test]
200
224
fn token_user_with_correct_endpoint_scope ( ) {
201
- let ( _ , _, client) = prepare ( ) ;
225
+ let ( app , _, client) = prepare ( ) ;
202
226
let client =
203
227
client. db_new_scoped_token ( "test-token" , None , Some ( vec ! [ EndpointScope :: Yank ] ) , None ) ;
204
228
205
229
let response = client. yank ( CRATE_NAME , CRATE_VERSION ) ;
206
230
assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
207
231
assert_eq ! ( response. into_json( ) , json!( { "ok" : true } ) ) ;
232
+ assert ! ( is_yanked( & app) ) ;
208
233
209
234
let response = client. unyank ( CRATE_NAME , CRATE_VERSION ) ;
210
235
assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
211
236
assert_eq ! ( response. into_json( ) , json!( { "ok" : true } ) ) ;
237
+ assert ! ( !is_yanked( & app) ) ;
212
238
}
213
239
214
240
#[ test]
215
241
fn token_user_with_incorrect_endpoint_scope ( ) {
216
- let ( _ , _, client) = prepare ( ) ;
242
+ let ( app , _, client) = prepare ( ) ;
217
243
let client = client. db_new_scoped_token (
218
244
"test-token" ,
219
245
None ,
@@ -227,18 +253,20 @@ mod auth {
227
253
response. into_json( ) ,
228
254
json!( { "errors" : [ { "detail" : "must be logged in to perform that action" } ] } )
229
255
) ;
256
+ assert ! ( !is_yanked( & app) ) ;
230
257
231
258
let response = client. unyank ( CRATE_NAME , CRATE_VERSION ) ;
232
259
assert_eq ! ( response. status( ) , StatusCode :: FORBIDDEN ) ;
233
260
assert_eq ! (
234
261
response. into_json( ) ,
235
262
json!( { "errors" : [ { "detail" : "must be logged in to perform that action" } ] } )
236
263
) ;
264
+ assert ! ( !is_yanked( & app) ) ;
237
265
}
238
266
239
267
#[ test]
240
268
fn token_user_with_correct_crate_scope ( ) {
241
- let ( _ , _, client) = prepare ( ) ;
269
+ let ( app , _, client) = prepare ( ) ;
242
270
let client = client. db_new_scoped_token (
243
271
"test-token" ,
244
272
Some ( vec ! [ CrateScope :: try_from( CRATE_NAME ) . unwrap( ) ] ) ,
@@ -249,15 +277,17 @@ mod auth {
249
277
let response = client. yank ( CRATE_NAME , CRATE_VERSION ) ;
250
278
assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
251
279
assert_eq ! ( response. into_json( ) , json!( { "ok" : true } ) ) ;
280
+ assert ! ( is_yanked( & app) ) ;
252
281
253
282
let response = client. unyank ( CRATE_NAME , CRATE_VERSION ) ;
254
283
assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
255
284
assert_eq ! ( response. into_json( ) , json!( { "ok" : true } ) ) ;
285
+ assert ! ( !is_yanked( & app) ) ;
256
286
}
257
287
258
288
#[ test]
259
289
fn token_user_with_correct_wildcard_crate_scope ( ) {
260
- let ( _ , _, client) = prepare ( ) ;
290
+ let ( app , _, client) = prepare ( ) ;
261
291
let wildcard = format ! ( "{}*" , CRATE_NAME . chars( ) . next( ) . unwrap( ) ) ;
262
292
let client = client. db_new_scoped_token (
263
293
"test-token" ,
@@ -269,15 +299,17 @@ mod auth {
269
299
let response = client. yank ( CRATE_NAME , CRATE_VERSION ) ;
270
300
assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
271
301
assert_eq ! ( response. into_json( ) , json!( { "ok" : true } ) ) ;
302
+ assert ! ( is_yanked( & app) ) ;
272
303
273
304
let response = client. unyank ( CRATE_NAME , CRATE_VERSION ) ;
274
305
assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
275
306
assert_eq ! ( response. into_json( ) , json!( { "ok" : true } ) ) ;
307
+ assert ! ( !is_yanked( & app) ) ;
276
308
}
277
309
278
310
#[ test]
279
311
fn token_user_with_incorrect_crate_scope ( ) {
280
- let ( _ , _, client) = prepare ( ) ;
312
+ let ( app , _, client) = prepare ( ) ;
281
313
let client = client. db_new_scoped_token (
282
314
"test-token" ,
283
315
Some ( vec ! [ CrateScope :: try_from( "foo" ) . unwrap( ) ] ) ,
@@ -291,18 +323,20 @@ mod auth {
291
323
response. into_json( ) ,
292
324
json!( { "errors" : [ { "detail" : "must be logged in to perform that action" } ] } )
293
325
) ;
326
+ assert ! ( !is_yanked( & app) ) ;
294
327
295
328
let response = client. unyank ( CRATE_NAME , CRATE_VERSION ) ;
296
329
assert_eq ! ( response. status( ) , StatusCode :: FORBIDDEN ) ;
297
330
assert_eq ! (
298
331
response. into_json( ) ,
299
332
json!( { "errors" : [ { "detail" : "must be logged in to perform that action" } ] } )
300
333
) ;
334
+ assert ! ( !is_yanked( & app) ) ;
301
335
}
302
336
303
337
#[ test]
304
338
fn token_user_with_incorrect_wildcard_crate_scope ( ) {
305
- let ( _ , _, client) = prepare ( ) ;
339
+ let ( app , _, client) = prepare ( ) ;
306
340
let client = client. db_new_scoped_token (
307
341
"test-token" ,
308
342
Some ( vec ! [ CrateScope :: try_from( "foo*" ) . unwrap( ) ] ) ,
@@ -316,12 +350,14 @@ mod auth {
316
350
response. into_json( ) ,
317
351
json!( { "errors" : [ { "detail" : "must be logged in to perform that action" } ] } )
318
352
) ;
353
+ assert ! ( !is_yanked( & app) ) ;
319
354
320
355
let response = client. unyank ( CRATE_NAME , CRATE_VERSION ) ;
321
356
assert_eq ! ( response. status( ) , StatusCode :: FORBIDDEN ) ;
322
357
assert_eq ! (
323
358
response. into_json( ) ,
324
359
json!( { "errors" : [ { "detail" : "must be logged in to perform that action" } ] } )
325
360
) ;
361
+ assert ! ( !is_yanked( & app) ) ;
326
362
}
327
363
}
0 commit comments