| 1 | //! Herkennen en maken van kaart-links (V1: een gewone HTML-link met leesbare ankertekst). |
| 2 | //! |
| 3 | //! Herkend worden: |
| 4 | //! - BC-webclient: `https://kvtmd365.kvt.nl/<KVTMDLive_AAD|KVTGermanyLive_AAD>/?company=…&page=<88|11333028|11333015|21|11332880|30>&filter='<No.|Contract No.>' IS '<nr>'` |
| 5 | //! (21 klant, 11332880 component en 30 artikel zijn aannames, zie de Mímir-README) |
| 6 | //! - Daedalus: `https://sleutels.kvt.nl/daedalus/…?company=…&workorder=<nr>` → werkorder |
| 7 | //! - Ponos: `https://sleutels.kvt.nl/ponos/…?company=…&project=<nr>` → project |
| 8 | //! |
| 9 | //! Elk ander patroon blijft een gewone link. |
| 10 | |
| 11 | use url::Url; |
| 12 | |
| 13 | use crate::BC_WEB_HOST; |
| 14 | |
| 15 | pub const MAX_CARDS_PER_MESSAGE: usize = 3; |
| 16 | const ENVIRONMENTS: [&str; 2] = ["KVTMDLive_AAD", "KVTGermanyLive_AAD"]; |
| 17 | const SLEUTELS_HOST: &str = "sleutels.kvt.nl"; |
| 18 | |
| 19 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 20 | pub enum CardKind { |
| 21 | Project, |
| 22 | Werkorder, |
| 23 | Contract, |
| 24 | Klant, |
| 25 | Component, |
| 26 | Artikel, |
| 27 | } |
| 28 | |
| 29 | impl CardKind { |
| 30 | pub const ALL: [CardKind; 6] = [ |
| 31 | CardKind::Project, |
| 32 | CardKind::Werkorder, |
| 33 | CardKind::Contract, |
| 34 | CardKind::Klant, |
| 35 | CardKind::Component, |
| 36 | CardKind::Artikel, |
| 37 | ]; |
| 38 | |
| 39 | /// Waarde van `type` in de kaart-API. |
| 40 | pub fn api_name(self) -> &'static str { |
| 41 | match self { |
| 42 | CardKind::Project => "project", |
| 43 | CardKind::Werkorder => "werkorder", |
| 44 | CardKind::Contract => "contract", |
| 45 | CardKind::Klant => "klant", |
| 46 | CardKind::Component => "component", |
| 47 | CardKind::Artikel => "artikel", |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub fn from_api_name(name: &str) -> Option<CardKind> { |
| 52 | CardKind::ALL |
| 53 | .into_iter() |
| 54 | .find(|kind| kind.api_name() == name) |
| 55 | } |
| 56 | |
| 57 | /// Nederlandse naam voor de ankertekst (leesbaar in elke Teams-client). |
| 58 | pub fn label_nl(self) -> &'static str { |
| 59 | match self { |
| 60 | CardKind::Project => "Project", |
| 61 | CardKind::Werkorder => "Werkorder", |
| 62 | CardKind::Contract => "Contract", |
| 63 | CardKind::Klant => "Klant", |
| 64 | CardKind::Component => "Component", |
| 65 | CardKind::Artikel => "Artikel", |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pub fn bc_page(self) -> u32 { |
| 70 | match self { |
| 71 | CardKind::Project => 88, |
| 72 | CardKind::Werkorder => 11333028, |
| 73 | CardKind::Contract => 11333015, |
| 74 | CardKind::Klant => 21, |
| 75 | CardKind::Component => 11332880, |
| 76 | CardKind::Artikel => 30, |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | pub fn bc_filter_field(self) -> &'static str { |
| 81 | match self { |
| 82 | CardKind::Contract => "Contract No.", |
| 83 | CardKind::Project |
| 84 | | CardKind::Werkorder |
| 85 | | CardKind::Klant |
| 86 | | CardKind::Component |
| 87 | | CardKind::Artikel => "No.", |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | fn from_bc_page(page: u32) -> Option<CardKind> { |
| 92 | CardKind::ALL |
| 93 | .into_iter() |
| 94 | .find(|kind| kind.bc_page() == page) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /// Verwijzing naar één BC-record. Bevat alleen type, bedrijf en nummer: de |
| 99 | /// details haalt elke kijker zelf op met eigen rechten. |
| 100 | #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 101 | pub struct CardRef { |
| 102 | pub kind: CardKind, |
| 103 | pub company: String, |
| 104 | pub no: String, |
| 105 | /// Padnaam in de webclient (`KVTMDLive_AAD`), alleen bekend uit een BC-link. |
| 106 | pub environment: Option<String>, |
| 107 | } |
| 108 | |
| 109 | pub fn number_is_valid(no: &str) -> bool { |
| 110 | !no.is_empty() |
| 111 | && no.len() <= 30 |
| 112 | && no |
| 113 | .bytes() |
| 114 | .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-')) |
| 115 | } |
| 116 | |
| 117 | fn company_is_valid(company: &str) -> bool { |
| 118 | let count = company.chars().count(); |
| 119 | count > 0 |
| 120 | && count <= 60 |
| 121 | && company |
| 122 | .chars() |
| 123 | .all(|c| c.is_alphanumeric() || " .,&()'-".contains(c)) |
| 124 | } |
| 125 | |
| 126 | /// `'No.' IS 'WO2699001'` → (`No.`, `WO2699001`). Aanhalingstekens om veld en |
| 127 | /// waarde zijn optioneel (BC schrijft ze alleen als er spaties in staan), `IS` mag |
| 128 | /// in elke hoofdletter-vorm. |
| 129 | fn parse_filter(filter: &str) -> Option<(String, String)> { |
| 130 | let filter = filter.trim(); |
| 131 | let (field, rest) = match filter.strip_prefix('\'') { |
| 132 | Some(rest) => rest.split_once('\'')?, |
| 133 | None => { |
| 134 | // Zonder quotes: het veld loopt tot « IS » (kan spaties hebben: Contract No.). |
| 135 | let lower = filter.to_ascii_lowercase(); |
| 136 | let at = lower.find(" is ")?; |
| 137 | (&filter[..at], &filter[at..]) |
| 138 | } |
| 139 | }; |
| 140 | let rest = rest.trim_start(); |
| 141 | let keyword = rest.get(..2)?; |
| 142 | if !keyword.eq_ignore_ascii_case("is") { |
| 143 | return None; |
| 144 | } |
| 145 | let rest = rest[2..].trim_start(); |
| 146 | let value = match rest.strip_prefix('\'') { |
| 147 | Some(quoted) => quoted.strip_suffix('\'')?, |
| 148 | None => rest, |
| 149 | }; |
| 150 | if value.contains('\'') || field.contains('\'') { |
| 151 | return None; |
| 152 | } |
| 153 | Some((field.trim().to_owned(), value.to_owned())) |
| 154 | } |
| 155 | |
| 156 | /// BC-webclient: `kvtmd365.kvt.nl` of een andere `*.kvt.nl`-host (andere omgeving), |
| 157 | /// niet de sleutels-host zelf. |
| 158 | fn is_bc_web_host(host: &str) -> bool { |
| 159 | host == BC_WEB_HOST |
| 160 | || (host != SLEUTELS_HOST |
| 161 | && host |
| 162 | .strip_suffix(".kvt.nl") |
| 163 | .is_some_and(|name| !name.is_empty() && !name.contains(".."))) |
| 164 | } |
| 165 | |
| 166 | fn environment_is_valid(environment: &str) -> bool { |
| 167 | !environment.is_empty() |
| 168 | && environment.len() <= 40 |
| 169 | && environment |
| 170 | .bytes() |
| 171 | .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-')) |
| 172 | } |
| 173 | |
| 174 | /// Eén link naar een kaart-verwijzing, of `None` (gewone link). |
| 175 | pub fn parse_card_link(href: &str) -> Option<CardRef> { |
| 176 | let url = Url::parse(href.trim()).ok()?; |
| 177 | if url.scheme() != "https" || !url.username().is_empty() || url.password().is_some() { |
| 178 | return None; |
| 179 | } |
| 180 | let host = url.host_str()?.to_ascii_lowercase(); |
| 181 | let query = |name: &str| { |
| 182 | url.query_pairs() |
| 183 | .find(|(key, _)| key == name) |
| 184 | .map(|(_, value)| value.into_owned()) |
| 185 | }; |
| 186 | let company = query("company")?; |
| 187 | if !company_is_valid(&company) { |
| 188 | return None; |
| 189 | } |
| 190 | |
| 191 | if is_bc_web_host(&host) { |
| 192 | let mut segments = url.path_segments()?.filter(|s| !s.is_empty()); |
| 193 | let environment = segments.next()?; |
| 194 | if !environment_is_valid(environment) { |
| 195 | return None; |
| 196 | } |
| 197 | // De bekende omgevingen krijgen hun vaste schrijfwijze; &bookmark= en &dc= negeren we. |
| 198 | let environment = ENVIRONMENTS |
| 199 | .into_iter() |
| 200 | .find(|known| known.eq_ignore_ascii_case(environment)) |
| 201 | .unwrap_or(environment); |
| 202 | if segments.next().is_some() { |
| 203 | return None; |
| 204 | } |
| 205 | let kind = CardKind::from_bc_page(query("page")?.parse().ok()?)?; |
| 206 | let (field, no) = parse_filter(&query("filter")?)?; |
| 207 | if field != kind.bc_filter_field() || !number_is_valid(&no) { |
| 208 | return None; |
| 209 | } |
| 210 | return Some(CardRef { |
| 211 | kind, |
| 212 | company, |
| 213 | no, |
| 214 | environment: Some(environment.to_owned()), |
| 215 | }); |
| 216 | } |
| 217 | |
| 218 | if host == SLEUTELS_HOST { |
| 219 | let app = url |
| 220 | .path_segments()? |
| 221 | .find(|s| !s.is_empty())? |
| 222 | .to_ascii_lowercase(); |
| 223 | let (kind, no) = match app.as_str() { |
| 224 | "daedalus" => (CardKind::Werkorder, query("workorder")?), |
| 225 | "ponos" => (CardKind::Project, query("project")?), |
| 226 | _ => return None, |
| 227 | }; |
| 228 | if !number_is_valid(&no) { |
| 229 | return None; |
| 230 | } |
| 231 | return Some(CardRef { |
| 232 | kind, |
| 233 | company, |
| 234 | no, |
| 235 | environment: None, |
| 236 | }); |
| 237 | } |
| 238 | |
| 239 | None |
| 240 | } |
| 241 | |
| 242 | /// Kaart-verwijzingen uit de links van één bericht: ontdubbeld, in volgorde, hooguit |
| 243 | /// [`MAX_CARDS_PER_MESSAGE`]. Daarna blijven het gewone links. |
| 244 | pub fn card_refs_in_urls<'a>(urls: impl IntoIterator<Item = &'a str>) -> Vec<CardRef> { |
| 245 | let mut refs: Vec<CardRef> = Vec::new(); |
| 246 | for url in urls { |
| 247 | let Some(card) = parse_card_link(url) else { |
| 248 | continue; |
| 249 | }; |
| 250 | if refs.iter().any(|known| { |
| 251 | known.kind == card.kind && known.company == card.company && known.no == card.no |
| 252 | }) { |
| 253 | continue; |
| 254 | } |
| 255 | refs.push(card); |
| 256 | if refs.len() == MAX_CARDS_PER_MESSAGE { |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | refs |
| 261 | } |
| 262 | |
| 263 | /// BC-webclient-link voor een verwijzing (zelfde vorm als Kairos en Mímir). |
| 264 | pub fn bc_url(kind: CardKind, company: &str, no: &str, environment: Option<&str>) -> String { |
| 265 | let environment = environment.unwrap_or(if company == "KVT Germany" { |
| 266 | "KVTGermanyLive_AAD" |
| 267 | } else { |
| 268 | "KVTMDLive_AAD" |
| 269 | }); |
| 270 | let filter = format!( |
| 271 | "'{}' IS '{}'", |
| 272 | kind.bc_filter_field(), |
| 273 | no.replace('\'', "''") |
| 274 | ); |
| 275 | format!( |
| 276 | "https://{BC_WEB_HOST}/{}/?company={}&page={}&filter={}", |
| 277 | encode(environment), |
| 278 | encode(company), |
| 279 | kind.bc_page(), |
| 280 | encode(&filter) |
| 281 | ) |
| 282 | } |
| 283 | |
| 284 | /// Zoals PHP `rawurlencode` (Kairos, Mímir): alles behalve `A-Za-z0-9-_.~`. |
| 285 | const RAWURL: &percent_encoding::AsciiSet = &percent_encoding::NON_ALPHANUMERIC |
| 286 | .remove(b'-') |
| 287 | .remove(b'_') |
| 288 | .remove(b'.') |
| 289 | .remove(b'~'); |
| 290 | |
| 291 | fn encode(value: &str) -> String { |
| 292 | percent_encoding::utf8_percent_encode(value, RAWURL).to_string() |
| 293 | } |
| 294 | |
| 295 | /// Alleen links naar de BC-webclient of sleutels.kvt.nl uit een Mímir-antwoord |
| 296 | /// mogen geopend of verzonden worden. |
| 297 | pub fn is_trusted_link(href: &str) -> bool { |
| 298 | let Ok(url) = Url::parse(href.trim()) else { |
| 299 | return false; |
| 300 | }; |
| 301 | url.scheme() == "https" |
| 302 | && url.username().is_empty() |
| 303 | && url.password().is_none() |
| 304 | && url.port().is_none() |
| 305 | && matches!(url.host_str(), Some(BC_WEB_HOST) | Some(SLEUTELS_HOST)) |
| 306 | } |
| 307 | |
| 308 | /// Ankertekst: «Werkorder WO2699001 - Onderhoud 500 uur» (omschrijving optioneel). |
| 309 | pub fn anchor_text(kind: CardKind, no: &str, title: Option<&str>) -> String { |
| 310 | match title.map(str::trim).filter(|title| !title.is_empty()) { |
| 311 | Some(title) => format!("{} {no} - {title}", kind.label_nl()), |
| 312 | None => format!("{} {no}", kind.label_nl()), |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /// Het HTML-fragment dat verzonden wordt: een gewone link, zodat de officiële Teams |
| 317 | /// een klikbare tekst toont en rusty-teams er een kaart van maakt. |
| 318 | pub fn anchor_html( |
| 319 | kind: CardKind, |
| 320 | company: &str, |
| 321 | no: &str, |
| 322 | title: Option<&str>, |
| 323 | environment: Option<&str>, |
| 324 | ) -> String { |
| 325 | format!( |
| 326 | "<a href=\"{}\">{}</a>", |
| 327 | escape_html(&bc_url(kind, company, no, environment)), |
| 328 | escape_html(&anchor_text(kind, no, title)) |
| 329 | ) |
| 330 | } |
| 331 | |
| 332 | fn escape_html(text: &str) -> String { |
| 333 | let mut out = String::with_capacity(text.len()); |
| 334 | for c in text.chars() { |
| 335 | match c { |
| 336 | '&' => out.push_str("&"), |
| 337 | '<' => out.push_str("<"), |
| 338 | '>' => out.push_str(">"), |
| 339 | '"' => out.push_str("""), |
| 340 | '\'' => out.push_str("'"), |
| 341 | _ => out.push(c), |
| 342 | } |
| 343 | } |
| 344 | out |
| 345 | } |
| 346 | |
| 347 | #[cfg(test)] |
| 348 | mod tests { |
| 349 | use super::*; |
| 350 | |
| 351 | const WO: &str = "https://kvtmd365.kvt.nl/KVTMDLive_AAD/?company=Koninklijke%20van%20Twist&page=11333028&filter=%27No.%27%20IS%20%27WO2699001%27"; |
| 352 | |
| 353 | #[test] |
| 354 | fn recognises_a_work_order_link() { |
| 355 | let card = parse_card_link(WO).unwrap(); |
| 356 | assert_eq!(card.kind, CardKind::Werkorder); |
| 357 | assert_eq!(card.company, "Koninklijke van Twist"); |
| 358 | assert_eq!(card.no, "WO2699001"); |
| 359 | assert_eq!(card.environment.as_deref(), Some("KVTMDLive_AAD")); |
| 360 | } |
| 361 | |
| 362 | #[test] |
| 363 | fn recognises_project_and_contract_pages() { |
| 364 | let project = parse_card_link("https://kvtmd365.kvt.nl/KVTMDLive_AAD/?company=Koninklijke%20van%20Twist&page=88&filter=%27No.%27%20IS%20%27PRJ2699001%27").unwrap(); |
| 365 | assert_eq!(project.kind, CardKind::Project); |
| 366 | let contract = parse_card_link("https://kvtmd365.kvt.nl/KVTGermanyLive_AAD/?company=KVT%20Germany&page=11333015&filter=%27Contract%20No.%27%20IS%20%27CT2699001%27").unwrap(); |
| 367 | assert_eq!(contract.kind, CardKind::Contract); |
| 368 | assert_eq!(contract.environment.as_deref(), Some("KVTGermanyLive_AAD")); |
| 369 | } |
| 370 | |
| 371 | #[test] |
| 372 | fn recognises_customer_component_and_item_pages() { |
| 373 | for (page, kind, no) in [ |
| 374 | (21, CardKind::Klant, "269901"), |
| 375 | (11332880, CardKind::Component, "COM2699001"), |
| 376 | (30, CardKind::Artikel, "2699001"), |
| 377 | ] { |
| 378 | let href = format!( |
| 379 | "https://kvtmd365.kvt.nl/KVTMDLive_AAD/?company=Koninklijke%20van%20Twist&page={page}&filter=%27No.%27%20IS%20%27{no}%27" |
| 380 | ); |
| 381 | let card = parse_card_link(&href).unwrap(); |
| 382 | assert_eq!((card.kind, card.no.as_str()), (kind, no)); |
| 383 | assert_eq!(bc_url(kind, "Koninklijke van Twist", no, None), href); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | #[test] |
| 388 | fn other_patterns_stay_plain_links() { |
| 389 | for href in [ |
| 390 | "http://kvtmd365.kvt.nl/KVTMDLive_AAD/?company=X&page=88&filter=%27No.%27%20IS%20%27P1%27", |
| 391 | "https://kvtmd365.kvt.nl.evil.example/KVTMDLive_AAD/?company=X&page=88&filter=%27No.%27%20IS%20%27P1%27", |
| 392 | "https://kvtmd365.kvt.nl/Other/Env/?company=X&page=88&filter=%27No.%27%20IS%20%27P1%27", |
| 393 | "https://bc.example.com/KVTMDLive_AAD/?company=X&page=88&filter=%27No.%27%20IS%20%27P1%27", |
| 394 | "https://kvt.nl/KVTMDLive_AAD/?company=X&page=88&filter=%27No.%27%20IS%20%27P1%27", |
| 395 | "https://kvtmd365.kvt.nl/KVTMDLive_AAD/?company=X&page=88&filter=%27No.%27%20IS%20%27P1%27%20AND%20x", |
| 396 | "https://kvtmd365.kvt.nl/KVTMDLive_AAD/?company=X&page=88", |
| 397 | "https://kvtmd365.kvt.nl/KVTMDLive_AAD/?company=X&page=999&filter=%27No.%27%20IS%20%27P1%27", |
| 398 | "https://kvtmd365.kvt.nl/KVTMDLive_AAD/?company=X&page=22&filter=%27No.%27%20IS%20%27C1%27", |
| 399 | "https://kvtmd365.kvt.nl/KVTMDLive_AAD/?company=X&page=11333015&filter=%27No.%27%20IS%20%27CT1%27", |
| 400 | "https://kvtmd365.kvt.nl/KVTMDLive_AAD/?company=X&page=88&filter=%27No.%27%20IS%20%27P1%27%27%20or%20%27x%27", |
| 401 | "https://kvtmd365.kvt.nl/KVTMDLive_AAD/?company=X&page=88&filter=%27No.%27%20IS%20%27P%201%27", |
| 402 | "https://kvtmd365.kvt.nl/KVTMDLive_AAD/?page=88&filter=%27No.%27%20IS%20%27P1%27", |
| 403 | "https://user:pw@kvtmd365.kvt.nl/KVTMDLive_AAD/?company=X&page=88&filter=%27No.%27%20IS%20%27P1%27", |
| 404 | "https://example.com/?company=X&workorder=WO1", |
| 405 | "https://sleutels.kvt.nl/asclepius/?company=X&workorder=WO1", |
| 406 | ] { |
| 407 | assert_eq!(parse_card_link(href), None, "{href}"); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | #[test] |
| 412 | fn recognises_bookmark_and_dc_variants() { |
| 413 | for tail in ["&bookmark=27%3BfwAAAAJ7", "&dc=0&bookmark=ABC", "&dc=0"] { |
| 414 | let href = format!("{WO}{tail}"); |
| 415 | let card = parse_card_link(&href).unwrap(); |
| 416 | assert_eq!( |
| 417 | (card.kind, card.no.as_str()), |
| 418 | (CardKind::Werkorder, "WO2699001") |
| 419 | ); |
| 420 | } |
| 421 | // Volgorde van de parameters maakt niet uit. |
| 422 | let reordered = "https://kvtmd365.kvt.nl/KVTMDLive_AAD/?page=88&dc=0&filter=%27No.%27%20IS%20%27PRJ2602195%27&company=Koninklijke%20van%20Twist"; |
| 423 | let card = parse_card_link(reordered).unwrap(); |
| 424 | assert_eq!( |
| 425 | (card.kind, card.no.as_str()), |
| 426 | (CardKind::Project, "PRJ2602195") |
| 427 | ); |
| 428 | } |
| 429 | |
| 430 | #[test] |
| 431 | fn filters_work_with_or_without_quotes_and_in_any_case() { |
| 432 | let link = |page: u32, filter: &str| { |
| 433 | format!( |
| 434 | "https://kvtmd365.kvt.nl/KVTMDLive_AAD/?company=Koninklijke%20van%20Twist&page={page}&filter={filter}" |
| 435 | ) |
| 436 | }; |
| 437 | for (filter, no) in [ |
| 438 | ("%27No.%27%20IS%20%27PRJ1%27", "PRJ1"), |
| 439 | ("No.%20IS%20PRJ1", "PRJ1"), |
| 440 | ("%27No.%27%20is%20PRJ1", "PRJ1"), |
| 441 | ("%27No.%27+IS+%27PRJ1%27", "PRJ1"), |
| 442 | ("No.%20Is%20%27PRJ1%27", "PRJ1"), |
| 443 | ] { |
| 444 | let card = parse_card_link(&link(88, filter)).unwrap_or_else(|| panic!("{filter}")); |
| 445 | assert_eq!(card.no, no, "{filter}"); |
| 446 | } |
| 447 | let contract = parse_card_link(&link(11333015, "Contract%20No.%20IS%20CT1")).unwrap(); |
| 448 | assert_eq!( |
| 449 | (contract.kind, contract.no.as_str()), |
| 450 | (CardKind::Contract, "CT1") |
| 451 | ); |
| 452 | let quoted = |
| 453 | parse_card_link(&link(11333015, "%27Contract%20No.%27%20IS%20%27CT1%27")).unwrap(); |
| 454 | assert_eq!(quoted.no, "CT1"); |
| 455 | } |
| 456 | |
| 457 | #[test] |
| 458 | fn other_kvt_hosts_and_environments_give_cards() { |
| 459 | let other_env = "https://kvtmd365.kvt.nl/KVTMDTest_AAD/?company=Koninklijke%20van%20Twist&page=88&filter=%27No.%27%20IS%20%27PRJ1%27"; |
| 460 | let card = parse_card_link(other_env).unwrap(); |
| 461 | assert_eq!(card.environment.as_deref(), Some("KVTMDTest_AAD")); |
| 462 | let other_host = "https://bc-test.kvt.nl/KVTMDLive_AAD/?company=Koninklijke%20van%20Twist&page=88&filter=%27No.%27%20IS%20%27PRJ1%27"; |
| 463 | assert_eq!(parse_card_link(other_host).unwrap().no, "PRJ1"); |
| 464 | // Hoofdletters in de omgeving: bekende schrijfwijze terug. |
| 465 | let lower = "https://kvtmd365.kvt.nl/kvtmdlive_aad/?company=X&page=88&filter=%27No.%27%20IS%20%27P1%27"; |
| 466 | assert_eq!( |
| 467 | parse_card_link(lower).unwrap().environment.as_deref(), |
| 468 | Some("KVTMDLive_AAD") |
| 469 | ); |
| 470 | } |
| 471 | |
| 472 | #[test] |
| 473 | fn only_bc_and_sleutels_links_are_trusted() { |
| 474 | assert!(is_trusted_link( |
| 475 | "https://kvtmd365.kvt.nl/KVTMDLive_AAD/?page=88" |
| 476 | )); |
| 477 | assert!(is_trusted_link( |
| 478 | "https://sleutels.kvt.nl/daedalus/index.php" |
| 479 | )); |
| 480 | assert!(!is_trusted_link("http://kvtmd365.kvt.nl/")); |
| 481 | assert!(!is_trusted_link("https://kvtmd365.kvt.nl.example.com/")); |
| 482 | assert!(!is_trusted_link("https://user@sleutels.kvt.nl/")); |
| 483 | assert!(!is_trusted_link("https://sleutels.kvt.nl:8443/")); |
| 484 | assert!(!is_trusted_link("javascript:alert(1)")); |
| 485 | } |
| 486 | |
| 487 | #[test] |
| 488 | fn recognises_sleutels_links() { |
| 489 | let daedalus = parse_card_link("https://sleutels.kvt.nl/daedalus/index.php?company=Koninklijke%20van%20Twist&person=9001&workorder=WO2699001").unwrap(); |
| 490 | assert_eq!( |
| 491 | (daedalus.kind, daedalus.no.as_str()), |
| 492 | (CardKind::Werkorder, "WO2699001") |
| 493 | ); |
| 494 | let ponos = parse_card_link("https://sleutels.kvt.nl/ponos/?company=Koninklijke%20van%20Twist&dept=X&project=PRJ2699001&task=1").unwrap(); |
| 495 | assert_eq!( |
| 496 | (ponos.kind, ponos.no.as_str()), |
| 497 | (CardKind::Project, "PRJ2699001") |
| 498 | ); |
| 499 | } |
| 500 | |
| 501 | #[test] |
| 502 | fn at_most_three_cards_and_no_duplicates() { |
| 503 | let p = |n: u32| { |
| 504 | format!( |
| 505 | "https://kvtmd365.kvt.nl/KVTMDLive_AAD/?company=X&page=88&filter=%27No.%27%20IS%20%27P{n}%27" |
| 506 | ) |
| 507 | }; |
| 508 | let urls = [p(1), p(1), p(2), p(3), p(4)]; |
| 509 | let refs = card_refs_in_urls(urls.iter().map(String::as_str)); |
| 510 | assert_eq!( |
| 511 | refs.iter().map(|r| r.no.as_str()).collect::<Vec<_>>(), |
| 512 | ["P1", "P2", "P3"] |
| 513 | ); |
| 514 | } |
| 515 | |
| 516 | #[test] |
| 517 | fn built_link_round_trips_and_matches_mimir() { |
| 518 | let url = bc_url( |
| 519 | CardKind::Werkorder, |
| 520 | "Koninklijke van Twist", |
| 521 | "WO2699001", |
| 522 | None, |
| 523 | ); |
| 524 | assert_eq!(url, WO); |
| 525 | let card = parse_card_link(&url).unwrap(); |
| 526 | assert_eq!(card.no, "WO2699001"); |
| 527 | let contract = bc_url( |
| 528 | CardKind::Contract, |
| 529 | "Koninklijke van Twist", |
| 530 | "CT25000004", |
| 531 | None, |
| 532 | ); |
| 533 | assert!( |
| 534 | contract.ends_with("page=11333015&filter=%27Contract%20No.%27%20IS%20%27CT25000004%27") |
| 535 | ); |
| 536 | assert_eq!(parse_card_link(&contract).unwrap().kind, CardKind::Contract); |
| 537 | } |
| 538 | |
| 539 | #[test] |
| 540 | fn anchor_is_readable_and_escaped() { |
| 541 | assert_eq!( |
| 542 | anchor_text(CardKind::Werkorder, "WO2699001", Some("Onderhoud 500 uur")), |
| 543 | "Werkorder WO2699001 - Onderhoud 500 uur" |
| 544 | ); |
| 545 | assert_eq!(anchor_text(CardKind::Project, "PRJ1", None), "Project PRJ1"); |
| 546 | let html = anchor_html( |
| 547 | CardKind::Project, |
| 548 | "Koninklijke van Twist", |
| 549 | "PRJ1", |
| 550 | Some("<b>A & B</b>"), |
| 551 | None, |
| 552 | ); |
| 553 | assert!(html.starts_with("<a href=\"https://kvtmd365.kvt.nl/KVTMDLive_AAD/?company=Koninklijke%20van%20Twist&page=88")); |
| 554 | assert!(html.ends_with(">Project PRJ1 - <b>A & B</b></a>")); |
| 555 | } |
| 556 | } |