Permalink
Browse files

Added history for oauth2 clients.

  • Loading branch information...
1 parent bd579f1 commit 99c8c5931189965ea419db7383fb7a71cbbdbc56 @jtadeulopes jtadeulopes committed Apr 23, 2011
Showing with 99 additions and 2 deletions.
  1. +17 −0 lib/googl/oauth2/utils.rb
  2. +35 −0 spec/googl/oauth2/native_spec.rb
  3. +35 −0 spec/googl/oauth2/server_spec.rb
  4. +12 −2 spec/spec_helper.rb
@@ -2,6 +2,8 @@ module Googl
module OAuth2
module Utils
+ attr_accessor :items
+
def request_token(code, request_uri="urn:ietf:wg:oauth:2.0:oob")
params = "code=#{code}&client_id=#{client_id}&client_secret=#{client_secret}&redirect_uri=#{request_uri}&grant_type=authorization_code"
modify_headers('Content-Type' => 'application/x-www-form-urlencoded')
@@ -29,6 +31,21 @@ def expires?
expires_at < Time.now
end
+ def authorized?
+ !access_token.nil? && !refresh_token.nil? && !expires_in.nil? && !expires_at.nil?
+ end
+
+ def history(options={})
+ if authorized?
+ resp = options.blank? ? get(Googl::Utils::API_HISTORY_URL) : get(Googl::Utils::API_HISTORY_URL, :query => options)
+ if resp.code == 200
+ self.items = resp.parsed_response.to_openstruct
+ else
+ raise exception("#{resp.code} #{resp.parsed_response}")
+ end
+ end
+ end
+
end
end
end
@@ -114,4 +114,39 @@
end
+ describe "#authorized?" do
+
+ it "should return false if client is not authorized" do
+ subject.authorized?.should be_false
+ end
+
+ let(:native) { subject.request_access_token("4/SuSud6RqPojUXsPpeh-wSVCwnmTQ") }
+
+ it "should return true if client is authorized" do
+ native.authorized?.should be_true
+ end
+
+ end
+
+ context "when gets a user history of shortened" do
+
+ it { subject.should respond_to(:history) }
+
+ it "should not return when client not authorized" do
+ subject.history.should be_nil
+ end
+
+ context "if authorized" do
+
+ let(:native) { subject.request_access_token("4/SuSud6RqPojUXsPpeh-wSVCwnmTQ") }
+ let(:history) { native.history }
+
+ it { history.should respond_to(:total_items) }
+ it { history.should respond_to(:items_per_page) }
+ it { history.should respond_to(:items) }
+
+ end
+
+ end
+
end
@@ -118,4 +118,39 @@
end
+ describe "#authorized?" do
+
+ it "should return false if client is not authorized" do
+ subject.authorized?.should be_false
+ end
+
+ let(:server) { subject.request_access_token("4/z43CZpNmqd0IO3dR1Y_ouase13CH") }
+
+ it "should return true if client is authorized" do
+ server.authorized?.should be_true
+ end
+
+ end
+
+ context "when gets a user history of shortened" do
+
+ it { subject.should respond_to(:history) }
+
+ it "should not return when client not authorized" do
+ subject.history.should be_nil
+ end
+
+ context "if authorized" do
+
+ let(:server) { subject.request_access_token("4/z43CZpNmqd0IO3dR1Y_ouase13CH") }
+ let(:history) { server.history }
+
+ it { history.should respond_to(:total_items) }
+ it { history.should respond_to(:items_per_page) }
+ it { history.should respond_to(:items) }
+
+ end
+
+ end
+
end
View
@@ -75,7 +75,7 @@ def fake_urls?(status)
:headers => {'Authorization'=>'GoogleLogin auth=DQAAAK8AAAC9ahL-o7g', 'Content-Type'=>'application/json'}).
to_return(load_fixture('shorten_authenticated.json'))
- # History
+ # History for ClientLogin
stub_request(:get, "https://www.googleapis.com/urlshortener/v1/url/history").
with(:headers => {'Authorization'=>'GoogleLogin auth=DQAAAK8AAAC9ahL-o7g'}).
to_return(load_fixture('history.json'))
@@ -101,7 +101,12 @@ def fake_urls?(status)
stub_request(:post, "https://accounts.google.com/o/oauth2/token").
with(:body => "code=4/JvkEhCtr7tv1A60ENmubQT-cosRl&client_id=185706845724.apps.googleusercontent.com&client_secret=DrBLCdCQ3gOybHrj7TPz/B0N&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code",
:headers => {'Content-Type'=>'application/x-www-form-urlencoded'}).
- to_return(load_fixture('oauth2/native_token_expires.json'))
+ to_return(load_fixture('oauth2/native_token_expires.json'))
+
+ # OAuth 2.0 for native applications (history)
+ stub_request(:get, "https://www.googleapis.com/urlshortener/v1/url/history").
+ with(:headers => {'Authorization'=>'OAuth 1/YCzoGAYT8XUuOifjNh_KqA', 'Content-Type'=>'application/x-www-form-urlencoded'}).
+ to_return(load_fixture('history.json'))
# OAuth 2.0 for server-side web applications
stub_request(:post, "https://accounts.google.com/o/oauth2/token").
@@ -121,6 +126,11 @@ def fake_urls?(status)
:headers => {'Content-Type'=>'application/x-www-form-urlencoded'}).
to_return(load_fixture('oauth2/server_token_expires.json'))
+ # OAuth 2.0 for server-side web applications (history)
+ stub_request(:get, "https://www.googleapis.com/urlshortener/v1/url/history").
+ with(:headers => {'Authorization'=>'OAuth 1/9eNgoHDXi-1u1fDzZ2wLLGATiaQZnWPB51nTvo8n9Sw'}).
+ to_return(load_fixture('history.json'))
+
else
WebMock.allow_net_connect!
end

0 comments on commit 99c8c59

Please sign in to comment.