github-changelog-generator/spec/unit/fetcher_spec.rb

232 lines
18 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
VALID_TOKEN = "0123456789abcdef"
INVALID_TOKEN = "0000000000000000"
2015-05-18 13:55:49 +00:00
2015-05-14 12:57:31 +00:00
describe GitHubChangelogGenerator::Fetcher do
let(:options) do
{
:user => "skywinder",
:project => "changelog_test",
}
2015-05-14 12:57:31 +00:00
end
let(:fetcher) { GitHubChangelogGenerator::Fetcher.new(options) }
2015-05-14 12:57:31 +00:00
describe "#fetch_github_token" do
token = GitHubChangelogGenerator::Fetcher::CHANGELOG_GITHUB_TOKEN
context "when token in ENV exist" do
2015-05-18 08:40:32 +00:00
before { stub_const("ENV", ENV.to_hash.merge(token => VALID_TOKEN)) }
subject { fetcher.fetch_github_token }
2015-05-18 08:40:32 +00:00
it { is_expected.to eq(VALID_TOKEN) }
2015-05-14 12:57:31 +00:00
end
2015-05-14 12:57:31 +00:00
context "when token in ENV is nil" do
before { stub_const("ENV", ENV.to_hash.merge(token => nil)) }
subject { fetcher.fetch_github_token }
2015-05-14 12:57:31 +00:00
it { is_expected.to be_nil }
end
2015-05-18 08:40:32 +00:00
context "when token in options and ENV is nil" do
let(:options) { { :token => VALID_TOKEN } }
2015-05-18 08:40:32 +00:00
before do
stub_const("ENV", ENV.to_hash.merge(token => nil))
end
subject { fetcher.fetch_github_token }
2015-05-18 08:40:32 +00:00
it { is_expected.to eq(VALID_TOKEN) }
end
2015-05-18 08:40:32 +00:00
context "when token in options and ENV specified" do
let(:options) { { :token => VALID_TOKEN } }
2015-05-18 08:40:32 +00:00
before do
stub_const("ENV", ENV.to_hash.merge(token => "no_matter_what"))
end
subject { fetcher.fetch_github_token }
2015-05-18 08:40:32 +00:00
it { is_expected.to eq(VALID_TOKEN) }
end
2015-05-14 12:57:31 +00:00
end
2015-05-18 13:55:49 +00:00
describe "#get_all_tags" do
context "when github_fetch_tags returns tags" do
it "returns tags" do
mock_tags = ['tag']
allow(fetcher).to receive(:github_fetch_tags).and_return(mock_tags)
expect(fetcher.get_all_tags).to eq(mock_tags)
end
end
end
2015-05-18 13:55:49 +00:00
describe "#github_fetch_tags" do
context "when wrong token provided" do
use_vcr_cassette
let(:options) do
{
:user => "skywinder",
:project => "changelog_test",
:token => INVALID_TOKEN
}
2015-05-18 13:55:49 +00:00
end
2015-05-18 13:55:49 +00:00
it "should raise Unauthorized error" do
expect { fetcher.github_fetch_tags }.to raise_error Github::Error::Unauthorized
end
end
context "when API call is valid" do
use_vcr_cassette
it "should return tags" do
expected_tags = [{"name"=>"v0.0.3", "zipball_url"=>"https://api.github.com/repos/skywinder/changelog_test/zipball/v0.0.3", "tarball_url"=>"https://api.github.com/repos/skywinder/changelog_test/tarball/v0.0.3", "commit"=>{"sha"=>"a0cba2b1a1ea9011ab07ee1ac140ba5a5eb8bd90", "url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/a0cba2b1a1ea9011ab07ee1ac140ba5a5eb8bd90"}}, {"name"=>"v0.0.2", "zipball_url"=>"https://api.github.com/repos/skywinder/changelog_test/zipball/v0.0.2", "tarball_url"=>"https://api.github.com/repos/skywinder/changelog_test/tarball/v0.0.2", "commit"=>{"sha"=>"9b35bb13dcd15b68e7bcbf10cde5eb937a54f710", "url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/9b35bb13dcd15b68e7bcbf10cde5eb937a54f710"}}, {"name"=>"v0.0.1", "zipball_url"=>"https://api.github.com/repos/skywinder/changelog_test/zipball/v0.0.1", "tarball_url"=>"https://api.github.com/repos/skywinder/changelog_test/tarball/v0.0.1", "commit"=>{"sha"=>"4c2d6d1ed58bdb24b870dcb5d9f2ceed0283d69d", "url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/4c2d6d1ed58bdb24b870dcb5d9f2ceed0283d69d"}}, {"name"=>"0.0.4", "zipball_url"=>"https://api.github.com/repos/skywinder/changelog_test/zipball/0.0.4", "tarball_url"=>"https://api.github.com/repos/skywinder/changelog_test/tarball/0.0.4", "commit"=>{"sha"=>"ece0c3ab7142b21064b885061c55ede00ef6ce94", "url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/ece0c3ab7142b21064b885061c55ede00ef6ce94"}}]
expect(fetcher.github_fetch_tags).to eq(expected_tags)
end
end
end
describe "#fetch_closed_issues_and_pr" do
context "when API call is valid" do
use_vcr_cassette
it "returns issues" do
issues, pull_requests = fetcher.fetch_closed_issues_and_pr
expect(issues.size).to eq(7)
expect(pull_requests.size).to eq(14)
end
it "returns issue with proper key/values" do
issues, pull_requests = fetcher.fetch_closed_issues_and_pr
expected_issue = {"url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14", "repository_url"=>"https://api.github.com/repos/skywinder/changelog_test", "labels_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/labels{/name}", "comments_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/comments", "events_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/events", "html_url"=>"https://github.com/skywinder/changelog_test/issues/14", "id"=>95419412, "number"=>14, "title"=>"Issue closed from commit from PR", "user"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "labels"=>[], "state"=>"closed", "locked"=>false, "assignee"=>nil, "milestone"=>nil, "comments"=>0, "created_at"=>"2015-07-16T12:06:08Z", "updated_at"=>"2015-07-16T12:21:42Z", "closed_at"=>"2015-07-16T12:21:42Z", "body"=>""}
expect(issues.first).to eq(expected_issue)
end
it "returns pull request with proper key/values" do
issues, pull_requests = fetcher.fetch_closed_issues_and_pr
expected_pr = {"url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/21", "repository_url"=>"https://api.github.com/repos/skywinder/changelog_test", "labels_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/21/labels{/name}", "comments_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/21/comments", "events_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/21/events", "html_url"=>"https://github.com/skywinder/changelog_test/pull/21", "id"=>124925759, "number"=>21, "title"=>"Merged br (should appear in change log with #20)", "user"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "labels"=>[], "state"=>"closed", "locked"=>false, "assignee"=>nil, "milestone"=>nil, "comments"=>0, "created_at"=>"2016-01-05T09:24:08Z", "updated_at"=>"2016-01-05T09:26:53Z", "closed_at"=>"2016-01-05T09:24:27Z", "pull_request"=>{"url"=>"https://api.github.com/repos/skywinder/changelog_test/pulls/21", "html_url"=>"https://github.com/skywinder/changelog_test/pull/21", "diff_url"=>"https://github.com/skywinder/changelog_test/pull/21.diff", "patch_url"=>"https://github.com/skywinder/changelog_test/pull/21.patch"}, "body"=>"to test https://github.com/skywinder/github-changelog-generator/pull/305\r\nshould appear in change log with #20"}
expect(pull_requests.first).to eq(expected_pr)
end
it "returns issues with labels" do
issues, pull_requests = fetcher.fetch_closed_issues_and_pr
expected = [[], [], ["Bug"], [], ["enhancement"], ["some label"], []]
expect(issues.map{|i| i.labels.map(&:name) }).to eq(expected)
end
it "returns pull_requests with labels" do
issues, pull_requests = fetcher.fetch_closed_issues_and_pr
expected = [[], [], [], [], [], ["enhancement"], [], [], ["invalid"], [], [], [], [], ["invalid"]]
expect(pull_requests.map{|i| i.labels.map(&:name) }).to eq(expected)
end
end
end
describe "#fetch_closed_pull_requests" do
context "when API call is valid" do
use_vcr_cassette
it "returns pull requests" do
pull_requests = fetcher.fetch_closed_pull_requests
expect(pull_requests.size).to eq(14)
end
it "returns correct pull request keys" do
pull_requests = fetcher.fetch_closed_pull_requests
pr = pull_requests.first
expect(pr.keys).to eq(["url", "id", "html_url", "diff_url", "patch_url", "issue_url", "number", "state", "locked", "title", "user", "body", "created_at", "updated_at", "closed_at", "merged_at", "merge_commit_sha", "assignee", "milestone", "commits_url", "review_comments_url", "review_comment_url", "comments_url", "statuses_url", "head", "base", "_links"])
end
end
end
describe "#fetch_events_async" do
context "when API call is valid" do
use_vcr_cassette
it "populates issues" do
issues = [{"url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14", "repository_url"=>"https://api.github.com/repos/skywinder/changelog_test", "labels_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/labels{/name}", "comments_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/comments", "events_url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/14/events", "html_url"=>"https://github.com/skywinder/changelog_test/issues/14", "id"=>95419412, "number"=>14, "title"=>"Issue closed from commit from PR", "user"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "labels"=>[], "state"=>"closed", "locked"=>false, "assignee"=>nil, "milestone"=>nil, "comments"=>0, "created_at"=>"2015-07-16T12:06:08Z", "updated_at"=>"2015-07-16T12:21:42Z", "closed_at"=>"2015-07-16T12:21:42Z", "body"=>""}]
# Check that they are blank to begin with
expect(issues.first[:events]).to be_nil
fetcher.fetch_events_async(issues)
issue_events = issues.first[:events]
expected_events = [{"id"=>357462189, "url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/events/357462189", "actor"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "event"=>"referenced", "commit_id"=>"decfe840d1a1b86e0c28700de5362d3365a29555", "commit_url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/decfe840d1a1b86e0c28700de5362d3365a29555", "created_at"=>"2015-07-16T12:21:16Z"}, {"id"=>357462542, "url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/events/357462542", "actor"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "event"=>"closed", "commit_id"=>"decfe840d1a1b86e0c28700de5362d3365a29555", "commit_url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/decfe840d1a1b86e0c28700de5362d3365a29555", "created_at"=>"2015-07-16T12:21:42Z"}]
expect(issue_events).to eq(expected_events)
2015-05-18 13:55:49 +00:00
end
2015-05-18 13:55:49 +00:00
end
end
describe "#fetch_date_of_tag" do
context "when API call is valid" do
use_vcr_cassette
it "returns date" do
tag = {"name"=>"v0.0.3", "zipball_url"=>"https://api.github.com/repos/skywinder/changelog_test/zipball/v0.0.3", "tarball_url"=>"https://api.github.com/repos/skywinder/changelog_test/tarball/v0.0.3", "commit"=>{"sha"=>"a0cba2b1a1ea9011ab07ee1ac140ba5a5eb8bd90", "url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/a0cba2b1a1ea9011ab07ee1ac140ba5a5eb8bd90"}}
dt = fetcher.fetch_date_of_tag(tag)
expect(dt).to eq(Time.parse("2015-03-04 19:01:48 UTC"))
end
end
end
describe "#fetch_commit" do
context "when API call is valid" do
use_vcr_cassette
it "returns commit" do
event = {"id"=>357462189, "url"=>"https://api.github.com/repos/skywinder/changelog_test/issues/events/357462189", "actor"=>{"login"=>"skywinder", "id"=>3356474, "avatar_url"=>"https://avatars.githubusercontent.com/u/3356474?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/skywinder", "html_url"=>"https://github.com/skywinder", "followers_url"=>"https://api.github.com/users/skywinder/followers", "following_url"=>"https://api.github.com/users/skywinder/following{/other_user}", "gists_url"=>"https://api.github.com/users/skywinder/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/skywinder/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/skywinder/subscriptions", "organizations_url"=>"https://api.github.com/users/skywinder/orgs", "repos_url"=>"https://api.github.com/users/skywinder/repos", "events_url"=>"https://api.github.com/users/skywinder/events{/privacy}", "received_events_url"=>"https://api.github.com/users/skywinder/received_events", "type"=>"User", "site_admin"=>false}, "event"=>"referenced", "commit_id"=>"decfe840d1a1b86e0c28700de5362d3365a29555", "commit_url"=>"https://api.github.com/repos/skywinder/changelog_test/commits/decfe840d1a1b86e0c28700de5362d3365a29555", "created_at"=>"2015-07-16T12:21:16Z"}
commit = fetcher.fetch_commit(event)
expectations = [
["sha", "decfe840d1a1b86e0c28700de5362d3365a29555"],
["url",
"https://api.github.com/repos/skywinder/changelog_test/git/commits/decfe840d1a1b86e0c28700de5362d3365a29555"],
["html_url",
"https://github.com/skywinder/changelog_test/commit/decfe840d1a1b86e0c28700de5362d3365a29555"],
["author",
{"name"=>"Petr Korolev",
"email"=>"sky4winder@gmail.com",
"date"=>"2015-07-16T12:11:01Z"}],
["committer",
{"name"=>"Petr Korolev",
"email"=>"sky4winder@gmail.com",
"date"=>"2015-07-16T12:11:01Z"}],
["tree",
{"sha"=>"0699c15258a7c2b2e157051fe19851d4f705cac8",
"url"=>
"https://api.github.com/repos/skywinder/changelog_test/git/trees/0699c15258a7c2b2e157051fe19851d4f705cac8"}],
["message", "fix #14"],
["parents",
[{"sha"=>"7ec095e5e3caceacedabf44d0b9b10da17c92e51",
"url"=>
"https://api.github.com/repos/skywinder/changelog_test/git/commits/7ec095e5e3caceacedabf44d0b9b10da17c92e51",
"html_url"=>
"https://github.com/skywinder/changelog_test/commit/7ec095e5e3caceacedabf44d0b9b10da17c92e51"}]
]
]
expectations.each do |property, val|
expect(commit.send(property)).to eq(val)
end
end
end
end
2015-05-14 12:57:31 +00:00
end