-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract session creation from access token response
- Loading branch information
Showing
7 changed files
with
177 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module ShopifyAPI | ||
module Auth | ||
module Oauth | ||
class AccessTokenResponse < T::Struct | ||
extend T::Sig | ||
|
||
const :access_token, String | ||
const :scope, String | ||
const :session, T.nilable(String) | ||
const :expires_in, T.nilable(Integer) | ||
const :associated_user, T.nilable(AssociatedUser) | ||
const :associated_user_scope, T.nilable(String) | ||
|
||
sig { returns(T::Boolean) } | ||
def online_token? | ||
!associated_user.nil? | ||
end | ||
|
||
alias_method :eql?, :== | ||
sig { params(other: T.nilable(AccessTokenResponse)).returns(T::Boolean) } | ||
def ==(other) | ||
return false unless other | ||
|
||
access_token == other.access_token && | ||
scope == other.scope && | ||
session == other.session && | ||
expires_in == other.expires_in && | ||
associated_user == other.associated_user && | ||
associated_user_scope == other.associated_user_scope | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require_relative "../../test_helper" | ||
|
||
module ShopifyAPITest | ||
module Auth | ||
module Oauth | ||
class AccessTokenResponseTest < Test::Unit::TestCase | ||
def test_online_token_is_false_when_no_associated_user | ||
token_response = ShopifyAPI::Auth::Oauth::AccessTokenResponse.new( | ||
access_token: "token", | ||
scope: "scope1, scope2", | ||
) | ||
|
||
assert(!token_response.online_token?) | ||
end | ||
|
||
def test_online_token_is_true_when_associated_user_is_present | ||
associated_user = ShopifyAPI::Auth::AssociatedUser.new( | ||
id: 902541635, | ||
first_name: "first", | ||
last_name: "last", | ||
email: "[email protected]", | ||
email_verified: true, | ||
account_owner: true, | ||
locale: "en", | ||
collaborator: false, | ||
) | ||
token_response = ShopifyAPI::Auth::Oauth::AccessTokenResponse.new( | ||
access_token: "token", | ||
scope: "scope1, scope2", | ||
expires_in: 1000, | ||
associated_user_scope: "scope1", | ||
associated_user: associated_user, | ||
) | ||
|
||
assert(token_response.online_token?) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,70 @@ def test_temp_with_block_var | |
assert_equal(session, ShopifyAPI::Context.active_session) | ||
end | ||
|
||
def test_for_with_offline_access_token_response | ||
shop = "test-shop" | ||
response = ShopifyAPI::Auth::Oauth::AccessTokenResponse.new( | ||
access_token: "token", | ||
scope: "scope1, scope2", | ||
session: "session", | ||
) | ||
expected_session = ShopifyAPI::Auth::Session.new( | ||
id: "offline_#{shop}", | ||
shop: shop, | ||
access_token: response.access_token, | ||
scope: response.scope, | ||
is_online: false, | ||
associated_user_scope: nil, | ||
associated_user: nil, | ||
expires: nil, | ||
shopify_session_id: response.session, | ||
) | ||
|
||
session = ShopifyAPI::Auth::Session.for(shop: shop, access_token_response: response) | ||
|
||
assert_equal(expected_session, session) | ||
end | ||
|
||
def test_for_with_online_access_token_response | ||
shop = "test-shop" | ||
associated_user = ShopifyAPI::Auth::AssociatedUser.new( | ||
id: 902541635, | ||
first_name: "first", | ||
last_name: "last", | ||
email: "[email protected]", | ||
email_verified: true, | ||
account_owner: true, | ||
locale: "en", | ||
collaborator: false, | ||
) | ||
response = ShopifyAPI::Auth::Oauth::AccessTokenResponse.new( | ||
access_token: "token", | ||
scope: "scope1, scope2", | ||
session: "session", | ||
expires_in: 1000, | ||
associated_user: associated_user, | ||
associated_user_scope: "scope1", | ||
) | ||
time_now = Time.now | ||
expected_session = ShopifyAPI::Auth::Session.new( | ||
id: "#{shop}_#{associated_user.id}", | ||
shop: shop, | ||
access_token: response.access_token, | ||
scope: response.scope, | ||
is_online: false, | ||
associated_user_scope: response.associated_user_scope, | ||
associated_user: associated_user, | ||
expires: time_now + response.expires_in, | ||
shopify_session_id: response.session, | ||
) | ||
|
||
session = Time.stub(:now, time_now) do | ||
ShopifyAPI::Auth::Session.for(shop: shop, access_token_response: response) | ||
end | ||
|
||
assert_equal(expected_session, session) | ||
end | ||
|
||
def teardown | ||
ShopifyAPI::Context.deactivate_session | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters