Skip to content

Use insta for API tests #2949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ url = "2.1"
conduit-test = "0.9.0-alpha.3"
diesel_migrations = { version = "1.3.0", features = ["postgres"] }
hyper-tls = "0.4"
insta = { version = "1.1.0", features = ["redactions"] }
lazy_static = "1.0"
tokio = { version = "0.2", default-features = false, features = ["stream"]}
tower-service = "0.3.0"
Expand Down
1 change: 1 addition & 0 deletions src/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ mod categories;
mod category;
mod dump_db;
mod git;
mod insta;
mod keyword;
mod krate;
mod owners;
Expand Down
9 changes: 9 additions & 0 deletions src/tests/insta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use chrono::DateTime;
use insta::{dynamic_redaction, internals::Redaction};

pub fn rfc3339_redaction() -> Redaction {
dynamic_redaction(|value, _| {
assert!(DateTime::parse_from_rfc3339(value.as_str().unwrap()).is_ok());
"[datetime]"
})
}
33 changes: 13 additions & 20 deletions src/tests/keyword.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
use crate::insta::rfc3339_redaction;
use crate::{builders::CrateBuilder, RequestHelper, TestApp};
use cargo_registry::{models::Keyword, views::EncodableKeyword};
use insta::assert_json_snapshot;
use serde_json::Value;

#[derive(Deserialize)]
struct KeywordList {
keywords: Vec<EncodableKeyword>,
meta: KeywordMeta,
}
#[derive(Deserialize)]
struct KeywordMeta {
total: i32,
}
#[derive(Deserialize)]
struct GoodKeyword {
keyword: EncodableKeyword,
Expand All @@ -19,18 +13,15 @@ struct GoodKeyword {
fn index() {
let url = "/api/v1/keywords";
let (app, anon) = TestApp::init().empty();
let json: KeywordList = anon.get(url).good();
assert_eq!(json.keywords.len(), 0);
assert_eq!(json.meta.total, 0);
let json: Value = anon.get(url).good();
assert_json_snapshot!(json);

app.db(|conn| {
Keyword::find_or_create_all(conn, &["foo"]).unwrap();
});

let json: KeywordList = anon.get(url).good();
assert_eq!(json.keywords.len(), 1);
assert_eq!(json.meta.total, 1);
assert_eq!(json.keywords[0].keyword.as_str(), "foo");
let json: Value = anon.get(url).good();
assert_json_snapshot!(json, { ".**.created_at" => rfc3339_redaction() });
}

#[test]
Expand All @@ -42,8 +33,9 @@ fn show() {
app.db(|conn| {
Keyword::find_or_create_all(conn, &["foo"]).unwrap();
});
let json: GoodKeyword = anon.get(url).good();
assert_eq!(json.keyword.keyword.as_str(), "foo");

let json: Value = anon.get(url).good();
assert_json_snapshot!(json, { ".**.created_at" => rfc3339_redaction() });
}

#[test]
Expand All @@ -55,8 +47,9 @@ fn uppercase() {
app.db(|conn| {
Keyword::find_or_create_all(conn, &["UPPER"]).unwrap();
});
let json: GoodKeyword = anon.get(url).good();
assert_eq!(json.keyword.keyword.as_str(), "upper");

let json: Value = anon.get(url).good();
assert_json_snapshot!(json, { ".**.created_at" => rfc3339_redaction() });
}

#[test]
Expand Down
53 changes: 11 additions & 42 deletions src/tests/krate.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use crate::{
builders::{CrateBuilder, DependencyBuilder, PublishBuilder, VersionBuilder},
insta::rfc3339_redaction,
new_category, new_dependency, new_user, CrateMeta, CrateResponse, GoodCrate, OkBool,
RequestHelper, TestApp,
};
use cargo_registry::{
models::{krate::MAX_NAME_LENGTH, Category, Crate},
schema::{api_tokens, crates, emails, metadata, versions, versions_published_by},
views::{
EncodableCategory, EncodableCrate, EncodableDependency, EncodableKeyword, EncodableVersion,
EncodableVersionDownload,
},
views::{EncodableDependency, EncodableVersion, EncodableVersionDownload},
};
use std::{
collections::HashMap,
Expand All @@ -22,6 +20,8 @@ use chrono::Utc;
use conduit::StatusCode;
use diesel::{dsl::*, prelude::*, update};
use flate2::{write::GzEncoder, Compression};
use insta::assert_json_snapshot;
use serde_json::Value;

#[derive(Deserialize)]
struct VersionsList {
Expand All @@ -42,18 +42,6 @@ struct Downloads {
version_downloads: Vec<EncodableVersionDownload>,
}

#[derive(Deserialize)]
struct SummaryResponse {
num_downloads: i64,
num_crates: i64,
new_crates: Vec<EncodableCrate>,
most_downloaded: Vec<EncodableCrate>,
most_recently_downloaded: Vec<EncodableCrate>,
just_updated: Vec<EncodableCrate>,
popular_keywords: Vec<EncodableKeyword>,
popular_categories: Vec<EncodableCategory>,
}

impl crate::util::MockAnonymousUser {
fn reverse_dependencies(&self, krate_name: &str) -> RevDeps {
let url = format!("/api/v1/crates/{}/reverse_dependencies", krate_name);
Expand Down Expand Up @@ -1330,7 +1318,8 @@ fn new_krate_records_verified_email() {
#[test]
fn summary_doesnt_die() {
let (_, anon) = TestApp::init().empty();
anon.get::<SummaryResponse>("/api/v1/summary").good();
let json: Value = anon.get("/api/v1/summary").good();
assert_json_snapshot!(json);
}

#[test]
Expand Down Expand Up @@ -1403,31 +1392,11 @@ fn summary_new_crates() {
.unwrap();
});

let json: SummaryResponse = anon.get("/api/v1/summary").good();

assert_eq!(json.num_crates, 5);
assert_eq!(json.num_downloads, 6000);
assert_eq!(json.most_downloaded[0].name, "most_recent_downloads");
assert_eq!(json.most_downloaded[0].downloads, 5000);
assert_eq!(json.most_downloaded[0].recent_downloads, Some(50));
assert_eq!(
json.most_recently_downloaded[0].name,
"most_recent_downloads"
);
assert_eq!(json.most_recently_downloaded[0].recent_downloads, Some(50));
assert_eq!(json.popular_keywords[0].keyword, "popular");
assert_eq!(json.popular_categories[0].category, "Category 1");
assert_eq!(json.just_updated.len(), 2);

assert_eq!(json.just_updated[0].name, "just_updated_patch");
assert_eq!(json.just_updated[0].max_version, "0.2.0");
assert_eq!(json.just_updated[0].newest_version, "0.1.1");

assert_eq!(json.just_updated[1].name, "just_updated");
assert_eq!(json.just_updated[1].max_version, "0.1.2");
assert_eq!(json.just_updated[1].newest_version, "0.1.2");

assert_eq!(json.new_crates.len(), 5);
let json: Value = anon.get("/api/v1/summary").good();
assert_json_snapshot!(json, {
".**.created_at" => rfc3339_redaction(),
".**.updated_at" => rfc3339_redaction(),
});
}

#[test]
Expand Down
17 changes: 17 additions & 0 deletions src/tests/snapshots/all__keyword__index-2.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: src/tests/keyword.rs
expression: json
---
{
"keywords": [
{
"crates_cnt": 0,
"created_at": "[datetime]",
"id": "foo",
"keyword": "foo"
}
],
"meta": {
"total": 1
}
}
10 changes: 10 additions & 0 deletions src/tests/snapshots/all__keyword__index.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: src/tests/keyword.rs
expression: json
---
{
"keywords": [],
"meta": {
"total": 0
}
}
12 changes: 12 additions & 0 deletions src/tests/snapshots/all__keyword__show.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: src/tests/keyword.rs
expression: json
---
{
"keyword": {
"crates_cnt": 0,
"created_at": "[datetime]",
"id": "foo",
"keyword": "foo"
}
}
12 changes: 12 additions & 0 deletions src/tests/snapshots/all__keyword__uppercase.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: src/tests/keyword.rs
expression: json
---
{
"keyword": {
"crates_cnt": 0,
"created_at": "[datetime]",
"id": "upper",
"keyword": "upper"
}
}
Loading