Skip to content

Commit 958ee4f

Browse files
Merge #1519
1519: Add `TestApp::simple()` for tests that don't need HTTP recording r=jtgeibel a=jtgeibel 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. Co-authored-by: Justin Geibel <jtgeibel@gmail.com>
2 parents 26ab58e + 15a4109 commit 958ee4f

File tree

11 files changed

+141
-102
lines changed

11 files changed

+141
-102
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: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl ::util::MockTokenUser {
7979

8080
#[test]
8181
fn index() {
82-
let (app, anon) = TestApp::empty();
82+
let (app, anon) = TestApp::init().empty();
8383
let json = anon.search("");
8484
assert_eq!(json.crates.len(), 0);
8585
assert_eq!(json.meta.total, 0);
@@ -98,7 +98,7 @@ fn index() {
9898

9999
#[test]
100100
fn index_queries() {
101-
let (app, anon, user) = TestApp::with_user();
101+
let (app, anon, user) = TestApp::init().with_user();
102102
let user = user.as_model();
103103

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

180180
#[test]
181181
fn search_includes_crates_where_name_is_stopword() {
182-
let (app, anon, user) = TestApp::with_user();
182+
let (app, anon, user) = TestApp::init().with_user();
183183
let user = user.as_model();
184184
app.db(|conn| {
185185
CrateBuilder::new("which", user.id).expect_build(conn);
@@ -194,7 +194,7 @@ fn search_includes_crates_where_name_is_stopword() {
194194

195195
#[test]
196196
fn exact_match_first_on_queries() {
197-
let (app, anon, user) = TestApp::with_user();
197+
let (app, anon, user) = TestApp::init().with_user();
198198
let user = user.as_model();
199199

200200
app.db(|conn| {
@@ -236,7 +236,7 @@ fn exact_match_first_on_queries() {
236236

237237
#[test]
238238
fn index_sorting() {
239-
let (app, anon, user) = TestApp::with_user();
239+
let (app, anon, user) = TestApp::init().with_user();
240240
let user = user.as_model();
241241

242242
app.db(|conn| {
@@ -318,7 +318,7 @@ fn index_sorting() {
318318

319319
#[test]
320320
fn exact_match_on_queries_with_sort() {
321-
let (app, anon, user) = TestApp::with_user();
321+
let (app, anon, user) = TestApp::init().with_user();
322322
let user = user.as_model();
323323

324324
app.db(|conn| {
@@ -416,7 +416,7 @@ fn exact_match_on_queries_with_sort() {
416416

417417
#[test]
418418
fn show() {
419-
let (app, anon, user) = TestApp::with_user();
419+
let (app, anon, user) = TestApp::init().with_user();
420420
let user = user.as_model();
421421

422422
let krate = app.db(|conn| {
@@ -463,7 +463,7 @@ fn show() {
463463

464464
#[test]
465465
fn yanked_versions_are_not_considered_for_max_version() {
466-
let (app, anon, user) = TestApp::with_user();
466+
let (app, anon, user) = TestApp::init().with_user();
467467
let user = user.as_model();
468468

469469
app.db(|conn| {
@@ -481,7 +481,7 @@ fn yanked_versions_are_not_considered_for_max_version() {
481481

482482
#[test]
483483
fn versions() {
484-
let (app, anon) = TestApp::empty();
484+
let (app, anon) = TestApp::init().empty();
485485
app.db(|conn| {
486486
let u = new_user("foo").create_or_update(conn).unwrap();
487487
CrateBuilder::new("foo_versions", u.id)
@@ -854,7 +854,7 @@ fn valid_feature_names() {
854854

855855
#[test]
856856
fn new_krate_too_big() {
857-
let (_, _, user) = TestApp::with_user();
857+
let (_, _, user) = TestApp::init().with_user();
858858
let files = [("foo_big-1.0.0/big", &[b'a'; 2000] as &[_])];
859859
let builder = PublishBuilder::new("foo_big").files(&files);
860860

@@ -1103,13 +1103,13 @@ fn new_krate_with_readme() {
11031103

11041104
#[test]
11051105
fn summary_doesnt_die() {
1106-
let (_, anon) = TestApp::empty();
1106+
let (_, anon) = TestApp::init().empty();
11071107
anon.get::<SummaryResponse>("/api/v1/summary").good();
11081108
}
11091109

11101110
#[test]
11111111
fn summary_new_crates() {
1112-
let (app, anon, user) = TestApp::with_user();
1112+
let (app, anon, user) = TestApp::init().with_user();
11131113
let user = user.as_model();
11141114
app.db(|conn| {
11151115
let krate = CrateBuilder::new("some_downloads", user.id)
@@ -1176,7 +1176,7 @@ fn summary_new_crates() {
11761176
#[test]
11771177
fn download() {
11781178
use chrono::{Duration, Utc};
1179-
let (app, anon, user) = TestApp::with_user();
1179+
let (app, anon, user) = TestApp::with_proxy().with_user();
11801180
let user = user.as_model();
11811181

11821182
app.db(|conn| {
@@ -1260,7 +1260,7 @@ fn dependencies() {
12601260

12611261
#[test]
12621262
fn diesel_not_found_results_in_404() {
1263-
let (_, _, user) = TestApp::with_user();
1263+
let (_, _, user) = TestApp::init().with_user();
12641264

12651265
user.get("/api/v1/crates/foo_following/following")
12661266
.assert_not_found();
@@ -1269,7 +1269,7 @@ fn diesel_not_found_results_in_404() {
12691269
#[test]
12701270
fn following() {
12711271
// TODO: Test anon requests as well?
1272-
let (app, _, user) = TestApp::with_user();
1272+
let (app, _, user) = TestApp::init().with_user();
12731273

12741274
app.db(|conn| {
12751275
CrateBuilder::new("foo_following", user.as_model().id).expect_build(&conn);
@@ -1399,7 +1399,7 @@ fn yank() {
13991399

14001400
#[test]
14011401
fn yank_not_owner() {
1402-
let (app, _, _, token) = TestApp::with_token();
1402+
let (app, _, _, token) = TestApp::init().with_token();
14031403
app.db(|conn| {
14041404
let another_user = new_user("bar").create_or_update(conn).unwrap();
14051405
CrateBuilder::new("foo_not", another_user.id).expect_build(conn);
@@ -2019,7 +2019,7 @@ fn author_license_and_description_required() {
20192019
*/
20202020
#[test]
20212021
fn test_recent_download_count() {
2022-
let (app, anon, user) = TestApp::with_user();
2022+
let (app, anon, user) = TestApp::init().with_user();
20232023
let user = user.as_model();
20242024

20252025
app.db(|conn| {
@@ -2057,7 +2057,7 @@ fn test_recent_download_count() {
20572057
*/
20582058
#[test]
20592059
fn test_zero_downloads() {
2060-
let (app, anon, user) = TestApp::with_user();
2060+
let (app, anon, user) = TestApp::init().with_user();
20612061
let user = user.as_model();
20622062

20632063
app.db(|conn| {
@@ -2082,7 +2082,7 @@ fn test_zero_downloads() {
20822082
*/
20832083
#[test]
20842084
fn test_default_sort_recent() {
2085-
let (app, anon, user) = TestApp::with_user();
2085+
let (app, anon, user) = TestApp::init().with_user();
20862086
let user = user.as_model();
20872087

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

21462146
#[test]
21472147
fn block_bad_documentation_url() {
2148-
let (app, anon, user) = TestApp::with_user();
2148+
let (app, anon, user) = TestApp::init().with_user();
21492149
let user = user.as_model();
21502150

21512151
app.db(|conn| {
@@ -2163,7 +2163,7 @@ fn block_bad_documentation_url() {
21632163
// which call the `PUT /crates/:crate_id/owners` route
21642164
#[test]
21652165
fn test_cargo_invite_owners() {
2166-
let (app, _, owner) = TestApp::with_user();
2166+
let (app, _, owner) = TestApp::init().with_user();
21672167

21682168
let new_user = app.db_new_user("cilantro");
21692169
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)