You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.6 KiB

  1. # Copyright (c) 2018 Tildes contributors <code@tildes.net>
  2. # SPDX-License-Identifier: AGPL-3.0-or-later
  3. from pyramid.security import principals_allowed_by_permission
  4. from pytest import fixture, raises
  5. from tildes.models.message import MessageConversation, MessageReply
  6. from tildes.models.user import User
  7. from tildes.schemas.fields import Markdown, SimpleString
  8. from tildes.schemas.message import MessageConversationSchema, MessageReplySchema
  9. @fixture
  10. def conversation(db, session_user, session_user2):
  11. """Create a message conversation and delete it as teardown."""
  12. new_conversation = MessageConversation(
  13. session_user, session_user2, "Subject", "Message"
  14. )
  15. db.add(new_conversation)
  16. db.commit()
  17. yield new_conversation
  18. # delete any replies that were added to the conversation
  19. for reply in new_conversation.replies:
  20. db.delete(reply)
  21. db.delete(new_conversation)
  22. db.commit()
  23. def test_message_conversation_validation(mocker, session_user, session_user2):
  24. """Ensure a new message conversation goes through expected validation."""
  25. mocker.spy(MessageConversationSchema, "load")
  26. mocker.spy(SimpleString, "_validate")
  27. mocker.spy(Markdown, "_validate")
  28. MessageConversation(session_user, session_user2, "Subject", "Message")
  29. assert MessageConversationSchema.load.called
  30. assert SimpleString._validate.call_args[0][1] == "Subject"
  31. assert Markdown._validate.call_args[0][1] == "Message"
  32. def test_message_reply_validation(mocker, conversation, session_user2):
  33. """Ensure a new message reply goes through expected validation."""
  34. mocker.spy(MessageReplySchema, "load")
  35. mocker.spy(Markdown, "_validate")
  36. MessageReply(conversation, session_user2, "A new reply")
  37. assert MessageReplySchema.load.called
  38. assert Markdown._validate.call_args[0][1] == "A new reply"
  39. def test_conversation_viewing_permission(conversation):
  40. """Ensure only the two involved users can view a message conversation."""
  41. principals = principals_allowed_by_permission(conversation, "view")
  42. users = {conversation.sender.user_id, conversation.recipient.user_id}
  43. assert principals == users
  44. def test_conversation_other_user(conversation):
  45. """Ensure that the "other user" method returns the expected user."""
  46. sender = conversation.sender
  47. recipient = conversation.recipient
  48. assert conversation.other_user(sender) == recipient
  49. assert conversation.other_user(recipient) == sender
  50. def test_conversation_other_user_invalid(conversation):
  51. """Ensure that "other user" method fails if the user isn't involved."""
  52. new_user = User("SomeOutsider", "super amazing password")
  53. with raises(ValueError):
  54. assert conversation.other_user(new_user)
  55. def test_replies_affect_num_replies(conversation, db):
  56. """Ensure adding replies to a conversation affects the reply count."""
  57. assert conversation.num_replies == 0
  58. # add replies and ensure each one increases the count
  59. for num in range(5):
  60. new_reply = MessageReply(conversation, conversation.recipient, "hi")
  61. db.add(new_reply)
  62. db.commit()
  63. db.refresh(conversation)
  64. assert conversation.num_replies == num + 1
  65. def test_replies_update_activity_time(conversation, db):
  66. """Ensure adding replies updates the last activity timestamp."""
  67. assert conversation.last_activity_time == conversation.created_time
  68. for _ in range(5):
  69. new_reply = MessageReply(conversation, conversation.recipient, "hi")
  70. db.add(new_reply)
  71. db.commit()
  72. assert conversation.last_activity_time == new_reply.created_time