stackless_laravel_cloud/
api_key.rs1use std::collections::BTreeMap;
5use std::path::Path;
6
7use crate::error::LaravelCloudError;
8
9pub const KEY_FILE: &str = ".laravel-cloud-token";
10pub const KEY_ENV: &str = "LARAVEL_CLOUD_API_TOKEN";
11
12pub fn resolve(
13 definition_dir: &Path,
14 secrets: &BTreeMap<String, String>,
15) -> Result<String, LaravelCloudError> {
16 stackless_cloud::credential::resolve(KEY_ENV, KEY_FILE, definition_dir, secrets).map_err(
17 |missing| LaravelCloudError::ApiKeyMissing {
18 key_file: missing.key_file,
19 },
20 )
21}
22
23#[cfg(test)]
24mod tests {
25 use super::*;
26
27 fn secret(key: &str, value: &str) -> BTreeMap<String, String> {
28 let mut map = BTreeMap::new();
29 map.insert(key.to_owned(), value.to_owned());
30 map
31 }
32
33 #[test]
34 fn resolves_from_stackless_env_secret() {
35 let dir = tempfile::tempdir().unwrap();
36 let key = resolve(dir.path(), &secret(KEY_ENV, "lc_from_secrets")).unwrap();
37 assert_eq!(key, "lc_from_secrets");
38 }
39
40 #[test]
41 fn key_file_is_a_fallback_when_secret_absent() {
42 let dir = tempfile::tempdir().unwrap();
43 std::fs::write(dir.path().join(KEY_FILE), "lc_from_file\n").unwrap();
44 let key = resolve(dir.path(), &BTreeMap::new()).unwrap();
45 assert_eq!(key, "lc_from_file");
46 }
47
48 #[test]
49 fn missing_everywhere_is_a_clean_fault() {
50 let dir = tempfile::tempdir().unwrap();
51 let err = resolve(dir.path(), &BTreeMap::new()).unwrap_err();
52 assert!(matches!(err, LaravelCloudError::ApiKeyMissing { .. }));
53 }
54}