Skip to content

Commit

Permalink
Merge branch 'main' of github.com:bitwarden/sdk into ps/crypto-crate
Browse files Browse the repository at this point in the history
# Conflicts:
#	crates/bitwarden/src/error.rs
#	crates/bitwarden/src/vault/send.rs
  • Loading branch information
Hinton committed Jan 8, 2024
2 parents c26c888 + 332293b commit 30c4489
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 55 deletions.
81 changes: 60 additions & 21 deletions crates/bitwarden/src/tool/generators/username_forwarders/firefox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async fn generate_with_api_url(
#[derive(serde::Serialize)]
struct Request {
enabled: bool,
#[serde(skip_serializing_if = "Option::is_none")]
generated_for: Option<String>,
description: String,
}
Expand Down Expand Up @@ -58,13 +59,13 @@ async fn generate_with_api_url(
#[cfg(test)]
mod tests {
use serde_json::json;

#[tokio::test]
async fn test_mock_server() {
async fn test_mock_success() {
use wiremock::{matchers, Mock, ResponseTemplate};

let (server, _client) = crate::util::start_mock(vec![
// Mock the request to the Firefox API, and verify that the correct request is made
Mock::given(matchers::path("/api/v1/relayaddresses/"))
let (server, _client) =
crate::util::start_mock(vec![Mock::given(matchers::path("/api/v1/relayaddresses/"))
.and(matchers::method("POST"))
.and(matchers::header("Content-Type", "application/json"))
.and(matchers::header("Authorization", "Token MY_TOKEN"))
Expand All @@ -76,33 +77,73 @@ mod tests {
.respond_with(ResponseTemplate::new(201).set_body_json(json!({
"full_address": "[email protected]"
})))
.expect(1),
// Mock an invalid API key
Mock::given(matchers::path("/api/v1/relayaddresses/"))
.expect(1)])
.await;

let address = super::generate_with_api_url(
&reqwest::Client::new(),
"MY_TOKEN".into(),
Some("example.com".into()),
format!("http://{}", server.address()),
)
.await
.unwrap();
assert_eq!(address, "[email protected]");

server.verify().await;
}

#[tokio::test]
async fn test_mock_without_website() {
use wiremock::{matchers, Mock, ResponseTemplate};

let (server, _client) =
crate::util::start_mock(vec![Mock::given(matchers::path("/api/v1/relayaddresses/"))
.and(matchers::method("POST"))
.and(matchers::header("Content-Type", "application/json"))
.and(matchers::header("Authorization", "Token MY_FAKE_TOKEN"))
.and(matchers::header("Authorization", "Token MY_OTHER_TOKEN"))
.and(matchers::body_json(json!({
"enabled": true,
"generated_for": "example.com",
"description": "example.com - Generated by Bitwarden."
"description": "Generated by Bitwarden."
})))
.respond_with(ResponseTemplate::new(401))
.expect(1),
])
.await;
.respond_with(ResponseTemplate::new(201).set_body_json(json!({
"full_address": "[email protected]"
})))
.expect(1)])
.await;

let address = super::generate_with_api_url(
&reqwest::Client::new(),
"MY_TOKEN".into(),
Some("example.com".into()),
"MY_OTHER_TOKEN".into(),
None,
format!("http://{}", server.address()),
)
.await
.unwrap();
assert_eq!(address, "[email protected]");
assert_eq!(address, "[email protected]");

server.verify().await;
}

#[tokio::test]
async fn test_mock_invalid_token() {
use wiremock::{matchers, Mock, ResponseTemplate};

let (server, _client) =
crate::util::start_mock(vec![Mock::given(matchers::path("/api/v1/relayaddresses/"))
.and(matchers::method("POST"))
.and(matchers::header("Content-Type", "application/json"))
.and(matchers::header("Authorization", "Token MY_FAKE_TOKEN"))
.and(matchers::body_json(json!({
"enabled": true,
"generated_for": "example.com",
"description": "example.com - Generated by Bitwarden."
})))
.respond_with(ResponseTemplate::new(401))
.expect(1)])
.await;

let fake_token_error = super::generate_with_api_url(
let error = super::generate_with_api_url(
&reqwest::Client::new(),
"MY_FAKE_TOKEN".into(),
Some("example.com".into()),
Expand All @@ -111,9 +152,7 @@ mod tests {
.await
.unwrap_err();

assert!(fake_token_error
.to_string()
.contains("Invalid Firefox Relay API key"));
assert!(error.to_string().contains("Invalid Firefox Relay API key"));

server.verify().await;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn generate_with_api_url(
let response = http
.post(format!("{api_url}/api/alias/random/new{query}"))
.header(CONTENT_TYPE, "application/json")
.bearer_auth(api_key)
.header("Authentication", api_key)
.json(&Request { note })
.send()
.await?;
Expand Down Expand Up @@ -65,7 +65,7 @@ mod tests {
.and(matchers::method("POST"))
.and(matchers::query_param("hostname", "example.com"))
.and(matchers::header("Content-Type", "application/json"))
.and(matchers::header("Authorization", "Bearer MY_TOKEN"))
.and(matchers::header("Authentication", "MY_TOKEN"))
.and(matchers::body_json(json!({
"note": "Website: example.com. Generated by Bitwarden."
})))
Expand All @@ -78,7 +78,7 @@ mod tests {
.and(matchers::method("POST"))
.and(matchers::query_param("hostname", "example.com"))
.and(matchers::header("Content-Type", "application/json"))
.and(matchers::header("Authorization", "Bearer MY_FAKE_TOKEN"))
.and(matchers::header("Authentication", "MY_FAKE_TOKEN"))
.and(matchers::body_json(json!({
"note": "Website: example.com. Generated by Bitwarden."
})))
Expand Down
Loading

0 comments on commit 30c4489

Please sign in to comment.