<commit_before>require "spec_helper"

module Kuva
  describe Authorizer do

    context ".new" do
      before do
        allow(Kuva).to receive(:api_key).and_return "<api_key>"
        allow(Kuva).to receive(:shared_secret).and_return "<shared_secret>"
      end

      it "configures the FlickRaw API key" do
        described_class.new
        expect(FlickRaw.api_key).to eq "<api_key>"
      end

      it "configures the FlickRaw shared secret" do
        described_class.new
        expect(FlickRaw.shared_secret).to eq "<shared_secret>"
      end
    end

    context ".authorize" do
      it "authorizes" do
        allow_any_instance_of(Authorizer).to receive :authorize
        described_class.authorize
      end
    end

    context "#authorize", :vcr do
      before do
        allow(STDIN).to receive(:gets).and_return "<authorization_code>"
      end

      let(:output) do
        capture(:stdout) { subject.authorize }
      end

      it "outputs a notice about the authentication url" do
        allow(subject).to receive(:authentication_url).and_return "<url>"
        expect(output).to include "\nCopy the URL below and follow the steps on Flickr to authorize kuva:\n<url>"
      end

      it "outputs a notice about the authentication code" do
        expect(output).to include "\nOnce you've authorized kuva, you'll receive a code (in the form of xxx-xxx-xxx) from Flickr. Copy and paste that code here:\n"
      end

      context "with a valid authentication code" do
        before do
          allow(subject).to receive(:flickr).and_return double.as_null_object
        end

        it "outputs a confirmation notice" do
          subject.stub_chain(:flickr, :test, :login, :username).and_return "<username>"
          subject.stub_chain(:flickr, :access_token).and_return "<access_token>"
          subject.stub_chain(:flickr, :access_secret).and_return "<access_secret>"

          expect(output).to include "\nYou've successfully authenticated kuva to use your account: '<username>' with the following token '<access_token>' and secret '<access_secret>'."
        end
      end

      context "with an invalid authentication code" do
        it "outputs an authentication error" do
          expect(output).to include "Authentication with Flickr failed: token_rejected"
        end

        context "with an incorrect API key and shared secret" do
          before do
            allow(FlickRaw).to receive(:api_key).and_return "<api_key>"<commit_msg>Stop using VCR for the Kuva::Authorizer spec.<commit_after>require "spec_helper"

module Kuva
  describe Authorizer do

    context ".new" do
      before do
        allow(Kuva).to receive(:api_key).and_return "<api_key>"
        allow(Kuva).to receive(:shared_secret).and_return "<shared_secret>"
      end

      it "configures the FlickRaw API key" do
        described_class.new
        expect(FlickRaw.api_key).to eq "<api_key>"
      end

      it "configures the FlickRaw shared secret" do
        described_class.new
        expect(FlickRaw.shared_secret).to eq "<shared_secret>"
      end
    end

    context ".authorize" do
      it "authorizes" do
        allow_any_instance_of(Authorizer).to receive :authorize
        described_class.authorize
      end
    end

    context "#authorize" do
      before do
        allow(STDIN).to receive(:gets).and_return "<authorization_code>"
      end

      let(:output) do
        capture(:stdout) { subject.authorize }
      end

      context "with a valid authentication code" do
        before do
          allow(subject).to receive(:flickr).and_return double.as_null_object
        end

        it "outputs a notice about the authentication url" do
          allow(subject).to receive(:authentication_url).and_return "<url>"
          expect(output).to include "\nCopy the URL below and follow the steps on Flickr to authorize kuva:\n<url>"
        end

        it "outputs a notice about the authentication code" do
          expect(output).to include "\nOnce you've authorized kuva, you'll receive a code (in the form of xxx-xxx-xxx) from Flickr. Copy and paste that code here:\n"
        end

        it "outputs a confirmation notice" do
          subject.stub_chain(:flickr, :test, :login, :username).and_return "<username>"
          subject.stub_chain(:flickr, :access_token).and_return "<access_token>"
          subject.stub_chain(:flickr, :access_secret).and_return "<access_secret>"

          expect(output).to include "\nYou've successfully authenticated kuva to use your account: '<username>' with the following token '<access_token>' and secret '<access_secret>'."
        end
      end

      context "with an invalid authentication code" do
        it "outputs an authentication error" do
          expect(output).to include "Authentication with Flickr failed: token_rejected"
        end

        context "with an incorrect API key and shared secret" do
          before do
            allow(FlickRaw).to receive(:api_key).and_return "<api_key>"
{'fraction_non_alphanumeric': 0.08852917665867306, 'fraction_numerical': 0.0, 'mean_word_length': 2.760330578512397, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '6932541', 'n_tokens_mistral': 1405, 'n_tokens_neox': 1350, 'n_words': 442}