Skip to content

Commit

Permalink
Fix firefox forwarder when website isn't set (#487)
Browse files Browse the repository at this point in the history
## Type of change
```
- [x] Bug fix
- [ ] New feature development
- [ ] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other
```

## Objective
When the website parameter is None, the request would include a
`generated_for: null` field, while the API expects the field to not be
present.
  • Loading branch information
dani-garcia authored Jan 8, 2024
1 parent fa4c2f7 commit 332293b
Showing 1 changed file with 60 additions and 21 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": "ofuj4d4qw@mozmail.com"
})))
.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, "ofuj4d4qw@mozmail.com");

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": "856f7765@mozmail.com"
})))
.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, "ofuj4d4qw@mozmail.com");
assert_eq!(address, "856f7765@mozmail.com");

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

0 comments on commit 332293b

Please sign in to comment.