Skip to content

Commit

Permalink
use explicit stubs instead of recorded responses
Browse files Browse the repository at this point in the history
never really worked out the way I thought it would
  • Loading branch information
theflow committed Jul 4, 2012
1 parent 5ac82c9 commit c0906c2
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 65 deletions.
16 changes: 0 additions & 16 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,8 @@ task :default => [:test]

desc 'Run tests'
Rake::TestTask.new(:test) do |t|
ENV['ENABLE_STUBS'] = 'true'
ENV['ENABLE_RECORD'] = 'false'
t.ruby_opts = ["-rubygems"] if defined? Gem
t.libs << "lib" << "test"
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Record responses'
task :record do
ENV['ENABLE_RECORD'] = 'true'
ENV['ENABLE_STUBS'] = 'false'

Dir['test/**/*_test.rb'].each do |f|
ruby("-Ilib:test", f)

folder_name = f.match(/test\/(.+)_test.rb/)[1]
FileUtils.mkdir_p("test/fixtures/#{folder_name}")
FileUtils.mv(Dir.glob('*.rack'), "test/fixtures/#{folder_name}")
end
end
1 change: 0 additions & 1 deletion lib/podio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
require 'podio/middleware/oauth2'
require 'podio/middleware/json_response'
require 'podio/middleware/error_response'
require 'podio/middleware/response_recorder'

require 'podio/active_podio/base'
require 'podio/active_podio/updatable'
Expand Down
2 changes: 0 additions & 2 deletions lib/podio/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def initialize(options = {})
@stubs = Faraday::Adapter::Test::Stubs.new
end
@test_mode = options[:test_mode]
@record_mode = options[:record_mode]

setup_connections
end
Expand Down Expand Up @@ -165,7 +164,6 @@ def configure_connection
# builder.use Middleware::DateConversion
builder.use Middleware::ErrorResponse
builder.use Middleware::JsonResponse
builder.use Middleware::ResponseRecorder if @record_mode
end
end

Expand Down
14 changes: 0 additions & 14 deletions lib/podio/middleware/response_recorder.rb

This file was deleted.

16 changes: 13 additions & 3 deletions test/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ class ClientTest < Test::Unit::TestCase
end

test 'should get an access token' do
Podio.client.stubs.post('/oauth/token') {[200, {}, {"access_token" => "a3345189a07b478284356c8b0b3c54d5", "expires_in" => 28799, "refresh_token" => "cdca6acfeb374598ba2790c9e5283b21"}]}

client = Podio.client
client.oauth_token = nil
assert_nil client.oauth_token

user = fixtures[:users][:professor]
client.authenticate_with_credentials(user[:mail], user[:password])

Expand All @@ -39,23 +41,28 @@ class ClientTest < Test::Unit::TestCase
end

test 'should be able to refresh access token' do
Podio.client.stubs.post('/oauth/token') {[200, {}, {"access_token" => "a3345189a07b478284356c8b0b3c54d5", "expires_in" => 28799, "refresh_token" => "cdca6acfeb374598ba2790c9e5283b21"}]}

login_as(:fry)
client = Podio.client
old_token = client.oauth_token.dup

Podio.client.stubs.post('/oauth/token') {[200, {}, {"access_token" => "efb7614007ae426481de69310ec14953", "expires_in" => 28799, "refresh_token" => "cdca6acfeb374598ba2790c9e5283b21"}]}

client.refresh_access_token

assert_not_equal old_token.access_token, client.oauth_token.access_token
assert_equal old_token.refresh_token, client.oauth_token.refresh_token
end

test 'should automatically refresh an expired token' do
# this token is already expired in the test database
# Podio.client.oauth_token = Podio::OAuthToken.new('access_token' => '30da4594eef93528c11df7fb5deb989cd629ea7060a1ce1ced628d19398214c942bcfe0334cf953ef70a80ea1afdfd80183d5c75d19c1f5526ca4c6f6f3471ef', 'refresh_token' => '82e7a11ae187f28a25261599aa6229cd89f8faee48cba18a75d70efae88ba665ced11d43143b7f5bebb31a4103662b851dd2db1879a3947b843259479fccfad3', 'expires_in' => -10)
Podio.client.stubs.post('/oauth/token') {[200, {}, {"access_token" => "a3345189a07b478284356c8b0b3c54d5", "expires_in" => 28799, "refresh_token" => "cdca6acfeb374598ba2790c9e5283b21"}]}

login_as(:professor)
Podio.client.reset

Podio.client.stubs.get('/org/') {[200, {}, [{"status" => "active"}]]}

assert_nothing_raised do
response = Podio.connection.get("/org/")
assert_equal 200, response.status
Expand All @@ -80,6 +87,9 @@ class ClientTest < Test::Unit::TestCase

test 'should be able to make arbitrary requests' do
login_as(:professor)

Podio.client.stubs.get('/org/') {[200, {}, [{"status" => "active"}]]}

response = Podio.connection.get("/org/")
assert_equal 200, response.status
assert response.body.is_a?(Array)
Expand Down
1 change: 0 additions & 1 deletion test/fixtures/client/1e1fb7999f3b19d6c8d6dfd0c5820133.rack

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/client/abfda7ef2946ffad654fc42d00627569.rack

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/client/cc73b5e3e28cce22bf8d644a68178b70.rack

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/client/eaa5ecfefb156b0a0bd29b3591786946.rack

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/client/f5a56d9a7e19777642508edf80a45bc8.rack

This file was deleted.

25 changes: 1 addition & 24 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

require 'podio'

ENABLE_STUBS = ENV['ENABLE_STUBS'] == 'true'
ENABLE_RECORD = ENV['ENABLE_RECORD'] == 'true'

class Test::Unit::TestCase

def setup
set_podio_client
stub_responses if ENABLE_STUBS
end

# test "verify something" do
Expand All @@ -33,8 +29,7 @@ def set_podio_client
:api_url => 'http://api-sandbox.podio.dev',
:api_key => '[email protected]',
:api_secret => 'sandbox_secret',
:enable_stubs => ENABLE_STUBS && !ENABLE_RECORD,
:record_mode => ENABLE_RECORD,
:enable_stubs => true,
:test_mode => true
)
end
Expand All @@ -47,24 +42,6 @@ def login_as(user_identifier)
Podio.client.reset
end

def stub_responses
folder_name = self.class.name.underscore.gsub('_test', '')
current_folder = File.join(File.dirname(__FILE__), 'fixtures', folder_name)

if File.exists?(current_folder)
Dir.foreach(current_folder) do |filename|
next unless filename.include?('.rack')

rack_response = eval(File.read(File.join(current_folder, filename)))

url = rack_response.shift
method = rack_response.shift

Podio.client.stubs.send(method, url) { rack_response }
end
end
end

def fixtures
@fixtures ||= YAML.load_file(File.join(File.dirname(__FILE__), 'fixtures', 'fixtures.yaml'))
end
Expand Down

0 comments on commit c0906c2

Please sign in to comment.