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

Tests for Supavisor.Jwt.authorize/2 #36

Merged
merged 4 commits into from Mar 23, 2023
Merged

Tests for Supavisor.Jwt.authorize/2 #36

merged 4 commits into from Mar 23, 2023

Conversation

abc3
Copy link
Member

@abc3 abc3 commented Mar 22, 2023

This PR adds tests for Supavisor.Jwt.authorize/2, as recommended by @wojtekmach here

@abc3 abc3 requested a review from chasers March 22, 2023 15:27
@abc3 abc3 linked an issue Mar 22, 2023 that may be closed by this pull request
@abc3 abc3 requested a review from a team March 22, 2023 15:43
Copy link
Contributor

@wojtekmach wojtekmach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

test/supavisor/jwt_test.exs Outdated Show resolved Hide resolved
end

test "returns {:error, :token_not_a_string} for non-binary token" do
assert {:error, :token_not_a_string} = Jwt.authorize(123, @secret)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double-checking: instead of gracefully handling non-string tokens would it make sense to crash? In other words, wouldn't it be a programmer's mistake if we called this function with invalid type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I don't have a strong opinion on this matter. What would you recommend? Should I remove the handling of other types?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'd remove it. Its type spec is @spec authorize(String.t(), String.t()) :: {:ok, map()} | {:error, any()}, it accepts only strings, it's fine to raise.

For completeness, here's a random example from stdlib:

iex> Version.parse("1.0.0")
{:ok, %Version{major: 1, minor: 0, patch: 0}}

iex> Version.parse("1")
:error

iex> Version.parse(1)
** (FunctionClauseError) no function clause matching in Version.parse/1

    The following arguments were given to Version.parse/1:

        # 1
        1

    Attempted function clauses (showing 1 out of 1):

        def parse(string) when is_binary(string)

    (elixir 1.15.0-dev) lib/version.ex:367: Version.parse/1
    iex:9: (file)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, thanks for the explanation!

test/supavisor/jwt_test.exs Outdated Show resolved Hide resolved
@abc3 abc3 merged commit 2a31a06 into main Mar 23, 2023
@abc3 abc3 deleted the jwt_tests branch March 23, 2023 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add tests for PgEdge.Jwt
2 participants