spec/support/koala_test.rb in koala-1.10.1 vs spec/support/koala_test.rb in koala-1.11.0rc
- old
+ new
@@ -3,51 +3,45 @@
class << self
attr_accessor :oauth_token, :app_id, :secret, :app_access_token, :code, :session_key
attr_accessor :oauth_test_data, :subscription_test_data, :search_time
attr_accessor :test_user_api
+ attr_accessor :vcr_oauth_token
end
# Test setup
def self.setup_test_environment!
setup_rspec
- unless ENV['LIVE']
- # By default the Koala specs are run using stubs for HTTP requests,
- # so they won't fail due to Facebook-imposed rate limits or server timeouts.
- #
- # However as a result they are more brittle since
- # we are not testing the latest responses from the Facebook servers.
- # To be certain all specs pass with the current Facebook services,
- # run LIVE=true bundle exec rake spec.
- Koala.http_service = Koala::MockHTTPService
- KoalaTest.setup_test_data(Koala::MockHTTPService::TEST_DATA)
- else
+ if live?
# Runs Koala specs through the Facebook servers
# using data for a real app
live_data = YAML.load_file(File.join(File.dirname(__FILE__), '../fixtures/facebook_data.yml'))
KoalaTest.setup_test_data(live_data)
- # allow live tests with different adapters
- adapter = ENV['ADAPTER'] || "typhoeus" # use Typhoeus by default if available
- begin
- require adapter
- require 'typhoeus/adapters/faraday' if adapter.to_s == "typhoeus"
- Faraday.default_adapter = adapter.to_sym
- rescue LoadError
- puts "Unable to load adapter #{adapter}, using Net::HTTP."
- end
+ activate_adapter!
Koala.http_service.http_options[:beta] = true if ENV["beta"] || ENV["BETA"]
# use a test user unless the developer wants to test against a real profile
unless token = KoalaTest.oauth_token
KoalaTest.setup_test_users
else
KoalaTest.validate_user_info(token)
end
+ else
+ # By default the Koala specs are run using stubs for HTTP requests,
+ # so they won't fail due to Facebook-imposed rate limits or server timeouts.
+ #
+ # However as a result they are more brittle since
+ # we are not testing the latest responses from the Facebook servers.
+ # To be certain all specs pass with the current Facebook services,
+ # run LIVE=true bundle exec rake spec.
+ activate_vcr!
+ Koala.http_service = Koala::MockHTTPService
+ KoalaTest.setup_test_data(Koala::MockHTTPService::TEST_DATA)
end
end
def self.setup_rspec
# set up a global before block to set the token for tests
@@ -77,10 +71,32 @@
end
end
end
+ # If we're running live tests against the Facebook API, don't use the VCR
+ # fixtures. (Another alternative would be to rerecord those fixtures, but we
+ # don't necessarily want to do that.
+ def self.with_vcr_unless_live(name)
+ if live?
+ yield
+ else
+ begin
+ # if we're using VCR, we don't want to use the mock service, which was
+ # an early implementation of the same type of tool
+ old_adapter = Koala.http_service
+ Koala.http_service = Koala::HTTPService
+ activate_adapter!
+ VCR.use_cassette(name) do
+ yield
+ end
+ ensure
+ Koala.http_service = old_adapter
+ end
+ end
+ end
+
def self.setup_test_data(data)
# make data accessible to all our tests
self.oauth_test_data = data["oauth_test_data"]
self.subscription_test_data = data["subscription_test_data"]
self.oauth_token = data["oauth_token"]
@@ -88,16 +104,18 @@
self.app_access_token = data["oauth_test_data"]["app_access_token"]
self.secret = data["oauth_test_data"]["secret"]
self.code = data["oauth_test_data"]["code"]
self.session_key = data["oauth_test_data"]["session_key"]
+ self.vcr_oauth_token = data["vcr_data"]["oauth_token"]
+
# fix the search time so it can be used in the mock responses
self.search_time = data["search_time"] || (Time.now - 3600).to_s
end
def self.testing_permissions
- "read_stream, publish_stream, user_photos, user_videos, read_insights"
+ "read_stream, publish_actions, user_photos, user_videos, read_insights"
end
def self.setup_test_users
print "Setting up test users..."
@test_user_api = Koala::Facebook::TestUsers.new(:app_id => self.app_id, :secret => self.secret)
@@ -141,11 +159,11 @@
api = Koala::Facebook::API.new(token)
perms = api.fql_query("select #{testing_permissions} from permissions where uid = me()")[0]
perms.each_pair do |perm, value|
if value == (perm == "read_insights" ? 1 : 0) # live testing depends on insights calls failing
puts "failed!\n" # put a new line after the print above
- raise ArgumentError, "Your access token must have the read_stream, publish_stream, and user_photos permissions, and lack read_insights. You have: #{perms.inspect}"
+ raise ArgumentError, "Your access token must have #{testing_permissions.join(", ")}, and lack read_insights. You have: #{perms.inspect}"
end
end
puts "done!"
end
@@ -196,7 +214,39 @@
"facebook"
end
def self.app_properties
mock_interface? ? {"desktop" => 0} : {"description" => "A test framework for Koala and its users. (#{rand(10000).to_i})"}
+ end
+
+ def self.live?
+ ENV['LIVE']
+ end
+
+ def self.activate_vcr!
+ require 'vcr'
+
+ VCR.configure do |c|
+ c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
+ c.hook_into :webmock # or :fakeweb
+ end
+ end
+
+ def self.activate_adapter!
+ unless @adapter_activation_attempted
+ # allow live tests with different adapters
+ adapter = ENV['ADAPTER'] || "typhoeus" # use Typhoeus by default if available
+ begin
+ # JRuby doesn't support typhoeus on Travis
+ unless defined? JRUBY_VERSION
+ require adapter
+ require 'typhoeus/adapters/faraday' if adapter.to_s == "typhoeus"
+ Faraday.default_adapter = adapter.to_sym
+ end
+ rescue LoadError
+ puts "Unable to load adapter #{adapter}, using Net::HTTP."
+ ensure
+ @adapter_activation_attempted = true
+ end
+ end
end
end