# encoding: utf-8

require 'spec_helper'

describe Github::Client::Repos, '#list' do
  let(:user) { 'peter-murach' }
  let(:repo) { 'github' }
  let(:request_path) { "/user/repos?access_token=#{OAUTH_TOKEN}" }
  let(:body)   { fixture('repos/repos.json') }
  let(:status) { 200 }

  after { reset_authentication_for subject }

  it { is_expected.to respond_to(:find) }

  context "resource found for authenticated user" do
    before {
      subject.oauth_token = OAUTH_TOKEN
      stub_get(request_path).to_return(:body => body, :status => status,
        :headers => {:content_type => "application/json; charset=utf-8"} )
      stub_get(request_path + "&sort=pushed").
        to_return(:body => fixture('repos/repos_sorted_by_pushed.json'), :status => 200,:headers => {:content_type => "application/json; charset=utf-8"} )
    }

    it "falls back to all if user is unauthenticated" do
      subject.oauth_token = nil
      stub_get("/user/repos").to_return(:body => '[]', :status => 200,
        :headers => {:content_type => "application/json; charset=utf-8"} )
      subject.list
      expect(a_get('/user/repos')).to have_been_made
    end

    it "should get the resources" do
      subject.list
      expect(a_get(request_path)).to have_been_made
    end

    it_should_behave_like 'an array of resources' do
      let(:requestable) { subject.list }
    end

    it "should return array of resources sorted by pushed_at time" do
      repositories = subject.list(:sort => 'pushed')
      expect(repositories.first.name).to eq "Hello-World-2"
    end

    it "should get resource information" do
      repositories = subject.list
      expect(repositories.first.name).to eq 'Hello-World'
    end

    it "should yield result to a block" do
      yielded = []
      result = subject.list { |obj| yielded << obj }
      expect(yielded).to eq result
    end
  end

  context 'all repositories' do
    let(:request_path) { '/repositories' }

    before {
      stub_get(request_path).to_return(:body => body, :status => status,
        :headers => {:content_type => "application/json; charset=utf-8"} )
    }

    it "should get the resources" do
      subject.list :every
      expect(a_get(request_path)).to have_been_made
    end
  end

  context "resource found for organization" do
    let(:org) { '37signals' }
    let(:request_path) { "/orgs/#{org}/repos" }

    before {
      stub_get(request_path).to_return(:body => body, :status => status,
          :headers => {:content_type => "application/json; charset=utf-8"} )
    }

    it "should get the resources" do
      subject.list :org => org
      expect(a_get(request_path)).to have_been_made
    end
  end

  context "resource found for a user" do
    let(:request_path) { "/users/#{user}/repos" }

    before {
      stub_get(request_path).to_return(:body => body, :status => status,
        :headers => {:content_type => "application/json; charset=utf-8"} )
    }

    it "should filter the parameters" do
      subject.list 'user' => user, :unknown => true
      expect(a_get(request_path).with(body: {})).to have_been_made
    end

    it "should get the resources" do
      subject.list :user => user
      expect(a_get(request_path)).to have_been_made
    end
  end

  context "rosource not found for authenticated user" do
    before {
      subject.oauth_token = OAUTH_TOKEN
      stub_get(request_path).to_return(:body => '', :status => 404,
          :headers => {:content_type => "application/json; charset=utf-8"} )
    }

    it "fail to find resources" do
      expect { subject.list }.to raise_error(Github::Error::NotFound)
    end
  end
end # list
