logo

pleroma

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

chat_test.exs (2388B)


  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.ChatTest do
  5. use Pleroma.DataCase, async: true
  6. alias Pleroma.Chat
  7. import Pleroma.Factory
  8. describe "creation and getting" do
  9. test "it only works if the recipient is a valid user (for now)" do
  10. user = insert(:user)
  11. assert {:error, _chat} = Chat.bump_or_create(user.id, "http://some/nonexisting/account")
  12. assert {:error, _chat} = Chat.get_or_create(user.id, "http://some/nonexisting/account")
  13. end
  14. test "it creates a chat for a user and recipient" do
  15. user = insert(:user)
  16. other_user = insert(:user)
  17. {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
  18. assert chat.id
  19. end
  20. test "deleting the user deletes the chat" do
  21. user = insert(:user)
  22. other_user = insert(:user)
  23. {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
  24. Repo.delete(user)
  25. refute Chat.get_by_id(chat.id)
  26. end
  27. test "deleting the recipient deletes the chat" do
  28. user = insert(:user)
  29. other_user = insert(:user)
  30. {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
  31. Repo.delete(other_user)
  32. refute Chat.get_by_id(chat.id)
  33. end
  34. test "it returns and bumps a chat for a user and recipient if it already exists" do
  35. user = insert(:user)
  36. other_user = insert(:user)
  37. {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
  38. {:ok, chat_two} = Chat.bump_or_create(user.id, other_user.ap_id)
  39. assert chat.id == chat_two.id
  40. end
  41. test "it returns a chat for a user and recipient if it already exists" do
  42. user = insert(:user)
  43. other_user = insert(:user)
  44. {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
  45. {:ok, chat_two} = Chat.get_or_create(user.id, other_user.ap_id)
  46. assert chat.id == chat_two.id
  47. end
  48. test "a returning chat will have an updated `update_at` field" do
  49. user = insert(:user)
  50. other_user = insert(:user)
  51. {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
  52. :timer.sleep(1500)
  53. {:ok, chat_two} = Chat.bump_or_create(user.id, other_user.ap_id)
  54. assert chat.id == chat_two.id
  55. assert chat.updated_at != chat_two.updated_at
  56. end
  57. end
  58. end