Skip to content

Commit 63108ed

Browse files
committed
Add access_scopes attribute to Session
1 parent 49ac8cc commit 63108ed

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

lib/shopify_api/session.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Session
1313
self.myshopify_domain = 'myshopify.com'
1414

1515
attr_accessor :domain, :token, :name, :extra
16-
attr_reader :api_version
16+
attr_reader :api_version, :access_scopes
1717
alias_method :url, :domain
1818

1919
class << self
@@ -92,10 +92,11 @@ def extract_current_session
9292
end
9393
end
9494

95-
def initialize(domain:, token:, api_version: ShopifyAPI::Base.api_version, extra: {})
95+
def initialize(domain:, token:, access_scopes: nil, api_version: ShopifyAPI::Base.api_version, extra: {})
9696
self.domain = self.class.prepare_domain(domain)
9797
self.api_version = api_version
9898
self.token = token
99+
self.access_scopes = access_scopes
99100
self.extra = extra
100101
end
101102

@@ -180,6 +181,11 @@ def state
180181

181182
private
182183

184+
def access_scopes=(access_scopes)
185+
return unless access_scopes
186+
@access_scopes = ShopifyAPI::ApiAccess.new(access_scopes)
187+
end
188+
183189
def parameterize(params)
184190
URI.escape(params.collect { |k, v| "#{k}=#{v}" }.join('&'))
185191
end

test/session_test.rb

+63
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,39 @@ def setup
5454
assert(session.valid?)
5555
end
5656

57+
test "be valid with nil access_scopes" do
58+
session = ShopifyAPI::Session.new(
59+
domain: "testshop.myshopify.com",
60+
token: "any-token",
61+
api_version: any_api_version,
62+
access_scopes: nil
63+
)
64+
65+
assert(session.valid?)
66+
end
67+
68+
test "be valid with string of access_scopes" do
69+
session = ShopifyAPI::Session.new(
70+
domain: "testshop.myshopify.com",
71+
token: "any-token",
72+
api_version: any_api_version,
73+
access_scopes: "read_products, write_orders"
74+
)
75+
76+
assert(session.valid?)
77+
end
78+
79+
test "be valid with a collection of access_scopes" do
80+
session = ShopifyAPI::Session.new(
81+
domain: "testshop.myshopify.com",
82+
token: "any-token",
83+
api_version: any_api_version,
84+
access_scopes: %w(read_products write_orders)
85+
)
86+
87+
assert(session.valid?)
88+
end
89+
5790
test "not raise error without params" do
5891
assert_nothing_raised do
5992
ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: "any-token", api_version: any_api_version)
@@ -84,6 +117,36 @@ def setup
84117
end
85118
end
86119

120+
test "provides default nil access_scopes attribute" do
121+
session = ShopifyAPI::Session.new(
122+
domain: "testshop.myshopify.com",
123+
token: "any-token",
124+
api_version: any_api_version
125+
)
126+
assert_nil session.access_scopes
127+
end
128+
129+
test "provides specified nil access_scopes attribute" do
130+
session = ShopifyAPI::Session.new(
131+
domain: "testshop.myshopify.com",
132+
token: "any-token",
133+
access_scopes: "read_products",
134+
api_version: any_api_version
135+
)
136+
assert_equal "read_products", session.access_scopes.to_s
137+
end
138+
139+
test "session instantiation raises error if bad access scopes are provided" do
140+
assert_raises do
141+
ShopifyAPI::Session.new(
142+
domain: "testshop.myshopify.com",
143+
token: "any-token",
144+
access_scopes: { bad_input: "bad_input" },
145+
api_version: any_api_version
146+
)
147+
end
148+
end
149+
87150
test "raise error if params passed but signature omitted" do
88151
assert_raises(ShopifyAPI::ValidationException) do
89152
session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: nil, api_version: any_api_version)

0 commit comments

Comments
 (0)