-
-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Improve Auth modules tests (#1267)
- Loading branch information
1 parent
92cb793
commit c09b328
Showing
5 changed files
with
266 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 24 additions & 24 deletions
48
test/realtime_web/channels/auth/channels_authorization_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,40 @@ | ||
defmodule RealtimeWeb.ChannelsAuthorizationTest do | ||
use ExUnit.Case | ||
|
||
import Mock | ||
import Generators | ||
|
||
alias RealtimeWeb.{ChannelsAuthorization, JwtVerification} | ||
|
||
@secret "" | ||
|
||
test "authorize/3 when token is authorized" do | ||
input_token = "\n token %20 1 %20 2 %20 3 " | ||
expected_token = "token123" | ||
|
||
with_mock JwtVerification, | ||
verify: fn token, @secret, _jwks -> | ||
assert token == expected_token | ||
{:ok, %{}} | ||
end do | ||
assert {:ok, %{}} = ChannelsAuthorization.authorize(input_token, @secret, nil) | ||
describe "authorize_conn/3" do | ||
test "when token is authorized" do | ||
input_token = "\n token %20 1 %20 2 %20 3 " | ||
expected_token = "token123" | ||
|
||
with_mock JwtVerification, | ||
verify: fn token, @secret, _jwks -> | ||
assert token == expected_token | ||
{:ok, %{}} | ||
end do | ||
assert {:ok, %{}} = ChannelsAuthorization.authorize(input_token, @secret, nil) | ||
end | ||
end | ||
end | ||
|
||
test "authorize/3 when token is unauthorized" do | ||
with_mock JwtVerification, verify: fn _token, _secret, _jwks -> :error end do | ||
assert :error = ChannelsAuthorization.authorize("bad_token", @secret, nil) | ||
test "when token is unauthorized" do | ||
with_mock JwtVerification, verify: fn _token, _secret, _jwks -> :error end do | ||
assert :error = ChannelsAuthorization.authorize("bad_token", @secret, nil) | ||
end | ||
end | ||
end | ||
|
||
test "authorize/3 when token is not a string" do | ||
assert :error = ChannelsAuthorization.authorize([], @secret, nil) | ||
end | ||
test "when token is not a string" do | ||
assert {:error, :invalid_token} = ChannelsAuthorization.authorize([], @secret, nil) | ||
end | ||
|
||
test "authorize_conn/3 fails when has missing headers" do | ||
jwt = generate_jwt_token(@secret, %{}) | ||
test "authorize_conn/3 fails when has missing headers" do | ||
jwt = generate_jwt_token(@secret, %{}) | ||
|
||
assert {:error, :missing_claims} = | ||
ChannelsAuthorization.authorize_conn(jwt, @secret, nil) | ||
assert {:error, :missing_claims} = | ||
ChannelsAuthorization.authorize_conn(jwt, @secret, nil) | ||
end | ||
end | ||
end |
Oops, something went wrong.