|
| 1 | +# typed: false |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require_relative "../test_helper" |
| 5 | + |
| 6 | +module ShopifyAPITest |
| 7 | + module Auth |
| 8 | + class AlwaysOnTokenTest < Test::Unit::TestCase |
| 9 | + def setup |
| 10 | + super() |
| 11 | + |
| 12 | + @shop = "test-shop.myshopify.com" |
| 13 | + @jwt_payload = { |
| 14 | + iss: "https://accounts.google.com", |
| 15 | + aud: @shop, |
| 16 | + } |
| 17 | + @id_token = JWT.encode(@jwt_payload, nil, "none") |
| 18 | + |
| 19 | + ShopifyAPI::Auth::IdToken::GoogleIdToken.stubs(:request).returns(@id_token) |
| 20 | + |
| 21 | + @token_exchange_request = { |
| 22 | + client_id: ShopifyAPI::Context.api_key, |
| 23 | + client_secret: ShopifyAPI::Context.api_secret_key, |
| 24 | + grant_type: "urn:ietf:params:oauth:grant-type:token-exchange", |
| 25 | + subject_token_type: "urn:ietf:params:oauth:token-type:id_token", |
| 26 | + subject_token: @id_token, |
| 27 | + requested_token_type: "urn:shopify:params:oauth:token-type:offline-access-token", |
| 28 | + } |
| 29 | + @token_response = { |
| 30 | + access_token: SecureRandom.alphanumeric(10), |
| 31 | + scope: "scope1,scope2", |
| 32 | + session: SecureRandom.alphanumeric(10), |
| 33 | + } |
| 34 | + end |
| 35 | + |
| 36 | + def test_exchange_token_context_not_setup |
| 37 | + modify_context(api_key: "", api_secret_key: "", host: "") |
| 38 | + |
| 39 | + assert_raises(ShopifyAPI::Errors::ContextNotSetupError) do |
| 40 | + ShopifyAPI::Auth::AlwaysOnToken.request(shop: @shop) |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + def test_exchange_token_unable_to_get_id_token |
| 45 | + ShopifyAPI::Auth::IdToken::GoogleIdToken.stubs(:request).returns(nil) |
| 46 | + |
| 47 | + assert_raises(ShopifyAPI::Errors::InvalidJwtTokenError) do |
| 48 | + ShopifyAPI::Auth::AlwaysOnToken.request(shop: @shop) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + def test_exchange_token_rejected_id_token |
| 53 | + stub_request(:post, "https://#{@shop}/admin/oauth/access_token") |
| 54 | + .with(body: @token_exchange_request) |
| 55 | + .to_return( |
| 56 | + status: 400, |
| 57 | + body: { error: "invalid_subject_token" }.to_json, |
| 58 | + headers: { content_type: "application/json" }, |
| 59 | + ) |
| 60 | + |
| 61 | + assert_raises(ShopifyAPI::Errors::InvalidJwtTokenError) do |
| 62 | + ShopifyAPI::Auth::AlwaysOnToken.request(shop: @shop) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + def test_request_token_succeeds |
| 67 | + stub_request(:post, "https://#{@shop}/admin/oauth/access_token") |
| 68 | + .with(body: @token_exchange_request) |
| 69 | + .to_return(body: @token_response.to_json, headers: { content_type: "application/json" }) |
| 70 | + expected_session = ShopifyAPI::Auth::Session.new( |
| 71 | + id: "offline_#{@shop}", |
| 72 | + shop: @shop, |
| 73 | + access_token: @token_response[:access_token], |
| 74 | + scope: @token_response[:scope], |
| 75 | + is_online: false, |
| 76 | + expires: nil, |
| 77 | + shopify_session_id: @token_response[:session], |
| 78 | + ) |
| 79 | + |
| 80 | + session = ShopifyAPI::Auth::AlwaysOnToken.request(shop: @shop) |
| 81 | + |
| 82 | + assert_equal(expected_session, session) |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments