forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.010.2 MiBb00f3f4Tahinli1 month ago147 commits

test_support.rs132 lines, 5.0 KiBHistoryRawDownload
1//! Shared fixtures for the in-crate router and state tests. Compiled only under `cfg(test)`.
2//!
3//! Every test that needs a `Config`, an `AppState` or a router builds them here, so a new config
4//! or state field is added in one place.
5
6use std::path::Path;
7use std::sync::Arc;
8use std::time::Duration;
9
10use forge_core::config::CiConfig;
11use forge_core::time::stamp_after;
12use forge_core::token::hash_token;
13use forge_core::{Config, Registration, Store};
14use forge_git::GitEnv;
15use tokio::sync::Notify;
16use topcoat::cookie::RouterBuilderCookieExt;
17use topcoat::router::tower::TowerLayer;
18use topcoat::router::{Compression, Router, RouterBuilderDiscoverExt};
19
20use crate::pages::repo_ctx::RepoMovedLayer;
21use crate::pages::settings_profile::avatar_body_limit;
22use crate::state::AppState;
23use crate::throttle::Throttle;
24
25/// Fixed instance key shared by every test state.
26pub const KEY: [u8; 32] = [7u8; 32];
27/// Fixed internal API token shared by every test state.
28pub const INTERNAL_TOKEN: &str = "test-token";
29/// Fixed admin token shared by every test state.
30pub const ADMIN_TOKEN: &str = "test-admin-token";
31
32/// The canonical test configuration. Data lives under `dir`, the database at `dir/forge.db`.
33pub fn test_config(dir: &Path) -> Config {
34 Config {
35 database: dir.join("forge.db"),
36 data_dir: dir.to_path_buf(),
37 listen: "127.0.0.1:7660".parse().unwrap(),
38 base_url: "https://forge.test/".into(),
39 user_content_base_url: "https://forge.test".into(),
40 ssh_listen: "127.0.0.1:2222".parse().unwrap(),
41 user_content_listen: "127.0.0.1:7661".parse().unwrap(),
42 ssh_public_host: "forge.test".into(),
43 ssh_public_port: 22,
44 registration: Registration::Closed,
45 trusted_proxies: Vec::new(),
46 webhooks: Default::default(),
47 backup: Default::default(),
48 releases: Default::default(),
49 storage: Default::default(),
50 replication: Default::default(),
51 lfs: Default::default(),
52 ci: CiConfig {
53 // Unit tests that claim on an empty queue should not wait out the production long poll.
54 claim_wait_ms: 50,
55 ..Default::default()
56 },
57 advisories: Default::default(),
58 mail: Default::default(),
59 orgs: Default::default(),
60 self_update: Default::default(),
61 banner: Default::default(),
62 alerts: Default::default(),
63 mirrors: Default::default(),
64 ssh_auth_rejection_secs: 1,
65 }
66}
67
68/// Opens (and migrates) the store at `dir/forge.db` and builds the shared state around it.
69pub async fn test_state(dir: &Path) -> AppState {
70 let store = Store::open(&dir.join("forge.db")).await.unwrap();
71 store.set_secret_key(KEY);
72 let config = Arc::new(test_config(dir));
73 AppState {
74 settings: crate::state::settings_for(&config),
75 config,
76 store,
77 git: Arc::new(GitEnv {
78 git_bin: "git".into(),
79 data_dir: dir.to_path_buf(),
80 }),
81 key: Arc::new(KEY),
82 internal_token: Arc::from(INTERNAL_TOKEN),
83 admin_token: Arc::from(ADMIN_TOKEN),
84 jobs: Arc::new(crate::backup::Jobs::default()),
85 internal_socket: dir.join("internal.sock"),
86 throttle: Arc::new(Throttle::default()),
87 webhook_waker: Arc::new(Notify::new()),
88 mirror_waker: Arc::new(Notify::new()),
89 blobs: crate::blobs::BlobStore::open(&dir.join("blobs")).expect("blob store"),
90 replication: Arc::new(crate::replication::ReplicationQueue::default()),
91 ci_waker: Arc::new(Notify::new()),
92 mailer: Arc::new(crate::mail::FakeMailer::default()),
93 mail_waker: Arc::new(Notify::new()),
94 notify_hub: Arc::new(crate::notify_hub::NotifyHub::new()),
95 ci_live: Arc::new(crate::ci::CiLive::default()),
96 live: Arc::new(crate::live::LiveHub::default()),
97 push_live: Arc::new(crate::web_push::PushLive::default()),
98 packages_live: Arc::new(crate::packages_live::PackagesLive::default()),
99 }
100}
101
102/// The public router as boot.rs builds it, minus the listeners.
103pub fn app_router(state: AppState) -> Router {
104 Router::builder()
105 .discover()
106 .app_context(state)
107 .layer(crate::pages::settings_live::AccountWriteLayer)
108 .cookies()
109 .layer(avatar_body_limit())
110 .layer(RepoMovedLayer)
111 .layer(crate::pages::repo_settings_live::RepoSettingsWriteLayer)
112 .layer(crate::pages::pull_live::PullWriteLayer)
113 .layer(crate::pages::discussion_live::DiscussionWriteLayer)
114 .layer(crate::pages::org_live::OrgWriteLayer)
115 .layer(TowerLayer::new(crate::compression::ResponseCompression))
116 .compression(Compression::off())
117 .build()
118}
119
120/// Creates a session for `user_id` that expires in an hour and has no user agent.
121pub async fn create_session(store: &Store, user_id: &str, token: &str, csrf: &str) {
122 store
123 .create_session(
124 user_id,
125 &hash_token(token),
126 csrf,
127 &stamp_after(Duration::from_secs(3600)),
128 None,
129 )
130 .await
131 .expect("session");
132}