Skip to content

Commit

Permalink
Refactor token to improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hakanensari committed Dec 20, 2024
1 parent 0bb888f commit 580a77b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
24 changes: 12 additions & 12 deletions lib/peddler/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
20 changes: 19 additions & 1 deletion test/peddler/token_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 580a77b

Please sign in to comment.