Merge branch 'fix/put-repeats-at-activitypub-outbox' into 'develop'
[akkoma] / test / web / oauth / oauth_controller_test.exs
1 defmodule Pleroma.Web.OAuth.OAuthControllerTest do
2 use Pleroma.Web.ConnCase
3 import Pleroma.Factory
4
5 alias Pleroma.Repo
6 alias Pleroma.Web.OAuth.{Authorization, Token}
7
8 test "redirects with oauth authorization" do
9 user = insert(:user)
10 app = insert(:oauth_app)
11
12 conn =
13 build_conn()
14 |> post("/oauth/authorize", %{
15 "authorization" => %{
16 "name" => user.nickname,
17 "password" => "test",
18 "client_id" => app.client_id,
19 "redirect_uri" => app.redirect_uris,
20 "state" => "statepassed"
21 }
22 })
23
24 target = redirected_to(conn)
25 assert target =~ app.redirect_uris
26
27 query = URI.parse(target).query |> URI.query_decoder() |> Map.new()
28
29 assert %{"state" => "statepassed", "code" => code} = query
30 assert Repo.get_by(Authorization, token: code)
31 end
32
33 test "issues a token for an all-body request" do
34 user = insert(:user)
35 app = insert(:oauth_app)
36
37 {:ok, auth} = Authorization.create_authorization(app, user)
38
39 conn =
40 build_conn()
41 |> post("/oauth/token", %{
42 "grant_type" => "authorization_code",
43 "code" => auth.token,
44 "redirect_uri" => app.redirect_uris,
45 "client_id" => app.client_id,
46 "client_secret" => app.client_secret
47 })
48
49 assert %{"access_token" => token} = json_response(conn, 200)
50 assert Repo.get_by(Token, token: token)
51 end
52
53 test "issues a token for request with HTTP basic auth client credentials" do
54 user = insert(:user)
55 app = insert(:oauth_app)
56
57 {:ok, auth} = Authorization.create_authorization(app, user)
58
59 app_encoded =
60 (URI.encode_www_form(app.client_id) <> ":" <> URI.encode_www_form(app.client_secret))
61 |> Base.encode64()
62
63 conn =
64 build_conn()
65 |> put_req_header("authorization", "Basic " <> app_encoded)
66 |> post("/oauth/token", %{
67 "grant_type" => "authorization_code",
68 "code" => auth.token,
69 "redirect_uri" => app.redirect_uris
70 })
71
72 assert %{"access_token" => token} = json_response(conn, 200)
73 assert Repo.get_by(Token, token: token)
74 end
75
76 test "rejects token exchange with invalid client credentials" do
77 user = insert(:user)
78 app = insert(:oauth_app)
79
80 {:ok, auth} = Authorization.create_authorization(app, user)
81
82 conn =
83 build_conn()
84 |> put_req_header("authorization", "Basic JTIxOiVGMCU5RiVBNCVCNwo=")
85 |> post("/oauth/token", %{
86 "grant_type" => "authorization_code",
87 "code" => auth.token,
88 "redirect_uri" => app.redirect_uris
89 })
90
91 assert resp = json_response(conn, 400)
92 assert %{"error" => _} = resp
93 refute Map.has_key?(resp, "access_token")
94 end
95
96 test "rejects an invalid authorization code" do
97 app = insert(:oauth_app)
98
99 conn =
100 build_conn()
101 |> post("/oauth/token", %{
102 "grant_type" => "authorization_code",
103 "code" => "Imobviouslyinvalid",
104 "redirect_uri" => app.redirect_uris,
105 "client_id" => app.client_id,
106 "client_secret" => app.client_secret
107 })
108
109 assert resp = json_response(conn, 400)
110 assert %{"error" => _} = json_response(conn, 400)
111 refute Map.has_key?(resp, "access_token")
112 end
113 end