2016-04-14 23:39:37 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-02-09 22:58:43 +01:00
|
|
|
from __future__ import absolute_import, print_function
|
|
|
|
|
2016-04-14 23:39:37 +02:00
|
|
|
import os
|
2017-02-09 22:58:43 +01:00
|
|
|
import glob
|
|
|
|
from datetime import timedelta
|
|
|
|
from mock import MagicMock, patch
|
|
|
|
from six.moves import map, filter
|
|
|
|
|
2016-04-14 23:39:37 +02:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.management import call_command
|
2017-02-09 22:58:43 +01:00
|
|
|
from django.test import TestCase
|
2017-07-07 20:35:31 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand, CommandError
|
2017-02-09 22:58:43 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zerver.lib.test_helpers import stdout_suppressed
|
2017-01-04 05:30:48 +01:00
|
|
|
from zerver.models import get_realm
|
2016-06-22 21:16:02 +02:00
|
|
|
from confirmation.models import RealmCreationKey, generate_realm_creation_url
|
2017-02-09 22:58:43 +01:00
|
|
|
|
2017-07-07 20:35:31 +02:00
|
|
|
class TestZulipBaseCommand(ZulipTestCase):
|
|
|
|
def test_get_realm(self):
|
|
|
|
# type: () -> None
|
|
|
|
command = ZulipBaseCommand()
|
|
|
|
self.assertEqual(command.get_realm(dict(realm_id='zulip')), get_realm("zulip"))
|
|
|
|
self.assertEqual(command.get_realm(dict(realm_id=None)), None)
|
|
|
|
self.assertEqual(command.get_realm(dict(realm_id='1')), get_realm("zulip"))
|
2017-08-07 21:32:10 +02:00
|
|
|
with self.assertRaisesRegex(CommandError, "There is no realm with id"):
|
2017-07-07 20:35:31 +02:00
|
|
|
command.get_realm(dict(realm_id='17'))
|
2017-08-07 21:32:10 +02:00
|
|
|
with self.assertRaisesRegex(CommandError, "There is no realm with id"):
|
2017-07-07 23:39:55 +02:00
|
|
|
command.get_realm(dict(realm_id='mit'))
|
2017-07-07 20:35:31 +02:00
|
|
|
|
|
|
|
def test_get_user(self):
|
|
|
|
# type: () -> None
|
|
|
|
command = ZulipBaseCommand()
|
|
|
|
zulip_realm = get_realm("zulip")
|
|
|
|
mit_realm = get_realm("zephyr")
|
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
email = user_profile.email
|
|
|
|
|
|
|
|
self.assertEqual(command.get_user(email, zulip_realm), user_profile)
|
|
|
|
self.assertEqual(command.get_user(email, None), user_profile)
|
|
|
|
with self.assertRaisesRegex(CommandError, "The realm '<Realm: zephyr 2>' does not contain a user with email"):
|
|
|
|
self.assertEqual(command.get_user(email, mit_realm), user_profile)
|
|
|
|
with self.assertRaisesRegex(CommandError, "server does not contain a user with email"):
|
|
|
|
self.assertEqual(command.get_user('invalid_email@example.com', None), user_profile)
|
|
|
|
# TODO: Add a test for the MultipleObjectsReturned case once we make that possible.
|
|
|
|
|
2017-02-09 22:58:43 +01:00
|
|
|
class TestCommandsCanStart(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.commands = filter(
|
|
|
|
lambda filename: filename != '__init__',
|
|
|
|
map(
|
|
|
|
lambda file: os.path.basename(file).replace('.py', ''),
|
|
|
|
glob.iglob('*/management/commands/*.py')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_management_commands_show_help(self):
|
|
|
|
# type: () -> None
|
|
|
|
with stdout_suppressed() as stdout:
|
|
|
|
for command in self.commands:
|
|
|
|
print('Testing management command: {}'.format(command),
|
|
|
|
file=stdout)
|
|
|
|
|
|
|
|
with self.assertRaises(SystemExit):
|
|
|
|
call_command(command, '--help')
|
2017-03-24 07:55:20 +01:00
|
|
|
# zerver/management/commands/runtornado.py sets this to True;
|
|
|
|
# we need to reset it here. See #3685 for details.
|
|
|
|
settings.RUNNING_INSIDE_TORNADO = False
|
2016-04-14 23:39:37 +02:00
|
|
|
|
|
|
|
class TestSendWebhookFixtureMessage(TestCase):
|
|
|
|
COMMAND_NAME = 'send_webhook_fixture_message'
|
|
|
|
|
|
|
|
def setUp(self):
|
2016-06-04 20:28:02 +02:00
|
|
|
# type: () -> None
|
2016-04-14 23:39:37 +02:00
|
|
|
self.fixture_path = os.path.join('some', 'fake', 'path.json')
|
|
|
|
self.url = '/some/url/with/hook'
|
|
|
|
|
|
|
|
@patch('zerver.management.commands.send_webhook_fixture_message.Command.print_help')
|
|
|
|
def test_check_if_command_exits_when_fixture_param_is_empty(self, print_help_mock):
|
2016-09-11 23:57:44 +02:00
|
|
|
# type: (MagicMock) -> None
|
2016-04-14 23:39:37 +02:00
|
|
|
with self.assertRaises(SystemExit):
|
|
|
|
call_command(self.COMMAND_NAME, url=self.url)
|
|
|
|
|
2016-11-22 01:44:16 +01:00
|
|
|
print_help_mock.assert_any_call('./manage.py', self.COMMAND_NAME)
|
2016-04-14 23:39:37 +02:00
|
|
|
|
|
|
|
@patch('zerver.management.commands.send_webhook_fixture_message.Command.print_help')
|
|
|
|
def test_check_if_command_exits_when_url_param_is_empty(self, print_help_mock):
|
2016-09-11 23:57:44 +02:00
|
|
|
# type: (MagicMock) -> None
|
2016-04-14 23:39:37 +02:00
|
|
|
with self.assertRaises(SystemExit):
|
|
|
|
call_command(self.COMMAND_NAME, fixture=self.fixture_path)
|
|
|
|
|
2016-11-22 01:44:16 +01:00
|
|
|
print_help_mock.assert_any_call('./manage.py', self.COMMAND_NAME)
|
2016-04-14 23:39:37 +02:00
|
|
|
|
|
|
|
@patch('zerver.management.commands.send_webhook_fixture_message.os.path.exists')
|
|
|
|
def test_check_if_command_exits_when_fixture_path_does_not_exist(self, os_path_exists_mock):
|
2016-09-11 23:57:44 +02:00
|
|
|
# type: (MagicMock) -> None
|
2016-04-14 23:39:37 +02:00
|
|
|
os_path_exists_mock.return_value = False
|
|
|
|
|
|
|
|
with self.assertRaises(SystemExit):
|
|
|
|
call_command(self.COMMAND_NAME, fixture=self.fixture_path, url=self.url)
|
|
|
|
|
|
|
|
os_path_exists_mock.assert_any_call(os.path.join(settings.DEPLOY_ROOT, self.fixture_path))
|
|
|
|
|
|
|
|
@patch('zerver.management.commands.send_webhook_fixture_message.os.path.exists')
|
|
|
|
@patch('zerver.management.commands.send_webhook_fixture_message.Client')
|
|
|
|
@patch('zerver.management.commands.send_webhook_fixture_message.ujson')
|
|
|
|
@patch("zerver.management.commands.send_webhook_fixture_message.open", create=True)
|
|
|
|
def test_check_if_command_post_request_to_url_with_fixture(self,
|
|
|
|
open_mock,
|
|
|
|
ujson_mock,
|
|
|
|
client_mock,
|
|
|
|
os_path_exists_mock):
|
2016-09-11 23:57:44 +02:00
|
|
|
# type: (MagicMock, MagicMock, MagicMock, MagicMock) -> None
|
2016-04-14 23:39:37 +02:00
|
|
|
ujson_mock.loads.return_value = '{}'
|
|
|
|
ujson_mock.dumps.return_value = {}
|
|
|
|
os_path_exists_mock.return_value = True
|
|
|
|
|
|
|
|
client = client_mock()
|
|
|
|
|
|
|
|
call_command(self.COMMAND_NAME, fixture=self.fixture_path, url=self.url)
|
|
|
|
self.assertTrue(ujson_mock.dumps.called)
|
|
|
|
self.assertTrue(ujson_mock.loads.called)
|
|
|
|
self.assertTrue(open_mock.called)
|
|
|
|
client.post.assert_called_once_with(self.url, {}, content_type="application/json")
|
2016-06-22 21:16:02 +02:00
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class TestGenerateRealmCreationLink(ZulipTestCase):
|
2016-06-22 21:16:02 +02:00
|
|
|
COMMAND_NAME = "generate_realm_creation_link"
|
|
|
|
|
|
|
|
def test_generate_link_and_create_realm(self):
|
2016-09-11 23:57:44 +02:00
|
|
|
# type: () -> None
|
2016-06-22 21:16:02 +02:00
|
|
|
email = "user1@test.com"
|
|
|
|
generated_link = generate_realm_creation_url()
|
|
|
|
|
|
|
|
with self.settings(OPEN_REALM_CREATION=False):
|
|
|
|
# Check realm creation page is accessible
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get(generated_link)
|
2017-04-27 20:12:09 +02:00
|
|
|
self.assert_in_success_response([u"Create a new Zulip organization"], result)
|
2016-06-22 21:16:02 +02:00
|
|
|
|
|
|
|
# Create Realm with generated link
|
2017-01-04 05:30:48 +01:00
|
|
|
self.assertIsNone(get_realm('test'))
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post(generated_link, {'email': email})
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2016-06-22 21:16:02 +02:00
|
|
|
self.assertTrue(result["Location"].endswith(
|
2017-01-24 07:06:13 +01:00
|
|
|
"/accounts/send_confirm/%s" % (email,)))
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get(result["Location"])
|
2016-07-12 15:41:45 +02:00
|
|
|
self.assert_in_response("Check your email so we can get started.", result)
|
2016-06-22 21:16:02 +02:00
|
|
|
|
|
|
|
# Generated link used for creating realm
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get(generated_link)
|
2017-01-29 00:08:08 +01:00
|
|
|
self.assert_in_success_response(["The organization creation link has expired or is not valid."], result)
|
2016-06-22 21:16:02 +02:00
|
|
|
|
|
|
|
def test_realm_creation_with_random_link(self):
|
2016-09-11 23:57:44 +02:00
|
|
|
# type: () -> None
|
2016-06-22 21:16:02 +02:00
|
|
|
with self.settings(OPEN_REALM_CREATION=False):
|
|
|
|
# Realm creation attempt with an invalid link should fail
|
|
|
|
random_link = "/create_realm/5e89081eb13984e0f3b130bf7a4121d153f1614b"
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get(random_link)
|
2017-01-29 00:08:08 +01:00
|
|
|
self.assert_in_success_response(["The organization creation link has expired or is not valid."], result)
|
2016-06-22 21:16:02 +02:00
|
|
|
|
|
|
|
def test_realm_creation_with_expired_link(self):
|
2016-09-11 23:57:44 +02:00
|
|
|
# type: () -> None
|
2016-06-22 21:16:02 +02:00
|
|
|
with self.settings(OPEN_REALM_CREATION=False):
|
|
|
|
generated_link = generate_realm_creation_url()
|
2017-07-11 20:52:27 +02:00
|
|
|
key = generated_link[-24:]
|
2016-06-22 21:16:02 +02:00
|
|
|
# Manually expire the link by changing the date of creation
|
|
|
|
obj = RealmCreationKey.objects.get(creation_key=key)
|
|
|
|
obj.date_created = obj.date_created - timedelta(days=settings.REALM_CREATION_LINK_VALIDITY_DAYS + 1)
|
|
|
|
obj.save()
|
|
|
|
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get(generated_link)
|
2017-01-29 00:08:08 +01:00
|
|
|
self.assert_in_success_response(["The organization creation link has expired or is not valid."], result)
|