Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expired? method to check if session is expired #1244

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions lib/shopify_api/auth/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions test/auth/session_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down