Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Add test case for unread counts
Browse files Browse the repository at this point in the history
  • Loading branch information
babolivier committed Jun 26, 2020
1 parent 41cdb80 commit c1b148c
Showing 1 changed file with 162 additions and 2 deletions.
164 changes: 162 additions & 2 deletions tests/rest/client/v2_alpha/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import json

import synapse.rest.admin
from synapse.api.constants import EventContentFields, EventTypes
from synapse.api.constants import EventContentFields, EventTypes, RelationTypes
from synapse.rest.client.v1 import login, room
from synapse.rest.client.v2_alpha import sync
from synapse.rest.client.v2_alpha import read_marker, sync

from tests import unittest
from tests.server import TimedOutException
Expand Down Expand Up @@ -324,3 +324,163 @@ def test_sync_backwards_typing(self):
"GET", sync_url % (access_token, next_batch)
)
self.assertRaises(TimedOutException, self.render, request)


class UnreadMessagesTestCase(unittest.HomeserverTestCase):
servlets = [
synapse.rest.admin.register_servlets,
login.register_servlets,
read_marker.register_servlets,
room.register_servlets,
sync.register_servlets,
]

def prepare(self, reactor, clock, hs):
self.url = "/sync?since=%s"
self.next_batch = "s0"

# Register the first user (used to check the unread counts).
self.user_id = self.register_user("kermit", "monkey")
self.tok = self.login("kermit", "monkey")

# Create the room we'll check unread counts for.
self.room_id = self.helper.create_room_as(self.user_id, tok=self.tok)

# Register the second user (used to send events to the room).
self.user2 = self.register_user("kermit2", "monkey")
self.tok2 = self.login("kermit2", "monkey")

# Change the power levels of the room so that the second user can send state
# events.
self.power_levels = {
"users": {
self.user_id: 100,
self.user2: 100,
},
"users_default": 0,
"events": {
"m.room.name": 50,
"m.room.power_levels": 100,
"m.room.history_visibility": 100,
"m.room.canonical_alias": 50,
"m.room.avatar": 50,
"m.room.tombstone": 100,
"m.room.server_acl": 100,
"m.room.encryption": 100
},
"events_default": 0,
"state_default": 50,
"ban": 50,
"kick": 50,
"redact": 50,
"invite": 0
}
self.helper.send_state(
self.room_id, EventTypes.PowerLevels, self.power_levels, tok=self.tok,
)

def test_unread_counts(self):
"""Tests that /sync returns the right value for the unread count (MSC2654)."""

# Check that our own messages don't increase the unread count.
self.helper.send(self.room_id, "hello", tok=self.tok)
self._check_unread_count(0)

# Join the new user and check that this doesn't increase the unread count.
self.helper.join(room=self.room_id, user=self.user2, tok=self.tok2)
self._check_unread_count(0)

# Check that the new user sending a message increases our unread count.
res = self.helper.send(self.room_id, "hello", tok=self.tok2)
self._check_unread_count(1)

# Check that redacting that message decreases our unread count.
self.helper.redact(self.room_id, res["event_id"], tok=self.tok2)
self._check_unread_count(0)

# Re-send a message to prepare for the next check.
res = self.helper.send(self.room_id, "hello", tok=self.tok2)
self._check_unread_count(1)

# Send a read receipt to tell the server we've read the latest event.
body = json.dumps({"m.read": res["event_id"]}).encode("utf8")
request, channel = self.make_request(
"POST", "/rooms/%s/read_markers" % self.room_id, body, access_token=self.tok,
)
self.render(request)
self.assertEqual(channel.code, 200, channel.json_body)

# Check that the unread counter is back to 0.
self._check_unread_count(0)

# Check that room name changes increase the unread counter.
self.helper.send_state(
self.room_id, "m.room.name", {"name": "my super room"}, tok=self.tok2,
)
self._check_unread_count(1)

# Check that room topic changes increase the unread counter.
self.helper.send_state(
self.room_id, "m.room.topic", {"topic": "welcome!!!"}, tok=self.tok2,
)
self._check_unread_count(2)

# Check that encrypted messages increase the unread counter.
self.helper.send_event(self.room_id, EventTypes.Encrypted, {}, tok=self.tok2)
self._check_unread_count(3)

# Check that custom events with a body increase the unread counter.
self.helper.send_event(
self.room_id, "org.matrix.custom_type", {"body": "hello"}, tok=self.tok2,
)
self._check_unread_count(4)

# Check that power level changes increase the unread counter.
self.power_levels["invite"] = 50
self.helper.send_state(
self.room_id, EventTypes.PowerLevels, self.power_levels, tok=self.tok2,
)
self._check_unread_count(5)

# Check that edits don't increase the unread counter.
self.helper.send_event(
room_id=self.room_id,
type=EventTypes.Message,
content={
"body": "hello",
"msgtype": "m.text",
"m.relates_to": {"rel_type": RelationTypes.REPLACE}
},
tok=self.tok2,
)
self._check_unread_count(5)

# Check that notices don't increase the unread counter.
self.helper.send_event(
room_id=self.room_id,
type=EventTypes.Message,
content={
"body": "hello",
"msgtype": "m.notice",
},
tok=self.tok2,
)
self._check_unread_count(5)

def _check_unread_count(self, expected_count: True):
"""Syncs and compares the unread count with the expected value."""

request, channel = self.make_request(
"GET", self.url % self.next_batch, access_token=self.tok,
)
self.render(request)

self.assertEqual(channel.code, 200, channel.json_body)

room_entry = channel.json_body["rooms"]["join"][self.room_id]
self.assertEqual(
room_entry["org.matrix.msc2654.unread_count"], expected_count, room_entry,
)

# Store the next batch for the next request.
self.next_batch = channel.json_body["next_batch"]

0 comments on commit c1b148c

Please sign in to comment.