diff options
Diffstat (limited to 'test')
4 files changed, 116 insertions, 84 deletions
diff --git a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs index 3036e25b3..d14c7ef2e 100644 --- a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs @@ -1049,7 +1049,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do clear_config([:instance, :account_activation_required], false) clear_config([:instance, :account_approval_required], false) - conn = + apps_response = conn |> put_req_header("content-type", "application/json") |> post("/api/v1/apps", %{ @@ -1057,6 +1057,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do redirect_uris: "urn:ietf:wg:oauth:2.0:oob", scopes: "read, write, follow" }) + |> json_response_and_validate_schema(200) assert %{ "client_id" => client_id, @@ -1066,17 +1067,24 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob", "vapid_key" => _, "website" => nil - } = json_response_and_validate_schema(conn, 200) + } = apps_response - conn = - post(conn, "/oauth/token", %{ - grant_type: "client_credentials", - client_id: client_id, - client_secret: client_secret - }) + token_response = + conn + |> put_req_header("content-type", "application/x-www-form-urlencoded") + |> post( + "/oauth/token?#{ + URI.encode_query(%{ + "grant_type" => "client_credentials", + "client_id" => client_id, + "client_secret" => client_secret + }) + }" + ) + |> json_response_and_validate_schema(200) assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} = - json_response(conn, 200) + token_response assert token token_from_db = Repo.get_by(Token, token: token) @@ -1131,7 +1139,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do clear_config([:instance, :account_activation_required], true) clear_config([:instance, :account_approval_required], false) - conn = + register_response = conn |> put_req_header("content-type", "application/json") |> post("/api/v1/apps", %{ @@ -1139,6 +1147,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do redirect_uris: "urn:ietf:wg:oauth:2.0:oob", scopes: "read, write, follow" }) + |> json_response_and_validate_schema(200) assert %{ "client_id" => client_id, @@ -1148,17 +1157,24 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob", "vapid_key" => _, "website" => nil - } = json_response_and_validate_schema(conn, 200) + } = register_response - conn = - post(conn, "/oauth/token", %{ - grant_type: "client_credentials", - client_id: client_id, - client_secret: client_secret - }) + token_response = + conn + |> put_req_header("content-type", "application/x-www-form-urlencoded") + |> post( + "/oauth/token?#{ + URI.encode_query(%{ + "grant_type" => "client_credentials", + "client_id" => client_id, + "client_secret" => client_secret + }) + }" + ) + |> json_response_and_validate_schema(200) assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} = - json_response(conn, 200) + token_response assert token token_from_db = Repo.get_by(Token, token: token) @@ -1166,19 +1182,19 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do assert refresh assert scope == "read write follow" - conn = - build_conn() - |> put_req_header("content-type", "multipart/form-data") - |> put_req_header("authorization", "Bearer " <> token) - |> post("/api/v1/accounts", %{ - username: "lain", - email: "lain@example.org", - password: "PlzDontHackLain", - bio: "Test Bio", - agreement: true - }) + assert response = + conn + |> put_req_header("content-type", "multipart/form-data") + |> put_req_header("authorization", "Bearer " <> token) + |> post("/api/v1/accounts", %{ + username: "lain", + email: "lain@example.org", + password: "PlzDontHackLain", + bio: "Test Bio", + agreement: true + }) + |> json_response_and_validate_schema(200) - response = json_response_and_validate_schema(conn, 200) assert %{"identifier" => "missing_confirmed_email"} = response refute response["access_token"] refute response["token_type"] @@ -1191,7 +1207,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do clear_config([:instance, :account_approval_required], true) clear_config([:instance, :account_activation_required], false) - conn = + apps_response = conn |> put_req_header("content-type", "application/json") |> post("/api/v1/apps", %{ @@ -1199,6 +1215,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do redirect_uris: "urn:ietf:wg:oauth:2.0:oob", scopes: "read, write, follow" }) + |> json_response_and_validate_schema(200) assert %{ "client_id" => client_id, @@ -1208,17 +1225,24 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob", "vapid_key" => _, "website" => nil - } = json_response_and_validate_schema(conn, 200) + } = apps_response - conn = - post(conn, "/oauth/token", %{ - grant_type: "client_credentials", - client_id: client_id, - client_secret: client_secret - }) + token_response = + conn + |> put_req_header("content-type", "application/x-www-form-urlencoded") + |> post( + "/oauth/token?#{ + URI.encode_query(%{ + "grant_type" => "client_credentials", + "client_id" => client_id, + "client_secret" => client_secret + }) + }" + ) + |> json_response_and_validate_schema(200) assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} = - json_response(conn, 200) + token_response assert token token_from_db = Repo.get_by(Token, token: token) @@ -1226,7 +1250,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do assert refresh assert scope == "read write follow" - conn = + response = build_conn() |> put_req_header("content-type", "multipart/form-data") |> put_req_header("authorization", "Bearer " <> token) @@ -1238,8 +1262,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do agreement: true, reason: "I'm a cool dude, bro" }) + |> json_response_and_validate_schema(200) - response = json_response_and_validate_schema(conn, 200) assert %{"identifier" => "awaiting_approval"} = response refute response["access_token"] refute response["token_type"] @@ -1372,13 +1396,19 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do conn = build_conn() - |> post("/oauth/token", %{ - "grant_type" => "client_credentials", - "client_id" => app.client_id, - "client_secret" => app.client_secret - }) + |> put_req_header("content-type", "application/x-www-form-urlencoded") + |> post( + "/oauth/token?#{ + URI.encode_query(%{ + "grant_type" => "client_credentials", + "client_id" => app.client_id, + "client_secret" => app.client_secret + }) + }" + ) - assert %{"access_token" => token, "token_type" => "Bearer"} = json_response(conn, 200) + assert %{"access_token" => token, "token_type" => "Bearer"} = + json_response_and_validate_schema(conn, 200) response = build_conn() diff --git a/test/pleroma/web/o_auth/ldap_authorization_test.exs b/test/pleroma/web/o_auth/ldap_authorization_test.exs index 61b9ce6b7..a839ed4a7 100644 --- a/test/pleroma/web/o_auth/ldap_authorization_test.exs +++ b/test/pleroma/web/o_auth/ldap_authorization_test.exs @@ -37,15 +37,15 @@ defmodule Pleroma.Web.OAuth.LDAPAuthorizationTest do ] do conn = build_conn() - |> post("/oauth/token", %{ + |> post("/oauth/token?#{URI.encode_query(%{ "grant_type" => "password", "username" => user.nickname, "password" => password, "client_id" => app.client_id, "client_secret" => app.client_secret - }) + })}") - assert %{"access_token" => token} = json_response(conn, 200) + assert %{"access_token" => token} = json_response_and_validate_schema(conn, 200) token = Repo.get_by(Token, token: token) @@ -81,15 +81,15 @@ defmodule Pleroma.Web.OAuth.LDAPAuthorizationTest do ] do conn = build_conn() - |> post("/oauth/token", %{ + |> post("/oauth/token?#{URI.encode_query(%{ "grant_type" => "password", "username" => user.nickname, "password" => password, "client_id" => app.client_id, "client_secret" => app.client_secret - }) + })}") - assert %{"access_token" => token} = json_response(conn, 200) + assert %{"access_token" => token} = json_response_and_validate_schema(conn, 200) token = Repo.get_by(Token, token: token) |> Repo.preload(:user) @@ -120,15 +120,15 @@ defmodule Pleroma.Web.OAuth.LDAPAuthorizationTest do ] do conn = build_conn() - |> post("/oauth/token", %{ + |> post("/oauth/token?#{URI.encode_query(%{ "grant_type" => "password", "username" => user.nickname, "password" => password, "client_id" => app.client_id, "client_secret" => app.client_secret - }) + })}") - assert %{"error" => "Invalid credentials"} = json_response(conn, 400) + assert %{"error" => "Invalid credentials"} = json_response_and_validate_schema(conn, 400) assert_received :close_connection end end diff --git a/test/pleroma/web/o_auth/mfa_controller_test.exs b/test/pleroma/web/o_auth/mfa_controller_test.exs index 17bbde85b..041ab3f41 100644 --- a/test/pleroma/web/o_auth/mfa_controller_test.exs +++ b/test/pleroma/web/o_auth/mfa_controller_test.exs @@ -164,7 +164,7 @@ defmodule Pleroma.Web.OAuth.MFAControllerTest do "client_id" => app.client_id, "client_secret" => app.client_secret }) - |> json_response(:ok) + |> json_response_and_validate_schema(:ok) ap_id = user.ap_id @@ -192,7 +192,7 @@ defmodule Pleroma.Web.OAuth.MFAControllerTest do "client_id" => app.client_id, "client_secret" => app.client_secret }) - |> json_response(400) + |> json_response_and_validate_schema(400) assert response == %{"error" => "Invalid code"} end @@ -209,7 +209,7 @@ defmodule Pleroma.Web.OAuth.MFAControllerTest do "client_id" => app.client_id, "client_secret" => app.client_secret }) - |> json_response(400) + |> json_response_and_validate_schema(400) assert response == %{"error" => "Invalid code"} end @@ -227,7 +227,7 @@ defmodule Pleroma.Web.OAuth.MFAControllerTest do "client_id" => "xxx", "client_secret" => "xxx" }) - |> json_response(400) + |> json_response_and_validate_schema(400) assert response == %{"error" => "Invalid code"} end @@ -272,7 +272,7 @@ defmodule Pleroma.Web.OAuth.MFAControllerTest do "client_id" => app.client_id, "client_secret" => app.client_secret }) - |> json_response(:ok) + |> json_response_and_validate_schema(:ok) ap_id = user.ap_id @@ -296,7 +296,7 @@ defmodule Pleroma.Web.OAuth.MFAControllerTest do "client_id" => app.client_id, "client_secret" => app.client_secret }) - |> json_response(400) + |> json_response_and_validate_schema(400) assert error_response == %{"error" => "Invalid code"} end diff --git a/test/pleroma/web/o_auth/o_auth_controller_test.exs b/test/pleroma/web/o_auth/o_auth_controller_test.exs index 312500feb..c0f243a75 100644 --- a/test/pleroma/web/o_auth/o_auth_controller_test.exs +++ b/test/pleroma/web/o_auth/o_auth_controller_test.exs @@ -780,7 +780,8 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do "client_secret" => app.client_secret }) - assert %{"access_token" => token, "me" => ap_id} = json_response(conn, 200) + assert %{"access_token" => token, "me" => ap_id} = + json_response_and_validate_schema(conn, 200) token = Repo.get_by(Token, token: token) assert token @@ -805,7 +806,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do "client_secret" => app.client_secret }) - assert %{"access_token" => token} = json_response(conn, 200) + assert %{"access_token" => token} = json_response_and_validate_schema(conn, 200) token = Repo.get_by(Token, token: token) assert token @@ -836,7 +837,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do "client_id" => app.client_id, "client_secret" => app.client_secret }) - |> json_response(403) + |> json_response_and_validate_schema(403) assert match?( %{ @@ -872,7 +873,8 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do "redirect_uri" => OAuthController.default_redirect_uri(app) }) - assert %{"access_token" => token, "scope" => scope} = json_response(conn, 200) + assert %{"access_token" => token, "scope" => scope} = + json_response_and_validate_schema(conn, 200) assert scope == "scope1 scope2" @@ -893,7 +895,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do }) assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} = - json_response(conn, 200) + json_response_and_validate_schema(conn, 200) assert token token_from_db = Repo.get_by(Token, token: token) @@ -917,7 +919,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do "redirect_uri" => OAuthController.default_redirect_uri(app) }) - assert resp = json_response(conn, 400) + assert resp = json_response_and_validate_schema(conn, 400) assert %{"error" => _} = resp refute Map.has_key?(resp, "access_token") end @@ -945,7 +947,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do "client_secret" => app.client_secret }) - assert resp = json_response(conn, 403) + assert resp = json_response_and_validate_schema(conn, 403) assert %{"error" => _} = resp refute Map.has_key?(resp, "access_token") end @@ -970,7 +972,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do "client_id" => app.client_id, "client_secret" => app.client_secret }) - |> json_response(403) + |> json_response_and_validate_schema(403) assert resp == %{ "error" => "Your account is currently disabled", @@ -998,7 +1000,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do "client_id" => app.client_id, "client_secret" => app.client_secret }) - |> json_response(403) + |> json_response_and_validate_schema(403) assert resp == %{ "error" => "Password reset is required", @@ -1027,7 +1029,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do "client_id" => app.client_id, "client_secret" => app.client_secret }) - |> json_response(403) + |> json_response_and_validate_schema(403) assert resp == %{ "error" => "Your login is missing a confirmed e-mail address", @@ -1058,7 +1060,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do "client_secret" => app.client_secret }) - assert resp = json_response(conn, 403) + assert resp = json_response_and_validate_schema(conn, 403) assert %{"error" => _} = resp refute Map.has_key?(resp, "access_token") end @@ -1076,8 +1078,8 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do "client_secret" => app.client_secret }) - assert resp = json_response(conn, 400) - assert %{"error" => _} = json_response(conn, 400) + assert resp = json_response_and_validate_schema(conn, 400) + assert %{"error" => _} = json_response_and_validate_schema(conn, 400) refute Map.has_key?(resp, "access_token") end end @@ -1101,7 +1103,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do "client_id" => app.client_id, "client_secret" => app.client_secret }) - |> json_response(200) + |> json_response_and_validate_schema(200) ap_id = user.ap_id @@ -1140,7 +1142,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do "client_id" => app.client_id, "client_secret" => app.client_secret }) - |> json_response(200) + |> json_response_and_validate_schema(200) ap_id = user.ap_id @@ -1178,7 +1180,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do "client_id" => app.client_id, "client_secret" => app.client_secret }) - |> json_response(400) + |> json_response_and_validate_schema(400) assert %{"error" => "Invalid credentials"} == response end @@ -1194,7 +1196,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do "client_id" => app.client_id, "client_secret" => app.client_secret }) - |> json_response(400) + |> json_response_and_validate_schema(400) assert %{"error" => "Invalid credentials"} == response end @@ -1222,7 +1224,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do "client_id" => app.client_id, "client_secret" => app.client_secret }) - |> json_response(200) + |> json_response_and_validate_schema(200) ap_id = user.ap_id @@ -1251,7 +1253,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do response = build_conn() |> post("/oauth/token", %{}) - |> json_response(500) + |> json_response_and_validate_schema(500) assert %{"error" => "Bad request"} == response end @@ -1268,7 +1270,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do |> AuthHelper.put_session_token(oauth_token.token) |> post("/oauth/revoke", %{"token" => oauth_token.token}) - assert json_response(conn, 200) + assert json_response_and_validate_schema(conn, 200) refute AuthHelper.get_session_token(conn) assert Token.get_by_token(oauth_token.token) == {:error, :not_found} @@ -1287,7 +1289,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do |> AuthHelper.put_session_token(oauth_token.token) |> post("/oauth/revoke", %{"token" => other_app_oauth_token.token}) - assert json_response(conn, 200) + assert json_response_and_validate_schema(conn, 200) assert AuthHelper.get_session_token(conn) == oauth_token.token assert Token.get_by_token(other_app_oauth_token.token) == {:error, :not_found} @@ -1297,7 +1299,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do response = build_conn() |> post("/oauth/revoke", %{}) - |> json_response(500) + |> json_response_and_validate_schema(500) assert %{"error" => "Bad request"} == response end |