logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

oauth_app_controller_test.exs (6039B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.AdminAPI.OAuthAppControllerTest do
  5. use Pleroma.Web.ConnCase, async: true
  6. use Oban.Testing, repo: Pleroma.Repo
  7. import Pleroma.Factory
  8. alias Pleroma.Config
  9. alias Pleroma.Web
  10. setup do
  11. admin = insert(:user, is_admin: true)
  12. token = insert(:oauth_admin_token, user: admin)
  13. conn =
  14. build_conn()
  15. |> assign(:user, admin)
  16. |> assign(:token, token)
  17. {:ok, %{admin: admin, token: token, conn: conn}}
  18. end
  19. describe "POST /api/pleroma/admin/oauth_app" do
  20. test "errors", %{conn: conn} do
  21. response =
  22. conn
  23. |> put_req_header("content-type", "application/json")
  24. |> post("/api/pleroma/admin/oauth_app", %{})
  25. |> json_response_and_validate_schema(400)
  26. assert %{
  27. "error" => "Missing field: name. Missing field: redirect_uris."
  28. } = response
  29. end
  30. test "success", %{conn: conn} do
  31. base_url = Web.base_url()
  32. app_name = "Trusted app"
  33. response =
  34. conn
  35. |> put_req_header("content-type", "application/json")
  36. |> post("/api/pleroma/admin/oauth_app", %{
  37. name: app_name,
  38. redirect_uris: base_url
  39. })
  40. |> json_response_and_validate_schema(200)
  41. assert %{
  42. "client_id" => _,
  43. "client_secret" => _,
  44. "name" => ^app_name,
  45. "redirect_uri" => ^base_url,
  46. "trusted" => false
  47. } = response
  48. end
  49. test "with trusted", %{conn: conn} do
  50. base_url = Web.base_url()
  51. app_name = "Trusted app"
  52. response =
  53. conn
  54. |> put_req_header("content-type", "application/json")
  55. |> post("/api/pleroma/admin/oauth_app", %{
  56. name: app_name,
  57. redirect_uris: base_url,
  58. trusted: true
  59. })
  60. |> json_response_and_validate_schema(200)
  61. assert %{
  62. "client_id" => _,
  63. "client_secret" => _,
  64. "name" => ^app_name,
  65. "redirect_uri" => ^base_url,
  66. "trusted" => true
  67. } = response
  68. end
  69. end
  70. describe "GET /api/pleroma/admin/oauth_app" do
  71. setup do
  72. app = insert(:oauth_app)
  73. {:ok, app: app}
  74. end
  75. test "list", %{conn: conn} do
  76. response =
  77. conn
  78. |> get("/api/pleroma/admin/oauth_app")
  79. |> json_response_and_validate_schema(200)
  80. assert %{"apps" => apps, "count" => count, "page_size" => _} = response
  81. assert length(apps) == count
  82. end
  83. test "with page size", %{conn: conn} do
  84. insert(:oauth_app)
  85. page_size = 1
  86. response =
  87. conn
  88. |> get("/api/pleroma/admin/oauth_app?page_size=#{page_size}")
  89. |> json_response_and_validate_schema(200)
  90. assert %{"apps" => apps, "count" => _, "page_size" => ^page_size} = response
  91. assert length(apps) == page_size
  92. end
  93. test "search by client name", %{conn: conn, app: app} do
  94. response =
  95. conn
  96. |> get("/api/pleroma/admin/oauth_app?name=#{app.client_name}")
  97. |> json_response_and_validate_schema(200)
  98. assert %{"apps" => [returned], "count" => _, "page_size" => _} = response
  99. assert returned["client_id"] == app.client_id
  100. assert returned["name"] == app.client_name
  101. end
  102. test "search by client id", %{conn: conn, app: app} do
  103. response =
  104. conn
  105. |> get("/api/pleroma/admin/oauth_app?client_id=#{app.client_id}")
  106. |> json_response_and_validate_schema(200)
  107. assert %{"apps" => [returned], "count" => _, "page_size" => _} = response
  108. assert returned["client_id"] == app.client_id
  109. assert returned["name"] == app.client_name
  110. end
  111. test "only trusted", %{conn: conn} do
  112. app = insert(:oauth_app, trusted: true)
  113. response =
  114. conn
  115. |> get("/api/pleroma/admin/oauth_app?trusted=true")
  116. |> json_response_and_validate_schema(200)
  117. assert %{"apps" => [returned], "count" => _, "page_size" => _} = response
  118. assert returned["client_id"] == app.client_id
  119. assert returned["name"] == app.client_name
  120. end
  121. end
  122. describe "DELETE /api/pleroma/admin/oauth_app/:id" do
  123. test "with id", %{conn: conn} do
  124. app = insert(:oauth_app)
  125. response =
  126. conn
  127. |> delete("/api/pleroma/admin/oauth_app/" <> to_string(app.id))
  128. |> json_response_and_validate_schema(:no_content)
  129. assert response == ""
  130. end
  131. test "with non existance id", %{conn: conn} do
  132. response =
  133. conn
  134. |> delete("/api/pleroma/admin/oauth_app/0")
  135. |> json_response_and_validate_schema(:bad_request)
  136. assert response == ""
  137. end
  138. end
  139. describe "PATCH /api/pleroma/admin/oauth_app/:id" do
  140. test "with id", %{conn: conn} do
  141. app = insert(:oauth_app)
  142. name = "another name"
  143. url = "https://example.com"
  144. scopes = ["admin"]
  145. id = app.id
  146. website = "http://website.com"
  147. response =
  148. conn
  149. |> put_req_header("content-type", "application/json")
  150. |> patch("/api/pleroma/admin/oauth_app/#{id}", %{
  151. name: name,
  152. trusted: true,
  153. redirect_uris: url,
  154. scopes: scopes,
  155. website: website
  156. })
  157. |> json_response_and_validate_schema(200)
  158. assert %{
  159. "client_id" => _,
  160. "client_secret" => _,
  161. "id" => ^id,
  162. "name" => ^name,
  163. "redirect_uri" => ^url,
  164. "trusted" => true,
  165. "website" => ^website
  166. } = response
  167. end
  168. test "without id", %{conn: conn} do
  169. response =
  170. conn
  171. |> put_req_header("content-type", "application/json")
  172. |> patch("/api/pleroma/admin/oauth_app/0")
  173. |> json_response_and_validate_schema(:bad_request)
  174. assert response == ""
  175. end
  176. end
  177. end