Skip to content

Commit

Permalink
Delete session with repository
Browse files Browse the repository at this point in the history
users can customize their session storage so we cant rely on models like shop
  • Loading branch information
lizkenyon committed Jan 9, 2024
1 parent a4b8307 commit ba52c6f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
17 changes: 12 additions & 5 deletions lib/shopify_app/session/session_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ def retrieve_user_session_by_shopify_user_id(user_id)
user_storage.retrieve_by_shopify_user_id(user_id)
end

def destroy_shop_session_by_domain(shopify_domain)
shop_storage.destroy_by_shopify_domain(shopify_domain)
end

def destroy_user_session_by_shopify_user_id(user_id)
user_storage.destroy_by_shopify_user_id(user_id)
end

def store_shop_session(session)
shop_storage.store(session)
end
Expand Down Expand Up @@ -73,18 +81,17 @@ def load_session(id)
def delete_session(id)
match = id.match(/^offline_(.*)/)

record = if match
if match
domain = match[1]
ShopifyApp::Logger.debug("Destroying session by domain - domain: #{domain}")
Shop.find_by(shopify_domain: match[1])
destroy_shop_session_by_domain(domain)

else
shopify_user_id = id.split("_").last
ShopifyApp::Logger.debug("Destroying session by user - user_id: #{shopify_user_id}")
User.find_by(shopify_user_id: shopify_user_id)
destroy_user_session_by_shopify_user_id(shopify_user_id)
end

record.destroy

true
end

Expand Down
4 changes: 4 additions & 0 deletions lib/shopify_app/session/shop_session_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def retrieve_by_shopify_domain(domain)
construct_session(shop)
end

def destroy_by_shopify_domain(domain)
destroy_by(shopify_domain: domain)
end

private

def construct_session(shop)
Expand Down
4 changes: 4 additions & 0 deletions lib/shopify_app/session/user_session_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def retrieve_by_shopify_user_id(user_id)
construct_session(user)
end

def destroy_by_shopify_user_id(user_id)
destroy_by(shopify_user_id: user_id)
end

private

def construct_session(user)
Expand Down
16 changes: 8 additions & 8 deletions test/shopify_app/session/session_repository_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,21 @@ class SessionRepositoryTest < ActiveSupport::TestCase
end

test(".delete_session destroys a shop record") do
shop = MockShopInstance.new(shopify_domain: "shop", shopify_token: "token")
SessionRepository.shop_storage = InMemoryShopSessionStore
mock_session_id = "offline_abra-shop"

Shop.expects(:find_by).with(shopify_domain: "shop").returns(shop)
shop.expects(:destroy)
InMemoryShopSessionStore.expects(:destroy_by_shopify_domain).with("abra-shop")

SessionRepository.delete_session("offline_shop")
SessionRepository.delete_session(mock_session_id)
end

test(".delete_session destroys a user record") do
user = MockUserInstance.new(shopify_domain: "shop", shopify_token: "token")
SessionRepository.user_storage = InMemoryUserSessionStore
mock_session_id = "test_shop.myshopify.com_1234"

User.expects(:find_by).with(shopify_user_id: "1234").returns(user)
user.expects(:destroy)
InMemoryUserSessionStore.expects(:destroy_by_shopify_user_id).with("1234")

SessionRepository.delete_session("shop_1234")
SessionRepository.delete_session(mock_session_id)
end

private
Expand Down
6 changes: 6 additions & 0 deletions test/shopify_app/session/shop_session_storage_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class ShopSessionStorageTest < ActiveSupport::TestCase
assert_equal expected_session.access_token, session.access_token
end

test ".destroy_by_shopify_domain destroys shop session records by JWT" do
ShopMockSessionStore.expects(:destroy_by).with(shopify_domain: TEST_SHOPIFY_DOMAIN)

ShopMockSessionStore.destroy_by_shopify_domain(TEST_SHOPIFY_DOMAIN)
end

test ".store can store shop session records" do
mock_shop_instance = MockShopInstance.new(id: 12345)
mock_shop_instance.stubs(:save!).returns(true)
Expand Down
6 changes: 6 additions & 0 deletions test/shopify_app/session/user_session_storage_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ class UserSessionStorageTest < ActiveSupport::TestCase
assert_equal expected_session.access_token, session.access_token
end

test ".destroy_by_shopify_user_id destroys user session by shopify_user_id" do
UserMockSessionStore.expects(:destroy_by).with(shopify_user_id: TEST_SHOPIFY_USER_ID)

UserMockSessionStore.destroy_by_shopify_user_id(TEST_SHOPIFY_USER_ID)
end

test ".store can store user session record" do
mock_user_instance = MockUserInstance.new(shopify_user_id: 100)
mock_user_instance.stubs(:save!).returns(true)
Expand Down

0 comments on commit ba52c6f

Please sign in to comment.