Skip to content

Commit

Permalink
Allow configuration of redirect uri in client
Browse files Browse the repository at this point in the history
  • Loading branch information
jgannonjr committed Oct 6, 2012
1 parent 4321b76 commit 56edd69
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 16 deletions.
2 changes: 2 additions & 0 deletions lib/instagram/configuration.rb
Expand Up @@ -9,6 +9,7 @@ module Configuration
:adapter,
:client_id,
:client_secret,
:redirect_uri,
:access_token,
:endpoint,
:format,
Expand Down Expand Up @@ -80,6 +81,7 @@ def reset
self.adapter = DEFAULT_ADAPTER
self.client_id = DEFAULT_CLIENT_ID
self.client_secret = DEFAULT_CLIENT_SECRET
self.redirect_uri = DEFAULT_REDIRECT_URI
self.access_token = DEFAULT_ACCESS_TOKEN
self.endpoint = DEFAULT_ENDPOINT
self.format = DEFAULT_FORMAT
Expand Down
2 changes: 2 additions & 0 deletions lib/instagram/oauth.rb
Expand Up @@ -4,13 +4,15 @@ module OAuth
# Return URL for OAuth authorization
def authorize_url(options={})
options[:response_type] ||= "code"
options[:redirect_uri] ||= self.redirect_uri
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] ||= "authorization_code"
options[:redirect_uri] ||= self.redirect_uri
params = access_token_params.merge(options)
post("/oauth/access_token/", params.merge(:code => code), raw=false, unformatted=true)
end
Expand Down
77 changes: 61 additions & 16 deletions spec/instagram/api_spec.rb
Expand Up @@ -32,6 +32,7 @@
@configuration = {
:client_id => 'CID',
:client_secret => 'CS',
:redirect_uri => 'http://http://localhost:4567/oauth/callback',
:access_token => 'AT',
:adapter => :typhoeus,
:endpoint => 'http://tumblr.com/',
Expand Down Expand Up @@ -82,29 +83,73 @@

url2.should == url
end

context "when redirect uri not passed" do

it "should generate an authorize URL using the redirect uri from the configuration" do
redirect_uri = "http://localhost:4567/oauth/callback"
params = { :client_id => "CID", :client_secret => "CS", :redirect_uri => redirect_uri }
client = Instagram::Client.new(params)
url = client.authorize_url

params2 = client.send(:access_token_params).merge({:client_id => "CID", :client_secret => "CS"})
params2[:response_type] = "code"
params2[:redirect_uri] = redirect_uri
url2 = client.send(:connection).build_url("/oauth/authorize/", params2).to_s

url2.should == url
end
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
context "when redirect uri passed" 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
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

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"
context 'when redirect uri not passed' do

before do
@client = Instagram::Client.new(:client_id => "CID", :client_secret => "CS", :redirect_uri => "http://localhost:4567/oauth/callback")
@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")
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")
response.access_token.should == "at"
response.user.username.should == "mikeyk"
end
end
end
end

0 comments on commit 56edd69

Please sign in to comment.