Browse files

get_access_token now includes a full user object in addition to an ac…

…cess_token

Added tests for OAuth module and cleaned up a little
  • Loading branch information...
1 parent 9afde99 commit e1e4148928953da815dcb79f9d637659dc37c477 Shayne Sweeney committed Feb 3, 2011
Showing with 59 additions and 6 deletions.
  1. +2 −1 README.md
  2. +4 −5 lib/instagram/oauth.rb
  3. +10 −0 spec/fixtures/access_token.json
  4. +43 −0 spec/instagram/api_spec.rb
View
3 README.md
@@ -47,7 +47,8 @@ Sample Application
end
get "/oauth/callback" do
- session[:access_token] = Instagram.get_access_token(params[:code], :redirect_uri => CALLBACK_URL)
+ response = Instagram.get_access_token(params[:code], :redirect_uri => CALLBACK_URL)
+ session[:access_token] = response.access_token
redirect "/feed"
end
View
9 lib/instagram/oauth.rb
@@ -3,17 +3,16 @@ module Instagram
module OAuth
# Return URL for OAuth authorization
def authorize_url(options={})
- options[:response_type] = options[:response_type] || "code"
+ options[:response_type] ||= "code"
params = access_token_params.merge(options)
connection.build_url("/oauth/authorize/", params).to_s
end
# Return an access token from authorization
def get_access_token(code, options={})
- options[:grant_type] = options[:grant_type] || "authorization_code"
- params = {:code => code}.merge(access_token_params).merge(options)
- response = post("/oauth/access_token/", params, raw=false, unformatted=true)
- response[:access_token]
+ options[:grant_type] ||= "authorization_code"
+ params = access_token_params.merge(options)
+ post("/oauth/access_token/", params.merge(:code => code), raw=false, unformatted=true)
end
private
View
10 spec/fixtures/access_token.json
@@ -0,0 +1,10 @@
+{
+ "access_token": "at",
+ "user": {
+ "username": "mikeyk",
+ "first_name": "Mike",
+ "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_4_75sq_1292324747_debug.jpg",
+ "id": "4",
+ "last_name": "Krieger!!"
+ }
+}
View
43 spec/instagram/api_spec.rb
@@ -64,4 +64,47 @@
end
end
end
+
+ describe ".authorize_url" do
+
+ it "should generate an authorize URL with necessary params" do
+ params = { :client_id => "CID", :client_secret => "CS" }
+
+ client = Instagram::Client.new(params)
+
+ redirect_uri = 'http://localhost:4567/oauth/callback'
+ url = client.authorize_url(:redirect_uri => redirect_uri)
+
+ params2 = client.send(:access_token_params).merge(params)
+ params2[:redirect_uri] = redirect_uri
+ params2[:response_type] = "code"
+ url2 = client.send(:connection).build_url("/oauth/authorize/", params2).to_s
+
+ url2.should == url
+ end
+ end
+
+ describe ".get_access_token" do
+
+ before do
+ @client = Instagram::Client.new(:client_id => "CID", :client_secret => "CS")
+ @url = @client.send(:connection).build_url("/oauth/access_token/").to_s
+ stub_request(:post, @url).
+ with(:body => {:client_id => "CID", :client_secret => "CS", :redirect_uri => "http://localhost:4567/oauth/callback", :grant_type => "authorization_code", :code => "C"}).
+ to_return(:status => 200, :body => fixture("access_token.json"), :headers => {})
+ end
+
+ it "should get the correct resource" do
+ @client.get_access_token(code="C", :redirect_uri => "http://localhost:4567/oauth/callback")
+ a_request(:post, @url).
+ with(:body => {:client_id => "CID", :client_secret => "CS", :redirect_uri => "http://localhost:4567/oauth/callback", :grant_type => "authorization_code", :code => "C"}).
+ should have_been_made
+ end
+
+ it "should return a hash with an access_token and user data" do
+ response = @client.get_access_token(code="C", :redirect_uri => "http://localhost:4567/oauth/callback")
+ response.access_token.should == "at"
+ response.user.username.should == "mikeyk"
+ end
+ end
end

0 comments on commit e1e4148

Please sign in to comment.