Skip to content

Commit daba1e6

Browse files
committed
Add TestApp::init() for tests that don't need HTTP recording
The unused `Uploader::NoOp` option is now `Uploader::Panic` which panics on any usage. The panic message directs users to initialize the app with `TestApp::with_proxy()`. While this adds more panics to the main lib, the application configuration isn't dynamic and it should be obvious that `Uploader::Panic` is not to be used to configure a production application.
1 parent 26ab58e commit daba1e6

File tree

11 files changed

+144
-104
lines changed

11 files changed

+144
-104
lines changed

src/app.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ impl App {
103103
/// Returns a client for making HTTP requests to upload crate files.
104104
///
105105
/// The handle will go through a proxy if the uploader being used has specified one, which
106-
/// is only done in test mode in order to be able to record and inspect the HTTP requests
107-
/// that tests make.
106+
/// is only done in tests with `TestApp::with_proxy()` in order to be able to record and
107+
/// inspect the HTTP requests that tests make.
108108
pub fn http_client(&self) -> CargoResult<reqwest::Client> {
109109
let mut builder = reqwest::Client::builder();
110110
if let Some(proxy) = self.config.uploader.proxy() {

src/tests/all.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,26 +135,30 @@ fn app() -> (
135135
conduit_middleware::MiddlewareBuilder,
136136
) {
137137
dotenv::dotenv().ok();
138-
git::init();
139138

140139
let (proxy, bomb) = record::proxy();
141-
142-
// When testing we route all API traffic over HTTP so we can
143-
// sniff/record it, but everywhere else we use https
144-
let api_protocol = String::from("http");
145-
146140
let uploader = cargo_registry::Uploader::S3 {
147141
bucket: s3::Bucket::new(
148142
String::from("alexcrichton-test"),
149143
None,
150144
std::env::var("S3_ACCESS_KEY").unwrap_or_default(),
151145
std::env::var("S3_SECRET_KEY").unwrap_or_default(),
152-
&api_protocol,
146+
// When testing we route all API traffic over HTTP so we can
147+
// sniff/record it, but everywhere else we use https
148+
"http",
153149
),
154150
proxy: Some(proxy),
155151
cdn: None,
156152
};
157153

154+
let (app, handler) = simple_app(uploader);
155+
(bomb, app, handler)
156+
}
157+
158+
fn simple_app(
159+
uploader: cargo_registry::Uploader,
160+
) -> (Arc<App>, conduit_middleware::MiddlewareBuilder) {
161+
git::init();
158162
let config = cargo_registry::Config {
159163
uploader,
160164
session_key: "test this has to be over 32 bytes long".to_string(),
@@ -166,13 +170,15 @@ fn app() -> (
166170
max_upload_size: 3000,
167171
max_unpack_size: 2000,
168172
mirror: Replica::Primary,
169-
api_protocol,
173+
// When testing we route all API traffic over HTTP so we can
174+
// sniff/record it, but everywhere else we use https
175+
api_protocol: String::from("http"),
170176
};
171177
let app = App::new(&config);
172178
t!(t!(app.diesel_database.get()).begin_test_transaction());
173179
let app = Arc::new(app);
174180
let handler = cargo_registry::build_handler(Arc::clone(&app));
175-
(bomb, app, handler)
181+
(app, handler)
176182
}
177183

178184
// Return the environment variable only if it has been defined

src/tests/category.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct CategoryWithSubcategories {
2424

2525
#[test]
2626
fn index() {
27-
let (app, anon) = TestApp::empty();
27+
let (app, anon) = TestApp::init().empty();
2828
let url = "/api/v1/categories";
2929

3030
// List 0 categories if none exist
@@ -51,7 +51,7 @@ fn index() {
5151

5252
#[test]
5353
fn show() {
54-
let (app, anon) = TestApp::empty();
54+
let (app, anon) = TestApp::init().empty();
5555
let url = "/api/v1/categories/foo-bar";
5656

5757
// Return not found if a category doesn't exist
@@ -161,7 +161,7 @@ fn update_crate() {
161161

162162
#[test]
163163
fn category_slugs_returns_all_slugs_in_alphabetical_order() {
164-
let (app, anon) = TestApp::empty();
164+
let (app, anon) = TestApp::init().empty();
165165
app.db(|conn| {
166166
new_category("Foo", "foo", "For crates that foo")
167167
.create_or_update(&conn)

src/tests/keyword.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct GoodKeyword {
1919
#[test]
2020
fn index() {
2121
let url = "/api/v1/keywords";
22-
let (app, anon) = TestApp::empty();
22+
let (app, anon) = TestApp::init().empty();
2323
let json: KeywordList = anon.get(url).good();
2424
assert_eq!(json.keywords.len(), 0);
2525
assert_eq!(json.meta.total, 0);
@@ -37,7 +37,7 @@ fn index() {
3737
#[test]
3838
fn show() {
3939
let url = "/api/v1/keywords/foo";
40-
let (app, anon) = TestApp::empty();
40+
let (app, anon) = TestApp::init().empty();
4141
anon.get(url).assert_not_found();
4242

4343
app.db(|conn| {
@@ -50,7 +50,7 @@ fn show() {
5050
#[test]
5151
fn uppercase() {
5252
let url = "/api/v1/keywords/UPPER";
53-
let (app, anon) = TestApp::empty();
53+
let (app, anon) = TestApp::init().empty();
5454
anon.get(url).assert_not_found();
5555

5656
app.db(|conn| {
@@ -62,7 +62,7 @@ fn uppercase() {
6262

6363
#[test]
6464
fn update_crate() {
65-
let (app, anon) = TestApp::empty();
65+
let (app, anon) = TestApp::init().empty();
6666
let cnt = |kw: &str| {
6767
let json: GoodKeyword = anon.get(&format!("/api/v1/keywords/{}", kw)).good();
6868
json.keyword.crates_cnt as usize

src/tests/krate.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use {
3434
new_crate_to_body_with_tarball, new_dependency, new_req, new_req_body_version_2, new_req_full,
3535
new_req_with_badges, new_req_with_categories, new_req_with_documentation,
3636
new_req_with_keywords, new_user, new_version, req, sign_in, sign_in_as, Bad, CrateBuilder,
37-
CrateMeta, CrateResponse, GoodCrate, OkBool, PublishBuilder, RequestHelper, TestApp,
37+
CrateList, CrateMeta, CrateResponse, GoodCrate, OkBool, PublishBuilder, RequestHelper, TestApp,
3838
VersionBuilder,
3939
};
4040

@@ -79,8 +79,9 @@ impl ::util::MockTokenUser {
7979

8080
#[test]
8181
fn index() {
82-
let (app, anon) = TestApp::empty();
83-
let json = anon.search("");
82+
let url = "/api/v1/crates";
83+
let (app, anon) = TestApp::init().empty();
84+
let json: CrateList = anon.get(url).good();
8485
assert_eq!(json.crates.len(), 0);
8586
assert_eq!(json.meta.total, 0);
8687

@@ -98,7 +99,7 @@ fn index() {
9899

99100
#[test]
100101
fn index_queries() {
101-
let (app, anon, user) = TestApp::with_user();
102+
let (app, anon, user) = TestApp::init().with_user();
102103
let user = user.as_model();
103104

104105
let (krate, krate2) = app.db(|conn| {
@@ -179,7 +180,7 @@ fn index_queries() {
179180

180181
#[test]
181182
fn search_includes_crates_where_name_is_stopword() {
182-
let (app, anon, user) = TestApp::with_user();
183+
let (app, anon, user) = TestApp::init().with_user();
183184
let user = user.as_model();
184185
app.db(|conn| {
185186
CrateBuilder::new("which", user.id).expect_build(conn);
@@ -194,7 +195,7 @@ fn search_includes_crates_where_name_is_stopword() {
194195

195196
#[test]
196197
fn exact_match_first_on_queries() {
197-
let (app, anon, user) = TestApp::with_user();
198+
let (app, anon, user) = TestApp::init().with_user();
198199
let user = user.as_model();
199200

200201
app.db(|conn| {
@@ -236,7 +237,7 @@ fn exact_match_first_on_queries() {
236237

237238
#[test]
238239
fn index_sorting() {
239-
let (app, anon, user) = TestApp::with_user();
240+
let (app, anon, user) = TestApp::init().with_user();
240241
let user = user.as_model();
241242

242243
app.db(|conn| {
@@ -318,7 +319,7 @@ fn index_sorting() {
318319

319320
#[test]
320321
fn exact_match_on_queries_with_sort() {
321-
let (app, anon, user) = TestApp::with_user();
322+
let (app, anon, user) = TestApp::init().with_user();
322323
let user = user.as_model();
323324

324325
app.db(|conn| {
@@ -416,7 +417,7 @@ fn exact_match_on_queries_with_sort() {
416417

417418
#[test]
418419
fn show() {
419-
let (app, anon, user) = TestApp::with_user();
420+
let (app, anon, user) = TestApp::init().with_user();
420421
let user = user.as_model();
421422

422423
let krate = app.db(|conn| {
@@ -463,7 +464,7 @@ fn show() {
463464

464465
#[test]
465466
fn yanked_versions_are_not_considered_for_max_version() {
466-
let (app, anon, user) = TestApp::with_user();
467+
let (app, anon, user) = TestApp::init().with_user();
467468
let user = user.as_model();
468469

469470
app.db(|conn| {
@@ -481,7 +482,7 @@ fn yanked_versions_are_not_considered_for_max_version() {
481482

482483
#[test]
483484
fn versions() {
484-
let (app, anon) = TestApp::empty();
485+
let (app, anon) = TestApp::init().empty();
485486
app.db(|conn| {
486487
let u = new_user("foo").create_or_update(conn).unwrap();
487488
CrateBuilder::new("foo_versions", u.id)
@@ -854,7 +855,7 @@ fn valid_feature_names() {
854855

855856
#[test]
856857
fn new_krate_too_big() {
857-
let (_, _, user) = TestApp::with_user();
858+
let (_, _, user) = TestApp::init().with_user();
858859
let files = [("foo_big-1.0.0/big", &[b'a'; 2000] as &[_])];
859860
let builder = PublishBuilder::new("foo_big").files(&files);
860861

@@ -1103,13 +1104,13 @@ fn new_krate_with_readme() {
11031104

11041105
#[test]
11051106
fn summary_doesnt_die() {
1106-
let (_, anon) = TestApp::empty();
1107+
let (_, anon) = TestApp::init().empty();
11071108
anon.get::<SummaryResponse>("/api/v1/summary").good();
11081109
}
11091110

11101111
#[test]
11111112
fn summary_new_crates() {
1112-
let (app, anon, user) = TestApp::with_user();
1113+
let (app, anon, user) = TestApp::init().with_user();
11131114
let user = user.as_model();
11141115
app.db(|conn| {
11151116
let krate = CrateBuilder::new("some_downloads", user.id)
@@ -1176,7 +1177,7 @@ fn summary_new_crates() {
11761177
#[test]
11771178
fn download() {
11781179
use chrono::{Duration, Utc};
1179-
let (app, anon, user) = TestApp::with_user();
1180+
let (app, anon, user) = TestApp::with_proxy().with_user();
11801181
let user = user.as_model();
11811182

11821183
app.db(|conn| {
@@ -1260,7 +1261,7 @@ fn dependencies() {
12601261

12611262
#[test]
12621263
fn diesel_not_found_results_in_404() {
1263-
let (_, _, user) = TestApp::with_user();
1264+
let (_, _, user) = TestApp::init().with_user();
12641265

12651266
user.get("/api/v1/crates/foo_following/following")
12661267
.assert_not_found();
@@ -1269,7 +1270,7 @@ fn diesel_not_found_results_in_404() {
12691270
#[test]
12701271
fn following() {
12711272
// TODO: Test anon requests as well?
1272-
let (app, _, user) = TestApp::with_user();
1273+
let (app, _, user) = TestApp::init().with_user();
12731274

12741275
app.db(|conn| {
12751276
CrateBuilder::new("foo_following", user.as_model().id).expect_build(&conn);
@@ -1399,7 +1400,7 @@ fn yank() {
13991400

14001401
#[test]
14011402
fn yank_not_owner() {
1402-
let (app, _, _, token) = TestApp::with_token();
1403+
let (app, _, _, token) = TestApp::init().with_token();
14031404
app.db(|conn| {
14041405
let another_user = new_user("bar").create_or_update(conn).unwrap();
14051406
CrateBuilder::new("foo_not", another_user.id).expect_build(conn);
@@ -2019,7 +2020,7 @@ fn author_license_and_description_required() {
20192020
*/
20202021
#[test]
20212022
fn test_recent_download_count() {
2022-
let (app, anon, user) = TestApp::with_user();
2023+
let (app, anon, user) = TestApp::init().with_user();
20232024
let user = user.as_model();
20242025

20252026
app.db(|conn| {
@@ -2057,7 +2058,7 @@ fn test_recent_download_count() {
20572058
*/
20582059
#[test]
20592060
fn test_zero_downloads() {
2060-
let (app, anon, user) = TestApp::with_user();
2061+
let (app, anon, user) = TestApp::init().with_user();
20612062
let user = user.as_model();
20622063

20632064
app.db(|conn| {
@@ -2082,7 +2083,7 @@ fn test_zero_downloads() {
20822083
*/
20832084
#[test]
20842085
fn test_default_sort_recent() {
2085-
let (app, anon, user) = TestApp::with_user();
2086+
let (app, anon, user) = TestApp::init().with_user();
20862087
let user = user.as_model();
20872088

20882089
let (green_crate, potato_crate) = app.db(|conn| {
@@ -2145,7 +2146,7 @@ fn test_default_sort_recent() {
21452146

21462147
#[test]
21472148
fn block_bad_documentation_url() {
2148-
let (app, anon, user) = TestApp::with_user();
2149+
let (app, anon, user) = TestApp::init().with_user();
21492150
let user = user.as_model();
21502151

21512152
app.db(|conn| {
@@ -2163,7 +2164,7 @@ fn block_bad_documentation_url() {
21632164
// which call the `PUT /crates/:crate_id/owners` route
21642165
#[test]
21652166
fn test_cargo_invite_owners() {
2166-
let (app, _, owner) = TestApp::with_user();
2167+
let (app, _, owner) = TestApp::init().with_user();
21672168

21682169
let new_user = app.db_new_user("cilantro");
21692170
app.db(|conn| {

src/tests/owners.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl ::util::MockCookieUser {
6363

6464
#[test]
6565
fn new_crate_owner() {
66-
let (app, _, user1, token) = TestApp::with_token();
66+
let (app, _, user1, token) = TestApp::with_proxy().with_token();
6767

6868
// Create a crate under one user
6969
let crate_to_publish = PublishBuilder::new("foo_owner").version("1.0.0");
@@ -203,7 +203,7 @@ fn owners_can_remove_self() {
203203
*/
204204
#[test]
205205
fn check_ownership_two_crates() {
206-
let (app, anon, user) = TestApp::with_user();
206+
let (app, anon, user) = TestApp::init().with_user();
207207
let user = user.as_model();
208208

209209
let (krate_owned_by_team, team) = app.db(|conn| {

0 commit comments

Comments
 (0)