mirror of
https://gitlab.com/keys.openpgp.org/hagrid.git
synced 2025-10-06 00:23:08 +02:00
Extract BASE_URI and BASE_URI_ONION to rstest::fixture's. Use fixture injection in tests.
Changes: - Create fixtures based on constants: - base_uri - base_uri_onion - Change tests which use BASE_URI or BASE_URI_ONION from #[test] declaration to #[rstest]. That allows to inject fixtures. - Replace usage of constants BASE_URI and BASE_URI_ONION with base_uri and base_uri_onion fixtures. There are still some usages of the constants which will be refactored later. - Propagate injected fixture values into check_* assertions to replace constant usages.
This commit is contained in:
committed by
Vincent Breitmoser
parent
f8c4871b61
commit
b11f7dc7b3
@@ -182,6 +182,9 @@ pub fn key_to_hkp_index(db: &rocket::State<Sqlite>, i18n: I18n, query: Query) ->
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::tests::common::*;
|
||||
use rstest::rstest;
|
||||
|
||||
use rocket::http::ContentType;
|
||||
use rocket::http::Status;
|
||||
|
||||
@@ -190,8 +193,8 @@ mod tests {
|
||||
use crate::mail::pop_mail;
|
||||
use crate::web::tests::*;
|
||||
|
||||
#[test]
|
||||
fn hkp() {
|
||||
#[rstest]
|
||||
fn hkp(base_uri: &str) {
|
||||
let (tmpdir, client) = client().unwrap();
|
||||
let filemail_into = tmpdir.path().join("filemail");
|
||||
|
||||
@@ -250,7 +253,7 @@ mod tests {
|
||||
check_mr_responses_by_fingerprint(&client, &tpk, 0);
|
||||
|
||||
// And check that we can see the human-readable result page.
|
||||
check_hr_responses_by_fingerprint(&client, &tpk, 0);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk, 0, base_uri);
|
||||
|
||||
// Upload the same key again, make sure the welcome mail is not sent again
|
||||
let response = client
|
||||
@@ -266,8 +269,8 @@ mod tests {
|
||||
assert_consistency(client.rocket());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hkp_add_two() {
|
||||
#[rstest]
|
||||
fn hkp_add_two(base_uri: &str) {
|
||||
let (tmpdir, client) = client().unwrap();
|
||||
let filemail_into = tmpdir.path().join("filemail");
|
||||
|
||||
@@ -325,8 +328,8 @@ mod tests {
|
||||
|
||||
check_mr_responses_by_fingerprint(&client, &tpk_0, 0);
|
||||
check_mr_responses_by_fingerprint(&client, &tpk_1, 0);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk_0, 0);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk_1, 0);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk_0, 0, base_uri);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk_1, 0, base_uri);
|
||||
|
||||
assert_consistency(client.rocket());
|
||||
}
|
||||
|
162
src/web/mod.rs
162
src/web/mod.rs
@@ -312,13 +312,28 @@ pub mod tests {
|
||||
use sequoia_openpgp::parse::Parse;
|
||||
use sequoia_openpgp::serialize::Serialize;
|
||||
|
||||
use std::time::SystemTime;
|
||||
|
||||
use super::*;
|
||||
use crate::app::configure_rocket;
|
||||
use crate::mail::pop_mail;
|
||||
use crate::web::tests::common::{base_uri, base_uri_onion};
|
||||
use rstest::rstest;
|
||||
use std::time::SystemTime;
|
||||
|
||||
pub mod common {
|
||||
use rstest::fixture;
|
||||
|
||||
/// Fake base URI to use in tests.
|
||||
#[fixture]
|
||||
pub fn base_uri() -> &'static str {
|
||||
"http://local.connection"
|
||||
}
|
||||
|
||||
#[fixture]
|
||||
pub fn base_uri_onion() -> &'static str {
|
||||
"http://local.connection.onion"
|
||||
}
|
||||
}
|
||||
|
||||
/// Fake base URI to use in tests.
|
||||
const BASE_URI: &str = "http://local.connection";
|
||||
const BASE_URI_ONION: &str = "http://local.connection.onion";
|
||||
|
||||
@@ -537,8 +552,8 @@ pub mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn upload_verify_single() {
|
||||
#[rstest]
|
||||
fn upload_verify_single(base_uri: &str, base_uri_onion: &str) {
|
||||
let (tmpdir, client) = client().unwrap();
|
||||
let filemail_into = tmpdir.path().join("filemail");
|
||||
|
||||
@@ -558,7 +573,7 @@ pub mod tests {
|
||||
check_mr_responses_by_fingerprint(&client, &tpk, 0);
|
||||
|
||||
// And check that we can see the human-readable result page.
|
||||
check_hr_responses_by_fingerprint(&client, &tpk, 0);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk, 0, base_uri);
|
||||
|
||||
// Check the verification link
|
||||
check_verify_link(&client, &token, "foo@invalid.example.com", "");
|
||||
@@ -567,10 +582,17 @@ pub mod tests {
|
||||
check_mails_and_verify_email(&client, filemail_into.as_path());
|
||||
|
||||
// Now lookups using the mail address should work.
|
||||
check_responses_by_email(&client, "foo@invalid.example.com", &tpk, 1);
|
||||
check_responses_by_email(
|
||||
&client,
|
||||
"foo@invalid.example.com",
|
||||
&tpk,
|
||||
1,
|
||||
base_uri,
|
||||
base_uri_onion,
|
||||
);
|
||||
|
||||
// And check that we can see the human-readable result page.
|
||||
check_hr_responses_by_fingerprint(&client, &tpk, 1);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk, 1, base_uri);
|
||||
|
||||
// Request deletion of the binding.
|
||||
vks_manage(&client, "foo@invalid.example.com");
|
||||
@@ -590,7 +612,7 @@ pub mod tests {
|
||||
check_mr_responses_by_fingerprint(&client, &tpk, 0);
|
||||
|
||||
// And check that we can see the human-readable result page.
|
||||
check_hr_responses_by_fingerprint(&client, &tpk, 0);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk, 0, base_uri);
|
||||
|
||||
assert_consistency(client.rocket());
|
||||
}
|
||||
@@ -613,8 +635,8 @@ pub mod tests {
|
||||
assert!(mail_content.contains("Subject: =?utf-8?b?QmVzdMOkdGlnZQ==?= foo@invalid.example.com \n =?utf-8?b?ZsO8cg==?= deinen =?utf-8?b?U2NobMO8c3NlbA==?= auf \n local.connection"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn upload_two() {
|
||||
#[rstest]
|
||||
fn upload_two(base_uri: &str) {
|
||||
let (_tmpdir, client) = client().expect("valid rocket instance");
|
||||
|
||||
// Generate two keys and upload them.
|
||||
@@ -637,12 +659,12 @@ pub mod tests {
|
||||
check_mr_responses_by_fingerprint(&client, &tpk_1, 0);
|
||||
|
||||
// And check that we can see the human-readable result page.
|
||||
check_hr_responses_by_fingerprint(&client, &tpk_0, 0);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk_1, 0);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk_0, 0, base_uri);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk_1, 0, base_uri);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn upload_verify_two() {
|
||||
#[rstest]
|
||||
fn upload_verify_two(base_uri: &str, base_uri_onion: &str) {
|
||||
let (tmpdir, client) = client().expect("valid rocket instance");
|
||||
let filemail_into = tmpdir.path().join("filemail");
|
||||
|
||||
@@ -669,8 +691,8 @@ pub mod tests {
|
||||
check_mr_responses_by_fingerprint(&client, &tpk_2, 0);
|
||||
|
||||
// And check that we can see the human-readable result page.
|
||||
check_hr_responses_by_fingerprint(&client, &tpk_1, 0);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk_2, 0);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk_1, 0, base_uri);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk_2, 0, base_uri);
|
||||
|
||||
// Check the verification link
|
||||
check_verify_link(&client, &token_1, "foo@invalid.example.com", "");
|
||||
@@ -681,8 +703,22 @@ pub mod tests {
|
||||
check_mails_and_verify_email(&client, &filemail_into);
|
||||
|
||||
// Now lookups using the mail address should work.
|
||||
check_responses_by_email(&client, "foo@invalid.example.com", &tpk_1, 1);
|
||||
check_responses_by_email(&client, "bar@invalid.example.com", &tpk_2, 1);
|
||||
check_responses_by_email(
|
||||
&client,
|
||||
"foo@invalid.example.com",
|
||||
&tpk_1,
|
||||
1,
|
||||
base_uri,
|
||||
base_uri_onion,
|
||||
);
|
||||
check_responses_by_email(
|
||||
&client,
|
||||
"bar@invalid.example.com",
|
||||
&tpk_2,
|
||||
1,
|
||||
base_uri,
|
||||
base_uri_onion,
|
||||
);
|
||||
|
||||
// Request deletion of the bindings.
|
||||
vks_manage(&client, "foo@invalid.example.com");
|
||||
@@ -700,8 +736,8 @@ pub mod tests {
|
||||
check_mr_responses_by_fingerprint(&client, &tpk_2, 0);
|
||||
|
||||
// And check that we can see the human-readable result page.
|
||||
check_hr_responses_by_fingerprint(&client, &tpk_1, 0);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk_2, 0);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk_1, 0, base_uri);
|
||||
check_hr_responses_by_fingerprint(&client, &tpk_2, 0, base_uri);
|
||||
|
||||
assert_consistency(client.rocket());
|
||||
}
|
||||
@@ -714,8 +750,8 @@ pub mod tests {
|
||||
assert_eq!(response.status(), Status::BadRequest);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn upload_verify_onion() {
|
||||
#[rstest]
|
||||
fn upload_verify_onion(base_uri_onion: &str) {
|
||||
let (tmpdir, client) = client().unwrap();
|
||||
let filemail_into = tmpdir.path().join("filemail");
|
||||
|
||||
@@ -741,7 +777,7 @@ pub mod tests {
|
||||
assert_eq!(response.status(), Status::Ok);
|
||||
|
||||
// Now check for the verification mail.
|
||||
let pattern = format!("{}(/verify/[^ \t\n]*)", BASE_URI_ONION);
|
||||
let pattern = format!("{}(/verify/[^ \t\n]*)", base_uri_onion);
|
||||
let confirm_uri = pop_mail_capture_pattern(&filemail_into, &pattern);
|
||||
|
||||
let response = client.get(&confirm_uri).dispatch();
|
||||
@@ -830,7 +866,14 @@ pub mod tests {
|
||||
}
|
||||
|
||||
/// Asserts that lookups by the given email are successful.
|
||||
pub fn check_responses_by_email(client: &Client, addr: &str, tpk: &Cert, nr_uids: usize) {
|
||||
pub fn check_responses_by_email(
|
||||
client: &Client,
|
||||
addr: &str,
|
||||
tpk: &Cert,
|
||||
nr_uids: usize,
|
||||
base_uri: &str,
|
||||
base_uri_onion: &str,
|
||||
) {
|
||||
check_mr_response(client, &format!("/vks/v1/by-email/{}", addr), tpk, nr_uids);
|
||||
check_mr_response(
|
||||
client,
|
||||
@@ -853,8 +896,20 @@ pub mod tests {
|
||||
tpk,
|
||||
nr_uids,
|
||||
);
|
||||
check_hr_response(client, &format!("/search?q={}", addr), tpk, nr_uids);
|
||||
check_hr_response_onion(client, &format!("/search?q={}", addr), tpk, nr_uids);
|
||||
check_hr_response(
|
||||
client,
|
||||
&format!("/search?q={}", addr),
|
||||
tpk,
|
||||
nr_uids,
|
||||
base_uri,
|
||||
);
|
||||
check_hr_response_onion(
|
||||
client,
|
||||
&format!("/search?q={}", addr),
|
||||
tpk,
|
||||
nr_uids,
|
||||
base_uri_onion,
|
||||
);
|
||||
|
||||
let (wkd_hash, domain) = hagrid_database::wkd::encode_wkd(addr).unwrap();
|
||||
check_wkd_response(
|
||||
@@ -973,7 +1028,13 @@ pub mod tests {
|
||||
|
||||
/// Asserts that the given URI returns human readable response
|
||||
/// page that contains a URI pointing to the Cert.
|
||||
pub fn check_hr_response(client: &Client, uri: &str, tpk: &Cert, nr_uids: usize) {
|
||||
pub fn check_hr_response(
|
||||
client: &Client,
|
||||
uri: &str,
|
||||
tpk: &Cert,
|
||||
nr_uids: usize,
|
||||
base_uri: &str,
|
||||
) {
|
||||
let response = client.get(uri).dispatch();
|
||||
assert_eq!(response.status(), Status::Ok);
|
||||
assert_eq!(response.content_type(), Some(ContentType::HTML));
|
||||
@@ -982,7 +1043,7 @@ pub mod tests {
|
||||
assert!(body.contains(&tpk.fingerprint().to_hex()));
|
||||
|
||||
// Extract the links.
|
||||
let link_re = regex::Regex::new(&format!("{}(/vks/[^ \t\n\"<]*)", BASE_URI)).unwrap();
|
||||
let link_re = regex::Regex::new(&format!("{}(/vks/[^ \t\n\"<]*)", base_uri)).unwrap();
|
||||
let mut n = 0;
|
||||
for link in link_re.captures_iter(&body) {
|
||||
check_mr_response(client, link.get(1).unwrap().as_str(), tpk, nr_uids);
|
||||
@@ -993,7 +1054,13 @@ pub mod tests {
|
||||
|
||||
/// Asserts that the given URI returns human readable response
|
||||
/// page that contains an onion URI pointing to the Cert.
|
||||
pub fn check_hr_response_onion(client: &Client, uri: &str, tpk: &Cert, _nr_uids: usize) {
|
||||
pub fn check_hr_response_onion(
|
||||
client: &Client,
|
||||
uri: &str,
|
||||
tpk: &Cert,
|
||||
_nr_uids: usize,
|
||||
base_uri_onion: &str,
|
||||
) {
|
||||
let response = client
|
||||
.get(uri)
|
||||
.header(Header::new("X-Is-Onion", "true"))
|
||||
@@ -1004,20 +1071,43 @@ pub mod tests {
|
||||
assert!(body.contains(&tpk.fingerprint().to_hex()));
|
||||
|
||||
// Extract the links.
|
||||
let link_re = regex::Regex::new(&format!("{}(/vks/[^ \t\n\"<]*)", BASE_URI_ONION)).unwrap();
|
||||
let link_re = regex::Regex::new(&format!("{}(/vks/[^ \t\n\"<]*)", base_uri_onion)).unwrap();
|
||||
assert!(link_re.is_match(&body));
|
||||
}
|
||||
|
||||
/// Asserts that we can get the given Cert back using the various
|
||||
/// by-fingerprint or by-keyid lookup mechanisms.
|
||||
pub fn check_hr_responses_by_fingerprint(client: &Client, tpk: &Cert, nr_uids: usize) {
|
||||
pub fn check_hr_responses_by_fingerprint(
|
||||
client: &Client,
|
||||
tpk: &Cert,
|
||||
nr_uids: usize,
|
||||
base_uri: &str,
|
||||
) {
|
||||
let fp = tpk.fingerprint().to_hex();
|
||||
let keyid = sequoia_openpgp::KeyID::from(tpk.fingerprint()).to_hex();
|
||||
|
||||
check_hr_response(client, &format!("/search?q={}", fp), tpk, nr_uids);
|
||||
check_hr_response(client, &format!("/search?q=0x{}", fp), tpk, nr_uids);
|
||||
check_hr_response(client, &format!("/search?q={}", keyid), tpk, nr_uids);
|
||||
check_hr_response(client, &format!("/search?q=0x{}", keyid), tpk, nr_uids);
|
||||
check_hr_response(client, &format!("/search?q={}", fp), tpk, nr_uids, base_uri);
|
||||
check_hr_response(
|
||||
client,
|
||||
&format!("/search?q=0x{}", fp),
|
||||
tpk,
|
||||
nr_uids,
|
||||
base_uri,
|
||||
);
|
||||
check_hr_response(
|
||||
client,
|
||||
&format!("/search?q={}", keyid),
|
||||
tpk,
|
||||
nr_uids,
|
||||
base_uri,
|
||||
);
|
||||
check_hr_response(
|
||||
client,
|
||||
&format!("/search?q=0x{}", keyid),
|
||||
tpk,
|
||||
nr_uids,
|
||||
base_uri,
|
||||
);
|
||||
}
|
||||
|
||||
/// Asserts that the given URI returns correct WKD response with a Cert
|
||||
|
Reference in New Issue
Block a user