BlueGit

ict rusty-teams

Files

rusty-teams / crates / chatsvc / tests / internal_endpoints.rs

internal_endpoints.rs

internal_endpoints.rs 4.4 KiB Download Raw
1use std::fs;
2use std::path::{Path, PathBuf};
3
4const NON_GRAPH_HOSTS: &[&str] = &[
5 "cloud.microsoft",
6 "microsoft.com",
7 "office.com",
8 "office.net",
9 "skype.com",
10 "live.com",
11 "sharepoint.com",
12 "microsoftonline.com",
13 "microsoft365.com",
14 "giphy.com",
15];
16
17const NOT_A_SERVICE: &[&str] = &["graph.microsoft.com", "schema.skype.com"];
18
19const ALLOWED_PATHS: &[(&str, &str)] = &[
20 ("chatsvc/", "Teams service clients, one module per service"),
21 ("calling/src/signaling.rs", "call signaling and flightproxy"),
22 ("calling/src/relay.rs", "TURN relay tokens"),
23 ("calling/src/meeting_options.rs", "meeting options MT"),
24 ("calling/src/whiteboard.rs", "meeting whiteboard MT"),
25 ("calling/src/breakout.rs", "breakout room join link parsing"),
26 ("calling/assets/", "in-page caption bot command"),
27 (
28 "session/",
29 "sign-in, token refresh, page hosts, token audiences",
30 ),
31 ("browser/", "headless browser page targets"),
32 ("webview/", "WebView2 page hosts"),
33 ("app-base/src/task_dialog/sdk.rs", "task dialog host origin"),
34 ("core/src/links.rs", "link classification"),
35 ("core/src/meeting_join.rs", "meeting join link parsing"),
36 ("core/src/message_link.rs", "message link parsing"),
37 ("core/src/call_event.rs", "recording link host check"),
38 ("core/src/external_image.rs", "GIF host check"),
39 ("app-call-model/src/agenda.rs", "demo join link"),
40 ("app-call-model/src/demo.rs", "demo data"),
41 ("app-state/src/demo.rs", "demo data"),
42 ("app-base/src/demo_", "demo data"),
43 ("app/src/layout_guard.rs", "layout test fixture"),
44 ("graph/src/tabs.rs", "channel tab web links"),
45 ("app/src/inline_video.rs", "SharePoint video player link"),
46 ("app/src/views/status_menu.rs", "View account web link"),
47 ("app-state/src/call/week.rs", "demo join link"),
48];
49
50#[test]
51fn non_graph_hosts_stay_in_their_service_modules() {
52 let crates_directory = Path::new(env!("CARGO_MANIFEST_DIR")).join("..");
53 let mut files = Vec::new();
54 source_files(&crates_directory, &mut files);
55 let mut offenders = Vec::new();
56 for file in files {
57 let relative = file
58 .strip_prefix(&crates_directory)
59 .expect("file below crates")
60 .to_string_lossy()
61 .replace('\\', "/");
62 if ALLOWED_PATHS
63 .iter()
64 .any(|(prefix, _)| relative.starts_with(prefix))
65 {
66 continue;
67 }
68 let source = fs::read_to_string(&file).expect("read source file");
69 for (index, line) in production_lines(&source) {
70 if names_non_graph_host(line) {
71 offenders.push(format!("{relative}:{}: {}", index + 1, line.trim()));
72 }
73 }
74 }
75 assert!(
76 offenders.is_empty(),
77 "non-Graph hosts outside their service modules (move them or extend ALLOWED_PATHS and docs/research/internal-endpoints.md):\n{}",
78 offenders.join("\n")
79 );
80}
81
82fn source_files(directory: &Path, files: &mut Vec<PathBuf>) {
83 for entry in fs::read_dir(directory).expect("read source directory") {
84 let path = entry.expect("directory entry").path();
85 let name = path
86 .file_name()
87 .and_then(|name| name.to_str())
88 .unwrap_or("");
89 if path.is_dir() {
90 if !matches!(name, "tests" | "examples" | "target" | "benches") {
91 source_files(&path, files);
92 }
93 } else if (name.ends_with(".rs") || name.ends_with(".js")) && name != "tests.rs" {
94 files.push(path);
95 }
96 }
97}
98
99fn production_lines(source: &str) -> impl Iterator<Item = (usize, &str)> {
100 source
101 .lines()
102 .enumerate()
103 .take_while(|(_, line)| line.trim() != "#[cfg(test)]")
104 .filter(|(_, line)| !line.trim_start().starts_with("//"))
105}
106
107fn names_non_graph_host(line: &str) -> bool {
108 let mut cleaned = line.to_owned();
109 for host in NOT_A_SERVICE {
110 cleaned = cleaned.replace(host, "");
111 }
112 NON_GRAPH_HOSTS.iter().any(|host| cleaned.contains(host))
113}
114
115#[test]
116fn host_check_ignores_graph_and_markup() {
117 assert!(!names_non_graph_host(
118 r#"const GRAPH: &str = "https://graph.microsoft.com";"#
119 ));
120 assert!(!names_non_graph_host(
121 r#"itemtype="http://schema.skype.com/Mention""#
122 ));
123 assert!(names_non_graph_host(
124 r#"format!("https://teams.cloud.microsoft/api/mt/{region}")"#
125 ));
126}