test(endpoints): fail when a non-Graph host appears outside its service module
- source scan over crates, allowlist of service, page-host, link-parsing and demo paths Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -0,0 +1,123 @@
1
use std::fs;
2
use std::path::{Path, PathBuf};
3
4
const 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
17
const NOT_A_SERVICE: &[&str] = &["graph.microsoft.com", "schema.skype.com"];
18
19
const 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
];
46
47
#[test]
48
fn non_graph_hosts_stay_in_their_service_modules() {
49
let crates_directory = Path::new(env!("CARGO_MANIFEST_DIR")).join("..");
50
let mut files = Vec::new();
51
source_files(&crates_directory, &mut files);
52
let mut offenders = Vec::new();
53
for file in files {
54
let relative = file
55
.strip_prefix(&crates_directory)
56
.expect("file below crates")
57
.to_string_lossy()
58
.replace('\\', "/");
59
if ALLOWED_PATHS
60
.iter()
61
.any(|(prefix, _)| relative.starts_with(prefix))
62
{
63
continue;
64
}
65
let source = fs::read_to_string(&file).expect("read source file");
66
for (index, line) in production_lines(&source) {
67
if names_non_graph_host(line) {
68
offenders.push(format!("{relative}:{}: {}", index + 1, line.trim()));
69
}
70
}
71
}
72
assert!(
73
offenders.is_empty(),
74
"non-Graph hosts outside their service modules (move them or extend ALLOWED_PATHS and docs/research/internal-endpoints.md):\n{}",
75
offenders.join("\n")
76
);
77
}
78
79
fn source_files(directory: &Path, files: &mut Vec<PathBuf>) {
80
for entry in fs::read_dir(directory).expect("read source directory") {
81
let path = entry.expect("directory entry").path();
82
let name = path
83
.file_name()
84
.and_then(|name| name.to_str())
85
.unwrap_or("");
86
if path.is_dir() {
87
if !matches!(name, "tests" | "examples" | "target" | "benches") {
88
source_files(&path, files);
89
}
90
} else if (name.ends_with(".rs") || name.ends_with(".js")) && name != "tests.rs" {
91
files.push(path);
92
}
93
}
94
}
95
96
fn production_lines(source: &str) -> impl Iterator<Item = (usize, &str)> {
97
source
98
.lines()
99
.enumerate()
100
.take_while(|(_, line)| line.trim() != "#[cfg(test)]")
101
.filter(|(_, line)| !line.trim_start().starts_with("//"))
102
}
103
104
fn names_non_graph_host(line: &str) -> bool {
105
let mut cleaned = line.to_owned();
106
for host in NOT_A_SERVICE {
107
cleaned = cleaned.replace(host, "");
108
}
109
NON_GRAPH_HOSTS.iter().any(|host| cleaned.contains(host))
110
}
111
112
#[test]
113
fn host_check_ignores_graph_and_markup() {
114
assert!(!names_non_graph_host(
115
r#"const GRAPH: &str = "https://graph.microsoft.com";"#
116
));
117
assert!(!names_non_graph_host(
118
r#"itemtype="http://schema.skype.com/Mention""#
119
));
120
assert!(names_non_graph_host(
121
r#"format!("https://teams.cloud.microsoft/api/mt/{region}")"#
122
));
123
}
@@ -0,0 +1,123 @@1
use std::fs;2
use std::path::{Path, PathBuf};3
4
const 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
17
const NOT_A_SERVICE: &[&str] = &["graph.microsoft.com", "schema.skype.com"];18
19
const 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
];46
47
#[test]48
fn non_graph_hosts_stay_in_their_service_modules() {49
let crates_directory = Path::new(env!("CARGO_MANIFEST_DIR")).join("..");50
let mut files = Vec::new();51
source_files(&crates_directory, &mut files);52
let mut offenders = Vec::new();53
for file in files {54
let relative = file55
.strip_prefix(&crates_directory)56
.expect("file below crates")57
.to_string_lossy()58
.replace('\\', "/");59
if ALLOWED_PATHS60
.iter()61
.any(|(prefix, _)| relative.starts_with(prefix))62
{63
continue;64
}65
let source = fs::read_to_string(&file).expect("read source file");66
for (index, line) in production_lines(&source) {67
if names_non_graph_host(line) {68
offenders.push(format!("{relative}:{}: {}", index + 1, line.trim()));69
}70
}71
}72
assert!(73
offenders.is_empty(),74
"non-Graph hosts outside their service modules (move them or extend ALLOWED_PATHS and docs/research/internal-endpoints.md):\n{}",75
offenders.join("\n")76
);77
}78
79
fn source_files(directory: &Path, files: &mut Vec<PathBuf>) {80
for entry in fs::read_dir(directory).expect("read source directory") {81
let path = entry.expect("directory entry").path();82
let name = path83
.file_name()84
.and_then(|name| name.to_str())85
.unwrap_or("");86
if path.is_dir() {87
if !matches!(name, "tests" | "examples" | "target" | "benches") {88
source_files(&path, files);89
}90
} else if (name.ends_with(".rs") || name.ends_with(".js")) && name != "tests.rs" {91
files.push(path);92
}93
}94
}95
96
fn production_lines(source: &str) -> impl Iterator<Item = (usize, &str)> {97
source98
.lines()99
.enumerate()100
.take_while(|(_, line)| line.trim() != "#[cfg(test)]")101
.filter(|(_, line)| !line.trim_start().starts_with("//"))102
}103
104
fn names_non_graph_host(line: &str) -> bool {105
let mut cleaned = line.to_owned();106
for host in NOT_A_SERVICE {107
cleaned = cleaned.replace(host, "");108
}109
NON_GRAPH_HOSTS.iter().any(|host| cleaned.contains(host))110
}111
112
#[test]113
fn host_check_ignores_graph_and_markup() {114
assert!(!names_non_graph_host(115
r#"const GRAPH: &str = "https://graph.microsoft.com";"#116
));117
assert!(!names_non_graph_host(118
r#"itemtype="http://schema.skype.com/Mention""#119
));120
assert!(names_non_graph_host(121
r#"format!("https://teams.cloud.microsoft/api/mt/{region}")"#122
));123
}