Created
May 17, 2013 05:06
-
-
Save jjasonclark/5597043 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/spec' | |
require 'minitest/autorun' | |
class Credentials < Struct.new(:login, :password) ; end | |
class EnvironmentCredentials < Credentials | |
def initialize() | |
super(ENV['USER'], ENV['PASSWORD']) | |
end | |
end | |
class SandboxCredentials < Credentials | |
def initialize() | |
super('SandboxUser', 'Password1') | |
end | |
end | |
module Addressable | |
class URI | |
attr_accessor :query_values | |
def self.parse input | |
URI.new | |
end | |
end | |
end | |
class ApiProvider | |
attr_accessor :uri | |
def initialize(credentials) | |
@uri = Addressable::URI.parse("http://api.example.org/query") | |
@uri.query_values = {usr: credentials.login, pwd: credentials.password} | |
end | |
end | |
describe "Environment Credentials" do | |
before do | |
@old_user = ENV['USER'] | |
@old_password = ENV['PASSWORD'] | |
end | |
after do | |
ENV['USER'] = @old_user unless @old_user.nil? | |
ENV['PASSWORD'] = @old_password unless @old_password.nil? | |
end | |
it "should get it's values from the environment" do | |
ENV['USER'] = 'UserBlahBlah' | |
ENV['PASSWORD'] = 'PasswordLaLa' | |
credentials = EnvironmentCredentials.new | |
assert_equal 'UserBlahBlah', credentials.login | |
assert_equal 'PasswordLaLa', credentials.password | |
end | |
end | |
describe "creation" do | |
it "requires credentials" do | |
assert_raises ArgumentError do | |
ApiProvider.new | |
end | |
end | |
it "sets user and password query values on creation" do | |
provider = ApiProvider.new SandboxCredentials.new | |
assert_equal 'SandboxUser', provider.uri.query_values[:usr] | |
assert_equal 'Password1', provider.uri.query_values[:pwd] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment