Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Aug 13, 2016
1 parent 89937fd commit 97171d0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/medium_sdk/connection/auth_code.rb
Expand Up @@ -13,6 +13,7 @@ class AuthCode

attr_reader :client_id

attr_accessor :authcode_client
attr_accessor :oauth2client
attr_accessor :oauth_redirect_uri
attr_accessor :http
Expand All @@ -25,7 +26,8 @@ def initialize(opts = {})
@oauth_redirect_uri = opts[:redirect_uri] if opts.key? :redirect_uri
@scope = opts[:scope] if opts.key? :scope
@instance_headers = opts[:instance_headers] if opts.key? :instance_headers
@oauth2client = new_oauth2_client()
@oauth2client = new_oauth2_client
@authcode_client = new_auth_code_client
end

def init_attributes()
Expand Down Expand Up @@ -91,26 +93,28 @@ def _add_redirect_uri(opts = {})

def authorize_code(code, opts = {})
#token = @oauth2client.auth_code.get_token(code, _add_redirect_uri(opts))

conn = Faraday.new(url: API_HOST) do |faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :json, content_type: /\bjson$/
faraday.response :logger # log requests to STDOUT
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
params = {
code: code,
client_id: @client_id,
client_secret: @client_secret,
redirect_uri: @oauth_redirect_uri,
grant_type: 'authorization_code'
}
res = conn.post '/v1/tokens', params
res = @authcode_client.post '/v1/tokens', params
set_token res.body
return @token
end

def new_oauth2_client()
def new_auth_code_client
return Faraday.new(url: API_HOST) do |faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :json, content_type: /\bjson$/
faraday.response :logger # log requests to STDOUT
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
end

def new_oauth2_client
return OAuth2::Client.new(@client_id, @client_secret,
site: OAUTH_HOST,
authorize_url: OAUTH_AUTHZ_ENDPOINT,
Expand Down
42 changes: 42 additions & 0 deletions test/test_oauth_stub.rb
@@ -0,0 +1,42 @@
require './test/test_base.rb'

require 'faraday'
require 'multi_json'
require 'pp'

class MediumSdkOAuthStubTest < Test::Unit::TestCase
def setup
@string = 'deadbeef'
@redirect_uri = 'https://example.com/callback'
@sdk = MediumSdk.new client_id: 'dead', client_secret: 'beef', redirect_uri: @redirect_uri

@body_token = MultiJson.decode('{
"token_type": "Bearer",
"access_token": "dead",
"refresh_token": "beef",
"scope": "listPublications",
"expires_at": 12345
}')

url = '/v1/tokens'
#url = '/m/oauth/authorize'
#url = 'tokens'
#url = 'v1/tokens'

stubs = Faraday::Adapter::Test::Stubs.new do |stub|
stub.post(url) { |env| [200, {}, @body_token] }
end
@client = Faraday.new do |builder|
builder.adapter :test, stubs do |stub|
stub.post(url) { |env| [ 200, {}, @body_token ]}
end
end
stubs.post(url) { |env| [ 200, {}, @body_token ]}
@sdk.connection.authcode_client = @client
end

def test_main
token = @sdk.connection.authorize_code 'myTestAuthCode'
assert_equal 12345, token.to_hash[:expires_at]
end
end

0 comments on commit 97171d0

Please sign in to comment.