Skip to content

Commit

Permalink
Clean up all configurations on test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomarg committed Apr 25, 2022
1 parent 7cd7bae commit 8f08f20
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 24 deletions.
7 changes: 1 addition & 6 deletions test/controllers/sessions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,11 @@ class SessionsControllerTest < ActionController::TestCase
test "#new starts OAuth requesting online token if user session is expected and there is a shop session" do
shop = "my-shop.myshopify.com"

mock_session = mock
mock_session.stubs(:shop).returns(shop)
mock_session.stubs(:access_token).returns("a-new-user_token!")
mock_session.stubs(:scope).returns(ShopifyAPI::Auth::AuthScopes.new("read_products,write_orders"))

ShopifyApp::SessionRepository.user_storage = ShopifyApp::InMemoryUserSessionStore
ShopifyApp::SessionRepository.shop_storage = ShopifyApp::InMemoryShopSessionStore
ShopifyApp::SessionRepository.shop_storage.stubs(:retrieve_by_shopify_domain)
.with(shop)
.returns(mock_session)
.returns(mock_session(shop: shop))

ShopifyAPI::Auth::Oauth.expects(:begin_auth)
.with(shop: shop, redirect_path: "/auth/shopify/callback", is_online: true)
Expand Down
7 changes: 7 additions & 0 deletions test/dummy/config/initializers/shopify_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ class ShopifyAppConfigurer
def self.call
ShopifyApp.configure do |config|
config.api_key = "key"
config.old_secret = nil
config.secret = "secret"
config.scope = "read_orders, read_products"
config.shop_access_scopes = nil
config.user_access_scopes = nil
config.embedded_app = true
config.myshopify_domain = "myshopify.com"
config.api_version = :unstable

config.shop_session_repository = ShopifyApp::InMemorySessionStore
config.after_authenticate_job = false
config.reauth_on_access_scope_changes = true
end
end
end
Expand Down
15 changes: 3 additions & 12 deletions test/shopify_app/controller_concerns/login_protection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class LoginProtectionControllerTest < ActionController::TestCase
shop = "my-shop.myshopify.com"
ShopifyApp::SessionRepository.shop_storage.stubs(:retrieve_by_shopify_domain)
.with(shop)
.returns(mock_session(shop))
.returns(mock_session(shop: shop))

cookies.encrypted[ShopifyAPI::Auth::Oauth::SessionCookie::SESSION_COOKIE_NAME] = "cookie"
request.headers["HTTP_AUTHORIZATION"] = "Bearer token"
Expand Down Expand Up @@ -169,7 +169,7 @@ class LoginProtectionControllerTest < ActionController::TestCase
shop = "my-shop.myshopify.com"
ShopifyApp::SessionRepository.shop_storage.stubs(:retrieve_by_shopify_domain)
.with(shop)
.returns(mock_session(shop))
.returns(mock_session(shop: shop))
ShopifyAPI::Context.stubs(:scope).returns(ShopifyAPI::Auth::AuthScopes.new(["scope1", "scope2"]))

cookies.encrypted[ShopifyAPI::Auth::Oauth::SessionCookie::SESSION_COOKIE_NAME] = "cookie"
Expand Down Expand Up @@ -197,7 +197,7 @@ class LoginProtectionControllerTest < ActionController::TestCase
shop = "my-shop.myshopify.com"
ShopifyApp::SessionRepository.shop_storage.stubs(:retrieve_by_shopify_domain)
.with(shop)
.returns(mock_session(shop))
.returns(mock_session(shop: shop))
ShopifyAPI::Context.stubs(:scope).returns(ShopifyAPI::Auth::AuthScopes.new(["scope1"]))

cookies.encrypted[ShopifyAPI::Auth::Oauth::SessionCookie::SESSION_COOKIE_NAME] = "cookie"
Expand Down Expand Up @@ -454,15 +454,6 @@ def with_custom_login_url(url)
ShopifyApp.configure { |config| config.login_url = original_url }
end

def mock_session(shop = "my-shop.myshopify.com")
mock_session = mock
mock_session.stubs(:shop).returns(shop)
mock_session.stubs(:access_token).returns("a-new-user_token!")
mock_session.stubs(:scope).returns(ShopifyAPI::Auth::AuthScopes.new("read_products,write_orders"))

mock_session
end

def mock_associated_user
ShopifyAPI::Auth::AssociatedUser.new(
id: 100,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ class UserSessionStorageWithScopesTest < ActiveSupport::TestCase

UserMockSessionStoreWithScopes.stubs(:find_or_initialize_by).returns(mock_user_instance)

mock_session = mock
mock_session.stubs(:shop).returns(mock_user_instance.shopify_domain)
mock_session.stubs(:access_token).returns("a-new-user_token!")
mock_session.stubs(:scope).returns(ShopifyAPI::Auth::AuthScopes.new(TEST_MERCHANT_SCOPES))

saved_id = UserMockSessionStoreWithScopes.store(mock_session, mock_associated_user)
saved_id = UserMockSessionStoreWithScopes.store(
mock_session(
shop: mock_user_instance.shopify_domain,
scope: TEST_MERCHANT_SCOPES
),
mock_associated_user
)

assert_equal "a-new-user_token!", mock_user_instance.shopify_token
assert_equal mock_user_instance.id, saved_id
Expand Down
10 changes: 10 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ def before_setup
super
WebMock.disable_net_connect!
WebMock.stub_request(:get, "https://app.shopify.com/services/apis.json").to_return(body: API_META_TEST_RESPONSE)
ShopifyApp::InMemorySessionStore.clear
ShopifyAppConfigurer.call
Rails.application.reload_routes!
end

def mock_session(shop: "my-shop.myshopify.com", scope: ShopifyApp.configuration.scope)
mock_session = mock
mock_session.stubs(:shop).returns(shop)
mock_session.stubs(:access_token).returns("a-new-user_token!")
mock_session.stubs(:scope).returns(ShopifyAPI::Auth::AuthScopes.new(scope))

mock_session
end
end

0 comments on commit 8f08f20

Please sign in to comment.