#[cfg(test)]
use crate::constants::{ ct, hdr };
use crate::test_utils::{ mock_client, seed };
use httpmock::Method::GET;
use httpmock::Method::POST;
use httpmock::MockServer;
#[tokio::test]
async fn list_repositories() {
let server = MockServer::start();
let _mock = server.mock(|when, then| {
when.method(GET).path("/_apis/git/repositories");
then.status(200).header(hdr::CONTENT_TYPE, ct::JSON).json_body(seed::repositories_list());
});
let client = mock_client(&server);
let repos = client.repositories().list(None).await.unwrap();
assert_eq!(repos.len(), 1);
assert_eq!(repos[0].name.as_deref(), Some("Veles"));
}
#[tokio::test]
async fn get_repository() {
let server = MockServer::start();
let _mock = server.mock(|when, then| {
when.method(GET).path("/_apis/git/repositories/repo-guid-1");
then.status(200)
.header(hdr::CONTENT_TYPE, ct::JSON)
.json_body(seed::repositories_list().as_array().unwrap()[0].clone());
});
let client = mock_client(&server);
let repo = client.repositories().get("repo-guid-1").await.unwrap();
assert_eq!(repo.name.as_deref(), Some("Veles"));
}
#[tokio::test]
async fn list_commits_smoke() {
let json =
serde_json::json!([{
"commitId": "abc123def456",
"comment": "Initial commit",
"author": { "name": "Ruslan", "email": "ruslan@example.com", "date": "2024-01-15T10:00:00Z" }
}]);
let commits: Vec<crate::repositories::models::GitCommit> = serde_json
::from_value(json)
.unwrap();
assert_eq!(commits.len(), 1);
assert_eq!(commits[0].commit_id.as_deref(), Some("abc123def456"));
}
#[tokio::test]
async fn list_pull_requests() {
let server = MockServer::start();
let _mock = server.mock(|when, then| {
when.method(GET).path("/_apis/git/repositories/repo-guid-1/pullrequests");
then.status(200).header(hdr::CONTENT_TYPE, ct::JSON).json_body(seed::pull_requests_list());
});
let client = mock_client(&server);
let criteria = crate::pull_requests::models::GitPullRequestSearchCriteria::default();
let prs = client.pull_requests().list("repo-guid-1", &criteria).await.unwrap();
assert_eq!(prs.len(), 1);
assert_eq!(prs[0].title.as_deref(), Some("Add feature X"));
}
#[tokio::test]
async fn get_pull_request() {
let server = MockServer::start();
let _mock = server.mock(|when, then| {
when.method(GET).path("/_apis/git/repositories/repo-guid-1/pullrequests/1");
then.status(200)
.header(hdr::CONTENT_TYPE, ct::JSON)
.json_body(seed::pull_requests_list().as_array().unwrap()[0].clone());
});
let client = mock_client(&server);
let pr = client.pull_requests().get("repo-guid-1", 1).await.unwrap();
assert_eq!(pr.pull_request_id, Some(1));
}
#[tokio::test]
async fn create_pull_request() {
let server = MockServer::start();
let _mock = server.mock(|when, then| {
when.method(POST).path("/_apis/git/repositories/repo-guid-1/pullrequests");
then.status(201)
.header(hdr::CONTENT_TYPE, ct::JSON)
.json_body(seed::pull_requests_list().as_array().unwrap()[0].clone());
});
let client = mock_client(&server);
let pr = crate::pull_requests::models::GitPullRequest {
title: Some("New PR".into()),
source_ref_name: Some("refs/heads/feature".into()),
target_ref_name: Some("refs/heads/main".into()),
..Default::default()
};
let result = client.pull_requests().create("repo-guid-1", &pr).await.unwrap();
assert_eq!(result.pull_request_id, Some(1));
}
#[tokio::test]
async fn list_pr_reviewers() {
let server = MockServer::start();
let _mock = server.mock(|when, then| {
when.method(GET).path("/_apis/git/repositories/repo-guid-1/pullrequests/1/reviewers");
then.status(200)
.header(hdr::CONTENT_TYPE, ct::JSON)
.json_body(
serde_json::json!([{
"displayName": "Reviewer One", "id": "r1", "vote": 10,
"hasDeclined": false, "isRequired": true
}])
);
});
let client = mock_client(&server);
let reviewers = client.pull_requests().list_reviewers("repo-guid-1", 1).await.unwrap();
assert_eq!(reviewers.len(), 1);
assert_eq!(reviewers[0].vote, 10);
}
#[tokio::test]
async fn list_pr_threads() {
let server = MockServer::start();
let _mock = server.mock(|when, then| {
when.method(GET).path("/_apis/git/repositories/repo-guid-1/pullrequests/1/threads");
then.status(200)
.header(hdr::CONTENT_TYPE, ct::JSON)
.json_body(
serde_json::json!([{
"id": 1, "status": "active",
"comments": [{ "id": 1, "content": "Looks good!", "commentType": "text" }]
}])
);
});
let client = mock_client(&server);
let threads = client.pull_requests().list_threads("repo-guid-1", 1).await.unwrap();
assert_eq!(threads.len(), 1);
let comments = threads[0].comments.as_ref().unwrap();
assert_eq!(comments[0].content.as_deref(), Some("Looks good!"));
}
#[tokio::test]
async fn get_pull_request_by_id() {
let server = MockServer::start();
let _mock = server.mock(|when, then| {
when.method(GET).path("/_apis/git/pullrequests/1");
then.status(200)
.header(hdr::CONTENT_TYPE, ct::JSON)
.json_body(seed::pull_requests_list().as_array().unwrap()[0].clone());
});
let client = mock_client(&server);
let pr = client.pull_requests().get_by_id(1).await.unwrap();
assert_eq!(pr.pull_request_id, Some(1));
}
#[tokio::test]
async fn list_pull_requests_by_project() {
let server = MockServer::start();
let _mock = server.mock(|when, then| {
when.method(GET).path("/Veles/_apis/git/pullrequests");
then.status(200).header(hdr::CONTENT_TYPE, ct::JSON).json_body(seed::pull_requests_list());
});
let client = mock_client(&server);
let criteria = crate::pull_requests::models::GitPullRequestSearchCriteria::default();
let prs = client.pull_requests().list_by_project("Veles", &criteria).await.unwrap();
assert_eq!(prs.len(), 1);
}
#[tokio::test]
async fn list_pr_commits() {
let server = MockServer::start();
let _mock = server.mock(|when, then| {
when.method(GET).path("/_apis/git/repositories/repo-guid-1/pullrequests/1/commits");
then.status(200)
.header(hdr::CONTENT_TYPE, ct::JSON)
.json_body(
serde_json::json!([{
"commitId": "abc123", "comment": "Fix bug",
"author": { "name": "Ruslan", "email": "ruslan@example.com", "date": "2024-01-15T10:00:00Z" }
}])
);
});
let client = mock_client(&server);
let commits = client.pull_requests().list_commits("repo-guid-1", 1).await.unwrap();
assert_eq!(commits.len(), 1);
assert_eq!(commits[0].commit_id.as_deref(), Some("abc123"));
}
#[tokio::test]
async fn list_pr_work_item_refs() {
let server = MockServer::start();
let _mock = server.mock(|when, then| {
when.method(GET).path("/_apis/git/repositories/repo-guid-1/pullrequests/1/workitems");
then.status(200)
.header(hdr::CONTENT_TYPE, ct::JSON)
.json_body(
serde_json::json!([{
"id": "123", "url": "https://dev.azure.com/ruslan/_apis/wit/workItems/123"
}])
);
});
let client = mock_client(&server);
let refs = client.pull_requests().list_work_item_refs("repo-guid-1", 1).await.unwrap();
assert_eq!(refs.len(), 1);
}
#[tokio::test]
async fn add_pr_reviewer() {
let server = MockServer::start();
let _mock = server.mock(|when, then| {
when.method(httpmock::Method::PUT).path(
"/_apis/git/repositories/repo-guid-1/pullrequests/1/reviewers/user1"
);
then.status(200)
.header(hdr::CONTENT_TYPE, ct::JSON)
.json_body(
serde_json::json!({
"displayName": "Reviewer One", "id": "user1", "vote": 0,
"hasDeclined": false, "isRequired": true
})
);
});
let client = mock_client(&server);
let reviewer = client.pull_requests().add_reviewer("repo-guid-1", 1, "user1").await.unwrap();
assert_eq!(reviewer.id.as_deref(), Some("user1"));
}
#[tokio::test]
async fn create_pr_comment() {
let server = MockServer::start();
let _mock = server.mock(|when, then| {
when.method(POST).path(
"/_apis/git/repositories/repo-guid-1/pullrequests/1/threads/1/comments"
);
then.status(200)
.header(hdr::CONTENT_TYPE, ct::JSON)
.json_body(
serde_json::json!({
"id": 2, "content": "Nice work!", "commentType": "text"
})
);
});
let client = mock_client(&server);
let comment = crate::pull_requests::models::Comment {
content: Some("Nice work!".into()),
..Default::default()
};
let result = client
.pull_requests()
.create_comment("repo-guid-1", 1, 1, &comment).await
.unwrap();
assert_eq!(result.id, Some(2));
}
#[tokio::test]
async fn create_pr_thread() {
let server = MockServer::start();
let _mock = server.mock(|when, then| {
when.method(POST).path("/_apis/git/repositories/repo-guid-1/pullrequests/1/threads");
then.status(200)
.header(hdr::CONTENT_TYPE, ct::JSON)
.json_body(
serde_json::json!({
"id": 2, "status": "active",
"comments": [{ "id": 1, "content": "Review comment", "commentType": "text" }]
})
);
});
let client = mock_client(&server);
let thread = crate::pull_requests::models::GitPullRequestCommentThread::default();
let result = client.pull_requests().create_thread("repo-guid-1", 1, &thread).await.unwrap();
assert_eq!(result.id, Some(2));
}
#[tokio::test]
async fn delete_pr_comment() {
let server = MockServer::start();
let _mock = server.mock(|when, then| {
when.method(httpmock::Method::DELETE).path(
"/_apis/git/repositories/repo-guid-1/pullrequests/1/threads/1/comments/1"
);
then.status(200)
.header(hdr::CONTENT_TYPE, ct::JSON)
.json_body(serde_json::json!({}));
});
let client = mock_client(&server);
let result = client.pull_requests().delete_comment("repo-guid-1", 1, 1, 1).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn update_pull_request() {
let server = MockServer::start();
let _mock = server.mock(|when, then| {
when.method(httpmock::Method::PATCH).path(
"/_apis/git/repositories/repo-guid-1/pullrequests/1"
);
then.status(200)
.header(hdr::CONTENT_TYPE, ct::JSON)
.json_body(seed::pull_requests_list().as_array().unwrap()[0].clone());
});
let client = mock_client(&server);
let pr = crate::pull_requests::models::GitPullRequest {
title: Some("Updated title".into()),
..Default::default()
};
let result = client.pull_requests().update("repo-guid-1", 1, &pr).await.unwrap();
assert_eq!(result.pull_request_id, Some(1));
}