spec/call_resource_spec.rb in chargify2-0.2.4 vs spec/call_resource_spec.rb in chargify2-0.2.5

- old
+ new

@@ -1,56 +1,75 @@ require 'spec_helper' module Chargify2 describe CallResource do - it "should have a URI of 'https://api.chargify.com/api/v2/calls'" do - CallResource.uri.should == 'https://api.chargify.com/api/v2/calls' + it "should have a path of 'calls'" do + CallResource.path.should == 'calls' end it "represents with the Call class" do CallResource.representation.should == Call end - - describe "#read" do - it "performs a GET request to 'https://api.chargify.com/api/v2/calls/123' (without authentication) when called with '123'" do - # What is this 'a_request' method? - pending 'Rewrite this test.' - WebMock.stub_request(:get, 'https://api.chargify.com/api/v2/calls/123') - CallResource.read('123') - a_request(:get, 'https://api.chargify.com/api/v2/calls/123').should have_been_made.once + + context 'without an instance configured with a client' do + describe '.read' do + it "performs a GET request to 'https://api.chargify.com/api/v2/calls/123' (without authentication) when called with '123'" do + WebMock.stub_request(:get, 'https://api.chargify.com/api/v2/calls/123') + CallResource.read('123') + a_request(:get, 'https://api.chargify.com/api/v2/calls/123').should have_been_made.once + end + + it "returns a Call representation" do + WebMock.stub_request(:get, 'https://api.chargify.com/api/v2/calls/123') + CallResource.read('123').should be_a(Call) + end end - - it "returns a Call representation" do - WebMock.stub_request(:get, 'https://api.chargify.com/api/v2/calls/123') - CallResource.read('123').should be_a(Call) - end end - - context "instance configured with a client and a non-standard base URI" do - it "has a URI of 'http://www.example.com/calls'" do - base_uri = 'http://www.example.com' - client = Client.new(valid_client_credentials.merge(:base_uri => base_uri)) - CallResource.new(client).uri.should == 'http://www.example.com/calls' + + context 'with an instance configured with a client' do + let(:client) { Client.new(valid_client_credentials) } + let!(:call_resource) { CallResource.new(client) } + + describe '.read' do + it 'ignores the instance configuration and uses class defaults' do + WebMock.stub_request(:get, 'https://api.chargify.com/api/v2/calls/123') + CallResource.read('123') + a_request(:get, 'https://api.chargify.com/api/v2/calls/123').should have_been_made.once + end end - end - - context "instance configured with a valid client" do - before(:each) do - @client = Client.new(valid_client_credentials) - @call_resource = CallResource.new(@client) + + describe '#read' do + it "performs a GET request to 'https://<api_login>:<api_password>@api.chargify.com/api/v2/calls/123' (with authentication) when called with '123'" do + WebMock.stub_request(:get, client_authenticated_uri(client, '/calls/123')) + call_resource.read('123') + a_request(:get, client_authenticated_uri(client, '/calls/123')).should have_been_made.once + end + + it "returns a Call representation" do + WebMock.stub_request(:get, client_authenticated_uri(client, '/calls/123')) + call_resource.read('123').should be_a(Call) + end end - - it "performs a GET request to 'https://<api_login>:<api_password>@api.chargify.com/api/v2/calls/123' (with authentication) when called with '123'" do - # What is this 'a_request' method? - pending 'Rewrite this test.' - WebMock.stub_request(:get, "https://#{@client.api_id}:#{@client.api_password}@api.chargify.com/api/v2/calls/123") - CallResource.read('123') - a_request(:get, "https://#{@client.api_id}:#{@client.api_password}@api.chargify.com/api/v2/calls/123").should have_been_made.once + + context 'with a non-standard base URI' do + let(:base_uri) { 'http://www.example.com' } + let(:client) { Client.new(valid_client_credentials.merge(:base_uri => base_uri)) } + + describe '#read' do + it "has a URI of 'http://www.example.com/calls'" do + WebMock.stub_request(:get, client_authenticated_uri(client, '/calls/123')) + call_resource.read('123') + a_request(:get, client_authenticated_uri(client, '/calls/123')).should have_been_made.once + end + end end - - it "returns a Call representation" do - WebMock.stub_request(:get, "https://#{@client.api_id}:#{@client.api_password}@api.chargify.com/api/v2/calls/123") - CallResource.read('123').should be_a(Call) - end + end + + def client_authenticated_uri(client, path) + uri = URI(client.base_uri) + uri.user = client.api_id + uri.password = client.api_password + + uri.to_s + path end end end