2017-01-24 01:48:35 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-08-31 18:20:03 +02:00
|
|
|
from zerver.lib.actions import do_mark_hotspot_as_read, do_create_user
|
2017-01-24 01:48:35 +01:00
|
|
|
from zerver.lib.hotspots import ALL_HOTSPOTS, get_next_hotspots
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2017-08-31 18:20:03 +02:00
|
|
|
from zerver.models import UserProfile, UserHotspot, get_realm
|
2017-01-24 01:48:35 +01:00
|
|
|
from zerver.views.hotspots import mark_hotspot_as_read
|
|
|
|
|
|
|
|
from typing import Any, Dict
|
|
|
|
import ujson
|
|
|
|
|
|
|
|
# Splitting this out, since I imagine this will eventually have most of the
|
|
|
|
# complicated hotspots logic.
|
|
|
|
class TestGetNextHotspots(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def setUp(self) -> None:
|
2017-08-31 18:20:03 +02:00
|
|
|
self.user = do_create_user(
|
|
|
|
'user@zulip.com', 'password', get_realm('zulip'), 'user', 'user')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_first_hotspot(self) -> None:
|
2017-08-31 18:20:03 +02:00
|
|
|
hotspots = get_next_hotspots(self.user)
|
2017-04-15 05:50:59 +02:00
|
|
|
self.assertEqual(len(hotspots), 1)
|
2017-08-30 02:13:04 +02:00
|
|
|
self.assertEqual(hotspots[0]['name'], 'intro_reply')
|
2017-01-24 01:48:35 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_some_done_some_not(self) -> None:
|
2017-08-31 18:20:03 +02:00
|
|
|
do_mark_hotspot_as_read(self.user, 'intro_reply')
|
|
|
|
do_mark_hotspot_as_read(self.user, 'intro_compose')
|
|
|
|
hotspots = get_next_hotspots(self.user)
|
2017-04-15 05:50:59 +02:00
|
|
|
self.assertEqual(len(hotspots), 1)
|
2017-08-30 02:19:55 +02:00
|
|
|
self.assertEqual(hotspots[0]['name'], 'intro_streams')
|
2017-01-24 01:48:35 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_all_done(self) -> None:
|
2017-08-31 18:20:03 +02:00
|
|
|
self.assertNotEqual(self.user.tutorial_status, UserProfile.TUTORIAL_FINISHED)
|
2017-01-24 01:48:35 +01:00
|
|
|
for hotspot in ALL_HOTSPOTS:
|
2017-08-31 18:20:03 +02:00
|
|
|
do_mark_hotspot_as_read(self.user, hotspot)
|
|
|
|
self.assertEqual(self.user.tutorial_status, UserProfile.TUTORIAL_FINISHED)
|
|
|
|
self.assertEqual(get_next_hotspots(self.user), [])
|
2017-01-24 01:48:35 +01:00
|
|
|
|
|
|
|
class TestHotspots(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_do_mark_hotspot_as_read(self) -> None:
|
2017-05-24 05:08:49 +02:00
|
|
|
user = self.example_user('hamlet')
|
2017-08-30 02:15:32 +02:00
|
|
|
do_mark_hotspot_as_read(user, 'intro_compose')
|
2017-01-24 01:48:35 +01:00
|
|
|
self.assertEqual(list(UserHotspot.objects.filter(user=user)
|
2017-08-30 02:15:32 +02:00
|
|
|
.values_list('hotspot', flat=True)), ['intro_compose'])
|
2017-01-24 01:48:35 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_hotspots_url_endpoint(self) -> None:
|
2017-05-24 05:08:49 +02:00
|
|
|
user = self.example_user('hamlet')
|
|
|
|
self.login(user.email)
|
2017-01-24 01:48:35 +01:00
|
|
|
result = self.client_post('/json/users/me/hotspots',
|
2017-08-30 02:13:04 +02:00
|
|
|
{'hotspot': ujson.dumps('intro_reply')})
|
2017-01-24 01:48:35 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(list(UserHotspot.objects.filter(user=user)
|
2017-08-30 02:13:04 +02:00
|
|
|
.values_list('hotspot', flat=True)), ['intro_reply'])
|
2017-03-29 23:04:25 +02:00
|
|
|
|
|
|
|
result = self.client_post('/json/users/me/hotspots',
|
|
|
|
{'hotspot': ujson.dumps('invalid')})
|
|
|
|
self.assert_json_error(result, "Unknown hotspot: invalid")
|
|
|
|
self.assertEqual(list(UserHotspot.objects.filter(user=user)
|
2017-08-30 02:13:04 +02:00
|
|
|
.values_list('hotspot', flat=True)), ['intro_reply'])
|