Example #1
0
class UserReceiverTest(ChannelTestCase):
    def setUp(self):
        self.client = WSClient()
        self.member = UserFactory()
        self.other_member = UserFactory()
        self.unrelated_user = UserFactory()
        self.group = GroupFactory(members=[self.member, self.other_member])
        pathlib.Path(settings.MEDIA_ROOT).mkdir(exist_ok=True)
        copyfile(os.path.join(os.path.dirname(__file__), './photo.jpg'),
                 os.path.join(settings.MEDIA_ROOT, 'photo.jpg'))
        self.member.photo = 'photo.jpg'
        self.member.save()
        self.other_member.photo = 'photo.jpg'
        self.other_member.save()

    def test_receive_own_user_changes(self):
        self.client.force_login(self.member)
        self.client.send_and_consume('websocket.connect', path='/')

        name = faker.name()
        self.member.display_name = name
        self.member.save()

        response = self.client.receive(json=True)
        self.assertEqual(response['topic'], 'auth:user')
        self.assertEqual(response['payload']['display_name'], name)
        self.assertTrue('current_group' in response['payload'])
        self.assertTrue(
            response['payload']['photo_urls']['full_size'].startswith(
                settings.HOSTNAME))

        self.assertIsNone(self.client.receive(json=True))

    def test_receive_changes_of_other_user(self):
        self.client.force_login(self.member)
        self.client.send_and_consume('websocket.connect', path='/')

        name = faker.name()
        self.other_member.display_name = name
        self.other_member.save()

        response = self.client.receive(json=True)
        self.assertEqual(response['topic'], 'users:user')
        self.assertEqual(response['payload']['display_name'], name)
        self.assertTrue('current_group' not in response['payload'])
        self.assertTrue(
            response['payload']['photo_urls']['full_size'].startswith(
                settings.HOSTNAME))

        self.assertIsNone(self.client.receive(json=True))

    def test_unrelated_user_receives_no_changes(self):
        self.client.force_login(self.unrelated_user)
        self.client.send_and_consume('websocket.connect', path='/')

        self.member.display_name = faker.name()
        self.member.save()

        self.assertIsNone(self.client.receive(json=True))
class UserReceiverTest(WSTestCase):
    def setUp(self):
        super().setUp()
        self.member = UserFactory()
        self.other_member = UserFactory()
        self.unrelated_user = UserFactory()
        self.group = GroupFactory(members=[self.member, self.other_member])
        pathlib.Path(settings.MEDIA_ROOT).mkdir(exist_ok=True)
        copyfile(
            os.path.join(os.path.dirname(__file__), './photo.jpg'), os.path.join(settings.MEDIA_ROOT, 'photo.jpg')
        )
        self.member.photo = 'photo.jpg'
        self.member.save()
        self.other_member.photo = 'photo.jpg'
        self.other_member.save()

    def test_receive_own_user_changes(self):
        self.client = self.connect_as(self.member)

        name = faker.name()
        self.member.display_name = name
        self.member.save()

        response = self.client.messages[0]
        self.assertEqual(response['topic'], 'auth:user')
        self.assertEqual(response['payload']['display_name'], name)
        self.assertTrue('current_group' in response['payload'])
        self.assertTrue(response['payload']['photo_urls']['full_size'].startswith(settings.HOSTNAME))

        self.assertEqual(len(self.client.messages), 1)

    def test_receive_changes_of_other_user(self):
        self.client = self.connect_as(self.member)

        name = faker.name()
        self.other_member.display_name = name
        self.other_member.save()

        response = self.client.messages[0]
        self.assertEqual(response['topic'], 'users:user')
        self.assertEqual(response['payload']['display_name'], name)
        self.assertTrue('current_group' not in response['payload'])
        self.assertTrue(response['payload']['photo_urls']['full_size'].startswith(settings.HOSTNAME))

        self.assertEqual(len(self.client.messages), 1)

    def test_unrelated_user_receives_no_changes(self):
        self.client = self.connect_as(self.unrelated_user)

        self.member.display_name = faker.name()
        self.member.save()

        self.assertEqual(len(self.client.messages), 0)
class TestEMailVerification(APITestCase):
    def setUp(self):
        self.user = UserFactory()
        self.verified_user = VerifiedUserFactory()
        self.url = '/api/auth/verify_mail/'

    def test_verify_mail_succeeds(self):
        self.client.force_login(user=self.user)
        response = self.client.post(self.url,
                                    {'key': self.user.activation_key})
        self.assertEqual(response.data, {})
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

    def test_verify_mail_fails_if_not_logged_in(self):
        response = self.client.post(self.url,
                                    {'key': self.user.activation_key})
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_verify_mail_fails_with_wrong_key(self):
        self.client.force_login(user=self.user)
        response = self.client.post(self.url, {'key': 'w' * 40})
        self.assertEqual(response.data, {'key': ['Key is invalid']})
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    def test_verify_mail_fails_if_key_too_old(self):
        self.client.force_login(user=self.user)
        backup = self.user.key_expires_at
        self.user.key_expires_at = timezone.now() - timedelta(days=1)
        self.user.save()
        response = self.client.post(self.url,
                                    {'key': self.user.activation_key})
        self.assertEqual(response.data, {'key': ['Key has expired']})
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.user.key_expires_at = backup
        self.user.save()

    def test_verify_mail_fails_if_already_verified(self):
        self.client.force_login(user=self.verified_user)
        response = self.client.post(self.url,
                                    {'key': self.user.activation_key})
        self.assertEqual(response.data['detail'], 'Mail is already verified.')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_verify_mail_fails_without_key(self):
        self.client.force_login(user=self.user)
        response = self.client.post(self.url)
        self.assertEqual(response.data, {'key': ['This field is required.']})
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)