Skip to content

Commit fae6984

Browse files
committed
trustpub: Add MockOidcKeyStore::with_test_key() fn
1 parent 02575bc commit fae6984

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

crates/crates_io_trustpub/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edition = "2024"
88
workspace = true
99

1010
[features]
11-
test-helpers = ["dep:mockall"]
11+
test-helpers = ["dep:mockall", "dep:serde_json"]
1212

1313
[dependencies]
1414
anyhow = "=1.0.98"
@@ -18,6 +18,7 @@ mockall = { version = "=0.13.1", optional = true }
1818
reqwest = { version = "=0.12.15", features = ["gzip", "json"] }
1919
regex = "=1.11.1"
2020
serde = { version = "=1.0.219", features = ["derive"] }
21+
serde_json = { version = "=1.0.140", optional = true }
2122
thiserror = "=2.0.12"
2223
tokio = { version = "=1.45.0", features = ["sync"] }
2324

crates/crates_io_trustpub/src/keystore/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,21 @@ pub trait OidcKeyStore: Send + Sync {
1818
/// is an error while fetching the key, it will return an error.
1919
async fn get_oidc_key(&self, key_id: &str) -> anyhow::Result<Option<DecodingKey>>;
2020
}
21+
22+
#[cfg(feature = "test-helpers")]
23+
impl MockOidcKeyStore {
24+
/// Creates a new instance of [`MockOidcKeyStore`] based on the RSA keys
25+
/// provided in the [`crate::test_keys`] module.
26+
pub fn with_test_key() -> Self {
27+
use crate::test_keys::{DECODING_KEY, KEY_ID};
28+
use mockall::predicate::*;
29+
30+
let mut mock = Self::new();
31+
32+
mock.expect_get_oidc_key()
33+
.with(eq(KEY_ID))
34+
.returning(|_| Ok(Some(DECODING_KEY.clone())));
35+
36+
mock
37+
}
38+
}

crates/crates_io_trustpub/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
pub mod github;
44
pub mod keystore;
5-
#[cfg(test)]
5+
#[cfg(any(test, feature = "test-helpers"))]
66
pub mod test_keys;

crates/crates_io_trustpub/src/test_keys.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ pub(crate) const KEY_ID: &str = "c0ffee";
5454
static ENCODING_KEY: LazyLock<EncodingKey> =
5555
LazyLock::new(|| EncodingKey::from_rsa_pem(PRIVATE_KEY_PEM).unwrap());
5656

57-
#[allow(unused)]
5857
pub(crate) static DECODING_KEY: LazyLock<DecodingKey> = LazyLock::new(|| {
5958
let jwk = serde_json::from_str(PUBLIC_KEY_JWK).unwrap();
6059
DecodingKey::from_jwk(&jwk).unwrap()

0 commit comments

Comments
 (0)