Skip to content

Commit 7c3b32a

Browse files
committed
tests/TestAppBuilder: Add with_oidc_keystore() fn
1 parent 0b9da09 commit 7c3b32a

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/tests/util/test_app.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ use crates_io_index::{Credentials, RepositoryConfig};
1717
use crates_io_team_repo::MockTeamRepo;
1818
use crates_io_test_db::TestDatabase;
1919
use crates_io_trustpub::github::test_helpers::AUDIENCE;
20+
use crates_io_trustpub::keystore::{MockOidcKeyStore, OidcKeyStore};
2021
use crates_io_worker::Runner;
2122
use diesel_async::AsyncPgConnection;
2223
use futures_util::TryStreamExt;
2324
use oauth2::{ClientId, ClientSecret};
2425
use regex::Regex;
25-
use std::collections::HashSet;
26+
use std::collections::{HashMap, HashSet};
2627
use std::sync::LazyLock;
2728
use std::{rc::Rc, sync::Arc, time::Duration};
2829
use tokio::runtime::Handle;
@@ -102,6 +103,7 @@ impl TestApp {
102103
use_chaos_proxy: false,
103104
team_repo: MockTeamRepo::new(),
104105
github: None,
106+
oidc_key_stores: Default::default(),
105107
}
106108
}
107109

@@ -243,6 +245,7 @@ pub struct TestAppBuilder {
243245
use_chaos_proxy: bool,
244246
team_repo: MockTeamRepo,
245247
github: Option<MockGitHubClient>,
248+
oidc_key_stores: HashMap<String, Box<dyn OidcKeyStore>>,
246249
}
247250

248251
impl TestAppBuilder {
@@ -281,7 +284,7 @@ impl TestAppBuilder {
281284
(primary_proxy, replica_proxy)
282285
};
283286

284-
let (app, router) = build_app(self.config, self.github);
287+
let (app, router) = build_app(self.config, self.github, self.oidc_key_stores);
285288

286289
let runner = if self.build_job_runner {
287290
let index = self
@@ -389,6 +392,17 @@ impl TestAppBuilder {
389392
self
390393
}
391394

395+
/// Add a new OIDC keystore to the application
396+
pub fn with_oidc_keystore(
397+
mut self,
398+
issuer_url: impl Into<String>,
399+
keystore: MockOidcKeyStore,
400+
) -> Self {
401+
self.oidc_key_stores
402+
.insert(issuer_url.into(), Box::new(keystore));
403+
self
404+
}
405+
392406
pub fn with_team_repo(mut self, team_repo: MockTeamRepo) -> Self {
393407
self.team_repo = team_repo;
394408
self
@@ -487,7 +501,11 @@ fn simple_config() -> config::Server {
487501
}
488502
}
489503

490-
fn build_app(config: config::Server, github: Option<MockGitHubClient>) -> (Arc<App>, axum::Router) {
504+
fn build_app(
505+
config: config::Server,
506+
github: Option<MockGitHubClient>,
507+
oidc_key_stores: HashMap<String, Box<dyn OidcKeyStore>>,
508+
) -> (Arc<App>, axum::Router) {
491509
// Use the in-memory email backend for all tests, allowing tests to analyze the emails sent by
492510
// the application. This will also prevent cluttering the filesystem.
493511
let emails = Emails::new_in_memory();
@@ -499,6 +517,7 @@ fn build_app(config: config::Server, github: Option<MockGitHubClient>) -> (Arc<A
499517
.databases_from_config(&config.db)
500518
.github(github)
501519
.github_oauth_from_config(&config)
520+
.oidc_key_stores(oidc_key_stores)
502521
.emails(emails)
503522
.storage_from_config(&config.storage)
504523
.rate_limiter_from_config(config.rate_limiter.clone())

0 commit comments

Comments
 (0)