Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend tests for Azure.create_pull_request(..., reviewers) #4056

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 113 additions & 15 deletions common/spec/dependabot/clients/azure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
end

describe "#fetch_commit" do
subject { client.fetch_commit(nil, branch) }
subject(:fetch_commit) { client.fetch_commit(nil, branch) }

context "when response is 200" do
before do
Expand All @@ -50,7 +50,7 @@
to_return(status: 200, body: fixture("azure", "master_branch.json"))
end

specify { expect { subject }.to_not raise_error }
specify { expect { fetch_commit }.to_not raise_error }

it { is_expected.to eq("9c8376e9b2e943c2c72fac4b239876f377f0305a") }
end
Expand All @@ -63,7 +63,7 @@
end

it "raises a helpful error" do
expect { subject }.to raise_error(Dependabot::Clients::Azure::NotFound)
expect { fetch_commit }.to raise_error(Dependabot::Clients::Azure::NotFound)
end
end

Expand All @@ -75,7 +75,7 @@
end

it "raises a helpful error" do
expect { subject }.to raise_error(Dependabot::Clients::Azure::Forbidden)
expect { fetch_commit }.to raise_error(Dependabot::Clients::Azure::Forbidden)
end
end

Expand All @@ -87,7 +87,7 @@
end

it "raises a helpful error" do
expect { subject }.to raise_error(Dependabot::Clients::Azure::Unauthorized)
expect { fetch_commit }.to raise_error(Dependabot::Clients::Azure::Unauthorized)
end
end

Expand All @@ -99,7 +99,7 @@
end

it "raises a helpful error" do
expect { subject }.to raise_error(Dependabot::Clients::Azure::NotFound)
expect { fetch_commit }.to raise_error(Dependabot::Clients::Azure::NotFound)
end
end
end
Expand Down Expand Up @@ -129,7 +129,7 @@
end

it "raises a helpful error" do
expect { subject }.to raise_error(Dependabot::Clients::Azure::Forbidden)
expect { create_commit }.to raise_error(Dependabot::Clients::Azure::Forbidden)
end
end

Expand Down Expand Up @@ -182,12 +182,98 @@
end

describe "#create_pull_request" do
subject do
client.create_pull_request("pr_name", "source_branch", "target_branch",
"", [], nil)
subject(:create_pull_request) do
client.create_pull_request(
pr_name, source_branch, target_branch,
pr_description, labels, reviewers, work_item
)
end

let(:pull_request_url) { repo_url + "/pullrequests?api-version=5.0" }
let(:pr_name) { "test-create-pr" }
let(:source_branch) { "feature/test-create-pr" }
let(:target_branch) { "main" }
let(:pr_description) { "PR description" }
let(:labels) { [] }
let(:reviewers) { [] }
let(:work_item) { nil }
let(:create_pull_request_url) { repo_url + "/pullrequests?api-version=5.0" }

context "when response is 200" do
response_body = fixture("azure", "create_pull_request_details.json")

before do
request_body = {
sourceRefName: "refs/heads/#{source_branch}",
targetRefName: "refs/heads/#{target_branch}", title: pr_name,
description: pr_description, labels: [], workItemRefs: [], reviewers: []
}

stub_request(:post, create_pull_request_url).
with(basic_auth: [username, password], body: JSON.dump(request_body)).
to_return(status: 200, body: response_body)
end

it "returns the parsed response" do
expect(create_pull_request).to eq(JSON.parse(response_body))
end
end

context "when response is 200 - with workItem" do
let(:work_item) { 42 }
response_body = fixture("azure", "create_pull_request_details.json")

before do
request_body = {
sourceRefName: "refs/heads/#{source_branch}",
targetRefName: "refs/heads/#{target_branch}", title: pr_name,
description: pr_description, labels: [], workItemRefs: [{ id: 42 }], reviewers: []
}

stub_request(:post, create_pull_request_url).
with(basic_auth: [username, password], body: JSON.dump(request_body)).
to_return(status: 200, body: response_body)
end

it "returns the parsed response" do
expect(create_pull_request).to eq(JSON.parse(response_body))
end
end

context "when response is 200 - with reviewer" do
let(:reviewers) { ["d6245f20-2af8-44f4-9451-8107cb2767db"] }
response_body = fixture("azure", "create_pull_request_details.json")

before do
request_body = {
sourceRefName: "refs/heads/#{source_branch}",
targetRefName: "refs/heads/#{target_branch}", title: pr_name,
description: pr_description, labels: [], workItemRefs: [],
reviewers: [{ id: "d6245f20-2af8-44f4-9451-8107cb2767db" }]
}

stub_request(:post, create_pull_request_url).
with(basic_auth: [username, password], body: JSON.dump(request_body)).
to_return(status: 200, body: response_body)
end

specify { expect { create_pull_request }.to_not raise_error }

it "returns the parsed response" do
expect(create_pull_request).to eq(JSON.parse(response_body))
end
end

context "when response is 401" do
before do
stub_request(:post, create_pull_request_url).
with(basic_auth: [username, password]).
to_return(status: 401)
end

it "raises a helpful error" do
expect { create_pull_request }.to raise_error(Dependabot::Clients::Azure::Unauthorized)
end
end

context "when response is 403 & tags creation is forbidden" do
before do
Expand Down Expand Up @@ -215,6 +301,18 @@
expect { subject }.to raise_error(Dependabot::Clients::Azure::Forbidden)
end
end

context "when response is 404" do
before do
stub_request(:post, create_pull_request_url).
with(basic_auth: [username, password]).
to_return(status: 404)
end

it "raises a helpful error" do
expect { create_pull_request }.to raise_error(Dependabot::Clients::Azure::NotFound)
end
end
end

describe "#autocomplete_pull_request" do
Expand Down Expand Up @@ -286,7 +384,7 @@
end

describe "#pull_request" do
subject { client.pull_request(pull_request_id) }
subject(:pull_request) { client.pull_request(pull_request_id) }

let(:pull_request_id) { "1" }
let(:pull_request_url) { base_url + "/_apis/git/pullrequests/#{pull_request_id}" }
Expand All @@ -300,7 +398,7 @@
to_return(status: 200, body: response_body)
end

specify { expect { subject }.to_not raise_error }
specify { expect { pull_request }.to_not raise_error }

it { is_expected.to eq(JSON.parse(response_body)) }
end
Expand All @@ -313,7 +411,7 @@
end

it "raises a helpful error" do
expect { subject }.to raise_error(Dependabot::Clients::Azure::Unauthorized)
expect { pull_request }.to raise_error(Dependabot::Clients::Azure::Unauthorized)
end
end

Expand All @@ -325,7 +423,7 @@
end

it "raises a helpful error" do
expect { subject }.to raise_error(Dependabot::Clients::Azure::NotFound)
expect { pull_request }.to raise_error(Dependabot::Clients::Azure::NotFound)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions common/spec/dependabot/pull_request_creator/azure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
automerge_candidate: false
)
end
let(:reviewers) { [] }
let(:work_item) { 123 }
let(:custom_labels) { nil }
let(:dependency) do
Expand Down Expand Up @@ -105,6 +106,7 @@
headers: json_header)
stub_request(:post, "#{repo_api_url}/pullrequests?api-version=5.0").
to_return(status: 200,
body: fixture("azure", "create_pull_request_details.json"),
headers: json_header)
end

Expand Down
84 changes: 84 additions & 0 deletions common/spec/fixtures/azure/create_pull_request_details.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"repository": {
"id": "3411ebc1-d5aa-464f-9615-0b527bc66719",
"name": "2016_10_31",
"url": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719",
"project": {
"id": "a7573007-bbb3-4341-b726-0c4148a07853",
"name": "2016_10_31",
"description": "test project created on Halloween 2016",
"url": "https://dev.azure.com/fabrikam/_apis/projects/a7573007-bbb3-4341-b726-0c4148a07853",
"state": "wellFormed",
"revision": 7
},
"remoteUrl": "https://dev.azure.com/fabrikam/_git/2016_10_31"
},
"pullRequestId": 22,
"codeReviewId": 22,
"status": "active",
"createdBy": {
"id": "d6245f20-2af8-44f4-9451-8107cb2767db",
"displayName": "Normal Paulk",
"uniqueName": "fabrikamfiber16@hotmail.com",
"url": "https://dev.azure.com/fabrikam/_apis/Identities/d6245f20-2af8-44f4-9451-8107cb2767db",
"imageUrl": "https://dev.azure.com/fabrikam/_api/_common/identityImage?id=d6245f20-2af8-44f4-9451-8107cb2767db"
},
"creationDate": "2016-11-01T16:30:31.6655471Z",
"title": "A new feature",
"description": "Adding a new feature",
"sourceRefName": "refs/heads/npaulk/my_work",
"targetRefName": "refs/heads/new_feature",
"mergeStatus": "queued",
"mergeId": "f5fc8381-3fb2-49fe-8a0d-27dcc2d6ef82",
"lastMergeSourceCommit": {
"commitId": "b60280bc6e62e2f880f1b63c1e24987664d3bda3",
"url": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/commits/b60280bc6e62e2f880f1b63c1e24987664d3bda3"
},
"lastMergeTargetCommit": {
"commitId": "f47bbc106853afe3c1b07a81754bce5f4b8dbf62",
"url": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/commits/f47bbc106853afe3c1b07a81754bce5f4b8dbf62"
},
"reviewers": [
{
"reviewerUrl": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/pullRequests/22/reviewers/d6245f20-2af8-44f4-9451-8107cb2767db",
"vote": 0,
"id": "d6245f20-2af8-44f4-9451-8107cb2767db",
"displayName": "Normal Paulk",
"uniqueName": "fabrikamfiber16@hotmail.com",
"url": "https://dev.azure.com/fabrikam/_apis/Identities/d6245f20-2af8-44f4-9451-8107cb2767db",
"imageUrl": "https://dev.azure.com/fabrikam/_api/_common/identityImage?id=d6245f20-2af8-44f4-9451-8107cb2767db"
}
],
"url": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/pullRequests/22",
"_links": {
"self": {
"href": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/pullRequests/22"
},
"repository": {
"href": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719"
},
"workItems": {
"href": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/pullRequests/22/workitems"
},
"sourceBranch": {
"href": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/refs"
},
"targetBranch": {
"href": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/refs"
},
"sourceCommit": {
"href": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/commits/b60280bc6e62e2f880f1b63c1e24987664d3bda3"
},
"targetCommit": {
"href": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/commits/f47bbc106853afe3c1b07a81754bce5f4b8dbf62"
},
"createdBy": {
"href": "https://dev.azure.com/fabrikam/_apis/Identities/d6245f20-2af8-44f4-9451-8107cb2767db"
},
"iterations": {
"href": "https://dev.azure.com/fabrikam/_apis/git/repositories/3411ebc1-d5aa-464f-9615-0b527bc66719/pullRequests/22/iterations"
}
},
"supportsIterations": true,
"artifactId": "vstfs:///Git/PullRequestId/a7573007-bbb3-4341-b726-0c4148a07853%2f3411ebc1-d5aa-464f-9615-0b527bc66719%2f22"
}