|
| 1 | +# typed: false |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require_relative "../test_helper" |
| 5 | + |
| 6 | +module ShopifyAPITest |
| 7 | + module Utils |
| 8 | + class SessionUtils < Test::Unit::TestCase |
| 9 | + def setup |
| 10 | + super |
| 11 | + @user_id = "my_user_id" |
| 12 | + @shop = "test-shop.myshopify.io" |
| 13 | + |
| 14 | + @jwt_payload = { |
| 15 | + iss: "https://#{@shop}/admin", |
| 16 | + dest: "https://#{@shop}", |
| 17 | + aud: ShopifyAPI::Context.api_key, |
| 18 | + sub: @user_id, |
| 19 | + exp: (Time.now + 10).to_i, |
| 20 | + nbf: 1234, |
| 21 | + iat: 1234, |
| 22 | + jti: "4321", |
| 23 | + sid: "abc123", |
| 24 | + } |
| 25 | + |
| 26 | + @jwt_token = JWT.encode(@jwt_payload, ShopifyAPI::Context.api_secret_key, "HS256") |
| 27 | + @auth_header = "Bearer #{@jwt_token}" |
| 28 | + @expected_online_session_id = "#{@shop}_#{@user_id}" |
| 29 | + @expected_offline_session_id = "offline_#{@shop}" |
| 30 | + end |
| 31 | + |
| 32 | + def test_gets_online_session_id_from_shopify_id_token |
| 33 | + assert_equal( |
| 34 | + @expected_online_session_id, |
| 35 | + ShopifyAPI::Utils::SessionUtils.session_id_from_shopify_id_token(id_token: @jwt_token, online: true), |
| 36 | + ) |
| 37 | + end |
| 38 | + |
| 39 | + def test_gets_offline_session_id_from_shopify_id_token |
| 40 | + assert_equal( |
| 41 | + @expected_offline_session_id, |
| 42 | + ShopifyAPI::Utils::SessionUtils.session_id_from_shopify_id_token(id_token: @jwt_token, online: false), |
| 43 | + ) |
| 44 | + end |
| 45 | + |
| 46 | + def test_session_id_from_shopify_id_token_raises_invalid_jwt_errors |
| 47 | + assert_raises(ShopifyAPI::Errors::InvalidJwtTokenError) do |
| 48 | + ShopifyAPI::Utils::SessionUtils.session_id_from_shopify_id_token(id_token: "invalid_token", online: true) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + def test_session_id_from_shopify_id_token_raises_missing_jwt_token_error |
| 53 | + [ |
| 54 | + nil, |
| 55 | + "", |
| 56 | + ].each do |missing_jwt| |
| 57 | + error = assert_raises(ShopifyAPI::Errors::MissingJwtTokenError) do |
| 58 | + ShopifyAPI::Utils::SessionUtils.session_id_from_shopify_id_token(id_token: missing_jwt, online: true) |
| 59 | + end |
| 60 | + |
| 61 | + assert_equal("Missing Shopify ID Token", error.message) |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + def test_non_embedded_app_current_session_id_raises_cookie_not_found_error |
| 66 | + ShopifyAPI::Context.stubs(:embedded?).returns(false) |
| 67 | + |
| 68 | + [ |
| 69 | + nil, |
| 70 | + {}, |
| 71 | + { "not-session-cookie-name": "not-this-cookie" }, |
| 72 | + ].each do |cookies| |
| 73 | + error = assert_raises(ShopifyAPI::Errors::CookieNotFoundError) do |
| 74 | + ShopifyAPI::Utils::SessionUtils.current_session_id(nil, cookies, true) |
| 75 | + end |
| 76 | + assert_equal("Session cookie not found for app", error.message) |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + def test_non_embedded_app_current_session_id_returns_id_from_cookie |
| 81 | + ShopifyAPI::Context.stubs(:embedded?).returns(false) |
| 82 | + expected_session_id = "cookie_value" |
| 83 | + cookies = { ShopifyAPI::Auth::Oauth::SessionCookie::SESSION_COOKIE_NAME => expected_session_id } |
| 84 | + |
| 85 | + assert_equal( |
| 86 | + expected_session_id, |
| 87 | + ShopifyAPI::Utils::SessionUtils.current_session_id(nil, cookies, true), |
| 88 | + ) |
| 89 | + end |
| 90 | + |
| 91 | + def test_embedded_app_current_session_id_raises_cookie_not_found_error |
| 92 | + ShopifyAPI::Context.stubs(:embedded?).returns(true) |
| 93 | + |
| 94 | + [ |
| 95 | + nil, |
| 96 | + {}, |
| 97 | + { "not-session-cookie-name": "not-this-cookie" }, |
| 98 | + ].each do |cookies| |
| 99 | + error = assert_raises(ShopifyAPI::Errors::CookieNotFoundError) do |
| 100 | + ShopifyAPI::Utils::SessionUtils.current_session_id(nil, cookies, true) |
| 101 | + end |
| 102 | + assert_equal("JWT token or Session cookie not found for app", error.message) |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + def test_embedded_app_current_session_id_raises_invalid_jwt_token_error |
| 107 | + ShopifyAPI::Context.stubs(:embedded?).returns(true) |
| 108 | + [ |
| 109 | + "Bearer invalid_token", |
| 110 | + "Bearer", |
| 111 | + "invalid_token", |
| 112 | + ].each do |invalid_token| |
| 113 | + assert_raises(ShopifyAPI::Errors::InvalidJwtTokenError, " - #{invalid_token}") do |
| 114 | + ShopifyAPI::Utils::SessionUtils.current_session_id(invalid_token, nil, true) |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + def test_embedded_app_current_session_id_raises_missing_jwt_token_error |
| 120 | + ShopifyAPI::Context.stubs(:embedded?).returns(true) |
| 121 | + |
| 122 | + error = assert_raises(ShopifyAPI::Errors::MissingJwtTokenError) do |
| 123 | + ShopifyAPI::Utils::SessionUtils.current_session_id("", nil, true) |
| 124 | + end |
| 125 | + |
| 126 | + assert_equal("Missing Shopify ID Token", error.message) |
| 127 | + end |
| 128 | + |
| 129 | + def test_embedded_app_current_session_id_returns_online_id_from_auth_header |
| 130 | + ShopifyAPI::Context.stubs(:embedded?).returns(true) |
| 131 | + |
| 132 | + assert_equal( |
| 133 | + @expected_online_session_id, |
| 134 | + ShopifyAPI::Utils::SessionUtils.current_session_id(@auth_header, nil, true), |
| 135 | + ) |
| 136 | + end |
| 137 | + |
| 138 | + def test_embedded_app_current_session_id_returns_offline_id_from_auth_header |
| 139 | + ShopifyAPI::Context.stubs(:embedded?).returns(true) |
| 140 | + |
| 141 | + assert_equal( |
| 142 | + @expected_offline_session_id, |
| 143 | + ShopifyAPI::Utils::SessionUtils.current_session_id(@auth_header, nil, false), |
| 144 | + ) |
| 145 | + end |
| 146 | + |
| 147 | + def test_embedded_app_current_session_id_returns_online_id_from_shopify_id_token |
| 148 | + ShopifyAPI::Context.stubs(:embedded?).returns(true) |
| 149 | + |
| 150 | + assert_equal( |
| 151 | + @expected_online_session_id, |
| 152 | + ShopifyAPI::Utils::SessionUtils.current_session_id(@jwt_token, nil, true), |
| 153 | + ) |
| 154 | + end |
| 155 | + |
| 156 | + def test_embedded_app_current_session_id_returns_offline_id_from_shopify_id_token |
| 157 | + ShopifyAPI::Context.stubs(:embedded?).returns(true) |
| 158 | + |
| 159 | + assert_equal( |
| 160 | + @expected_offline_session_id, |
| 161 | + ShopifyAPI::Utils::SessionUtils.current_session_id(@jwt_token, nil, false), |
| 162 | + ) |
| 163 | + end |
| 164 | + |
| 165 | + def test_embedded_app_current_session_id_returns_id_from_auth_header_even_with_cookies |
| 166 | + ShopifyAPI::Context.stubs(:embedded?).returns(true) |
| 167 | + cookies = { ShopifyAPI::Auth::Oauth::SessionCookie::SESSION_COOKIE_NAME => "cookie_value" } |
| 168 | + |
| 169 | + assert_equal( |
| 170 | + @expected_online_session_id, |
| 171 | + ShopifyAPI::Utils::SessionUtils.current_session_id(@auth_header, cookies, true), |
| 172 | + ) |
| 173 | + end |
| 174 | + end |
| 175 | + end |
| 176 | +end |
0 commit comments