forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.010.9 MiB197fa34Tahinli1 month ago68 commits

test_support.rs117 lines, 4.3 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::time::stamp_after;
11use forge_core::token::hash_token;
12use forge_core::{Config, Registration, Store};
13use forge_git::GitEnv;
14use tokio::sync::Notify;
15use topcoat::cookie::RouterBuilderCookieExt;
16use topcoat::router::tower::TowerLayer;
17use topcoat::router::{Compression, Router, RouterBuilderDiscoverExt};
18
19use crate::pages::repo_ctx::RepoMovedLayer;
20use crate::pages::settings_profile::avatar_body_limit;
21use crate::state::AppState;
22use crate::throttle::Throttle;
23
24/// Fixed instance key shared by every test state.
25pub const KEY: [u8; 32] = [7u8; 32];
26/// Fixed internal API token shared by every test state.
27pub const INTERNAL_TOKEN: &str = "test-token";
28/// Fixed admin token shared by every test state.
29pub const ADMIN_TOKEN: &str = "test-admin-token";
30
31/// The canonical test configuration. Data lives under `dir`, the database at `dir/forge.db`.
32pub fn test_config(dir: &Path) -> Config {
33 Config {
34 database: dir.join("forge.db"),
35 data_dir: dir.to_path_buf(),
36 listen: "127.0.0.1:7660".parse().unwrap(),
37 base_url: "https://forge.test/".into(),
38 user_content_base_url: "https://forge.test".into(),
39 ssh_listen: "127.0.0.1:2222".parse().unwrap(),
40 user_content_listen: "127.0.0.1:7661".parse().unwrap(),
41 ssh_public_host: "forge.test".into(),
42 ssh_public_port: 22,
43 registration: Registration::Closed,
44 trusted_proxies: Vec::new(),
45 webhooks: Default::default(),
46 backup: Default::default(),
47 releases: Default::default(),
48 storage: Default::default(),
49 replication: Default::default(),
50 lfs: Default::default(),
51 ci: Default::default(),
52 advisories: Default::default(),
53 mail: Default::default(),
54 orgs: Default::default(),
55 self_update: Default::default(),
56 banner: Default::default(),
57 alerts: Default::default(),
58 }
59}
60
61/// Opens (and migrates) the store at `dir/forge.db` and builds the shared state around it.
62pub async fn test_state(dir: &Path) -> AppState {
63 let store = Store::open(&dir.join("forge.db")).await.unwrap();
64 store.set_secret_key(KEY);
65 let config = Arc::new(test_config(dir));
66 AppState {
67 settings: crate::state::settings_for(&config),
68 config,
69 store,
70 git: Arc::new(GitEnv {
71 git_bin: "git".into(),
72 data_dir: dir.to_path_buf(),
73 }),
74 key: Arc::new(KEY),
75 internal_token: Arc::from(INTERNAL_TOKEN),
76 admin_token: Arc::from(ADMIN_TOKEN),
77 jobs: Arc::new(crate::backup::Jobs::default()),
78 internal_socket: dir.join("internal.sock"),
79 throttle: Arc::new(Throttle::default()),
80 webhook_waker: Arc::new(Notify::new()),
81 mirror_waker: Arc::new(Notify::new()),
82 blobs: crate::blobs::BlobStore::open(&dir.join("blobs")).expect("blob store"),
83 replication: Arc::new(crate::replication::ReplicationQueue::default()),
84 ci_waker: Arc::new(Notify::new()),
85 mailer: Arc::new(crate::mail::FakeMailer::default()),
86 mail_waker: Arc::new(Notify::new()),
87 notify_hub: Arc::new(crate::notify_hub::NotifyHub::new()),
88 ci_live: Arc::new(crate::ci::CiLive::default()),
89 }
90}
91
92/// The public router as boot.rs builds it, minus the listeners.
93pub fn app_router(state: AppState) -> Router {
94 Router::builder()
95 .discover()
96 .app_context(state)
97 .cookies()
98 .layer(avatar_body_limit())
99 .layer(RepoMovedLayer)
100 .layer(TowerLayer::new(crate::compression::ResponseCompression))
101 .compression(Compression::off())
102 .build()
103}
104
105/// Creates a session for `user_id` that expires in an hour and has no user agent.
106pub async fn create_session(store: &Store, user_id: &str, token: &str, csrf: &str) {
107 store
108 .create_session(
109 user_id,
110 &hash_token(token),
111 csrf,
112 &stamp_after(Duration::from_secs(3600)),
113 None,
114 )
115 .await
116 .expect("session");
117}