Skip to content

Commit febdf7f

Browse files
committed
tests/routes/crates/versions/yank_unyank: Assert yank status in the database too
1 parent c9f37d1 commit febdf7f

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

src/tests/routes/crates/versions/yank_unyank.rs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ mod auth {
9797
use crate::util::{MockAnonymousUser, MockCookieUser};
9898
use chrono::{Duration, Utc};
9999
use crates_io::models::token::{CrateScope, EndpointScope};
100+
use crates_io::schema::{crates, versions};
101+
use diesel::prelude::*;
100102

101103
const CRATE_NAME: &str = "fyk";
102104
const CRATE_VERSION: &str = "1.0.0";
@@ -110,74 +112,94 @@ mod auth {
110112
(app, anon, cookie)
111113
}
112114

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+
113127
#[test]
114128
fn unauthenticated() {
115-
let (_, client, _) = prepare();
129+
let (app, client, _) = prepare();
116130

117131
let response = client.yank(CRATE_NAME, CRATE_VERSION);
118132
assert_eq!(response.status(), StatusCode::FORBIDDEN);
119133
assert_eq!(
120134
response.into_json(),
121135
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
122136
);
137+
assert!(!is_yanked(&app));
123138

124139
let response = client.unyank(CRATE_NAME, CRATE_VERSION);
125140
assert_eq!(response.status(), StatusCode::FORBIDDEN);
126141
assert_eq!(
127142
response.into_json(),
128143
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
129144
);
145+
assert!(!is_yanked(&app));
130146
}
131147

132148
#[test]
133149
fn cookie_user() {
134-
let (_, _, client) = prepare();
150+
let (app, _, client) = prepare();
135151

136152
let response = client.yank(CRATE_NAME, CRATE_VERSION);
137153
assert_eq!(response.status(), StatusCode::OK);
138154
assert_eq!(response.into_json(), json!({ "ok": true }));
155+
assert!(is_yanked(&app));
139156

140157
let response = client.unyank(CRATE_NAME, CRATE_VERSION);
141158
assert_eq!(response.status(), StatusCode::OK);
142159
assert_eq!(response.into_json(), json!({ "ok": true }));
160+
assert!(!is_yanked(&app));
143161
}
144162

145163
#[test]
146164
fn token_user() {
147-
let (_, _, client) = prepare();
165+
let (app, _, client) = prepare();
148166
let client = client.db_new_token("test-token");
149167

150168
let response = client.yank(CRATE_NAME, CRATE_VERSION);
151169
assert_eq!(response.status(), StatusCode::OK);
152170
assert_eq!(response.into_json(), json!({ "ok": true }));
171+
assert!(is_yanked(&app));
153172

154173
let response = client.unyank(CRATE_NAME, CRATE_VERSION);
155174
assert_eq!(response.status(), StatusCode::OK);
156175
assert_eq!(response.into_json(), json!({ "ok": true }));
176+
assert!(!is_yanked(&app));
157177
}
158178

159179
#[test]
160180
fn token_user_not_expired() {
161181
let expired_at = Utc::now() + Duration::days(7);
162182

163-
let (_, _, client) = prepare();
183+
let (app, _, client) = prepare();
164184
let client =
165185
client.db_new_scoped_token("test-token", None, None, Some(expired_at.naive_utc()));
166186

167187
let response = client.yank(CRATE_NAME, CRATE_VERSION);
168188
assert_eq!(response.status(), StatusCode::OK);
169189
assert_eq!(response.into_json(), json!({ "ok": true }));
190+
assert!(is_yanked(&app));
170191

171192
let response = client.unyank(CRATE_NAME, CRATE_VERSION);
172193
assert_eq!(response.status(), StatusCode::OK);
173194
assert_eq!(response.into_json(), json!({ "ok": true }));
195+
assert!(!is_yanked(&app));
174196
}
175197

176198
#[test]
177199
fn token_user_expired() {
178200
let expired_at = Utc::now() - Duration::days(7);
179201

180-
let (_, _, client) = prepare();
202+
let (app, _, client) = prepare();
181203
let client =
182204
client.db_new_scoped_token("test-token", None, None, Some(expired_at.naive_utc()));
183205

@@ -187,33 +209,37 @@ mod auth {
187209
response.into_json(),
188210
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
189211
);
212+
assert!(!is_yanked(&app));
190213

191214
let response = client.unyank(CRATE_NAME, CRATE_VERSION);
192215
assert_eq!(response.status(), StatusCode::FORBIDDEN);
193216
assert_eq!(
194217
response.into_json(),
195218
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
196219
);
220+
assert!(!is_yanked(&app));
197221
}
198222

199223
#[test]
200224
fn token_user_with_correct_endpoint_scope() {
201-
let (_, _, client) = prepare();
225+
let (app, _, client) = prepare();
202226
let client =
203227
client.db_new_scoped_token("test-token", None, Some(vec![EndpointScope::Yank]), None);
204228

205229
let response = client.yank(CRATE_NAME, CRATE_VERSION);
206230
assert_eq!(response.status(), StatusCode::OK);
207231
assert_eq!(response.into_json(), json!({ "ok": true }));
232+
assert!(is_yanked(&app));
208233

209234
let response = client.unyank(CRATE_NAME, CRATE_VERSION);
210235
assert_eq!(response.status(), StatusCode::OK);
211236
assert_eq!(response.into_json(), json!({ "ok": true }));
237+
assert!(!is_yanked(&app));
212238
}
213239

214240
#[test]
215241
fn token_user_with_incorrect_endpoint_scope() {
216-
let (_, _, client) = prepare();
242+
let (app, _, client) = prepare();
217243
let client = client.db_new_scoped_token(
218244
"test-token",
219245
None,
@@ -227,18 +253,20 @@ mod auth {
227253
response.into_json(),
228254
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
229255
);
256+
assert!(!is_yanked(&app));
230257

231258
let response = client.unyank(CRATE_NAME, CRATE_VERSION);
232259
assert_eq!(response.status(), StatusCode::FORBIDDEN);
233260
assert_eq!(
234261
response.into_json(),
235262
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
236263
);
264+
assert!(!is_yanked(&app));
237265
}
238266

239267
#[test]
240268
fn token_user_with_correct_crate_scope() {
241-
let (_, _, client) = prepare();
269+
let (app, _, client) = prepare();
242270
let client = client.db_new_scoped_token(
243271
"test-token",
244272
Some(vec![CrateScope::try_from(CRATE_NAME).unwrap()]),
@@ -249,15 +277,17 @@ mod auth {
249277
let response = client.yank(CRATE_NAME, CRATE_VERSION);
250278
assert_eq!(response.status(), StatusCode::OK);
251279
assert_eq!(response.into_json(), json!({ "ok": true }));
280+
assert!(is_yanked(&app));
252281

253282
let response = client.unyank(CRATE_NAME, CRATE_VERSION);
254283
assert_eq!(response.status(), StatusCode::OK);
255284
assert_eq!(response.into_json(), json!({ "ok": true }));
285+
assert!(!is_yanked(&app));
256286
}
257287

258288
#[test]
259289
fn token_user_with_correct_wildcard_crate_scope() {
260-
let (_, _, client) = prepare();
290+
let (app, _, client) = prepare();
261291
let wildcard = format!("{}*", CRATE_NAME.chars().next().unwrap());
262292
let client = client.db_new_scoped_token(
263293
"test-token",
@@ -269,15 +299,17 @@ mod auth {
269299
let response = client.yank(CRATE_NAME, CRATE_VERSION);
270300
assert_eq!(response.status(), StatusCode::OK);
271301
assert_eq!(response.into_json(), json!({ "ok": true }));
302+
assert!(is_yanked(&app));
272303

273304
let response = client.unyank(CRATE_NAME, CRATE_VERSION);
274305
assert_eq!(response.status(), StatusCode::OK);
275306
assert_eq!(response.into_json(), json!({ "ok": true }));
307+
assert!(!is_yanked(&app));
276308
}
277309

278310
#[test]
279311
fn token_user_with_incorrect_crate_scope() {
280-
let (_, _, client) = prepare();
312+
let (app, _, client) = prepare();
281313
let client = client.db_new_scoped_token(
282314
"test-token",
283315
Some(vec![CrateScope::try_from("foo").unwrap()]),
@@ -291,18 +323,20 @@ mod auth {
291323
response.into_json(),
292324
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
293325
);
326+
assert!(!is_yanked(&app));
294327

295328
let response = client.unyank(CRATE_NAME, CRATE_VERSION);
296329
assert_eq!(response.status(), StatusCode::FORBIDDEN);
297330
assert_eq!(
298331
response.into_json(),
299332
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
300333
);
334+
assert!(!is_yanked(&app));
301335
}
302336

303337
#[test]
304338
fn token_user_with_incorrect_wildcard_crate_scope() {
305-
let (_, _, client) = prepare();
339+
let (app, _, client) = prepare();
306340
let client = client.db_new_scoped_token(
307341
"test-token",
308342
Some(vec![CrateScope::try_from("foo*").unwrap()]),
@@ -316,12 +350,14 @@ mod auth {
316350
response.into_json(),
317351
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
318352
);
353+
assert!(!is_yanked(&app));
319354

320355
let response = client.unyank(CRATE_NAME, CRATE_VERSION);
321356
assert_eq!(response.status(), StatusCode::FORBIDDEN);
322357
assert_eq!(
323358
response.into_json(),
324359
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
325360
);
361+
assert!(!is_yanked(&app));
326362
}
327363
}

0 commit comments

Comments
 (0)