-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:bitwarden/sdk into ps/crypto-crate
# Conflicts: # crates/bitwarden/src/error.rs # crates/bitwarden/src/vault/send.rs
- Loading branch information
Showing
5 changed files
with
229 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
} | ||
|
@@ -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")) | ||
|
@@ -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()), | ||
|
@@ -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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.