2018-04-27 15:48:55 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zerver.lib.actions import do_change_stream_web_public
|
2018-05-08 21:01:34 +02:00
|
|
|
from zerver.lib.actions import get_web_public_streams, get_web_public_subs, \
|
|
|
|
do_deactivate_stream
|
|
|
|
from zerver.models import get_realm
|
2018-04-27 15:48:55 +02:00
|
|
|
|
|
|
|
class GlobalPublicStreamTest(ZulipTestCase):
|
|
|
|
def test_non_existant_stream_id(self) -> None:
|
2020-03-28 01:25:56 +01:00
|
|
|
# Here we use a relatively big number as stream id assuming such an id
|
2018-04-27 15:48:55 +02:00
|
|
|
# won't exist in the test DB.
|
2018-07-12 10:27:14 +02:00
|
|
|
result = self.client_get("/archive/streams/100000000/topics/TopicGlobal")
|
2018-05-02 20:08:10 +02:00
|
|
|
self.assert_in_success_response(["This stream does not exist."], result)
|
2018-04-27 15:48:55 +02:00
|
|
|
|
|
|
|
def test_non_web_public_stream(self) -> None:
|
|
|
|
test_stream = self.make_stream('Test Public Archives')
|
|
|
|
result = self.client_get(
|
2018-07-12 10:27:14 +02:00
|
|
|
"/archive/streams/" + str(test_stream.id) + "/topics/notpublicglobalstream"
|
2018-04-27 15:48:55 +02:00
|
|
|
)
|
|
|
|
self.assert_in_success_response(["This stream does not exist."], result)
|
|
|
|
|
|
|
|
def test_non_existant_topic(self) -> None:
|
|
|
|
test_stream = self.make_stream('Test Public Archives')
|
|
|
|
do_change_stream_web_public(test_stream, True)
|
|
|
|
result = self.client_get(
|
2018-07-12 10:27:14 +02:00
|
|
|
"/archive/streams/" + str(test_stream.id) + "/topics/nonexistenttopic"
|
2018-04-27 15:48:55 +02:00
|
|
|
)
|
|
|
|
self.assert_in_success_response(["This topic does not exist."], result)
|
|
|
|
|
|
|
|
def test_web_public_stream_topic(self) -> None:
|
|
|
|
test_stream = self.make_stream('Test Public Archives')
|
|
|
|
do_change_stream_web_public(test_stream, True)
|
|
|
|
|
|
|
|
def send_msg_and_get_result(msg: str) -> HttpResponse:
|
|
|
|
self.send_stream_message(
|
2020-03-07 11:43:05 +01:00
|
|
|
self.example_user("iago"),
|
2018-04-27 15:48:55 +02:00
|
|
|
"Test Public Archives",
|
|
|
|
msg,
|
|
|
|
'TopicGlobal'
|
|
|
|
)
|
|
|
|
return self.client_get(
|
2018-07-12 10:27:14 +02:00
|
|
|
"/archive/streams/" + str(test_stream.id) + "/topics/TopicGlobal"
|
2018-04-27 15:48:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
result = send_msg_and_get_result('Test Message 1')
|
|
|
|
self.assert_in_success_response(["Test Message 1"], result)
|
|
|
|
result = send_msg_and_get_result('/me goes testing.')
|
|
|
|
self.assert_in_success_response(["goes testing."], result)
|
2018-05-08 21:01:34 +02:00
|
|
|
|
|
|
|
def test_get_web_public_streams(self) -> None:
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
public_streams = get_web_public_streams(realm)
|
|
|
|
self.assert_length(public_streams, 1)
|
|
|
|
public_stream = public_streams[0]
|
|
|
|
self.assertEqual(public_stream['name'], "Rome")
|
|
|
|
|
|
|
|
public_subs, public_unsubs, public_neversubs = get_web_public_subs(realm)
|
|
|
|
self.assert_length(public_subs, 1)
|
|
|
|
public_sub = public_subs[0]
|
|
|
|
self.assertEqual(public_sub['name'], "Rome")
|
|
|
|
self.assert_length(public_unsubs, 0)
|
|
|
|
self.assert_length(public_neversubs, 0)
|
|
|
|
|
|
|
|
# Now add a second public stream
|
|
|
|
test_stream = self.make_stream('Test Public Archives')
|
|
|
|
do_change_stream_web_public(test_stream, True)
|
|
|
|
public_streams = get_web_public_streams(realm)
|
|
|
|
self.assert_length(public_streams, 2)
|
|
|
|
public_subs, public_unsubs, public_neversubs = get_web_public_subs(realm)
|
|
|
|
self.assert_length(public_subs, 2)
|
|
|
|
self.assert_length(public_unsubs, 0)
|
|
|
|
self.assert_length(public_neversubs, 0)
|
|
|
|
self.assertNotEqual(public_subs[0]['color'], public_subs[1]['color'])
|
|
|
|
|
|
|
|
do_deactivate_stream(test_stream)
|
|
|
|
public_streams = get_web_public_streams(realm)
|
|
|
|
self.assert_length(public_streams, 1)
|
|
|
|
public_subs, public_unsubs, public_neversubs = get_web_public_subs(realm)
|
|
|
|
self.assert_length(public_subs, 1)
|
|
|
|
self.assert_length(public_unsubs, 0)
|
|
|
|
self.assert_length(public_neversubs, 0)
|
2018-06-14 00:44:22 +02:00
|
|
|
|
|
|
|
class WebPublicTopicHistoryTest(ZulipTestCase):
|
|
|
|
def test_non_existant_stream_id(self) -> None:
|
|
|
|
result = self.client_get("/archive/streams/100000000/topics")
|
|
|
|
self.assert_json_success(result)
|
|
|
|
history = result.json()['topics']
|
|
|
|
self.assertEqual(history, [])
|
|
|
|
|
|
|
|
def test_non_web_public_stream(self) -> None:
|
|
|
|
test_stream = self.make_stream('Test Public Archives')
|
|
|
|
|
|
|
|
self.send_stream_message(
|
2020-03-07 11:43:05 +01:00
|
|
|
self.example_user("iago"),
|
2018-06-14 00:44:22 +02:00
|
|
|
"Test Public Archives",
|
|
|
|
'Test Message',
|
|
|
|
'TopicGlobal'
|
|
|
|
)
|
|
|
|
|
|
|
|
result = self.client_get(
|
|
|
|
"/archive/streams/" + str(test_stream.id) + "/topics"
|
|
|
|
)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
history = result.json()['topics']
|
|
|
|
self.assertEqual(history, [])
|
|
|
|
|
|
|
|
def test_web_public_stream(self) -> None:
|
|
|
|
test_stream = self.make_stream('Test Public Archives')
|
|
|
|
do_change_stream_web_public(test_stream, True)
|
|
|
|
|
2018-07-14 06:38:35 +02:00
|
|
|
self.send_stream_message(
|
2020-03-07 11:43:05 +01:00
|
|
|
self.example_user("iago"),
|
2018-07-14 06:38:35 +02:00
|
|
|
"Test Public Archives",
|
|
|
|
'Test Message 3',
|
|
|
|
topic_name='first_topic'
|
|
|
|
)
|
2018-06-14 00:44:22 +02:00
|
|
|
self.send_stream_message(
|
2020-03-07 11:43:05 +01:00
|
|
|
self.example_user("iago"),
|
2018-06-14 00:44:22 +02:00
|
|
|
"Test Public Archives",
|
|
|
|
'Test Message',
|
2018-07-14 06:38:35 +02:00
|
|
|
topic_name='TopicGlobal'
|
|
|
|
)
|
|
|
|
self.send_stream_message(
|
2020-03-07 11:43:05 +01:00
|
|
|
self.example_user("iago"),
|
2018-07-14 06:38:35 +02:00
|
|
|
"Test Public Archives",
|
|
|
|
'Test Message 2',
|
|
|
|
topic_name='topicglobal'
|
|
|
|
)
|
|
|
|
self.send_stream_message(
|
2020-03-07 11:43:05 +01:00
|
|
|
self.example_user("iago"),
|
2018-07-14 06:38:35 +02:00
|
|
|
"Test Public Archives",
|
|
|
|
'Test Message 3',
|
|
|
|
topic_name='second_topic'
|
|
|
|
)
|
|
|
|
self.send_stream_message(
|
2020-03-07 11:43:05 +01:00
|
|
|
self.example_user("iago"),
|
2018-07-14 06:38:35 +02:00
|
|
|
"Test Public Archives",
|
|
|
|
'Test Message 4',
|
|
|
|
topic_name='TopicGlobal'
|
2018-06-14 00:44:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
result = self.client_get(
|
|
|
|
"/archive/streams/" + str(test_stream.id) + "/topics"
|
|
|
|
)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
history = result.json()['topics']
|
2018-07-14 06:38:35 +02:00
|
|
|
self.assert_length(history, 3)
|
|
|
|
# Should be sorted with latest topic first
|
2018-06-14 00:44:22 +02:00
|
|
|
self.assertEqual(history[0]['name'], 'TopicGlobal')
|
2018-07-14 06:38:35 +02:00
|
|
|
self.assertEqual(history[1]['name'], 'second_topic')
|
|
|
|
self.assertEqual(history[2]['name'], 'first_topic')
|