-
Notifications
You must be signed in to change notification settings - Fork 706
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ownership for session persistence from library to this gem (#1563)
* session stored in rails instead of library * move responsiblity for session persistence * move load_current_session to login_protection * FIXME notes * Move session concerns back to session utils * Add `embedded` param to `splash_page` (#1549) * use online tokens appropriately if there is a user session storage available * move load_current_session to login_protection * Move session concerns back to session utils * refactor login protection and tests with sesison ownership change * rubocop'd * no more session_storage in generated example * add x86_65-linux platform for CI * no more local pointer to api lib * more gemlock Co-authored-by: rdillensnyder <[email protected]> Co-authored-by: Teddy Hwang <[email protected]>
- Loading branch information
1 parent
d746c7f
commit a4d5c84
Showing
9 changed files
with
183 additions
and
164 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
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 |
---|---|---|
|
@@ -28,23 +28,22 @@ class CallbackControllerTest < ActionController::TestCase | |
ShopifyApp::SessionRepository.user_storage = nil | ||
ShopifyAppConfigurer.setup_context | ||
I18n.locale = :en | ||
|
||
@stubbed_session = ShopifyAPI::Auth::Session.new(shop: "shop", access_token: "token") | ||
@stubbed_cookie = ShopifyAPI::Auth::Oauth::SessionCookie.new(value: "", expires: Time.now) | ||
host = Base64.strict_encode64("#{ShopifyAPI::Context.host_name}/admin") | ||
@callback_params = { shop: "shop", code: "code", state: "state", timestamp: "timestamp", host: host, | ||
hmac: "hmac", } | ||
@auth_query = ShopifyAPI::Auth::Oauth::AuthQuery.new(**@callback_params) | ||
request.env["HTTP_USER_AGENT"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6)"\ | ||
"AppleWebKit/537.36 (KHTML, like Gecko)"\ | ||
"Chrome/69.0.3497.100 Safari/537.36" | ||
end | ||
|
||
test "#callback flashes error when omniauth is not present" do | ||
get :callback, | ||
params: { shop: "shop", code: "code", state: "state", timestamp: "timestamp", host: "host", hmac: "hmac" } | ||
assert_equal flash[:error], "Could not log in to Shopify store" | ||
ShopifyApp::SessionRepository.stubs(:store_session) | ||
end | ||
|
||
test "#callback flashes error in Spanish" do | ||
I18n.locale = :es | ||
I18n.expects(:t).with("could_not_log_in") | ||
get :callback, | ||
params: { shop: "shop", code: "code", state: "state", timestamp: "timestamp", host: "host", hmac: "hmac" } | ||
assert_match "sesión", flash[:error] | ||
end | ||
|
||
test "#callback rescued errors of ShopifyAPI::Error will not emit a deprecation notice" do | ||
|
@@ -89,6 +88,53 @@ class CallbackControllerTest < ActionController::TestCase | |
get :callback, params: @callback_params | ||
end | ||
|
||
test "#callback saves the session when validated by API library" do | ||
mock_oauth | ||
ShopifyApp::SessionRepository.expects(:store_session).with(@stubbed_session) | ||
|
||
get :callback, params: @callback_params | ||
end | ||
|
||
test "#callback sets the shopify_user_id in the Rails session when session is online" do | ||
associated_user = ShopifyAPI::Auth::AssociatedUser.new( | ||
id: 42, | ||
first_name: "LeeeEEeeeeee3roy", | ||
last_name: "Jenkins", | ||
email: "[email protected]", | ||
email_verified: true, | ||
locale: "en", | ||
collaborator: true, | ||
account_owner: true, | ||
) | ||
mock_session = ShopifyAPI::Auth::Session.new(shop: "shop", access_token: "token", is_online: true, | ||
associated_user: associated_user) | ||
mock_oauth(session: mock_session) | ||
get :callback, params: @callback_params | ||
assert_equal session[:shopify_user_id], associated_user.id | ||
end | ||
|
||
test "#callback DOES NOT set the shopify_user_id in the Rails session when session is offline" do | ||
mock_session = ShopifyAPI::Auth::Session.new(shop: "shop", access_token: "token", is_online: false) | ||
mock_oauth(session: mock_session) | ||
get :callback, params: @callback_params | ||
assert_nil session[:shopify_user_id] | ||
end | ||
|
||
test "#callback sets encrypted cookie if API library returns cookie object" do | ||
cookie = ShopifyAPI::Auth::Oauth::SessionCookie.new(value: "snickerdoodle", expires: Time.now + 1.day) | ||
mock_oauth(cookie: cookie) | ||
|
||
get :callback, params: @callback_params | ||
assert_equal cookies.encrypted[cookie.name], cookie.value | ||
end | ||
|
||
test "#callback does not set encrypted cookie if API library returns empty cookie" do | ||
mock_oauth | ||
|
||
get :callback, params: @callback_params | ||
refute_equal cookies.encrypted[@stubbed_cookie.name], @stubbed_cookie.value | ||
end | ||
|
||
test "#callback starts the WebhooksManager if webhooks are configured" do | ||
ShopifyApp.configure do |config| | ||
config.webhooks = [{ topic: "carts/update", address: "example-app.com/webhooks" }] | ||
|
@@ -238,11 +284,7 @@ class CallbackControllerTest < ActionController::TestCase | |
|
||
private | ||
|
||
def mock_oauth | ||
host = Base64.strict_encode64("#{ShopifyAPI::Context.host_name}/admin") | ||
@callback_params = { shop: "shop", code: "code", state: "state", timestamp: "timestamp", host: host, | ||
hmac: "hmac", } | ||
@auth_query = ShopifyAPI::Auth::Oauth::AuthQuery.new(**@callback_params) | ||
def mock_oauth(cookie: @stubbed_cookie, session: @stubbed_session) | ||
ShopifyAPI::Auth::Oauth::AuthQuery.stubs(:new).with(**@callback_params).returns(@auth_query) | ||
|
||
cookies.encrypted[ShopifyAPI::Auth::Oauth::SessionCookie::SESSION_COOKIE_NAME] = "nonce" | ||
|
@@ -253,8 +295,8 @@ def mock_oauth | |
cookies.encrypted[ShopifyAPI::Auth::Oauth::SessionCookie::SESSION_COOKIE_NAME], | ||
}, auth_query: @auth_query) | ||
.returns({ | ||
cookie: ShopifyAPI::Auth::Oauth::SessionCookie.new(value: "", expires: Time.now), | ||
session: ShopifyAPI::Auth::Session.new(shop: "shop", access_token: "token"), | ||
cookie: cookie, | ||
session: session, | ||
}) | ||
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
Oops, something went wrong.