diff --git a/CHANGELOG.md b/CHANGELOG.md index 833392374..328bd9c0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api ## Unreleased +- [#1244](https://github.com/Shopify/shopify-api-ruby/pull/1244) Add `expired?` to `ShopifyAPI::Auth::Session` to check if the session is expired (mainly for user sessions) + ## 13.3.0 - [#1241](https://github.com/Shopify/shopify-api-ruby/pull/1241) Add `api_host` to `ShopifyAPI::Context.setup`, allowing the API host to be overridden in `ShopifyAPI::Clients::HttpClient`. This context option is intended for internal Shopify use only. diff --git a/lib/shopify_api/auth/session.rb b/lib/shopify_api/auth/session.rb index d79cb5b66..b58ba8522 100644 --- a/lib/shopify_api/auth/session.rb +++ b/lib/shopify_api/auth/session.rb @@ -35,6 +35,11 @@ def online? @is_online end + sig { returns(T::Boolean) } + def expired? + @expires ? @expires < Time.now : false + end + sig do params( shop: String, diff --git a/test/auth/session_test.rb b/test/auth/session_test.rb index 46c8617f2..3f479d63e 100644 --- a/test/auth/session_test.rb +++ b/test/auth/session_test.rb @@ -34,6 +34,24 @@ def test_is_online_with_associated_user assert(session.online?) end + def test_expired_with_no_expiry_date + session = ShopifyAPI::Auth::Session.new(shop: "test-shop") + + assert_equal(false, session.expired?) + end + + def test_expired_with_future_expiry_date + session = ShopifyAPI::Auth::Session.new(shop: "test-shop", expires: Time.now + 1 * 60 * 60) + + assert_equal(false, session.expired?) + end + + def test_expired_with_passed_expiry_date + session = ShopifyAPI::Auth::Session.new(shop: "test-shop", expires: Time.now - 1) + + assert(session.expired?) + end + def test_temp session = ShopifyAPI::Auth::Session.new(shop: "test-shop1", access_token: "token1")