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

Commit

Permalink
Refactor email tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Jun 7, 2019
1 parent 928d1cc commit 8182a1c
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions tests/push/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import os

import attr
import pkg_resources

from twisted.internet.defer import Deferred
Expand All @@ -30,6 +31,13 @@
load_jinja2_templates = None


@attr.s
class _User(object):
"Helper wrapper for user ID and access token"
id = attr.ib()
token = attr.ib()


class EmailPusherTests(HomeserverTestCase):

skip = "No Jinja installed" if not load_jinja2_templates else None
Expand Down Expand Up @@ -77,25 +85,32 @@ def sendmail(*args, **kwargs):

return hs

def test_sends_email(self):

def prepare(self, reactor, clock, hs):
# Register the user who gets notified
user_id = self.register_user("user", "pass")
access_token = self.login("user", "pass")

# Register the user who sends the message
other_user_id = self.register_user("otheruser", "pass")
other_access_token = self.login("otheruser", "pass")
self.user_id = self.register_user("user", "pass")
self.access_token = self.login("user", "pass")

# Register other users
self.others = [
_User(
id=self.register_user("otheruser1", "pass"),
token=self.login("otheruser1", "pass"),
),
_User(
id=self.register_user("otheruser2", "pass"),
token=self.login("otheruser2", "pass"),
),
]

# Register the pusher
user_tuple = self.get_success(
self.hs.get_datastore().get_user_by_access_token(access_token)
self.hs.get_datastore().get_user_by_access_token(self.access_token)
)
token_id = user_tuple["token_id"]

self.get_success(
self.hs.get_pusherpool().add_pusher(
user_id=user_id,
user_id=self.user_id,
access_token=token_id,
kind="email",
app_id="m.email",
Expand All @@ -107,22 +122,27 @@ def test_sends_email(self):
)
)

# Create a room
room = self.helper.create_room_as(user_id, tok=access_token)
def test_simple_sends_email(self):
# Create a simple room with two users
room = self.helper.create_room_as(self.user_id, tok=self.access_token)
self.helper.invite(
room=room, src=self.user_id, tok=self.access_token, targ=self.others[0].id,
)
self.helper.join(room=room, user=self.others[0].id, tok=self.others[0].token)

# Invite the other person
self.helper.invite(room=room, src=user_id, tok=access_token, targ=other_user_id)
# The other user sends some messages
self.helper.send(room, body="Hi!", tok=self.others[0].token)
self.helper.send(room, body="There!", tok=self.others[0].token)

# The other user joins
self.helper.join(room=room, user=other_user_id, tok=other_access_token)
# We should get emailed about that message
self._check_for_mail()

# The other user sends some messages
self.helper.send(room, body="Hi!", tok=other_access_token)
self.helper.send(room, body="There!", tok=other_access_token)
def _check_for_mail(self):
"Check that the user receives an email notification"

# Get the stream ordering before it gets sent
pushers = self.get_success(
self.hs.get_datastore().get_pushers_by(dict(user_name=user_id))
self.hs.get_datastore().get_pushers_by(dict(user_name=self.user_id))
)
self.assertEqual(len(pushers), 1)
last_stream_ordering = pushers[0]["last_stream_ordering"]
Expand All @@ -132,7 +152,7 @@ def test_sends_email(self):

# It hasn't succeeded yet, so the stream ordering shouldn't have moved
pushers = self.get_success(
self.hs.get_datastore().get_pushers_by(dict(user_name=user_id))
self.hs.get_datastore().get_pushers_by(dict(user_name=self.user_id))
)
self.assertEqual(len(pushers), 1)
self.assertEqual(last_stream_ordering, pushers[0]["last_stream_ordering"])
Expand All @@ -149,7 +169,7 @@ def test_sends_email(self):

# The stream ordering has increased
pushers = self.get_success(
self.hs.get_datastore().get_pushers_by(dict(user_name=user_id))
self.hs.get_datastore().get_pushers_by(dict(user_name=self.user_id))
)
self.assertEqual(len(pushers), 1)
self.assertTrue(pushers[0]["last_stream_ordering"] > last_stream_ordering)

0 comments on commit 8182a1c

Please sign in to comment.