require 'helper' require 'webmock' FACEBOOK_APP_ID = "123456789" FACEBOOK_SECRET = "abcdef" FACEBOOK_CALLBACK = "callbacks" FLICKR_TOKEN = "abcde" FLICKR_SECRET = "lmnop" FLICKR_CALLBACK = "callbacks" # reopen MealTicket so we can set the @env instance variable and call private methods class MealTicket attr_accessor :env def _get_query_string_parameter(param) get_query_string_parameter(param) end def _get_root_url get_root_url end end class TestMealTicket < Test::Unit::TestCase include MealTicketRoutes include WebMock::API # utility methods context 'Calling the get_query_string_parameter method' do setup do @rack_app = MealTicket.new({}) end tests = ["", "one=1", "code=calliope", "one=1@code=calliope", "code=calliope&two=1", "one=1&code=calliope&two=2"] expectations = [nil, nil, "calliope", "calliope", "calliope", "calliope"] tests.each_with_index do |query_string, i| context "on #{query_string}" do setup do @rack_app.env = {"QUERY_STRING" => query_string} end should "return #{expectations[i]}" do assert_equal expectations[i], @rack_app._get_query_string_parameter("code") end end end end context 'Calling the get_root_url method' do setup do @rack_app = MealTicket.new({}) end tests = ["http://localhost:3000/", "http://localhost:3000/u/r/i", "https://localhost:3000/", "http://a.b.c.d.e.f.g.com/", "http://a.b.c/d/e/f"] expectations = ["http://localhost:3000/", "http://localhost:3000/", "https://localhost:3000/", "http://a.b.c.d.e.f.g.com/", "http://a.b.c/"] tests.each_with_index do |uri, i| context "on #{uri}" do setup do @rack_app.env = {"REQUEST_URI" => uri} end should "return #{expectations[i]}" do assert_equal expectations[i], @rack_app._get_root_url end end end end # facebook context 'Requesting the facebook auth url' do context 'with valid parameters' do setup do @url = facebook_auth_url("http://localhost:3000/", "user_photos") end should 'return a valid url' do assert_equal "https://graph.facebook.com/oauth/authorize?client_id=123456789&redirect_uri=http://localhost:3000/meal_ticket/facebook_callback&scope=user_photos", @url end end end context "When a facebook authentication is successful, the facebook_callback method" do setup do stub_request(:get, "https://graph.facebook.com/oauth/access_token?client_id=123456789&client_secret=abcdef&code=12345&redirect_uri=http://localhost:3000/meal_ticket/facebook_callback"). with(:headers => {'Accept'=>'*/*'}). to_return(:status => 200, :body => "access_token=12345&expires=1000", :headers => {}) # create a new instance rack_app = MealTicket.new({}) # set up the environment - the request where facebook is redirecting the user to our callback rack_app.env = {"REQUEST_URI" => "http://localhost:3000/meal_ticket/facebook_callback?code=12345", "QUERY_STRING" => "code=12345", "PATH_INFO" => "/meal_ticket/facebook_callback"} # process the request @response = rack_app.facebook_callback end should "redirect to a URL that includes the facebook token and expiration" do assert_equal 302, @response[0] assert_equal "http://localhost:3000/callbacks?facebook[token]=12345&facebook[expires]=1000", @response[1]['Location'] end end context "When a facebook authentication is unsuccessful, the facebook_callback method" do setup do stub_request(:get, "https://graph.facebook.com/oauth/access_token?client_id=123456789&client_secret=abcdef&code=12345&redirect_uri=http://localhost:3000/meal_ticket/facebook_callback"). with(:headers => {'Accept'=>'*/*'}). to_return(:status => 400, :body => '{"error": {"type": "OAuthException","message": "Error validating verification code."}}', :headers => {}) # create a new instance rack_app = MealTicket.new({}) # set up the environment - the request where facebook is redirecting the user to our callback rack_app.env = {"REQUEST_URI" => "http://localhost:3000/meal_ticket/facebook_callback?code=12345", "QUERY_STRING" => "code=12345", "PATH_INFO" => "/meal_ticket/facebook_callback"} # process the request @response = rack_app.facebook_callback end should "redirect to a URL that includes the facebook token and expiration" do assert_equal 302, @response[0] assert_equal "http://localhost:3000/callbacks?facebook[error][type]=OAuthException&facebook[error][message]=Error+validating+verification+code.", @response[1]['Location'] end end #flickr context "When a flickr authentication is successful, the flickr_callback method" do setup do stub_request(:get, "http://api.flickr.com/services/rest/?api_key=abcde&api_sig=5a97a411c4281b61eda6d3d746d7afc6&frob=12345&method=flickr.auth.getToken"). with(:headers => {'Accept'=>'*/*'}). to_return(:status => 200, :body => ' 12345 delete ', :headers => {}) # create a new instance rack_app = MealTicket.new({}) # set up the environment - the request where facebook is redirecting the user to our callback rack_app.env = {"REQUEST_URI" => "http://localhost:3000/meal_ticket/flickr_callback?frob=12345", "QUERY_STRING" => "frob=12345", "PATH_INFO" => "/meal_ticket/flickr_callback"} # process the request @response = rack_app.flickr_callback end should "redirect to a URL that includes the facebook token and expiration" do assert_equal 302, @response[0] assert_equal "http://localhost:3000/callbacks?flickr[token]=12345&flickr[user_id]=12345%40N00", @response[1]['Location'] end end context "When a flickr authentication is successful, the flickr_callback method" do setup do stub_request(:get, "http://api.flickr.com/services/rest/?api_key=abcde&api_sig=5a97a411c4281b61eda6d3d746d7afc6&frob=12345&method=flickr.auth.getToken"). with(:headers => {'Accept'=>'*/*'}). to_return(:status => 200, :body => ' ', :headers => {}) # create a new instance rack_app = MealTicket.new({}) # set up the environment - the request where facebook is redirecting the user to our callback rack_app.env = {"REQUEST_URI" => "http://localhost:3000/meal_ticket/flickr_callback?frob=12345", "QUERY_STRING" => "frob=12345", "PATH_INFO" => "/meal_ticket/flickr_callback"} # process the request @response = rack_app.flickr_callback end should "redirect to a URL that includes the facebook token and expiration" do assert_equal 302, @response[0] assert_equal "http://localhost:3000/callbacks?flickr[error][code]=108&flickr[error][msg]=Invalid+frob", @response[1]['Location'] end end end