Skip to content

Commit bdcb5f5

Browse files
authored
[PM-23712] Migrate remaining folder domain to SDK (#340)
Fully migrate folder create and edit to the SDK to demonstrate the repository and api capabilities.
1 parent 812865f commit bdcb5f5

File tree

20 files changed

+720
-54
lines changed

20 files changed

+720
-54
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"spki",
2828
"totp",
2929
"uniffi",
30+
"wiremock",
3031
"wordlist",
3132
"XCHACHA",
3233
"Zeroize",

Cargo.lock

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ bitwarden-fido = { path = "crates/bitwarden-fido", version = "=1.0.0" }
3131
bitwarden-generators = { path = "crates/bitwarden-generators", version = "=1.0.0" }
3232
bitwarden-ipc = { path = "crates/bitwarden-ipc", version = "=1.0.0" }
3333
bitwarden-send = { path = "crates/bitwarden-send", version = "=1.0.0" }
34-
bitwarden-state = { path = "crates/bitwarden-state", version = "=1.0.0" }
35-
bitwarden-threading = { path = "crates/bitwarden-threading", version = "=1.0.0" }
3634
bitwarden-sm = { path = "bitwarden_license/bitwarden-sm", version = "=1.0.0" }
3735
bitwarden-ssh = { path = "crates/bitwarden-ssh", version = "=1.0.0" }
36+
bitwarden-state = { path = "crates/bitwarden-state", version = "=1.0.0" }
37+
bitwarden-test = { path = "crates/bitwarden-test", version = "=1.0.0" }
38+
bitwarden-threading = { path = "crates/bitwarden-threading", version = "=1.0.0" }
3839
bitwarden-uuid = { path = "crates/bitwarden-uuid", version = "=1.0.0" }
3940
bitwarden-uuid-macro = { path = "crates/bitwarden-uuid-macro", version = "=1.0.0" }
4041
bitwarden-vault = { path = "crates/bitwarden-vault", version = "=1.0.0" }
@@ -61,6 +62,7 @@ serde_bytes = { version = ">=0.11.17, <0.12.0" }
6162
serde_json = ">=1.0.96, <2.0"
6263
serde_qs = ">=0.12.0, <0.16"
6364
serde_repr = ">=0.1.12, <0.2"
65+
serde-wasm-bindgen = ">=0.6.0, <0.7"
6466
syn = ">=2.0.87, <3"
6567
thiserror = ">=1.0.40, <3"
6668
tokio = { version = "1.36.0", features = ["macros"] }
@@ -73,7 +75,7 @@ validator = { version = ">=0.18.1, <0.21", features = ["derive"] }
7375
wasm-bindgen = { version = ">=0.2.91, <0.3", features = ["serde-serialize"] }
7476
wasm-bindgen-futures = "0.4.41"
7577
wasm-bindgen-test = "0.3.45"
76-
serde-wasm-bindgen = ">=0.6.0, <0.7"
78+
wiremock = ">=0.6.0, <0.7"
7779

7880
# There is an incompatibility when using pkcs5 and chacha20 on wasm builds. This can be removed once a new
7981
# rustcrypto-formats crate version is released since the fix has been upstreamed.

bitwarden_license/bitwarden-sm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ validator = { workspace = true }
2828

2929
[dev-dependencies]
3030
tokio = { workspace = true, features = ["rt"] }
31-
wiremock = "0.6.0"
31+
wiremock = { workspace = true }
3232

3333
[lints]
3434
workspace = true

crates/bitwarden-generators/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ wasm-bindgen = { workspace = true, optional = true }
3939
[dev-dependencies]
4040
rand_chacha = "0.3.1"
4141
tokio = { workspace = true, features = ["rt"] }
42-
wiremock = "0.6.0"
42+
wiremock = { workspace = true }
4343

4444
[lints]
4545
workspace = true

crates/bitwarden-test/Cargo.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "bitwarden-test"
3+
description = """
4+
Internal crate for the bitwarden crate. Do not use.
5+
"""
6+
7+
version.workspace = true
8+
authors.workspace = true
9+
edition.workspace = true
10+
rust-version.workspace = true
11+
readme.workspace = true
12+
homepage.workspace = true
13+
repository.workspace = true
14+
license-file.workspace = true
15+
keywords.workspace = true
16+
17+
[dependencies]
18+
async-trait = { workspace = true }
19+
bitwarden-api-api.workspace = true
20+
bitwarden-state = { workspace = true }
21+
reqwest = { workspace = true }
22+
wiremock = { workspace = true }
23+
24+
[lints]
25+
workspace = true

crates/bitwarden-test/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Bitwarden Test
2+
3+
<div class="warning">
4+
This crate should only be used in tests and should not be included in production builds.
5+
</div>
6+
7+
Contains test utilities for Bitwarden.

crates/bitwarden-test/src/api.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use bitwarden_api_api::apis::configuration::Configuration;
2+
3+
/// Helper for testing the Bitwarden API using wiremock.
4+
///
5+
/// Warning: when using `Mock::expected` ensure `server` is not dropped before the test completes,
6+
pub async fn start_api_mock(mocks: Vec<wiremock::Mock>) -> (wiremock::MockServer, Configuration) {
7+
let server = wiremock::MockServer::start().await;
8+
9+
for mock in mocks {
10+
server.register(mock).await;
11+
}
12+
13+
let config = Configuration {
14+
base_path: server.uri(),
15+
user_agent: Some("test-agent".to_string()),
16+
client: reqwest::Client::new(),
17+
basic_auth: None,
18+
oauth_access_token: None,
19+
bearer_access_token: None,
20+
api_key: None,
21+
};
22+
23+
(server, config)
24+
}

crates/bitwarden-test/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![doc = include_str!("../README.md")]
2+
3+
mod api;
4+
pub use api::*;
5+
6+
mod repository;
7+
pub use repository::*;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use bitwarden_state::repository::{Repository, RepositoryError, RepositoryItem};
2+
3+
/// A simple in-memory repository implementation. The data is only stored in memory and will not
4+
/// persist beyond the lifetime of the repository instance.
5+
///
6+
/// Primary use case is for unit and integration tests.
7+
pub struct MemoryRepository<V: RepositoryItem> {
8+
store: std::sync::Mutex<std::collections::HashMap<String, V>>,
9+
}
10+
11+
impl<V: RepositoryItem + Clone> Default for MemoryRepository<V> {
12+
fn default() -> Self {
13+
Self {
14+
store: std::sync::Mutex::new(std::collections::HashMap::new()),
15+
}
16+
}
17+
}
18+
19+
#[async_trait::async_trait]
20+
impl<V: RepositoryItem + Clone> Repository<V> for MemoryRepository<V> {
21+
async fn get(&self, key: String) -> Result<Option<V>, RepositoryError> {
22+
let store = self
23+
.store
24+
.lock()
25+
.map_err(|e| RepositoryError::Internal(e.to_string()))?;
26+
Ok(store.get(&key).cloned())
27+
}
28+
29+
async fn list(&self) -> Result<Vec<V>, RepositoryError> {
30+
let store = self
31+
.store
32+
.lock()
33+
.map_err(|e| RepositoryError::Internal(e.to_string()))?;
34+
Ok(store.values().cloned().collect())
35+
}
36+
37+
async fn set(&self, key: String, value: V) -> Result<(), RepositoryError> {
38+
let mut store = self
39+
.store
40+
.lock()
41+
.map_err(|e| RepositoryError::Internal(e.to_string()))?;
42+
store.insert(key, value);
43+
Ok(())
44+
}
45+
46+
async fn remove(&self, key: String) -> Result<(), RepositoryError> {
47+
let mut store = self
48+
.store
49+
.lock()
50+
.map_err(|e| RepositoryError::Internal(e.to_string()))?;
51+
store.remove(&key);
52+
Ok(())
53+
}
54+
}

0 commit comments

Comments
 (0)