<commit_before>VALID_TOKEN = "<KEY>"
INVALID_TOKEN = "0000000000000000"

describe GitHubChangelogGenerator::OctoFetcher do
  let(:options) do
    {
      user: "skywinder",
      project: "changelog_test"
    }
  end

  let(:fetcher) { GitHubChangelogGenerator::OctoFetcher.new(options) }

  describe "#fetch_github_token" do
    token = GitHubChangelogGenerator::OctoFetcher::CHANGELOG_GITHUB_TOKEN
    context "when token in ENV exist" do
      before { stub_const("ENV", ENV.to_hash.merge(token => VALID_TOKEN)) }
      subject { fetcher.send(:fetch_github_token) }
      it { is_expected.to eq(VALID_TOKEN) }
    end

    context "when token in ENV is nil" do
      before { stub_const("ENV", ENV.to_hash.merge(token => nil)) }
      subject { fetcher.send(:fetch_github_token) }
      it { is_expected.to be_nil }
    end

    context "when token in options and ENV is nil" do
      let(:options) { { token: VALID_TOKEN } }

      before do
        stub_const("ENV", ENV.to_hash.merge(token => nil))
      end

      subject { fetcher.send(:fetch_github_token) }
      it { is_expected.to eq(VALID_TOKEN) }
    end

    context "when token in options and ENV specified" do
      let(:options) { { token: VALID_TOKEN } }

      before do
        stub_const("ENV", ENV.to_hash.merge(token => "no_matter_what"))
      end

      subject { fetcher.send(:fetch_github_token) }
      it { is_expected.to eq(VALID_TOKEN) }
    end
  end

  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

  describe "#github_fetch_tags" do
    context "when wrong token provided", vcr: true do

      let(:options) do
        {
          user: "skywinder",
          project: "changelog_test",
          token: INVALID_TOKEN
        }
      end

      it "should raise Unauthorized error" do
        expect { fetcher.github_fetch_tags }.to raise_error SystemExit, "Error: wrong GitHub token"
      end
    end

    context "when API call is valid", :vcr do

      it "should return tags" do
        expected_tags = [{ "name"        => "v0.0.3",
                           "zipball_url" =><commit_msg>Style: use the short form for RSpec metadata<commit_after>VALID_TOKEN = "<KEY>"
INVALID_TOKEN = "0000000<PASSWORD>000"

describe GitHubChangelogGenerator::OctoFetcher do
  let(:options) do
    {
      user: "skywinder",
      project: "changelog_test"
    }
  end

  let(:fetcher) { GitHubChangelogGenerator::OctoFetcher.new(options) }

  describe "#fetch_github_token" do
    token = GitHubChangelogGenerator::OctoFetcher::CHANGELOG_GITHUB_TOKEN
    context "when token in ENV exist" do
      before { stub_const("ENV", ENV.to_hash.merge(token => VALID_TOKEN)) }
      subject { fetcher.send(:fetch_github_token) }
      it { is_expected.to eq(VALID_TOKEN) }
    end

    context "when token in ENV is nil" do
      before { stub_const("ENV", ENV.to_hash.merge(token => nil)) }
      subject { fetcher.send(:fetch_github_token) }
      it { is_expected.to be_nil }
    end

    context "when token in options and ENV is nil" do
      let(:options) { { token: VALID_TOKEN } }

      before do
        stub_const("ENV", ENV.to_hash.merge(token => nil))
      end

      subject { fetcher.send(:fetch_github_token) }
      it { is_expected.to eq(VALID_TOKEN) }
    end

    context "when token in options and ENV specified" do
      let(:options) { { token: VALID_TOKEN } }

      before do
        stub_const("ENV", ENV.to_hash.merge(token => "<PASSWORD>"))
      end

      subject { fetcher.send(:fetch_github_token) }
      it { is_expected.to eq(VALID_TOKEN) }
    end
  end

  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

  describe "#github_fetch_tags" do
    context "when wrong token provided", :vcr do

      let(:options) do
        {
          user: "skywinder",
          project: "changelog_test",
          token: INVALID_TOKEN
        }
      end

      it "should raise Unauthorized error" do
        expect { fetcher.github_fetch_tags }.to raise_error SystemExit, "Error: wrong GitHub token"
      end
    end

    context "when API call is valid", :vcr do

      it "should return tags" do
        expected_tags = [{ "name"        => "v0.0.3",
                           "zipball_url" =>
{'fraction_non_alphanumeric': 0.10333761232349166, 'fraction_numerical': 0.0068463842533162175, 'mean_word_length': 2.6609240407204386, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3720851', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1507, 'n_words': 370}