Skip to content

Commit cda9f33

Browse files
test(rust-guard): add list_commits tests for default-branch vs feature-branch integrity
Add two unit tests to response_paths.rs covering the list_commits labeling branch — the only response-path handler that was untested. The sha field drives is_default_branch_ref() → merged-level integrity promotion, which is a security-relevant decision: treating all commits as default-branch would over-elevate integrity labels. Tests added: - list_commits_default_branch_gets_merged_integrity: sha=main → default_labels integrity contains merged:octocat/hello-world; items_path is None (root array) - list_commits_feature_branch_public_repo_has_no_merged_integrity: sha=feature/my-branch → default_labels integrity has no merged: prefix; items_path is None All 413 Rust guard unit tests pass (411 pre-existing + 2 new). Closes #6086 (Improvement 2) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b63f8fb commit cda9f33

1 file changed

Lines changed: 88 additions & 0 deletions

File tree

guards/github-guard/rust-guard/src/labels/response_paths.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,4 +751,92 @@ mod tests {
751751
let result = label_response_paths("unknown_tool", &json!({}), &json!({}), &ctx());
752752
assert!(result.is_none(), "unknown tool should produce no path labels");
753753
}
754+
755+
// === list_commits tests ===
756+
// The sha field drives is_default_branch → merged-level integrity, which is a
757+
// security-relevant decision. A regression here (e.g. treating all commits as
758+
// default-branch) would over-elevate integrity labels. Both tests are self-contained
759+
// and require no backend mocking.
760+
761+
#[test]
762+
fn list_commits_default_branch_gets_merged_integrity() {
763+
let tool_args = json!({"owner": "octocat", "repo": "hello-world", "sha": "main"});
764+
let commit = json!({
765+
"sha": "abc1234def5678",
766+
"commit": {"message": "fix: a bug"},
767+
"author": {"login": "octocat"},
768+
"author_association": "OWNER"
769+
});
770+
let response = json!({
771+
"content": [{
772+
"type": "text",
773+
"text": serde_json::to_string(&json!([commit])).expect("response should serialize")
774+
}]
775+
});
776+
777+
let result = label_response_paths("list_commits", &tool_args, &response, &ctx())
778+
.expect("list_commits should produce path labels");
779+
780+
assert_eq!(result.labeled_paths.len(), 1);
781+
assert_eq!(result.labeled_paths[0].path, "/0");
782+
assert!(
783+
result.items_path.is_none(),
784+
"list_commits root array should have items_path = None, got {:?}",
785+
result.items_path
786+
);
787+
788+
// Default branch (main) → default_labels integrity must include a merged: label
789+
let default_integrity = &result
790+
.default_labels
791+
.as_ref()
792+
.expect("default_labels should be set")
793+
.integrity;
794+
let merged_label = format!("{}octocat/hello-world", label_constants::MERGED_PREFIX);
795+
assert!(
796+
default_integrity.contains(&merged_label),
797+
"default-branch default_labels should have merged-level integrity; got {:?}",
798+
default_integrity
799+
);
800+
}
801+
802+
#[test]
803+
fn list_commits_feature_branch_public_repo_has_no_merged_integrity() {
804+
let tool_args =
805+
json!({"owner": "octocat", "repo": "hello-world", "sha": "feature/my-branch"});
806+
let commit = json!({
807+
"sha": "deadbeef12345678",
808+
"commit": {"message": "wip: in progress"},
809+
"author_association": "CONTRIBUTOR"
810+
});
811+
let response = json!({
812+
"content": [{
813+
"type": "text",
814+
"text": serde_json::to_string(&json!([commit])).expect("response should serialize")
815+
}]
816+
});
817+
818+
let result = label_response_paths("list_commits", &tool_args, &response, &ctx())
819+
.expect("list_commits should produce path labels");
820+
821+
assert_eq!(result.labeled_paths.len(), 1);
822+
assert!(
823+
result.items_path.is_none(),
824+
"list_commits root array should have items_path = None"
825+
);
826+
827+
// Non-default branch of public repo (is_repo_private returns None → false in
828+
// test cfg) → default_labels integrity must NOT contain any merged: label.
829+
let default_integrity = &result
830+
.default_labels
831+
.as_ref()
832+
.expect("default_labels should be set")
833+
.integrity;
834+
assert!(
835+
!default_integrity
836+
.iter()
837+
.any(|l| l.starts_with(label_constants::MERGED_PREFIX)),
838+
"feature-branch commit on public repo should NOT have merged-level integrity; got {:?}",
839+
default_integrity
840+
);
841+
}
754842
}

0 commit comments

Comments
 (0)