# encoding: utf-8

require 'spec_helper'

RSpec.describe Github::Client::Authorizations::App, '#create' do
  let(:basic_auth) { 'login:password' }
  let(:host)       { "https://api.github.com" }
  let(:inputs)     { { :scopes => ['repo'] } }
  let(:request_path) { "/authorizations/clients/#{client_id}" }

  subject { described_class.new(basic_auth: basic_auth) }

  before do
    stub_put(request_path, host).to_return(body: body, status: status,
      headers: {content_type: 'application/json; charset=utf-8'})
  end

  after { reset_authentication_for(subject) }

  context 'when app creates token' do
    let(:body)      { fixture('auths/authorization.json') }
    let(:status)    { 201 }
    let(:client_id) { 1 }

    it "creates resource successfully" do
      subject.create client_id
      expect(a_put(request_path, host)).to have_been_made
    end

    it "fails without client_id" do
      expect { subject.create }.to raise_error(ArgumentError)
    end

    it "returns the resource" do
      authorization = subject.create client_id
      expect(authorization).to be_a Github::ResponseWrapper
    end

    it "gets the authorization information" do
      authorization = subject.create client_id
      expect(authorization.token).to eq 'abc123'
    end
  end
end # create
