1mod profile;
18mod table;
19
20struct DirectoryEntry {
23 blz: &'static str,
24 bic: &'static str,
25 name: &'static str,
26 url: &'static str,
27}
28
29impl DirectoryEntry {
30 fn config(&self) -> BankConfig {
31 BankConfig::new(self.name, self.blz, self.bic, self.url)
32 }
33}
34
35include!(concat!(env!("OUT_DIR"), "/bank_directory.rs"));
37
38fn directory_entry(blz: &str) -> Option<&'static DirectoryEntry> {
40 let at = DIRECTORY.binary_search_by(|e| e.blz.cmp(blz)).ok()?;
41 Some(&DIRECTORY[at])
42}
43
44use serde::{Deserialize, Serialize};
45
46pub use profile::{BankProfile, BookingIdSource, InitMode, ProfileId, UserIdRule};
47
48use crate::protocol::TanOverride;
49use crate::types::{BankName, Bic, Blz, FinTSUrl};
50
51#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct BankConfig {
54 pub name: BankName,
55 pub blz: Blz,
56 pub bic: Bic,
57 pub url: FinTSUrl,
58 pub profile: BankProfile,
60}
61
62impl BankConfig {
63 pub fn new(
67 name: impl Into<String>,
68 blz: impl Into<String>,
69 bic: impl Into<String>,
70 url: impl Into<String>,
71 ) -> Self {
72 let profile = BankProfile::generic();
73 let (name, blz) = (BankName::new(name), Blz::new(blz));
74 let (bic, url) = (Bic::new(bic), FinTSUrl::new(url));
75 Self {
76 name,
77 blz,
78 bic,
79 url,
80 profile,
81 }
82 }
83
84 pub fn with_url(mut self, url: impl Into<String>) -> Self {
87 self.url = FinTSUrl::new(url);
88 self
89 }
90
91 pub fn with_profile(mut self, profile: BankProfile) -> Self {
93 self.profile = profile;
94 self
95 }
96
97 pub fn with_tan_override(mut self, code: &str, rule: TanOverride) -> Self {
99 let mut all: Vec<_> = self.profile.tan_overrides.iter().collect();
100 all.retain(|(c, _)| c.as_str() != code);
101 all.push((crate::types::SegmentType::new(code), rule));
102 self.profile.tan_overrides = all.into_iter().collect();
103 self
104 }
105}
106
107pub const TESTBANK_BLZ: &str = "12345678";
110pub const TESTBANK_PORT: u16 = 47331;
113
114#[cfg(feature = "testbank")]
116fn testbank_config() -> BankConfig {
117 BankConfig::new(
118 "Testbank (synthetic, local)",
119 TESTBANK_BLZ,
120 "TESTDEFFXXX",
121 format!("http://127.0.0.1:{TESTBANK_PORT}/fints"),
122 )
123}
124
125pub fn profile_by_blz(blz: &str) -> BankProfile {
128 table::profile_for(blz)
129}
130
131fn with_profile(config: BankConfig) -> BankConfig {
132 let profile = profile_by_blz(config.blz.as_str());
133 config.with_profile(profile)
134}
135
136pub fn all_banks() -> Vec<BankConfig> {
138 DIRECTORY
139 .iter()
140 .map(|entry| with_profile(entry.config()))
141 .collect()
142}
143
144pub fn bank_by_blz(blz: &str) -> Option<BankConfig> {
146 #[cfg(feature = "testbank")]
147 if blz == TESTBANK_BLZ {
148 return Some(testbank_config());
149 }
150 directory_entry(blz).map(|entry| with_profile(entry.config()))
151}
152
153#[cfg(test)]
154mod tests {
155 use std::collections::{BTreeSet, HashSet};
156
157 use super::*;
158 use crate::protocol::TanOverrides;
159
160 fn directory_url(blz: &str) -> Option<&'static str> {
161 directory_entry(blz).map(|entry| entry.url)
162 }
163
164 #[test]
165 fn every_profiled_code_is_in_the_directory() {
166 for (codes, make) in table::PROFILES {
167 for blz in *codes {
168 assert!(directory_url(blz).is_some(), "{} {blz}", make().id);
169 }
170 }
171 }
172
173 #[test]
174 fn no_code_is_in_two_profiles() {
175 let mut seen = HashSet::new();
176 for (codes, _) in table::PROFILES {
177 for blz in *codes {
178 assert!(seen.insert(*blz), "{blz} listed twice");
179 }
180 }
181 }
182
183 #[test]
186 fn every_code_at_a_profiled_url_is_listed() {
187 for (codes, make) in table::PROFILES {
188 let urls: BTreeSet<_> = codes.iter().filter_map(|b| directory_url(b)).collect();
189 let at_urls: BTreeSet<_> = DIRECTORY
190 .iter()
191 .filter(|entry| urls.contains(entry.url))
192 .map(|entry| entry.blz)
193 .collect();
194 let listed: BTreeSet<_> = codes.iter().copied().collect();
195 assert_eq!(at_urls, listed, "{}", make().id);
196 }
197 }
198
199 #[test]
200 fn comdirect_has_all_eight_directory_codes() {
201 let codes: Vec<_> = all_banks()
202 .into_iter()
203 .filter(|b| b.profile.id == ProfileId::Comdirect)
204 .map(|b| b.blz.as_str().to_string())
205 .collect();
206 assert_eq!(codes.len(), 8, "{codes:?}");
207 }
208
209 #[test]
210 fn lookup_and_list_agree() {
211 for bank in all_banks() {
212 let one = bank_by_blz(bank.blz.as_str()).unwrap();
213 assert_eq!(one.profile, bank.profile, "{}", bank.blz.as_str());
214 }
215 }
216
217 #[test]
218 fn profiles_by_code() {
219 let id = |blz| bank_by_blz(blz).unwrap().profile.id;
220 assert_eq!(id("12030000"), ProfileId::Dkb);
221 assert_eq!(id("20041155"), ProfileId::Comdirect);
222 assert_eq!(id("76030080"), ProfileId::Consorsbank);
223 assert_eq!(id("50010517"), ProfileId::Ing);
224 assert_eq!(id("10010010"), ProfileId::Generic);
225 assert_eq!(profile_by_blz("00000000"), BankProfile::generic());
226 }
227
228 #[test]
229 fn ing_opens_without_a_sync_dialog_and_others_do_not() {
230 let ing = bank_by_blz("50010517").expect("ING is in the directory");
231 assert_eq!(
232 ing.profile.init,
233 InitMode::Direct {
234 security_function: "900".into()
235 }
236 );
237 let dkb = bank_by_blz("12030000").expect("DKB is in the directory");
238 assert_eq!(dkb.profile.init, InitMode::Standard);
239 }
240
241 #[test]
242 fn with_url_keeps_the_profile() {
243 let ing = bank_by_blz("50010517")
244 .unwrap()
245 .with_tan_override("HKSAL", TanOverride::Always);
246 let moved = ing.clone().with_url("http://127.0.0.1:1/fints");
247 assert_eq!(moved.url.as_str(), "http://127.0.0.1:1/fints");
248 assert_eq!(moved.profile, ing.profile);
249 assert!(matches!(moved.profile.init, InitMode::Direct { .. }));
250 assert_eq!(
251 moved
252 .profile
253 .tan_overrides
254 .get(&crate::types::SegmentType::new("HKSAL")),
255 Some(TanOverride::Always)
256 );
257 }
258
259 #[test]
263 fn the_shipped_profiles_have_no_tan_overrides() {
264 for (_, make) in table::PROFILES {
265 assert_eq!(make().tan_overrides, TanOverrides::default());
266 }
267 }
268
269 #[test]
270 fn a_profile_round_trips_through_json() {
271 let p = bank_by_blz("50010517").unwrap().profile;
272 let json = serde_json::to_string(&p).unwrap();
273 assert!(json.contains("\"ing\""), "{json}");
274 assert_eq!(serde_json::from_str::<BankProfile>(&json).unwrap(), p);
275 }
276
277 #[test]
278 fn consorsbank_completes_short_user_ids() {
279 let rule = table::PROFILES
280 .iter()
281 .find(|(codes, _)| codes.contains(&"76030080"))
282 .and_then(|(_, make)| make().user_id)
283 .expect("rule");
284 assert_eq!(rule.apply("123456789"), "123456789001");
285 assert_eq!(rule.apply("1234567890"), "1234567890001");
286 assert_eq!(rule.apply("123456789001"), "123456789001");
288 assert_eq!(rule.apply("1234567890001"), "1234567890001");
289 assert_eq!(rule.apply("12345678"), "12345678");
290 assert_eq!(rule.apply("12345678901"), "12345678901");
291 assert_eq!(rule.apply("12345678a"), "12345678a");
292 assert_eq!(rule.apply("abcdefghi"), "abcdefghi");
293 assert_eq!(rule.apply(""), "");
294 assert_eq!(profile_by_blz("70120400").user_id, Some(rule));
295 assert_eq!(profile_by_blz("12030000").user_id, None);
296 }
297}
298
299#[cfg(all(test, feature = "testbank"))]
300mod testbank_tests {
301 #[test]
302 fn testbank_is_a_known_bank_with_the_feature() {
303 let bank = super::bank_by_blz(super::TESTBANK_BLZ).unwrap();
304 assert_eq!(bank.url.as_str(), "http://127.0.0.1:47331/fints");
305 assert!(
306 super::all_banks()
307 .iter()
308 .all(|b| b.blz.as_str() != super::TESTBANK_BLZ),
309 "the bank list stays the published one"
310 );
311 }
312}