2016-09-15 18:45:04 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
from django.conf import settings
|
|
|
|
|
2016-09-15 18:45:04 +02:00
|
|
|
from zerver.lib.test_helpers import (
|
|
|
|
most_recent_message,
|
2016-11-10 19:30:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
from zerver.lib.test_classes import (
|
2016-09-15 18:45:04 +02:00
|
|
|
ZulipTestCase,
|
|
|
|
)
|
|
|
|
|
|
|
|
from zerver.models import (
|
2017-05-24 02:42:31 +02:00
|
|
|
get_system_bot,
|
|
|
|
UserProfile
|
2016-09-15 18:45:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
import ujson
|
|
|
|
|
|
|
|
def fix_params(raw_params):
|
|
|
|
# type: (Dict[str, Any]) -> Dict[str, str]
|
|
|
|
# A few of our few legacy endpoints need their
|
|
|
|
# individual parameters serialized as JSON.
|
|
|
|
return {k: ujson.dumps(v) for k, v in raw_params.items()}
|
|
|
|
|
|
|
|
class TutorialTests(ZulipTestCase):
|
|
|
|
def test_send_message(self):
|
|
|
|
# type: () -> None
|
2017-05-07 19:39:30 +02:00
|
|
|
user = self.example_user('hamlet')
|
|
|
|
email = user.email
|
2016-09-15 18:45:04 +02:00
|
|
|
self.login(email)
|
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
welcome_bot = get_system_bot(settings.WELCOME_BOT)
|
2016-09-15 18:45:04 +02:00
|
|
|
|
|
|
|
raw_params = dict(
|
|
|
|
type='stream',
|
|
|
|
recipient='Denmark',
|
|
|
|
topic='welcome',
|
|
|
|
content='hello'
|
|
|
|
)
|
|
|
|
params = fix_params(raw_params)
|
|
|
|
|
|
|
|
result = self.client_post("/json/tutorial_send_message", params)
|
|
|
|
|
|
|
|
self.assert_json_success(result)
|
|
|
|
message = most_recent_message(user)
|
|
|
|
self.assertEqual(message.content, 'hello')
|
|
|
|
self.assertEqual(message.sender, welcome_bot)
|
|
|
|
|
|
|
|
# now test some error cases
|
|
|
|
|
|
|
|
result = self.client_post("/json/tutorial_send_message", {})
|
|
|
|
self.assert_json_error(result, "Missing 'type' argument")
|
|
|
|
|
|
|
|
result = self.client_post("/json/tutorial_send_message", raw_params)
|
2017-06-26 13:12:20 +02:00
|
|
|
self.assert_json_error(result, 'Argument "type" is not valid JSON.')
|
2016-09-15 18:45:04 +02:00
|
|
|
|
|
|
|
raw_params = dict(
|
|
|
|
type='INVALID',
|
|
|
|
recipient='Denmark',
|
|
|
|
topic='welcome',
|
|
|
|
content='hello'
|
|
|
|
)
|
|
|
|
params = fix_params(raw_params)
|
|
|
|
result = self.client_post("/json/tutorial_send_message", params)
|
|
|
|
self.assert_json_error(result, 'Bad data passed in to tutorial_send_message')
|
|
|
|
|
|
|
|
def test_tutorial_status(self):
|
|
|
|
# type: () -> None
|
2017-05-24 02:42:31 +02:00
|
|
|
email = self.example_email('hamlet')
|
2016-09-15 18:45:04 +02:00
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
cases = [
|
|
|
|
('started', UserProfile.TUTORIAL_STARTED),
|
|
|
|
('finished', UserProfile.TUTORIAL_FINISHED),
|
|
|
|
]
|
|
|
|
for incoming_status, expected_db_status in cases:
|
|
|
|
raw_params = dict(status=incoming_status)
|
|
|
|
params = fix_params(raw_params)
|
|
|
|
result = self.client_post('/json/tutorial_status', params)
|
|
|
|
self.assert_json_success(result)
|
2017-05-24 02:42:31 +02:00
|
|
|
user = self.example_user('hamlet')
|
2016-09-15 18:45:04 +02:00
|
|
|
self.assertEqual(user.tutorial_status, expected_db_status)
|