logo

pleroma

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

twitter_api_controller_test.exs (3909B)


  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.TwitterAPI.ControllerTest do
  5. use Pleroma.Web.ConnCase
  6. alias Pleroma.Builders.ActivityBuilder
  7. alias Pleroma.Repo
  8. alias Pleroma.User
  9. alias Pleroma.Web.OAuth.Token
  10. import Pleroma.Factory
  11. describe "POST /api/qvitter/statuses/notifications/read" do
  12. test "without valid credentials", %{conn: conn} do
  13. conn = post(conn, "/api/qvitter/statuses/notifications/read", %{"latest_id" => 1_234_567})
  14. assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
  15. end
  16. test "with credentials, without any params" do
  17. %{conn: conn} = oauth_access(["write:notifications"])
  18. conn = post(conn, "/api/qvitter/statuses/notifications/read")
  19. assert json_response(conn, 400) == %{
  20. "error" => "You need to specify latest_id",
  21. "request" => "/api/qvitter/statuses/notifications/read"
  22. }
  23. end
  24. test "with credentials, with params" do
  25. %{user: current_user, conn: conn} =
  26. oauth_access(["read:notifications", "write:notifications"])
  27. other_user = insert(:user)
  28. {:ok, _activity} =
  29. ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: other_user})
  30. response_conn =
  31. conn
  32. |> assign(:user, current_user)
  33. |> get("/api/v1/notifications")
  34. [notification] = response = json_response(response_conn, 200)
  35. assert length(response) == 1
  36. assert notification["pleroma"]["is_seen"] == false
  37. response_conn =
  38. conn
  39. |> assign(:user, current_user)
  40. |> post("/api/qvitter/statuses/notifications/read", %{"latest_id" => notification["id"]})
  41. [notification] = response = json_response(response_conn, 200)
  42. assert length(response) == 1
  43. assert notification["pleroma"]["is_seen"] == true
  44. end
  45. end
  46. describe "GET /api/account/confirm_email/:id/:token" do
  47. setup do
  48. {:ok, user} =
  49. insert(:user)
  50. |> User.confirmation_changeset(need_confirmation: true)
  51. |> Repo.update()
  52. assert user.confirmation_pending
  53. [user: user]
  54. end
  55. test "it redirects to root url", %{conn: conn, user: user} do
  56. conn = get(conn, "/api/account/confirm_email/#{user.id}/#{user.confirmation_token}")
  57. assert 302 == conn.status
  58. end
  59. test "it confirms the user account", %{conn: conn, user: user} do
  60. get(conn, "/api/account/confirm_email/#{user.id}/#{user.confirmation_token}")
  61. user = User.get_cached_by_id(user.id)
  62. refute user.confirmation_pending
  63. refute user.confirmation_token
  64. end
  65. test "it returns 500 if user cannot be found by id", %{conn: conn, user: user} do
  66. conn = get(conn, "/api/account/confirm_email/0/#{user.confirmation_token}")
  67. assert 500 == conn.status
  68. end
  69. test "it returns 500 if token is invalid", %{conn: conn, user: user} do
  70. conn = get(conn, "/api/account/confirm_email/#{user.id}/wrong_token")
  71. assert 500 == conn.status
  72. end
  73. end
  74. describe "GET /api/oauth_tokens" do
  75. setup do
  76. token = insert(:oauth_token) |> Repo.preload(:user)
  77. %{token: token}
  78. end
  79. test "renders list", %{token: token} do
  80. response =
  81. build_conn()
  82. |> assign(:user, token.user)
  83. |> get("/api/oauth_tokens")
  84. keys =
  85. json_response(response, 200)
  86. |> hd()
  87. |> Map.keys()
  88. assert keys -- ["id", "app_name", "valid_until"] == []
  89. end
  90. test "revoke token", %{token: token} do
  91. response =
  92. build_conn()
  93. |> assign(:user, token.user)
  94. |> delete("/api/oauth_tokens/#{token.id}")
  95. tokens = Token.get_user_tokens(token.user)
  96. assert tokens == []
  97. assert response.status == 201
  98. end
  99. end
  100. end