Skip to content

Commit

Permalink
Wrote tests for OAuth2::Response
Browse files Browse the repository at this point in the history
  • Loading branch information
KentonWhite committed Dec 20, 2011
1 parent 0fe1413 commit 6c03b02
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions test/unit/garb/request/data_test.rb
Expand Up @@ -51,7 +51,7 @@ class DataTest < MiniTest::Unit::TestCase
assert_received(data_request, :single_user_request) assert_received(data_request, :single_user_request)
end end


should "be able to send a request for an oauth user" do should "be able to send a request for an oauth user (Net::HTTP)" do
@session.stubs(:single_user?).returns(false) @session.stubs(:single_user?).returns(false)
@session.stubs(:oauth_user?).returns(true) @session.stubs(:oauth_user?).returns(true)
response = mock('Net::HTTPOK') do |m| response = mock('Net::HTTPOK') do |m|
Expand All @@ -65,7 +65,21 @@ class DataTest < MiniTest::Unit::TestCase
assert_received(data_request, :oauth_user_request) assert_received(data_request, :oauth_user_request)
end end


should "raise if the request is unauthorized" do should "be able to send a request for an oauth user (OAuth2)" do
@session.stubs(:single_user?).returns(false)
@session.stubs(:oauth_user?).returns(true)
response = mock('OAuth2::Response') do |m|
m.expects(:status).returns(200)
end

data_request = Request::Data.new(@session, 'https://example.com/data', 'key' => 'value')
data_request.stubs(:oauth_user_request).returns(response)
data_request.send_request

assert_received(data_request, :oauth_user_request)
end

should "raise if the request is unauthorized (Net::HTTP)" do
@session.stubs(:single_user?).returns(false) @session.stubs(:single_user?).returns(false)
@session.stubs(:oauth_user?).returns(true) @session.stubs(:oauth_user?).returns(true)
response = mock('Net::HTTPUnauthorized', :body => 'Error') response = mock('Net::HTTPUnauthorized', :body => 'Error')
Expand All @@ -78,6 +92,22 @@ class DataTest < MiniTest::Unit::TestCase
end end
end end


should "raise if the request is unauthorized (OAuth2)" do
@session.stubs(:single_user?).returns(false)
@session.stubs(:oauth_user?).returns(true)
response = mock('OAuth2::Response') do |m|
m.expects(:status).returns(401)
m.expects(:body).returns('Error')
end

data_request = Request::Data.new(@session, 'https://example.com/data', 'key' => 'value')
data_request.stubs(:oauth_user_request).returns(response)

assert_raises(Garb::Request::Data::ClientError) do
data_request.send_request
end
end

should "be able to request via the ouath access token" do should "be able to request via the ouath access token" do
access_token = stub(:get => "responseobject") access_token = stub(:get => "responseobject")
@session.stubs(:access_token).returns(access_token) @session.stubs(:access_token).returns(access_token)
Expand Down

0 comments on commit 6c03b02

Please sign in to comment.