Added "GET /oauth/authorize" tests.

This commit is contained in:
Ivan Tashkinov 2019-04-02 13:43:33 +03:00
parent 6910fb371b
commit 15ce710460
2 changed files with 323 additions and 245 deletions

View file

@ -216,7 +216,7 @@ def oauth_app_factory do
redirect_uris: "https://example.com/callback", redirect_uris: "https://example.com/callback",
scopes: ["read", "write", "follow", "push"], scopes: ["read", "write", "follow", "push"],
website: "https://example.com", website: "https://example.com",
client_id: "aaabbb==", client_id: Ecto.UUID.generate(),
client_secret: "aaa;/&bbb" client_secret: "aaa;/&bbb"
} }
end end

View file

@ -10,6 +10,81 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
alias Pleroma.Web.OAuth.Authorization alias Pleroma.Web.OAuth.Authorization
alias Pleroma.Web.OAuth.Token alias Pleroma.Web.OAuth.Token
describe "GET /oauth/authorize" do
setup do
session_opts = [
store: :cookie,
key: "_test",
signing_salt: "cooldude"
]
[
app: insert(:oauth_app, redirect_uris: "https://redirect.url"),
conn:
build_conn()
|> Plug.Session.call(Plug.Session.init(session_opts))
|> fetch_session()
]
end
test "renders authentication page", %{app: app, conn: conn} do
conn =
get(
conn,
"/oauth/authorize",
%{
"response_type" => "code",
"client_id" => app.client_id,
"redirect_uri" => app.redirect_uris,
"scope" => "read"
}
)
assert html_response(conn, 200) =~ ~s(type="submit")
end
test "renders authentication page if user is already authenticated but `force_login` is tru-ish",
%{app: app, conn: conn} do
token = insert(:oauth_token, app_id: app.id)
conn =
conn
|> put_session(:oauth_token, token.token)
|> get(
"/oauth/authorize",
%{
"response_type" => "code",
"client_id" => app.client_id,
"redirect_uri" => app.redirect_uris,
"scope" => "read",
"force_login" => "true"
}
)
assert html_response(conn, 200) =~ ~s(type="submit")
end
test "redirects to app if user is already authenticated", %{app: app, conn: conn} do
token = insert(:oauth_token, app_id: app.id)
conn =
conn
|> put_session(:oauth_token, token.token)
|> get(
"/oauth/authorize",
%{
"response_type" => "code",
"client_id" => app.client_id,
"redirect_uri" => app.redirect_uris,
"scope" => "read"
}
)
assert redirected_to(conn) == "https://redirect.url"
end
end
describe "POST /oauth/authorize" do
test "redirects with oauth authorization" do test "redirects with oauth authorization" do
user = insert(:user) user = insert(:user)
app = insert(:oauth_app, scopes: ["read", "write", "follow"]) app = insert(:oauth_app, scopes: ["read", "write", "follow"])
@ -115,7 +190,9 @@ test "returns 401 for scopes beyond app scopes", %{conn: conn} do
# Error message # Error message
assert result =~ "This action is outside the authorized scopes" assert result =~ "This action is outside the authorized scopes"
end end
end
describe "POST /oauth/token" do
test "issues a token for an all-body request" do test "issues a token for an all-body request" do
user = insert(:user) user = insert(:user)
app = insert(:oauth_app, scopes: ["read", "write"]) app = insert(:oauth_app, scopes: ["read", "write"])
@ -268,3 +345,4 @@ test "rejects an invalid authorization code" do
refute Map.has_key?(resp, "access_token") refute Map.has_key?(resp, "access_token")
end end
end end
end