|
@@ -9,46 +9,51 @@ class SessionTest < Test::Unit::TestCase
|
|
end
|
|
end
|
|
|
|
|
|
test "not be valid without a url" do
|
|
test "not be valid without a url" do
|
|
- session = ShopifyAPI::Session.new(nil, "any-token")
|
|
|
|
|
|
+ session = ShopifyAPI::Session.new(nil, "any-token", any_api_version)
|
|
assert_not session.valid?
|
|
assert_not session.valid?
|
|
end
|
|
end
|
|
|
|
|
|
test "not be valid without token" do
|
|
test "not be valid without token" do
|
|
- session = ShopifyAPI::Session.new("testshop.myshopify.com")
|
|
|
|
|
|
+ session = ShopifyAPI::Session.new("testshop.myshopify.com", nil, any_api_version)
|
|
assert_not session.valid?
|
|
assert_not session.valid?
|
|
end
|
|
end
|
|
|
|
|
|
- test "be valid with any token and any url" do
|
|
|
|
- session = ShopifyAPI::Session.new("testshop.myshopify.com", "any-token")
|
|
|
|
|
|
+ test "not be valid without an api version" do
|
|
|
|
+ session = ShopifyAPI::Session.new("testshop.myshopify.com", "any-token", nil)
|
|
|
|
+ assert_not session.valid?
|
|
|
|
+ end
|
|
|
|
+
|
|
|
|
+ test "be valid with any token, any url and version" do
|
|
|
|
+ session = ShopifyAPI::Session.new("testshop.myshopify.com", "any-token", any_api_version)
|
|
assert session.valid?
|
|
assert session.valid?
|
|
end
|
|
end
|
|
|
|
|
|
test "not raise error without params" do
|
|
test "not raise error without params" do
|
|
assert_nothing_raised do
|
|
assert_nothing_raised do
|
|
- session = ShopifyAPI::Session.new("testshop.myshopify.com", "any-token")
|
|
|
|
|
|
+ ShopifyAPI::Session.new("testshop.myshopify.com", "any-token", any_api_version)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
test "ignore everything but the subdomain in the shop" do
|
|
test "ignore everything but the subdomain in the shop" do
|
|
assert_equal(
|
|
assert_equal(
|
|
"https://testshop.myshopify.com",
|
|
"https://testshop.myshopify.com",
|
|
- ShopifyAPI::Session.new("http://user:pass@testshop.notshopify.net/path", "any-token").site
|
|
|
|
|
|
+ ShopifyAPI::Session.new("http://user:pass@testshop.notshopify.net/path", "any-token", any_api_version).site
|
|
)
|
|
)
|
|
end
|
|
end
|
|
|
|
|
|
test "append the myshopify domain if not given" do
|
|
test "append the myshopify domain if not given" do
|
|
- assert_equal "https://testshop.myshopify.com", ShopifyAPI::Session.new("testshop", "any-token").site
|
|
|
|
|
|
+ assert_equal "https://testshop.myshopify.com", ShopifyAPI::Session.new("testshop", "any-token", any_api_version).site
|
|
end
|
|
end
|
|
|
|
|
|
test "not raise error without params" do
|
|
test "not raise error without params" do
|
|
assert_nothing_raised do
|
|
assert_nothing_raised do
|
|
- session = ShopifyAPI::Session.new("testshop.myshopify.com", "any-token")
|
|
|
|
|
|
+ ShopifyAPI::Session.new("testshop.myshopify.com", "any-token", any_api_version)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
test "raise error if params passed but signature omitted" do
|
|
test "raise error if params passed but signature omitted" do
|
|
assert_raises(ShopifyAPI::ValidationException) do
|
|
assert_raises(ShopifyAPI::ValidationException) do
|
|
- session = ShopifyAPI::Session.new("testshop.myshopify.com")
|
|
|
|
|
|
+ session = ShopifyAPI::Session.new("testshop.myshopify.com", nil, any_api_version)
|
|
session.request_token({'code' => 'any-code'})
|
|
session.request_token({'code' => 'any-code'})
|
|
end
|
|
end
|
|
end
|
|
end
|
|
@@ -62,19 +67,23 @@ class SessionTest < Test::Unit::TestCase
|
|
test "#temp reset ShopifyAPI::Base.site to original value" do
|
|
test "#temp reset ShopifyAPI::Base.site to original value" do
|
|
|
|
|
|
ShopifyAPI::Session.setup(:api_key => "key", :secret => "secret")
|
|
ShopifyAPI::Session.setup(:api_key => "key", :secret => "secret")
|
|
- session1 = ShopifyAPI::Session.new('fakeshop.myshopify.com', 'token1')
|
|
|
|
|
|
+ session1 = ShopifyAPI::Session.new('fakeshop.myshopify.com', 'token1', :no_version)
|
|
ShopifyAPI::Base.activate_session(session1)
|
|
ShopifyAPI::Base.activate_session(session1)
|
|
|
|
|
|
- ShopifyAPI::Session.temp("testshop.myshopify.com", "any-token") {
|
|
|
|
|
|
+ ShopifyAPI::Session.temp("testshop.myshopify.com", "any-token", :unstable) {
|
|
@assigned_site = ShopifyAPI::Base.site
|
|
@assigned_site = ShopifyAPI::Base.site
|
|
|
|
+ @assigned_version = ShopifyAPI::Base.api_version
|
|
}
|
|
}
|
|
- assert_equal 'https://testshop.myshopify.com', @assigned_site.to_s
|
|
|
|
- assert_equal 'https://fakeshop.myshopify.com', ShopifyAPI::Base.site.to_s
|
|
|
|
|
|
+ assert_equal('https://testshop.myshopify.com', @assigned_site.to_s)
|
|
|
|
+ assert_equal('https://fakeshop.myshopify.com', ShopifyAPI::Base.site.to_s)
|
|
|
|
+
|
|
|
|
+ assert_equal(ShopifyAPI::ApiVersion::Unstable.new, @assigned_version)
|
|
|
|
+ assert_equal(ShopifyAPI::ApiVersion::NoVersion.new, ShopifyAPI::Base.api_version)
|
|
end
|
|
end
|
|
|
|
|
|
test "create_permission_url returns correct url with single scope no redirect uri" do
|
|
test "create_permission_url returns correct url with single scope no redirect uri" do
|
|
ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
|
|
ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
|
|
- session = ShopifyAPI::Session.new('http://localhost.myshopify.com')
|
|
|
|
|
|
+ session = ShopifyAPI::Session.new('http://localhost.myshopify.com', 'any-token', any_api_version)
|
|
scope = ["write_products"]
|
|
scope = ["write_products"]
|
|
permission_url = session.create_permission_url(scope)
|
|
permission_url = session.create_permission_url(scope)
|
|
assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=write_products", permission_url
|
|
assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=write_products", permission_url
|
|
@@ -82,7 +91,7 @@ class SessionTest < Test::Unit::TestCase
|
|
|
|
|
|
test "create_permission_url returns correct url with single scope and redirect uri" do
|
|
test "create_permission_url returns correct url with single scope and redirect uri" do
|
|
ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
|
|
ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
|
|
- session = ShopifyAPI::Session.new('http://localhost.myshopify.com')
|
|
|
|
|
|
+ session = ShopifyAPI::Session.new('http://localhost.myshopify.com', 'any-token', any_api_version)
|
|
scope = ["write_products"]
|
|
scope = ["write_products"]
|
|
permission_url = session.create_permission_url(scope, "http://my_redirect_uri.com")
|
|
permission_url = session.create_permission_url(scope, "http://my_redirect_uri.com")
|
|
assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=write_products&redirect_uri=http://my_redirect_uri.com", permission_url
|
|
assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=write_products&redirect_uri=http://my_redirect_uri.com", permission_url
|
|
@@ -90,7 +99,7 @@ class SessionTest < Test::Unit::TestCase
|
|
|
|
|
|
test "create_permission_url returns correct url with dual scope no redirect uri" do
|
|
test "create_permission_url returns correct url with dual scope no redirect uri" do
|
|
ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
|
|
ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
|
|
- session = ShopifyAPI::Session.new('http://localhost.myshopify.com')
|
|
|
|
|
|
+ session = ShopifyAPI::Session.new('http://localhost.myshopify.com', 'any-token', any_api_version)
|
|
scope = ["write_products","write_customers"]
|
|
scope = ["write_products","write_customers"]
|
|
permission_url = session.create_permission_url(scope)
|
|
permission_url = session.create_permission_url(scope)
|
|
assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=write_products,write_customers", permission_url
|
|
assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=write_products,write_customers", permission_url
|
|
@@ -98,7 +107,7 @@ class SessionTest < Test::Unit::TestCase
|
|
|
|
|
|
test "create_permission_url returns correct url with no scope no redirect uri" do
|
|
test "create_permission_url returns correct url with no scope no redirect uri" do
|
|
ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
|
|
ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
|
|
- session = ShopifyAPI::Session.new('http://localhost.myshopify.com')
|
|
|
|
|
|
+ session = ShopifyAPI::Session.new('http://localhost.myshopify.com', 'any-token', any_api_version)
|
|
scope = []
|
|
scope = []
|
|
permission_url = session.create_permission_url(scope)
|
|
permission_url = session.create_permission_url(scope)
|
|
assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=", permission_url
|
|
assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=", permission_url
|
|
@@ -106,7 +115,7 @@ class SessionTest < Test::Unit::TestCase
|
|
|
|
|
|
test "raise exception if code invalid in request token" do
|
|
test "raise exception if code invalid in request token" do
|
|
ShopifyAPI::Session.setup(:api_key => "My test key", :secret => "My test secret")
|
|
ShopifyAPI::Session.setup(:api_key => "My test key", :secret => "My test secret")
|
|
- session = ShopifyAPI::Session.new('http://localhost.myshopify.com')
|
|
|
|
|
|
+ session = ShopifyAPI::Session.new('http://localhost.myshopify.com', nil, any_api_version)
|
|
fake nil, :url => 'https://localhost.myshopify.com/admin/oauth/access_token',:method => :post, :status => 404, :body => '{"error" : "invalid_request"}'
|
|
fake nil, :url => 'https://localhost.myshopify.com/admin/oauth/access_token',:method => :post, :status => 404, :body => '{"error" : "invalid_request"}'
|
|
assert_raises(ShopifyAPI::ValidationException) do
|
|
assert_raises(ShopifyAPI::ValidationException) do
|
|
session.request_token(params={:code => "bad-code"})
|
|
session.request_token(params={:code => "bad-code"})
|
|
@@ -115,16 +124,17 @@ class SessionTest < Test::Unit::TestCase
|
|
end
|
|
end
|
|
|
|
|
|
test "return site for session" do
|
|
test "return site for session" do
|
|
- session = ShopifyAPI::Session.new("testshop.myshopify.com", "any-token")
|
|
|
|
|
|
+ session = ShopifyAPI::Session.new("testshop.myshopify.com", "any-token", any_api_version)
|
|
assert_equal "https://testshop.myshopify.com", session.site
|
|
assert_equal "https://testshop.myshopify.com", session.site
|
|
end
|
|
end
|
|
|
|
|
|
test "return_token_if_signature_is_valid" do
|
|
test "return_token_if_signature_is_valid" do
|
|
|
|
+ api_version = any_api_version
|
|
fake nil,
|
|
fake nil,
|
|
- url: 'https://testshop.myshopify.com/admin/oauth/access_token',
|
|
|
|
|
|
+ url: "https://testshop.myshopify.com/admin/oauth/access_token",
|
|
method: :post,
|
|
method: :post,
|
|
body: '{"access_token":"any-token"}'
|
|
body: '{"access_token":"any-token"}'
|
|
- session = ShopifyAPI::Session.new("testshop.myshopify.com")
|
|
|
|
|
|
+ session = ShopifyAPI::Session.new("testshop.myshopify.com", nil, api_version)
|
|
|
|
|
|
params = { code: 'any-code', timestamp: Time.now }
|
|
params = { code: 'any-code', timestamp: Time.now }
|
|
token = session.request_token(params.merge(hmac: generate_signature(params)))
|
|
token = session.request_token(params.merge(hmac: generate_signature(params)))
|
|
@@ -134,11 +144,12 @@ class SessionTest < Test::Unit::TestCase
|
|
end
|
|
end
|
|
|
|
|
|
test "extra parameters are stored in session" do
|
|
test "extra parameters are stored in session" do
|
|
|
|
+ api_version = ShopifyAPI::ApiVersion::Unstable.new
|
|
fake nil,
|
|
fake nil,
|
|
- url: 'https://testshop.myshopify.com/admin/oauth/access_token',
|
|
|
|
|
|
+ url: "https://testshop.myshopify.com/admin/oauth/access_token",
|
|
method: :post,
|
|
method: :post,
|
|
body: '{"access_token":"any-token","foo":"example"}'
|
|
body: '{"access_token":"any-token","foo":"example"}'
|
|
- session = ShopifyAPI::Session.new("testshop.myshopify.com")
|
|
|
|
|
|
+ session = ShopifyAPI::Session.new("testshop.myshopify.com", nil, api_version)
|
|
|
|
|
|
params = { code: 'any-code', timestamp: Time.now }
|
|
params = { code: 'any-code', timestamp: Time.now }
|
|
assert session.request_token(params.merge(hmac: generate_signature(params)))
|
|
assert session.request_token(params.merge(hmac: generate_signature(params)))
|
|
@@ -147,11 +158,12 @@ class SessionTest < Test::Unit::TestCase
|
|
end
|
|
end
|
|
|
|
|
|
test "expires_in is automatically converted in expires_at" do
|
|
test "expires_in is automatically converted in expires_at" do
|
|
|
|
+ api_version = any_api_version
|
|
fake nil,
|
|
fake nil,
|
|
- url: 'https://testshop.myshopify.com/admin/oauth/access_token',
|
|
|
|
|
|
+ url: "https://testshop.myshopify.com/admin/oauth/access_token",
|
|
method: :post,
|
|
method: :post,
|
|
body: '{"access_token":"any-token","expires_in":86393}'
|
|
body: '{"access_token":"any-token","expires_in":86393}'
|
|
- session = ShopifyAPI::Session.new("testshop.myshopify.com")
|
|
|
|
|
|
+ session = ShopifyAPI::Session.new("testshop.myshopify.com", nil, api_version)
|
|
|
|
|
|
Timecop.freeze do
|
|
Timecop.freeze do
|
|
params = { code: 'any-code', timestamp: Time.now }
|
|
params = { code: 'any-code', timestamp: Time.now }
|
|
@@ -175,7 +187,7 @@ class SessionTest < Test::Unit::TestCase
|
|
signature = generate_signature(params)
|
|
signature = generate_signature(params)
|
|
params[:foo] = 'world'
|
|
params[:foo] = 'world'
|
|
assert_raises(ShopifyAPI::ValidationException) do
|
|
assert_raises(ShopifyAPI::ValidationException) do
|
|
- session = ShopifyAPI::Session.new("testshop.myshopify.com")
|
|
|
|
|
|
+ session = ShopifyAPI::Session.new("testshop.myshopify.com", nil, any_api_version)
|
|
session.request_token(params.merge(:hmac => signature))
|
|
session.request_token(params.merge(:hmac => signature))
|
|
end
|
|
end
|
|
end
|
|
end
|
|
@@ -185,7 +197,7 @@ class SessionTest < Test::Unit::TestCase
|
|
signature = generate_signature(params)
|
|
signature = generate_signature(params)
|
|
params[:foo] = 'world'
|
|
params[:foo] = 'world'
|
|
assert_raises(ShopifyAPI::ValidationException) do
|
|
assert_raises(ShopifyAPI::ValidationException) do
|
|
- session = ShopifyAPI::Session.new("testshop.myshopify.com")
|
|
|
|
|
|
+ session = ShopifyAPI::Session.new("testshop.myshopify.com", nil, any_api_version)
|
|
session.request_token(params.merge(:hmac => signature))
|
|
session.request_token(params.merge(:hmac => signature))
|
|
end
|
|
end
|
|
end
|
|
end
|
|
@@ -224,4 +236,8 @@ class SessionTest < Test::Unit::TestCase
|
|
params = make_sorted_params(params) if params.is_a?(Hash)
|
|
params = make_sorted_params(params) if params.is_a?(Hash)
|
|
OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, ShopifyAPI::Session.secret, params)
|
|
OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, ShopifyAPI::Session.secret, params)
|
|
end
|
|
end
|
|
|
|
+
|
|
|
|
+ def any_api_version
|
|
|
|
+ [ShopifyAPI::ApiVersion::NoVersion.new, ShopifyAPI::ApiVersion::Unstable.new].sample(1).first
|
|
|
|
+ end
|
|
end
|
|
end
|