Browse Source

added tests for request token

Kevin Hughes 11 years ago
parent
commit
ea651f061a
3 changed files with 25 additions and 3 deletions
  1. 1 1
      lib/shopify_api/session.rb
  2. 17 0
      test/session_test.rb
  3. 7 2
      test/test_helper.rb

+ 1 - 1
lib/shopify_api/session.rb

@@ -93,7 +93,7 @@ module ShopifyAPI
       if response.code == "200"
         self.token = JSON.parse(response.body)['access_token']
       else
-        raise response.msg
+        raise RuntimeError, response.msg
       end
     end
 

+ 17 - 0
test/session_test.rb

@@ -53,6 +53,23 @@ class SessionTest < Test::Unit::TestCase
       assert_equal 'https://fakeshop.myshopify.com/admin', ShopifyAPI::Base.site.to_s
     end
 
+    should "request token should get token" do
+      ShopifyAPI::Session.setup(:api_key => "My test key", :secret => "My test secret")
+      session = ShopifyAPI::Session.new('http://localhost.myshopify.com')
+      fake nil, :url => 'https://localhost.myshopify.com/admin/oauth/access_token',:method => :post, :body => '{"access_token" : "token"}'
+      assert_equal "token", session.request_token("code")
+    end
+
+    should "raise exception if code invalid in request token" do
+      ShopifyAPI::Session.setup(:api_key => "My test key", :secret => "My test secret")
+      session = ShopifyAPI::Session.new('http://localhost.myshopify.com')
+      fake nil, :url => 'https://localhost.myshopify.com/admin/oauth/access_token',:method => :post, :status => 404, :body => '{"error" : "invalid_request"}'
+      assert_raises(RuntimeError) do
+        session.request_token("bad_code")
+      end
+      assert_equal false, session.valid?
+    end
+
     should "#temp reset ShopifyAPI::Base.site to original value when using a non-standard port" do
       ShopifyAPI::Session.setup(:api_key => "key", :secret => "secret")
       session1 = ShopifyAPI::Session.new('fakeshop.myshopify.com:3000', 'token1')

+ 7 - 2
test/test_helper.rb

@@ -58,8 +58,13 @@ class Test::Unit::TestCase
     format = options.delete(:format) || :json
     method = options.delete(:method) || :get
     extension = ".#{options.delete(:extension)||'json'}" unless options[:extension]==false
-
-    url = "http://localhost/admin/#{endpoint}#{extension}"
+    
+    url = if options.has_key?(:url)
+      options[:url]
+    else  
+      "http://localhost/admin/#{endpoint}#{extension}"
+    end
+    
     FakeWeb.register_uri(method, url, {:body => body, :status => 200, :content_type => "text/#{format}", :content_length => 1}.merge(options))
   end
 end