Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add access_scopes attribute to session object #843

Merged
merged 1 commit into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/shopify_api/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Session
self.myshopify_domain = 'myshopify.com'

attr_accessor :domain, :token, :name, :extra
attr_reader :api_version
attr_reader :api_version, :access_scopes
alias_method :url, :domain

class << self
Expand Down Expand Up @@ -92,10 +92,11 @@ def extract_current_session
end
end

def initialize(domain:, token:, api_version: ShopifyAPI::Base.api_version, extra: {})
def initialize(domain:, token:, access_scopes: nil, api_version: ShopifyAPI::Base.api_version, extra: {})
self.domain = self.class.prepare_domain(domain)
self.api_version = api_version
self.token = token
self.access_scopes = access_scopes
self.extra = extra
end

Expand Down Expand Up @@ -180,6 +181,11 @@ def state

private

def access_scopes=(access_scopes)
return unless access_scopes
@access_scopes = ShopifyAPI::ApiAccess.new(access_scopes)
end

def parameterize(params)
URI.escape(params.collect { |k, v| "#{k}=#{v}" }.join('&'))
end
Expand Down
63 changes: 63 additions & 0 deletions test/session_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,39 @@ def setup
assert(session.valid?)
end

test "be valid with nil access_scopes" do
session = ShopifyAPI::Session.new(
domain: "testshop.myshopify.com",
token: "any-token",
api_version: any_api_version,
access_scopes: nil
)

assert(session.valid?)
end

test "be valid with string of access_scopes" do
session = ShopifyAPI::Session.new(
domain: "testshop.myshopify.com",
token: "any-token",
api_version: any_api_version,
access_scopes: "read_products, write_orders"
)

assert(session.valid?)
end

test "be valid with a collection of access_scopes" do
session = ShopifyAPI::Session.new(
domain: "testshop.myshopify.com",
token: "any-token",
api_version: any_api_version,
access_scopes: %w(read_products write_orders)
)

assert(session.valid?)
end

test "not raise error without params" do
assert_nothing_raised do
ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: "any-token", api_version: any_api_version)
Expand Down Expand Up @@ -84,6 +117,36 @@ def setup
end
end

test "provides default nil access_scopes attribute" do
session = ShopifyAPI::Session.new(
domain: "testshop.myshopify.com",
token: "any-token",
api_version: any_api_version
)
assert_nil session.access_scopes
end

test "provides specified nil access_scopes attribute" do
session = ShopifyAPI::Session.new(
domain: "testshop.myshopify.com",
token: "any-token",
access_scopes: "read_products",
api_version: any_api_version
)
assert_equal "read_products", session.access_scopes.to_s
end

test "session instantiation raises error if bad access scopes are provided" do
assert_raises NoMethodError do
ShopifyAPI::Session.new(
domain: "testshop.myshopify.com",
token: "any-token",
access_scopes: { bad_input: "bad_input" },
api_version: any_api_version
)
end
end

test "raise error if params passed but signature omitted" do
assert_raises(ShopifyAPI::ValidationException) do
session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: nil, api_version: any_api_version)
Expand Down