-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Queries to count code, token and credential
- Loading branch information
1 parent
26a60c0
commit 24d3a3d
Showing
6 changed files
with
292 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
defmodule Teiserver.OAuthFixtures do | ||
alias Teiserver.OAuth.{Application, Code, Token, Credential} | ||
alias Teiserver.Repo | ||
|
||
def app_attrs(owner_id) do | ||
%{ | ||
name: "fixture app", | ||
uid: "fixture_app", | ||
owner_id: owner_id, | ||
scopes: ["tachyon.lobby"], | ||
redirect_uris: ["http://localhost/foo"], | ||
description: "app created as part of a test" | ||
} | ||
end | ||
|
||
def create_app(attrs) do | ||
%Application{} |> Application.changeset(attrs) |> Repo.insert!() | ||
end | ||
|
||
def code_attrs(user_id, app) do | ||
now = DateTime.utc_now() | ||
|
||
%{ | ||
value: Base.hex_encode32(:crypto.strong_rand_bytes(32)), | ||
owner_id: user_id, | ||
application_id: app.id, | ||
scopes: app.scopes, | ||
expires_at: Timex.add(now, Timex.Duration.from_minutes(5)), | ||
redirect_uri: hd(app.redirect_uris), | ||
challenge: "TODO", | ||
challenge_method: :plain | ||
} | ||
end | ||
|
||
def create_code(attrs) do | ||
%Code{} |> Code.changeset(attrs) |> Repo.insert!() | ||
end | ||
|
||
def token_attrs(user_id, application) do | ||
now = DateTime.utc_now() | ||
|
||
%{ | ||
value: Base.hex_encode32(:crypto.strong_rand_bytes(32), padding: false), | ||
owner_id: user_id, | ||
application_id: application.id, | ||
scopes: application.scopes, | ||
expires_at: Timex.add(now, Timex.Duration.from_days(60)), | ||
type: :access, | ||
refresh_token: nil | ||
} | ||
end | ||
|
||
def create_token(attrs) do | ||
%Token{} |> Token.changeset(attrs) |> Repo.insert!() | ||
end | ||
|
||
def credential_attrs(autohost, app_id) do | ||
%{ | ||
application_id: app_id, | ||
autohost_id: autohost.id, | ||
client_id: UUID.uuid4(), | ||
hashed_secret: UUID.uuid4() | ||
} | ||
end | ||
|
||
def create_credential(attrs) do | ||
%Credential{} |> Credential.changeset(attrs) |> Repo.insert!() | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
defmodule Teiserver.OAuth.ApplicationQueryTest do | ||
use Teiserver.DataCase | ||
alias Teiserver.Repo | ||
|
||
alias Teiserver.OAuth.{Application, Code, ApplicationQueries, Token, Credential} | ||
alias Teiserver.OAuthFixtures | ||
|
||
defp setup_app(_context) do | ||
user = Teiserver.TeiserverTestLib.new_user() | ||
|
||
app = OAuthFixtures.app_attrs(user.id) |> OAuthFixtures.create_app() | ||
|
||
%{user: user, app: app} | ||
end | ||
|
||
defp setup_autohost(_context) do | ||
alias Teiserver.Autohost.Autohost | ||
|
||
autohost = | ||
%Autohost{} | ||
|> Autohost.changeset(%{name: "fixture autohost"}) | ||
|> Repo.insert!() | ||
|
||
%{autohost: autohost} | ||
end | ||
|
||
describe "app stats" do | ||
setup [:setup_app, :setup_autohost] | ||
|
||
test "nothing associated", %{app: app} do | ||
assert [ | ||
%{ | ||
code_count: 0, | ||
token_count: 0, | ||
credential_count: 0 | ||
} | ||
] == ApplicationQueries.get_stats(app.id) | ||
end | ||
|
||
test "bit of everything", %{app: app, autohost: autohost} do | ||
OAuthFixtures.code_attrs(app.owner_id, app) | ||
|> OAuthFixtures.create_code() | ||
|
||
Enum.each(1..2, fn _ -> | ||
OAuthFixtures.token_attrs(app.owner_id, app) | ||
|> OAuthFixtures.create_token() | ||
end) | ||
|
||
Enum.each(1..3, fn _ -> | ||
OAuthFixtures.credential_attrs(autohost, app.id) | ||
|> OAuthFixtures.create_credential() | ||
end) | ||
|
||
assert [ | ||
%{ | ||
code_count: 1, | ||
token_count: 2, | ||
credential_count: 3 | ||
} | ||
] == ApplicationQueries.get_stats(app.id) | ||
end | ||
|
||
test "select only correct applications", %{user: user, app: app} do | ||
other_app = | ||
OAuthFixtures.app_attrs(user.id) | ||
|> Map.merge(%{name: "other app", uid: "other_app"}) | ||
|> OAuthFixtures.create_app() | ||
|
||
OAuthFixtures.code_attrs(user.id, other_app) |> OAuthFixtures.create_code() | ||
|
||
assert [%{code_count: 1}] = ApplicationQueries.get_stats(other_app.id) | ||
end | ||
|
||
test "ignore expired code and tokens", %{user: user, app: app} do | ||
yesterday = DateTime.utc_now() |> Timex.subtract(Timex.Duration.from_days(1)) | ||
|
||
OAuthFixtures.code_attrs(user.id, app) | ||
|> Map.put(:expires_at, yesterday) | ||
|> OAuthFixtures.create_code() | ||
|
||
OAuthFixtures.token_attrs(user.id, app) | ||
|> Map.put(:expires_at, yesterday) | ||
|> OAuthFixtures.create_token() | ||
|
||
assert [%{code_count: 0, token_count: 0}] = ApplicationQueries.get_stats(app.id) | ||
end | ||
|
||
test "don't mix up different applications", %{user: user, app: app} do | ||
other_app = | ||
OAuthFixtures.app_attrs(user.id) | ||
|> Map.merge(%{name: "other app", uid: "other_app"}) | ||
|> OAuthFixtures.create_app() | ||
|
||
OAuthFixtures.code_attrs(user.id, app) | ||
|> OAuthFixtures.create_code() | ||
|
||
Enum.each(1..2, fn i -> | ||
OAuthFixtures.code_attrs(user.id, other_app) | ||
|> Map.put(:value, "value_#{i}") | ||
|> OAuthFixtures.create_code() | ||
end) | ||
|
||
assert [%{code_count: 2}, %{code_count: 1}] = | ||
ApplicationQueries.get_stats([other_app.id, app.id]) | ||
|
||
# check that it also works with different id ordering | ||
assert [%{code_count: 1}, %{code_count: 2}] = | ||
ApplicationQueries.get_stats([app.id, other_app.id]) | ||
end | ||
end | ||
end |