From 580a77bc7e7062fbe52cb3141a836db4efcd94f1 Mon Sep 17 00:00:00 2001 From: Hakan Ensari Date: Fri, 20 Dec 2024 19:49:22 +0100 Subject: [PATCH] Refactor token to improve test coverage --- lib/peddler/token.rb | 24 +++++++++---------- test/peddler/token_test.rb | 20 +++++++++++++++- ...est_bad_Token.yml => test_token_error.yml} | 0 3 files changed, 31 insertions(+), 13 deletions(-) rename test/vcr_cassettes/Peddler/TokenTest/{test_bad_Token.yml => test_token_error.yml} (100%) diff --git a/lib/peddler/token.rb b/lib/peddler/token.rb index 71a62c90..ac0ecaae 100644 --- a/lib/peddler/token.rb +++ b/lib/peddler/token.rb @@ -45,6 +45,18 @@ def request response end + def grant_type + if options.key?(:grant_type) + options[:grant_type] + elsif options.key?(:refresh_token) + "refresh_token" + elsif options.key?(:scope) + "client_credentials" + elsif options.key?(:code) + "authorization_code" + end + end + private def params @@ -54,17 +66,5 @@ def params client_secret: client_secret, }.compact.merge(options) end - - def grant_type - return if options.key?(:grant_type) - - if options.key?(:refresh_token) - "refresh_token" - elsif options.key?(:scope) - "client_credentials" - elsif options.key?(:code) - "authorization_code" - end - end end end diff --git a/test/peddler/token_test.rb b/test/peddler/token_test.rb index dd646438..4286a045 100644 --- a/test/peddler/token_test.rb +++ b/test/peddler/token_test.rb @@ -21,12 +21,30 @@ def test_grantless_operations assert(access_token) end - def test_bad_Token + def test_token_error assert_raises(Token::Error) do Token.request(client_id:, client_secret:) end end + def test_grant_type_with_code + token = Token.new(code: "dummy_code") + + assert_equal("authorization_code", token.grant_type) + end + + def test_grant_type_with_scope + token = Token.new(scope: "dummy_scope") + + assert_equal("client_credentials", token.grant_type) + end + + def test_grant_type_with_refresh_token + token = Token.new(refresh_token: "dummy_refresh_token") + + assert_equal("refresh_token", token.grant_type) + end + private def scope diff --git a/test/vcr_cassettes/Peddler/TokenTest/test_bad_Token.yml b/test/vcr_cassettes/Peddler/TokenTest/test_token_error.yml similarity index 100% rename from test/vcr_cassettes/Peddler/TokenTest/test_bad_Token.yml rename to test/vcr_cassettes/Peddler/TokenTest/test_token_error.yml