2020-06-05 06:55:20 +02:00
from datetime import datetime , timedelta , timezone
2019-02-02 23:53:19 +01:00
from typing import List , Optional
2020-05-26 07:16:25 +02:00
from unittest import mock
2020-06-11 00:54:34 +02:00
2020-08-07 01:09:47 +02:00
import orjson
2020-06-11 00:54:34 +02:00
from django . http import HttpResponse
2020-03-31 12:01:48 +02:00
from django . utils . timezone import now as timezone_now
2017-02-10 21:52:14 +01:00
2017-11-16 00:55:49 +01:00
from analytics . lib . counts import COUNT_STATS , CountStat
from analytics . lib . time_utils import time_range
2020-12-22 18:09:34 +01:00
from analytics . models import FillState , RealmCount , UserCount
2020-06-11 00:54:34 +02:00
from analytics . views import rewrite_client_arrays , sort_by_totals , sort_client_labels
2020-07-17 12:56:06 +02:00
from corporate . lib . stripe import add_months , update_sponsorship_status
2020-07-03 20:21:13 +02:00
from corporate . models import Customer , CustomerPlan , LicenseLedger , get_customer_by_realm
2021-03-19 16:57:23 +01:00
from zerver . lib . actions import (
do_create_multiuse_invite_link ,
do_send_realm_reactivation_email ,
do_set_realm_property ,
)
2017-11-16 00:55:49 +01:00
from zerver . lib . test_classes import ZulipTestCase
2020-06-11 00:54:34 +02:00
from zerver . lib . test_helpers import reset_emails_in_zulip_realm
from zerver . lib . timestamp import ceiling_to_day , ceiling_to_hour , datetime_to_timestamp
2020-07-17 12:56:06 +02:00
from zerver . models import (
Client ,
MultiuseInvite ,
PreregistrationUser ,
Realm ,
UserMessage ,
UserProfile ,
get_realm ,
)
2020-06-11 00:54:34 +02:00
2017-02-10 21:52:14 +01:00
class TestStatsEndpoint ( ZulipTestCase ) :
2017-11-05 06:54:00 +01:00
def test_stats ( self ) - > None :
2021-02-12 08:20:45 +01:00
self . user = self . example_user ( " hamlet " )
2020-03-06 18:40:46 +01:00
self . login_user ( self . user )
2021-02-12 08:20:45 +01:00
result = self . client_get ( " /stats " )
2017-02-10 21:52:14 +01:00
self . assertEqual ( result . status_code , 200 )
# Check that we get something back
2017-07-09 17:14:14 +02:00
self . assert_in_response ( " Zulip analytics for " , result )
2017-02-10 21:52:14 +01:00
2018-10-31 21:09:33 +01:00
def test_guest_user_cant_access_stats ( self ) - > None :
2021-02-12 08:20:45 +01:00
self . user = self . example_user ( " polonius " )
2020-03-06 18:40:46 +01:00
self . login_user ( self . user )
2021-02-12 08:20:45 +01:00
result = self . client_get ( " /stats " )
2018-10-31 21:09:33 +01:00
self . assert_json_error ( result , " Not allowed for guest users " , 400 )
2021-02-12 08:20:45 +01:00
result = self . client_get ( " /json/analytics/chart_data " )
2018-10-31 21:09:33 +01:00
self . assert_json_error ( result , " Not allowed for guest users " , 400 )
2018-04-15 18:43:48 +02:00
def test_stats_for_realm ( self ) - > None :
2021-02-12 08:20:45 +01:00
user = self . example_user ( " hamlet " )
2020-03-06 18:40:46 +01:00
self . login_user ( user )
2018-04-15 18:43:48 +02:00
2021-02-12 08:20:45 +01:00
result = self . client_get ( " /stats/realm/zulip/ " )
2018-04-15 18:43:48 +02:00
self . assertEqual ( result . status_code , 302 )
2021-02-12 08:20:45 +01:00
result = self . client_get ( " /stats/realm/not_existing_realm/ " )
2020-09-01 02:56:35 +02:00
self . assertEqual ( result . status_code , 302 )
2021-02-12 08:20:45 +01:00
user = self . example_user ( " hamlet " )
2020-03-06 18:40:46 +01:00
user . is_staff = True
2021-02-12 08:20:45 +01:00
user . save ( update_fields = [ " is_staff " ] )
2018-04-15 18:43:48 +02:00
2021-02-12 08:20:45 +01:00
result = self . client_get ( " /stats/realm/not_existing_realm/ " )
2020-09-01 02:56:35 +02:00
self . assertEqual ( result . status_code , 404 )
2018-04-15 18:43:48 +02:00
2021-02-12 08:20:45 +01:00
result = self . client_get ( " /stats/realm/zulip/ " )
2018-04-15 18:43:48 +02:00
self . assertEqual ( result . status_code , 200 )
self . assert_in_response ( " Zulip analytics for " , result )
2018-05-18 02:16:29 +02:00
def test_stats_for_installation ( self ) - > None :
2021-02-12 08:20:45 +01:00
user = self . example_user ( " hamlet " )
2020-03-06 18:40:46 +01:00
self . login_user ( user )
2018-05-18 02:16:29 +02:00
2021-02-12 08:20:45 +01:00
result = self . client_get ( " /stats/installation " )
2018-05-18 02:16:29 +02:00
self . assertEqual ( result . status_code , 302 )
2021-02-12 08:20:45 +01:00
user = self . example_user ( " hamlet " )
2020-03-06 18:40:46 +01:00
user . is_staff = True
2021-02-12 08:20:45 +01:00
user . save ( update_fields = [ " is_staff " ] )
2018-05-18 02:16:29 +02:00
2021-02-12 08:20:45 +01:00
result = self . client_get ( " /stats/installation " )
2018-05-18 02:16:29 +02:00
self . assertEqual ( result . status_code , 200 )
self . assert_in_response ( " Zulip analytics for " , result )
2021-02-12 08:19:30 +01:00
2017-02-10 21:52:14 +01:00
class TestGetChartData ( ZulipTestCase ) :
2017-11-05 06:54:00 +01:00
def setUp ( self ) - > None :
2019-10-19 20:47:00 +02:00
super ( ) . setUp ( )
2021-02-12 08:20:45 +01:00
self . realm = get_realm ( " zulip " )
self . user = self . example_user ( " hamlet " )
2020-03-06 18:40:46 +01:00
self . login_user ( self . user )
2021-02-12 08:19:30 +01:00
self . end_times_hour = [
ceiling_to_hour ( self . realm . date_created ) + timedelta ( hours = i ) for i in range ( 4 )
]
self . end_times_day = [
ceiling_to_day ( self . realm . date_created ) + timedelta ( days = i ) for i in range ( 4 )
]
2017-02-10 21:52:14 +01:00
2017-11-05 06:54:00 +01:00
def data ( self , i : int ) - > List [ int ] :
2017-02-10 21:52:14 +01:00
return [ 0 , 0 , i , 0 ]
2021-02-12 08:19:30 +01:00
def insert_data (
self , stat : CountStat , realm_subgroups : List [ Optional [ str ] ] , user_subgroups : List [ str ]
) - > None :
2017-02-10 21:52:14 +01:00
if stat . frequency == CountStat . HOUR :
insert_time = self . end_times_hour [ 2 ]
fill_time = self . end_times_hour [ - 1 ]
if stat . frequency == CountStat . DAY :
insert_time = self . end_times_day [ 2 ]
fill_time = self . end_times_day [ - 1 ]
2020-09-02 06:20:26 +02:00
RealmCount . objects . bulk_create (
2021-02-12 08:19:30 +01:00
RealmCount (
property = stat . property ,
subgroup = subgroup ,
end_time = insert_time ,
value = 100 + i ,
realm = self . realm ,
)
for i , subgroup in enumerate ( realm_subgroups )
)
2020-09-02 06:20:26 +02:00
UserCount . objects . bulk_create (
2021-02-12 08:19:30 +01:00
UserCount (
property = stat . property ,
subgroup = subgroup ,
end_time = insert_time ,
value = 200 + i ,
realm = self . realm ,
user = self . user ,
)
for i , subgroup in enumerate ( user_subgroups )
)
2017-02-10 21:52:14 +01:00
FillState . objects . create ( property = stat . property , end_time = fill_time , state = FillState . DONE )
2017-11-05 06:54:00 +01:00
def test_number_of_humans ( self ) - > None :
2021-02-12 08:20:45 +01:00
stat = COUNT_STATS [ " realm_active_humans::day " ]
2017-04-25 23:54:30 +02:00
self . insert_data ( stat , [ None ] , [ ] )
2021-02-12 08:20:45 +01:00
stat = COUNT_STATS [ " 1day_actives::day " ]
2018-05-19 22:43:02 +02:00
self . insert_data ( stat , [ None ] , [ ] )
2021-02-12 08:20:45 +01:00
stat = COUNT_STATS [ " active_users_audit:is_bot:day " ]
self . insert_data ( stat , [ " false " ] , [ ] )
result = self . client_get ( " /json/analytics/chart_data " , { " chart_name " : " number_of_humans " } )
2017-02-10 21:52:14 +01:00
self . assert_json_success ( result )
2017-08-17 08:26:42 +02:00
data = result . json ( )
2021-02-12 08:19:30 +01:00
self . assertEqual (
data ,
{
2021-02-12 08:20:45 +01:00
" msg " : " " ,
" end_times " : [ datetime_to_timestamp ( dt ) for dt in self . end_times_day ] ,
" frequency " : CountStat . DAY ,
" everyone " : {
" _1day " : self . data ( 100 ) ,
" _15day " : self . data ( 100 ) ,
" all_time " : self . data ( 100 ) ,
2021-02-12 08:19:30 +01:00
} ,
2021-02-12 08:20:45 +01:00
" display_order " : None ,
" result " : " success " ,
2021-02-12 08:19:30 +01:00
} ,
)
2017-02-10 21:52:14 +01:00
2017-11-05 06:54:00 +01:00
def test_messages_sent_over_time ( self ) - > None :
2021-02-12 08:20:45 +01:00
stat = COUNT_STATS [ " messages_sent:is_bot:hour " ]
self . insert_data ( stat , [ " true " , " false " ] , [ " false " ] )
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " messages_sent_over_time " }
2021-02-12 08:19:30 +01:00
)
2017-02-10 21:52:14 +01:00
self . assert_json_success ( result )
2017-08-17 08:26:42 +02:00
data = result . json ( )
2021-02-12 08:19:30 +01:00
self . assertEqual (
data ,
{
2021-02-12 08:20:45 +01:00
" msg " : " " ,
" end_times " : [ datetime_to_timestamp ( dt ) for dt in self . end_times_hour ] ,
" frequency " : CountStat . HOUR ,
" everyone " : { " bot " : self . data ( 100 ) , " human " : self . data ( 101 ) } ,
" user " : { " bot " : self . data ( 0 ) , " human " : self . data ( 200 ) } ,
" display_order " : None ,
" result " : " success " ,
2021-02-12 08:19:30 +01:00
} ,
)
2017-02-10 21:52:14 +01:00
2017-11-05 06:54:00 +01:00
def test_messages_sent_by_message_type ( self ) - > None :
2021-02-12 08:20:45 +01:00
stat = COUNT_STATS [ " messages_sent:message_type:day " ]
2021-02-12 08:19:30 +01:00
self . insert_data (
2021-02-12 08:20:45 +01:00
stat , [ " public_stream " , " private_message " ] , [ " public_stream " , " private_stream " ]
2021-02-12 08:19:30 +01:00
)
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " messages_sent_by_message_type " }
2021-02-12 08:19:30 +01:00
)
2017-02-10 21:52:14 +01:00
self . assert_json_success ( result )
2017-08-17 08:26:42 +02:00
data = result . json ( )
2021-02-12 08:19:30 +01:00
self . assertEqual (
data ,
{
2021-02-12 08:20:45 +01:00
" msg " : " " ,
" end_times " : [ datetime_to_timestamp ( dt ) for dt in self . end_times_day ] ,
" frequency " : CountStat . DAY ,
" everyone " : {
" Public streams " : self . data ( 100 ) ,
" Private streams " : self . data ( 0 ) ,
" Private messages " : self . data ( 101 ) ,
" Group private messages " : self . data ( 0 ) ,
2021-02-12 08:19:30 +01:00
} ,
2021-02-12 08:20:45 +01:00
" user " : {
" Public streams " : self . data ( 200 ) ,
" Private streams " : self . data ( 201 ) ,
" Private messages " : self . data ( 0 ) ,
" Group private messages " : self . data ( 0 ) ,
2021-02-12 08:19:30 +01:00
} ,
2021-02-12 08:20:45 +01:00
" display_order " : [
" Private messages " ,
" Public streams " ,
" Private streams " ,
" Group private messages " ,
2021-02-12 08:19:30 +01:00
] ,
2021-02-12 08:20:45 +01:00
" result " : " success " ,
2021-02-12 08:19:30 +01:00
} ,
)
2017-02-10 21:52:14 +01:00
2017-11-05 06:54:00 +01:00
def test_messages_sent_by_client ( self ) - > None :
2021-02-12 08:20:45 +01:00
stat = COUNT_STATS [ " messages_sent:client:day " ]
client1 = Client . objects . create ( name = " client 1 " )
client2 = Client . objects . create ( name = " client 2 " )
client3 = Client . objects . create ( name = " client 3 " )
client4 = Client . objects . create ( name = " client 4 " )
2021-02-12 08:19:30 +01:00
self . insert_data ( stat , [ client4 . id , client3 . id , client2 . id ] , [ client3 . id , client1 . id ] )
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " messages_sent_by_client " }
2021-02-12 08:19:30 +01:00
)
2017-02-10 21:52:14 +01:00
self . assert_json_success ( result )
2017-08-17 08:26:42 +02:00
data = result . json ( )
2021-02-12 08:19:30 +01:00
self . assertEqual (
data ,
{
2021-02-12 08:20:45 +01:00
" msg " : " " ,
" end_times " : [ datetime_to_timestamp ( dt ) for dt in self . end_times_day ] ,
" frequency " : CountStat . DAY ,
" everyone " : {
" client 4 " : self . data ( 100 ) ,
" client 3 " : self . data ( 101 ) ,
" client 2 " : self . data ( 102 ) ,
2021-02-12 08:19:30 +01:00
} ,
2021-02-12 08:20:45 +01:00
" user " : { " client 3 " : self . data ( 200 ) , " client 1 " : self . data ( 201 ) } ,
" display_order " : [ " client 1 " , " client 2 " , " client 3 " , " client 4 " ] ,
" result " : " success " ,
2021-02-12 08:19:30 +01:00
} ,
)
2017-02-10 21:52:14 +01:00
2020-06-11 12:56:06 +02:00
def test_messages_read_over_time ( self ) - > None :
2021-02-12 08:20:45 +01:00
stat = COUNT_STATS [ " messages_read::hour " ]
2020-06-11 12:56:06 +02:00
self . insert_data ( stat , [ None ] , [ ] )
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " messages_read_over_time " }
2021-02-12 08:19:30 +01:00
)
2020-06-11 12:56:06 +02:00
self . assert_json_success ( result )
data = result . json ( )
2021-02-12 08:19:30 +01:00
self . assertEqual (
data ,
{
2021-02-12 08:20:45 +01:00
" msg " : " " ,
" end_times " : [ datetime_to_timestamp ( dt ) for dt in self . end_times_hour ] ,
" frequency " : CountStat . HOUR ,
" everyone " : { " read " : self . data ( 100 ) } ,
" user " : { " read " : self . data ( 0 ) } ,
" display_order " : None ,
" result " : " success " ,
2021-02-12 08:19:30 +01:00
} ,
)
2020-06-11 12:56:06 +02:00
2017-11-05 06:54:00 +01:00
def test_include_empty_subgroups ( self ) - > None :
2017-02-10 21:52:14 +01:00
FillState . objects . create (
2021-02-12 08:20:45 +01:00
property = " realm_active_humans::day " ,
2021-02-12 08:19:30 +01:00
end_time = self . end_times_day [ 0 ] ,
state = FillState . DONE ,
)
2021-02-12 08:20:45 +01:00
result = self . client_get ( " /json/analytics/chart_data " , { " chart_name " : " number_of_humans " } )
2017-02-10 21:52:14 +01:00
self . assert_json_success ( result )
2017-08-17 08:26:42 +02:00
data = result . json ( )
2021-02-12 08:20:45 +01:00
self . assertEqual ( data [ " everyone " ] , { " _1day " : [ 0 ] , " _15day " : [ 0 ] , " all_time " : [ 0 ] } )
self . assertFalse ( " user " in data )
2017-02-10 21:52:14 +01:00
FillState . objects . create (
2021-02-12 08:20:45 +01:00
property = " messages_sent:is_bot:hour " ,
2021-02-12 08:19:30 +01:00
end_time = self . end_times_hour [ 0 ] ,
state = FillState . DONE ,
)
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " messages_sent_over_time " }
2021-02-12 08:19:30 +01:00
)
2017-02-10 21:52:14 +01:00
self . assert_json_success ( result )
2017-08-17 08:26:42 +02:00
data = result . json ( )
2021-02-12 08:20:45 +01:00
self . assertEqual ( data [ " everyone " ] , { " human " : [ 0 ] , " bot " : [ 0 ] } )
self . assertEqual ( data [ " user " ] , { " human " : [ 0 ] , " bot " : [ 0 ] } )
2017-02-10 21:52:14 +01:00
FillState . objects . create (
2021-02-12 08:20:45 +01:00
property = " messages_sent:message_type:day " ,
2021-02-12 08:19:30 +01:00
end_time = self . end_times_day [ 0 ] ,
state = FillState . DONE ,
)
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " messages_sent_by_message_type " }
2021-02-12 08:19:30 +01:00
)
2017-02-10 21:52:14 +01:00
self . assert_json_success ( result )
2017-08-17 08:26:42 +02:00
data = result . json ( )
2021-02-12 08:19:30 +01:00
self . assertEqual (
2021-02-12 08:20:45 +01:00
data [ " everyone " ] ,
2021-02-12 08:19:30 +01:00
{
2021-02-12 08:20:45 +01:00
" Public streams " : [ 0 ] ,
" Private streams " : [ 0 ] ,
" Private messages " : [ 0 ] ,
" Group private messages " : [ 0 ] ,
2021-02-12 08:19:30 +01:00
} ,
)
self . assertEqual (
2021-02-12 08:20:45 +01:00
data [ " user " ] ,
2021-02-12 08:19:30 +01:00
{
2021-02-12 08:20:45 +01:00
" Public streams " : [ 0 ] ,
" Private streams " : [ 0 ] ,
" Private messages " : [ 0 ] ,
" Group private messages " : [ 0 ] ,
2021-02-12 08:19:30 +01:00
} ,
)
2017-02-10 21:52:14 +01:00
FillState . objects . create (
2021-02-12 08:20:45 +01:00
property = " messages_sent:client:day " ,
2021-02-12 08:19:30 +01:00
end_time = self . end_times_day [ 0 ] ,
state = FillState . DONE ,
)
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " messages_sent_by_client " }
2021-02-12 08:19:30 +01:00
)
2017-02-10 21:52:14 +01:00
self . assert_json_success ( result )
2017-08-17 08:26:42 +02:00
data = result . json ( )
2021-02-12 08:20:45 +01:00
self . assertEqual ( data [ " everyone " ] , { } )
self . assertEqual ( data [ " user " ] , { } )
2017-02-10 21:52:14 +01:00
2017-11-05 06:54:00 +01:00
def test_start_and_end ( self ) - > None :
2021-02-12 08:20:45 +01:00
stat = COUNT_STATS [ " realm_active_humans::day " ]
2017-04-25 23:54:30 +02:00
self . insert_data ( stat , [ None ] , [ ] )
2021-02-12 08:20:45 +01:00
stat = COUNT_STATS [ " 1day_actives::day " ]
2018-05-19 22:43:02 +02:00
self . insert_data ( stat , [ None ] , [ ] )
2021-02-12 08:20:45 +01:00
stat = COUNT_STATS [ " active_users_audit:is_bot:day " ]
self . insert_data ( stat , [ " false " ] , [ ] )
2017-02-10 21:52:14 +01:00
end_time_timestamps = [ datetime_to_timestamp ( dt ) for dt in self . end_times_day ]
# valid start and end
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " ,
2021-02-12 08:19:30 +01:00
{
2021-02-12 08:20:45 +01:00
" chart_name " : " number_of_humans " ,
" start " : end_time_timestamps [ 1 ] ,
" end " : end_time_timestamps [ 2 ] ,
2021-02-12 08:19:30 +01:00
} ,
)
2017-02-10 21:52:14 +01:00
self . assert_json_success ( result )
2017-08-17 08:26:42 +02:00
data = result . json ( )
2021-02-12 08:20:45 +01:00
self . assertEqual ( data [ " end_times " ] , end_time_timestamps [ 1 : 3 ] )
2021-02-12 08:19:30 +01:00
self . assertEqual (
2021-02-12 08:20:45 +01:00
data [ " everyone " ] , { " _1day " : [ 0 , 100 ] , " _15day " : [ 0 , 100 ] , " all_time " : [ 0 , 100 ] }
2021-02-12 08:19:30 +01:00
)
2017-02-10 21:52:14 +01:00
# start later then end
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " ,
2021-02-12 08:19:30 +01:00
{
2021-02-12 08:20:45 +01:00
" chart_name " : " number_of_humans " ,
" start " : end_time_timestamps [ 2 ] ,
" end " : end_time_timestamps [ 1 ] ,
2021-02-12 08:19:30 +01:00
} ,
)
2021-02-12 08:20:45 +01:00
self . assert_json_error_contains ( result , " Start time is later than " )
2017-02-10 21:52:14 +01:00
2017-11-05 06:54:00 +01:00
def test_min_length ( self ) - > None :
2021-02-12 08:20:45 +01:00
stat = COUNT_STATS [ " realm_active_humans::day " ]
2017-04-25 23:54:30 +02:00
self . insert_data ( stat , [ None ] , [ ] )
2021-02-12 08:20:45 +01:00
stat = COUNT_STATS [ " 1day_actives::day " ]
2018-05-19 22:43:02 +02:00
self . insert_data ( stat , [ None ] , [ ] )
2021-02-12 08:20:45 +01:00
stat = COUNT_STATS [ " active_users_audit:is_bot:day " ]
self . insert_data ( stat , [ " false " ] , [ ] )
2017-02-10 21:52:14 +01:00
# test min_length is too short to change anything
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " number_of_humans " , " min_length " : 2 }
2021-02-12 08:19:30 +01:00
)
2017-02-10 21:52:14 +01:00
self . assert_json_success ( result )
2017-08-17 08:26:42 +02:00
data = result . json ( )
2021-02-12 08:19:30 +01:00
self . assertEqual (
2021-02-12 08:20:45 +01:00
data [ " end_times " ] , [ datetime_to_timestamp ( dt ) for dt in self . end_times_day ]
2021-02-12 08:19:30 +01:00
)
self . assertEqual (
2021-02-12 08:20:45 +01:00
data [ " everyone " ] ,
{ " _1day " : self . data ( 100 ) , " _15day " : self . data ( 100 ) , " all_time " : self . data ( 100 ) } ,
2021-02-12 08:19:30 +01:00
)
2017-02-10 21:52:14 +01:00
# test min_length larger than filled data
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " number_of_humans " , " min_length " : 5 }
2021-02-12 08:19:30 +01:00
)
2017-02-10 21:52:14 +01:00
self . assert_json_success ( result )
2017-08-17 08:26:42 +02:00
data = result . json ( )
2021-02-12 08:19:30 +01:00
end_times = [
ceiling_to_day ( self . realm . date_created ) + timedelta ( days = i ) for i in range ( - 1 , 4 )
]
2021-02-12 08:20:45 +01:00
self . assertEqual ( data [ " end_times " ] , [ datetime_to_timestamp ( dt ) for dt in end_times ] )
2021-02-12 08:19:30 +01:00
self . assertEqual (
2021-02-12 08:20:45 +01:00
data [ " everyone " ] ,
2021-02-12 08:19:30 +01:00
{
2021-02-12 08:20:45 +01:00
" _1day " : [ 0 , * self . data ( 100 ) ] ,
" _15day " : [ 0 , * self . data ( 100 ) ] ,
" all_time " : [ 0 , * self . data ( 100 ) ] ,
2021-02-12 08:19:30 +01:00
} ,
)
2017-02-10 21:52:14 +01:00
2017-11-05 06:54:00 +01:00
def test_non_existent_chart ( self ) - > None :
2021-02-12 08:20:45 +01:00
result = self . client_get ( " /json/analytics/chart_data " , { " chart_name " : " does_not_exist " } )
self . assert_json_error_contains ( result , " Unknown chart name " )
2017-02-10 21:52:14 +01:00
2017-11-05 06:54:00 +01:00
def test_analytics_not_running ( self ) - > None :
2020-03-31 12:01:48 +02:00
realm = get_realm ( " zulip " )
self . assertEqual ( FillState . objects . count ( ) , 0 )
realm . date_created = timezone_now ( ) - timedelta ( days = 3 )
realm . save ( update_fields = [ " date_created " ] )
2020-10-29 20:21:18 +01:00
with self . assertLogs ( level = " WARNING " ) as m :
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " messages_sent_over_time " }
2021-02-12 08:19:30 +01:00
)
self . assertEqual (
m . output ,
[
f " WARNING:root:User from realm zulip attempted to access /stats, but the computed start time: { realm . date_created } (creation of realm or installation) is later than the computed end time: 0001-01-01 00:00:00+00:00 (last successful analytics update). Is the analytics cron job running? "
] ,
)
2020-10-29 20:21:18 +01:00
2021-02-12 08:20:45 +01:00
self . assert_json_error_contains ( result , " No analytics data available " )
2020-03-31 12:01:48 +02:00
realm . date_created = timezone_now ( ) - timedelta ( days = 1 , hours = 2 )
realm . save ( update_fields = [ " date_created " ] )
2020-10-29 20:21:18 +01:00
with self . assertLogs ( level = " WARNING " ) as m :
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " messages_sent_over_time " }
2021-02-12 08:19:30 +01:00
)
self . assertEqual (
m . output ,
[
f " WARNING:root:User from realm zulip attempted to access /stats, but the computed start time: { realm . date_created } (creation of realm or installation) is later than the computed end time: 0001-01-01 00:00:00+00:00 (last successful analytics update). Is the analytics cron job running? "
] ,
)
2020-10-29 20:21:18 +01:00
2021-02-12 08:20:45 +01:00
self . assert_json_error_contains ( result , " No analytics data available " )
2020-03-31 12:01:48 +02:00
realm . date_created = timezone_now ( ) - timedelta ( days = 1 , minutes = 10 )
realm . save ( update_fields = [ " date_created " ] )
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " messages_sent_over_time " }
2021-02-12 08:19:30 +01:00
)
2020-03-31 12:01:48 +02:00
self . assert_json_success ( result )
realm . date_created = timezone_now ( ) - timedelta ( hours = 10 )
realm . save ( update_fields = [ " date_created " ] )
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " messages_sent_over_time " }
2021-02-12 08:19:30 +01:00
)
2020-03-31 12:01:48 +02:00
self . assert_json_success ( result )
end_time = timezone_now ( ) - timedelta ( days = 5 )
2021-02-12 08:19:30 +01:00
fill_state = FillState . objects . create (
2021-02-12 08:20:45 +01:00
property = " messages_sent:is_bot:hour " , end_time = end_time , state = FillState . DONE
2021-02-12 08:19:30 +01:00
)
2020-03-31 12:01:48 +02:00
realm . date_created = timezone_now ( ) - timedelta ( days = 3 )
realm . save ( update_fields = [ " date_created " ] )
2020-10-29 20:21:18 +01:00
with self . assertLogs ( level = " WARNING " ) as m :
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " messages_sent_over_time " }
2021-02-12 08:19:30 +01:00
)
self . assertEqual (
m . output ,
[
f " WARNING:root:User from realm zulip attempted to access /stats, but the computed start time: { realm . date_created } (creation of realm or installation) is later than the computed end time: { end_time } (last successful analytics update). Is the analytics cron job running? "
] ,
)
2020-10-29 20:21:18 +01:00
2021-02-12 08:20:45 +01:00
self . assert_json_error_contains ( result , " No analytics data available " )
2017-02-10 21:52:14 +01:00
2020-03-31 12:01:48 +02:00
realm . date_created = timezone_now ( ) - timedelta ( days = 1 , minutes = 10 )
realm . save ( update_fields = [ " date_created " ] )
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " messages_sent_over_time " }
2021-02-12 08:19:30 +01:00
)
2020-03-31 12:01:48 +02:00
self . assert_json_success ( result )
end_time = timezone_now ( ) - timedelta ( days = 2 )
fill_state . end_time = end_time
fill_state . save ( update_fields = [ " end_time " ] )
realm . date_created = timezone_now ( ) - timedelta ( days = 3 )
realm . save ( update_fields = [ " date_created " ] )
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " messages_sent_over_time " }
2021-02-12 08:19:30 +01:00
)
2020-03-31 12:01:48 +02:00
self . assert_json_success ( result )
realm . date_created = timezone_now ( ) - timedelta ( days = 1 , hours = 2 )
realm . save ( update_fields = [ " date_created " ] )
2020-10-29 20:21:18 +01:00
with self . assertLogs ( level = " WARNING " ) as m :
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " messages_sent_over_time " }
2021-02-12 08:19:30 +01:00
)
self . assertEqual (
m . output ,
[
f " WARNING:root:User from realm zulip attempted to access /stats, but the computed start time: { realm . date_created } (creation of realm or installation) is later than the computed end time: { end_time } (last successful analytics update). Is the analytics cron job running? "
] ,
)
2020-10-29 20:21:18 +01:00
2021-02-12 08:20:45 +01:00
self . assert_json_error_contains ( result , " No analytics data available " )
2020-03-31 12:01:48 +02:00
realm . date_created = timezone_now ( ) - timedelta ( days = 1 , minutes = 10 )
realm . save ( update_fields = [ " date_created " ] )
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data " , { " chart_name " : " messages_sent_over_time " }
2021-02-12 08:19:30 +01:00
)
2020-03-31 12:01:48 +02:00
self . assert_json_success ( result )
2018-04-15 18:43:48 +02:00
def test_get_chart_data_for_realm ( self ) - > None :
2021-02-12 08:20:45 +01:00
user = self . example_user ( " hamlet " )
2020-03-06 18:40:46 +01:00
self . login_user ( user )
2018-04-15 18:43:48 +02:00
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data/realm/zulip " , { " chart_name " : " number_of_humans " }
2021-02-12 08:19:30 +01:00
)
2018-04-15 18:43:48 +02:00
self . assert_json_error ( result , " Must be an server administrator " , 400 )
2021-02-12 08:20:45 +01:00
user = self . example_user ( " hamlet " )
2020-03-06 18:40:46 +01:00
user . is_staff = True
2021-02-12 08:20:45 +01:00
user . save ( update_fields = [ " is_staff " ] )
stat = COUNT_STATS [ " realm_active_humans::day " ]
2018-04-15 18:43:48 +02:00
self . insert_data ( stat , [ None ] , [ ] )
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data/realm/not_existing_realm " ,
{ " chart_name " : " number_of_humans " } ,
2021-02-12 08:19:30 +01:00
)
2021-02-12 08:20:45 +01:00
self . assert_json_error ( result , " Invalid organization " , 400 )
2018-04-15 18:43:48 +02:00
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data/realm/zulip " , { " chart_name " : " number_of_humans " }
2021-02-12 08:19:30 +01:00
)
2018-04-15 18:43:48 +02:00
self . assert_json_success ( result )
2018-05-18 02:16:29 +02:00
def test_get_chart_data_for_installation ( self ) - > None :
2021-02-12 08:20:45 +01:00
user = self . example_user ( " hamlet " )
2020-03-06 18:40:46 +01:00
self . login_user ( user )
2018-05-18 02:16:29 +02:00
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data/installation " , { " chart_name " : " number_of_humans " }
2021-02-12 08:19:30 +01:00
)
2018-05-18 02:16:29 +02:00
self . assert_json_error ( result , " Must be an server administrator " , 400 )
2021-02-12 08:20:45 +01:00
user = self . example_user ( " hamlet " )
2020-03-06 18:40:46 +01:00
user . is_staff = True
2021-02-12 08:20:45 +01:00
user . save ( update_fields = [ " is_staff " ] )
stat = COUNT_STATS [ " realm_active_humans::day " ]
2018-05-18 02:16:29 +02:00
self . insert_data ( stat , [ None ] , [ ] )
2021-02-12 08:19:30 +01:00
result = self . client_get (
2021-02-12 08:20:45 +01:00
" /json/analytics/chart_data/installation " , { " chart_name " : " number_of_humans " }
2021-02-12 08:19:30 +01:00
)
2018-05-18 02:16:29 +02:00
self . assert_json_success ( result )
2021-02-12 08:19:30 +01:00
2019-03-08 13:02:10 +01:00
class TestSupportEndpoint ( ZulipTestCase ) :
def test_search ( self ) - > None :
2020-03-12 14:17:25 +01:00
reset_emails_in_zulip_realm ( )
2021-03-19 17:56:06 +01:00
def assert_user_details_in_html_response (
html_response : str , full_name : str , email : str , role : str
) - > None :
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
[
' <span class= " label " >user</span> \n ' ,
2021-03-19 17:56:06 +01:00
f " <h3> { full_name } </h3> " ,
f " <b>Email</b>: { email } " ,
2021-02-12 08:20:45 +01:00
" <b>Is active</b>: True<br> " ,
2021-03-19 17:56:06 +01:00
f " <b>Role</b>: { role } <br> " ,
2021-02-12 08:19:30 +01:00
] ,
2021-03-19 17:56:06 +01:00
html_response ,
2021-02-12 08:19:30 +01:00
)
2019-03-08 13:02:10 +01:00
2021-03-19 17:56:06 +01:00
def check_hamlet_user_query_result ( result : HttpResponse ) - > None :
assert_user_details_in_html_response (
2021-03-19 19:15:35 +01:00
result , " King Hamlet " , self . example_email ( " hamlet " ) , " Member "
2021-03-19 17:56:06 +01:00
)
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
[
2021-04-01 17:28:27 +02:00
f " <b>Admins</b>: { self . example_email ( ' iago ' ) } \n " ,
f " <b>Owners</b>: { self . example_email ( ' desdemona ' ) } \n " ,
' class= " copy-button " data-copytext= " {} " > ' . format ( self . example_email ( " iago " ) ) ,
' class= " copy-button " data-copytext= " {} " > ' . format (
self . example_email ( " desdemona " )
2021-03-19 19:15:35 +01:00
) ,
2021-03-19 16:50:50 +01:00
] ,
result ,
)
2021-03-19 17:56:06 +01:00
def check_othello_user_query_result ( result : HttpResponse ) - > None :
assert_user_details_in_html_response (
2021-03-19 19:15:35 +01:00
result , " Othello, the Moor of Venice " , self . example_email ( " othello " ) , " Member "
2021-02-12 08:19:30 +01:00
)
2020-10-30 20:23:20 +01:00
2021-03-19 17:56:06 +01:00
def check_polonius_user_query_result ( result : HttpResponse ) - > None :
2021-03-19 19:15:35 +01:00
assert_user_details_in_html_response (
result , " Polonius " , self . example_email ( " polonius " ) , " Guest "
)
2021-03-19 17:56:06 +01:00
2019-06-12 16:09:24 +02:00
def check_zulip_realm_query_result ( result : HttpResponse ) - > None :
2019-07-24 07:22:48 +02:00
zulip_realm = get_realm ( " zulip " )
2021-04-16 20:17:48 +02:00
first_human_user = zulip_realm . get_first_human_user ( )
assert first_human_user is not None
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
[
2021-04-16 20:17:48 +02:00
f " <b>First human user</b>: { first_human_user . delivery_email } \n " ,
2021-02-12 08:19:30 +01:00
f ' <input type= " hidden " name= " realm_id " value= " { zulip_realm . id } " ' ,
2021-02-12 08:20:45 +01:00
" Zulip Dev</h3> " ,
2021-02-12 08:19:30 +01:00
' <option value= " 1 " selected>Self hosted</option> ' ,
' <option value= " 2 " >Limited</option> ' ,
' input type= " number " name= " discount " value= " None " ' ,
' <option value= " active " selected>Active</option> ' ,
' <option value= " deactivated " >Deactivated</option> ' ,
' scrub-realm-button " > ' ,
' data-string-id= " zulip " ' ,
] ,
result ,
)
2019-03-08 13:02:10 +01:00
2019-06-12 16:09:24 +02:00
def check_lear_realm_query_result ( result : HttpResponse ) - > None :
2019-07-24 07:22:48 +02:00
lear_realm = get_realm ( " lear " )
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
[
f ' <input type= " hidden " name= " realm_id " value= " { lear_realm . id } " ' ,
2021-02-12 08:20:45 +01:00
" Lear & Co.</h3> " ,
2021-02-12 08:19:30 +01:00
' <option value= " 1 " selected>Self hosted</option> ' ,
' <option value= " 2 " >Limited</option> ' ,
' input type= " number " name= " discount " value= " None " ' ,
' <option value= " active " selected>Active</option> ' ,
' <option value= " deactivated " >Deactivated</option> ' ,
' scrub-realm-button " > ' ,
' data-string-id= " lear " ' ,
2021-02-12 08:20:45 +01:00
" <b>Name</b>: Zulip Standard " ,
" <b>Status</b>: Active " ,
" <b>Billing schedule</b>: Annual " ,
" <b>Licenses</b>: 2/10 (Manual) " ,
" <b>Price per license</b>: $80.0 " ,
" <b>Next invoice date</b>: 02 January 2017 " ,
2021-02-12 08:19:30 +01:00
' <option value= " send_invoice " selected> ' ,
' <option value= " charge_automatically " > ' ,
] ,
result ,
)
def check_preregistration_user_query_result (
result : HttpResponse , email : str , invite : bool = False
) - > None :
self . assert_in_success_response (
[
' <span class= " label " >preregistration user</span> \n ' ,
2021-02-12 08:20:45 +01:00
f " <b>Email</b>: { email } " ,
2021-02-12 08:19:30 +01:00
] ,
result ,
)
2019-09-18 15:04:36 +02:00
if invite :
self . assert_in_success_response ( [ ' <span class= " label " >invite</span> ' ] , result )
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
[
2021-04-01 16:56:37 +02:00
" <b>Expires in</b>: 1 \xa0 week, 3 \xa0 days " ,
2021-02-12 08:20:45 +01:00
" <b>Status</b>: Link has never been clicked " ,
2021-02-12 08:19:30 +01:00
] ,
result ,
)
2019-09-18 15:04:36 +02:00
self . assert_in_success_response ( [ ] , result )
else :
self . assert_not_in_success_response ( [ ' <span class= " label " >invite</span> ' ] , result )
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
2021-04-01 16:56:37 +02:00
[
" <b>Expires in</b>: 1 \xa0 day " ,
" <b>Status</b>: Link has never been clicked " ,
] ,
2021-02-12 08:19:30 +01:00
result ,
)
2019-09-18 15:04:36 +02:00
def check_realm_creation_query_result ( result : HttpResponse , email : str ) - > None :
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
[
' <span class= " label " >preregistration user</span> \n ' ,
' <span class= " label " >realm creation</span> \n ' ,
2021-02-12 08:20:45 +01:00
" <b>Link</b>: http://testserver/accounts/do_confirm/ " ,
2021-04-01 16:56:37 +02:00
" <b>Expires in</b>: 1 \xa0 day " ,
2021-02-12 08:19:30 +01:00
] ,
result ,
)
2019-09-18 15:04:36 +02:00
def check_multiuse_invite_link_query_result ( result : HttpResponse ) - > None :
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
[
' <span class= " label " >multiuse invite</span> \n ' ,
2021-02-12 08:20:45 +01:00
" <b>Link</b>: http://zulip.testserver/join/ " ,
2021-04-01 16:56:37 +02:00
" <b>Expires in</b>: 1 \xa0 week, 3 \xa0 days " ,
2021-02-12 08:19:30 +01:00
] ,
result ,
)
2019-09-18 15:04:36 +02:00
def check_realm_reactivation_link_query_result ( result : HttpResponse ) - > None :
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
[
' <span class= " label " >realm reactivation</span> \n ' ,
2021-02-12 08:20:45 +01:00
" <b>Link</b>: http://zulip.testserver/reactivate/ " ,
" <b>Expires in</b>: 1 \xa0 day " ,
2021-02-12 08:19:30 +01:00
] ,
result ,
)
2019-09-18 15:04:36 +02:00
2021-02-12 08:20:45 +01:00
self . login ( " cordelia " )
2019-03-08 13:02:10 +01:00
result = self . client_get ( " /activity/support " )
self . assertEqual ( result . status_code , 302 )
self . assertEqual ( result [ " Location " ] , " /login/ " )
2021-02-12 08:20:45 +01:00
self . login ( " iago " )
2019-03-08 13:02:10 +01:00
2021-03-19 16:57:23 +01:00
do_set_realm_property (
2021-03-01 11:33:24 +01:00
get_realm ( " zulip " ) ,
" email_address_visibility " ,
Realm . EMAIL_ADDRESS_VISIBILITY_NOBODY ,
acting_user = None ,
2021-03-19 16:57:23 +01:00
)
2021-02-12 08:20:45 +01:00
customer = Customer . objects . create ( realm = get_realm ( " lear " ) , stripe_customer_id = " cus_123 " )
2020-07-03 20:21:13 +02:00
now = datetime ( 2016 , 1 , 2 , tzinfo = timezone . utc )
2021-02-12 08:19:30 +01:00
plan = CustomerPlan . objects . create (
customer = customer ,
billing_cycle_anchor = now ,
billing_schedule = CustomerPlan . ANNUAL ,
tier = CustomerPlan . STANDARD ,
price_per_license = 8000 ,
next_invoice_date = add_months ( now , 12 ) ,
)
LicenseLedger . objects . create (
licenses = 10 ,
licenses_at_next_renewal = 10 ,
event_time = timezone_now ( ) ,
is_renewal = True ,
plan = plan ,
)
2020-07-03 20:21:13 +02:00
2019-03-08 13:02:10 +01:00
result = self . client_get ( " /activity/support " )
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
[ ' <input type= " text " name= " q " class= " input-xxlarge search-query " ' ] , result
)
2019-03-08 13:02:10 +01:00
2021-03-19 19:15:35 +01:00
result = self . client_get ( " /activity/support " , { " q " : self . example_email ( " hamlet " ) } )
2019-06-12 16:09:24 +02:00
check_hamlet_user_query_result ( result )
2021-03-19 16:50:50 +01:00
check_zulip_realm_query_result ( result )
result = self . client_get ( " /activity/support " , { " q " : self . example_email ( " polonius " ) } )
check_polonius_user_query_result ( result )
2019-06-12 16:09:24 +02:00
check_zulip_realm_query_result ( result )
2019-03-08 13:02:10 +01:00
result = self . client_get ( " /activity/support " , { " q " : " lear " } )
2019-06-12 16:09:24 +02:00
check_lear_realm_query_result ( result )
2019-03-08 13:02:10 +01:00
result = self . client_get ( " /activity/support " , { " q " : " http://lear.testserver " } )
2019-06-12 16:09:24 +02:00
check_lear_realm_query_result ( result )
2019-03-08 13:02:10 +01:00
2021-02-12 08:20:45 +01:00
with self . settings ( REALM_HOSTS = { " zulip " : " localhost " } ) :
2019-03-08 13:02:10 +01:00
result = self . client_get ( " /activity/support " , { " q " : " http://localhost " } )
2019-06-12 16:09:24 +02:00
check_zulip_realm_query_result ( result )
2019-03-08 13:02:10 +01:00
result = self . client_get ( " /activity/support " , { " q " : " hamlet@zulip.com, lear " } )
2019-06-12 16:09:24 +02:00
check_hamlet_user_query_result ( result )
check_zulip_realm_query_result ( result )
check_lear_realm_query_result ( result )
2019-03-08 13:02:10 +01:00
2020-10-30 20:23:20 +01:00
result = self . client_get ( " /activity/support " , { " q " : " King hamlet,lear " } )
check_hamlet_user_query_result ( result )
check_zulip_realm_query_result ( result )
check_lear_realm_query_result ( result )
result = self . client_get ( " /activity/support " , { " q " : " Othello, the Moor of Venice " } )
check_othello_user_query_result ( result )
check_zulip_realm_query_result ( result )
2019-03-08 13:02:10 +01:00
result = self . client_get ( " /activity/support " , { " q " : " lear, Hamlet <hamlet@zulip.com> " } )
2019-06-12 16:09:24 +02:00
check_hamlet_user_query_result ( result )
check_zulip_realm_query_result ( result )
check_lear_realm_query_result ( result )
2019-03-08 13:02:10 +01:00
2021-04-01 16:56:37 +02:00
with mock . patch (
" analytics.views.timezone_now " , return_value = timezone_now ( ) - timedelta ( minutes = 50 )
) :
self . client_post ( " /accounts/home/ " , { " email " : self . nonreg_email ( " test " ) } )
self . login ( " iago " )
result = self . client_get ( " /activity/support " , { " q " : self . nonreg_email ( " test " ) } )
check_preregistration_user_query_result ( result , self . nonreg_email ( " test " ) )
check_zulip_realm_query_result ( result )
2019-09-18 15:04:36 +02:00
2021-04-01 16:56:37 +02:00
stream_ids = [ self . get_stream_id ( " Denmark " ) ]
invitee_emails = [ self . nonreg_email ( " test1 " ) ]
self . client_post (
" /json/invites " ,
{
" invitee_emails " : invitee_emails ,
" stream_ids " : orjson . dumps ( stream_ids ) . decode ( ) ,
" invite_as " : PreregistrationUser . INVITE_AS [ " MEMBER " ] ,
} ,
)
result = self . client_get ( " /activity/support " , { " q " : self . nonreg_email ( " test1 " ) } )
check_preregistration_user_query_result ( result , self . nonreg_email ( " test1 " ) , invite = True )
check_zulip_realm_query_result ( result )
2019-09-18 15:04:36 +02:00
2021-04-01 16:56:37 +02:00
email = self . nonreg_email ( " alice " )
self . client_post ( " /new/ " , { " email " : email } )
result = self . client_get ( " /activity/support " , { " q " : email } )
check_realm_creation_query_result ( result , email )
2019-09-18 15:04:36 +02:00
2021-04-01 16:56:37 +02:00
do_create_multiuse_invite_link ( self . example_user ( " hamlet " ) , invited_as = 1 )
result = self . client_get ( " /activity/support " , { " q " : " zulip " } )
check_multiuse_invite_link_query_result ( result )
check_zulip_realm_query_result ( result )
MultiuseInvite . objects . all ( ) . delete ( )
2019-09-18 15:04:36 +02:00
2020-12-04 11:46:51 +01:00
do_send_realm_reactivation_email ( get_realm ( " zulip " ) , acting_user = None )
2021-04-01 16:56:37 +02:00
result = self . client_get ( " /activity/support " , { " q " : " zulip " } )
check_realm_reactivation_link_query_result ( result )
check_zulip_realm_query_result ( result )
2019-09-18 15:04:36 +02:00
2020-08-18 13:48:11 +02:00
@mock.patch ( " analytics.views.update_billing_method_of_current_plan " )
def test_change_billing_method ( self , m : mock . Mock ) - > None :
2021-02-12 08:20:45 +01:00
cordelia = self . example_user ( " cordelia " )
2020-08-18 13:48:11 +02:00
self . login_user ( cordelia )
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { cordelia . realm_id } " , " plan_type " : " 2 " }
)
2020-08-18 13:48:11 +02:00
self . assertEqual ( result . status_code , 302 )
self . assertEqual ( result [ " Location " ] , " /login/ " )
iago = self . example_user ( " iago " )
self . login_user ( iago )
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " ,
{ " realm_id " : f " { iago . realm_id } " , " billing_method " : " charge_automatically " } ,
)
2020-12-04 11:29:02 +01:00
m . assert_called_once_with ( get_realm ( " zulip " ) , charge_automatically = True , acting_user = iago )
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
[ " Billing method of zulip updated to charge automatically " ] , result
)
2020-08-18 13:48:11 +02:00
m . reset_mock ( )
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { iago . realm_id } " , " billing_method " : " send_invoice " }
)
2020-12-04 11:29:02 +01:00
m . assert_called_once_with ( get_realm ( " zulip " ) , charge_automatically = False , acting_user = iago )
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
[ " Billing method of zulip updated to pay by invoice " ] , result
)
2020-08-18 13:48:11 +02:00
2019-03-08 13:02:10 +01:00
def test_change_plan_type ( self ) - > None :
2021-02-12 08:20:45 +01:00
cordelia = self . example_user ( " cordelia " )
2020-03-06 18:40:46 +01:00
self . login_user ( cordelia )
2019-03-08 13:02:10 +01:00
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { cordelia . realm_id } " , " plan_type " : " 2 " }
)
2019-03-08 13:02:10 +01:00
self . assertEqual ( result . status_code , 302 )
self . assertEqual ( result [ " Location " ] , " /login/ " )
2019-07-24 07:22:48 +02:00
iago = self . example_user ( " iago " )
2020-03-06 18:40:46 +01:00
self . login_user ( iago )
2019-03-08 13:02:10 +01:00
with mock . patch ( " analytics.views.do_change_plan_type " ) as m :
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { iago . realm_id } " , " plan_type " : " 2 " }
)
2020-12-04 10:54:15 +01:00
m . assert_called_once_with ( get_realm ( " zulip " ) , 2 , acting_user = iago )
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
[ " Plan type of zulip changed from self hosted to limited " ] , result
)
2019-03-08 13:02:10 +01:00
def test_attach_discount ( self ) - > None :
2021-02-12 08:20:45 +01:00
cordelia = self . example_user ( " cordelia " )
lear_realm = get_realm ( " lear " )
2020-03-06 18:40:46 +01:00
self . login_user ( cordelia )
2019-03-08 13:02:10 +01:00
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { lear_realm . id } " , " discount " : " 25 " }
)
2019-03-08 13:02:10 +01:00
self . assertEqual ( result . status_code , 302 )
self . assertEqual ( result [ " Location " ] , " /login/ " )
2020-12-04 11:08:10 +01:00
iago = self . example_user ( " iago " )
2021-02-12 08:20:45 +01:00
self . login ( " iago " )
2019-03-08 13:02:10 +01:00
with mock . patch ( " analytics.views.attach_discount_to_realm " ) as m :
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { lear_realm . id } " , " discount " : " 25 " }
)
2020-12-04 11:08:10 +01:00
m . assert_called_once_with ( get_realm ( " lear " ) , 25 , acting_user = iago )
2020-12-04 15:16:40 +01:00
self . assert_in_success_response ( [ " Discount of lear changed to 25 % f rom 0 % " ] , result )
2019-03-08 13:02:10 +01:00
2020-06-09 12:24:32 +02:00
def test_change_sponsorship_status ( self ) - > None :
lear_realm = get_realm ( " lear " )
self . assertIsNone ( get_customer_by_realm ( lear_realm ) )
2021-02-12 08:20:45 +01:00
cordelia = self . example_user ( " cordelia " )
2020-06-09 12:24:32 +02:00
self . login_user ( cordelia )
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { lear_realm . id } " , " sponsorship_pending " : " true " }
)
2020-06-09 12:24:32 +02:00
self . assertEqual ( result . status_code , 302 )
self . assertEqual ( result [ " Location " ] , " /login/ " )
iago = self . example_user ( " iago " )
self . login_user ( iago )
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { lear_realm . id } " , " sponsorship_pending " : " true " }
)
2020-08-18 14:10:53 +02:00
self . assert_in_success_response ( [ " lear marked as pending sponsorship. " ] , result )
2020-06-09 12:24:32 +02:00
customer = get_customer_by_realm ( lear_realm )
2021-02-12 08:19:30 +01:00
assert customer is not None
2020-06-09 12:24:32 +02:00
self . assertTrue ( customer . sponsorship_pending )
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { lear_realm . id } " , " sponsorship_pending " : " false " }
)
2020-08-18 14:10:53 +02:00
self . assert_in_success_response ( [ " lear is no longer pending sponsorship. " ] , result )
2020-06-09 12:24:32 +02:00
customer = get_customer_by_realm ( lear_realm )
2021-02-12 08:19:30 +01:00
assert customer is not None
2020-06-09 12:24:32 +02:00
self . assertFalse ( customer . sponsorship_pending )
2020-07-17 12:56:06 +02:00
def test_approve_sponsorship ( self ) - > None :
lear_realm = get_realm ( " lear " )
2020-12-04 12:14:51 +01:00
update_sponsorship_status ( lear_realm , True , acting_user = None )
2020-07-17 12:56:06 +02:00
king_user = self . lear_user ( " king " )
king_user . role = UserProfile . ROLE_REALM_OWNER
king_user . save ( )
2021-02-12 08:20:45 +01:00
cordelia = self . example_user ( " cordelia " )
2020-07-17 12:56:06 +02:00
self . login_user ( cordelia )
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " ,
{ " realm_id " : f " { lear_realm . id } " , " approve_sponsorship " : " approve_sponsorship " } ,
)
2020-07-17 12:56:06 +02:00
self . assertEqual ( result . status_code , 302 )
self . assertEqual ( result [ " Location " ] , " /login/ " )
iago = self . example_user ( " iago " )
self . login_user ( iago )
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " ,
{ " realm_id " : f " { lear_realm . id } " , " approve_sponsorship " : " approve_sponsorship " } ,
)
2020-08-18 14:10:53 +02:00
self . assert_in_success_response ( [ " Sponsorship approved for lear " ] , result )
2020-07-17 12:56:06 +02:00
lear_realm . refresh_from_db ( )
self . assertEqual ( lear_realm . plan_type , Realm . STANDARD_FREE )
customer = get_customer_by_realm ( lear_realm )
2021-02-12 08:19:30 +01:00
assert customer is not None
2020-07-17 12:56:06 +02:00
self . assertFalse ( customer . sponsorship_pending )
messages = UserMessage . objects . filter ( user_profile = king_user )
2021-02-12 08:19:30 +01:00
self . assertIn (
" request for sponsored hosting has been approved " , messages [ 0 ] . message . content
)
2020-07-17 12:56:06 +02:00
self . assertEqual ( len ( messages ) , 1 )
2019-04-19 15:19:49 +02:00
def test_activate_or_deactivate_realm ( self ) - > None :
2021-02-12 08:20:45 +01:00
cordelia = self . example_user ( " cordelia " )
lear_realm = get_realm ( " lear " )
2020-03-06 18:40:46 +01:00
self . login_user ( cordelia )
2019-04-19 15:19:49 +02:00
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { lear_realm . id } " , " status " : " deactivated " }
)
2019-04-19 15:19:49 +02:00
self . assertEqual ( result . status_code , 302 )
self . assertEqual ( result [ " Location " ] , " /login/ " )
2021-02-12 08:20:45 +01:00
self . login ( " iago " )
2019-04-19 15:19:49 +02:00
with mock . patch ( " analytics.views.do_deactivate_realm " ) as m :
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { lear_realm . id } " , " status " : " deactivated " }
)
2021-04-02 17:11:25 +02:00
m . assert_called_once_with ( lear_realm , acting_user = self . example_user ( " iago " ) )
2020-08-18 14:10:53 +02:00
self . assert_in_success_response ( [ " lear deactivated " ] , result )
2019-04-19 15:19:49 +02:00
2019-11-17 09:51:46 +01:00
with mock . patch ( " analytics.views.do_send_realm_reactivation_email " ) as m :
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { lear_realm . id } " , " status " : " active " }
)
2020-12-04 11:46:51 +01:00
m . assert_called_once_with ( lear_realm , acting_user = self . example_user ( " iago " ) )
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
[ " Realm reactivation email sent to admins of lear " ] , result
)
2019-04-19 15:19:49 +02:00
2020-11-17 19:18:22 +01:00
def test_change_subdomain ( self ) - > None :
2021-02-12 08:20:45 +01:00
cordelia = self . example_user ( " cordelia " )
lear_realm = get_realm ( " lear " )
2020-11-17 19:18:22 +01:00
self . login_user ( cordelia )
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { lear_realm . id } " , " new_subdomain " : " new_name " }
)
2020-11-17 19:18:22 +01:00
self . assertEqual ( result . status_code , 302 )
self . assertEqual ( result [ " Location " ] , " /login/ " )
2021-02-12 08:20:45 +01:00
self . login ( " iago " )
2020-11-17 19:18:22 +01:00
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { lear_realm . id } " , " new_subdomain " : " new-name " }
)
2020-11-17 19:18:22 +01:00
self . assertEqual ( result . status_code , 302 )
self . assertEqual ( result [ " Location " ] , " /activity/support?q=new-name " )
realm_id = lear_realm . id
2021-02-12 08:20:45 +01:00
lear_realm = get_realm ( " new-name " )
2020-11-17 19:18:22 +01:00
self . assertEqual ( lear_realm . id , realm_id )
2021-02-12 08:20:45 +01:00
self . assertTrue ( Realm . objects . filter ( string_id = " lear " ) . exists ( ) )
self . assertTrue ( Realm . objects . filter ( string_id = " lear " ) [ 0 ] . deactivated )
2020-11-17 19:18:22 +01:00
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { lear_realm . id } " , " new_subdomain " : " new-name " }
)
self . assert_in_success_response (
[ " Subdomain unavailable. Please choose a different one. " ] , result
)
result = self . client_post (
" /activity/support " , { " realm_id " : f " { lear_realm . id } " , " new_subdomain " : " zulip " }
)
self . assert_in_success_response (
[ " Subdomain unavailable. Please choose a different one. " ] , result
)
result = self . client_post (
" /activity/support " , { " realm_id " : f " { lear_realm . id } " , " new_subdomain " : " lear " }
)
self . assert_in_success_response (
[ " Subdomain unavailable. Please choose a different one. " ] , result
)
2020-12-18 20:17:20 +01:00
2020-08-13 13:20:18 +02:00
def test_downgrade_realm ( self ) - > None :
2021-02-12 08:20:45 +01:00
cordelia = self . example_user ( " cordelia " )
2020-08-13 13:20:18 +02:00
self . login_user ( cordelia )
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { cordelia . realm_id } " , " plan_type " : " 2 " }
)
2020-08-13 13:20:18 +02:00
self . assertEqual ( result . status_code , 302 )
self . assertEqual ( result [ " Location " ] , " /login/ " )
iago = self . example_user ( " iago " )
self . login_user ( iago )
with mock . patch ( " analytics.views.downgrade_at_the_end_of_billing_cycle " ) as m :
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " ,
{
" realm_id " : f " { iago . realm_id } " ,
" downgrade_method " : " downgrade_at_billing_cycle_end " ,
} ,
)
2020-08-13 13:20:18 +02:00
m . assert_called_once_with ( get_realm ( " zulip " ) )
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
[ " zulip marked for downgrade at the end of billing cycle " ] , result
)
2020-08-13 13:20:18 +02:00
with mock . patch ( " analytics.views.downgrade_now_without_creating_additional_invoices " ) as m :
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " ,
{
" realm_id " : f " { iago . realm_id } " ,
" downgrade_method " : " downgrade_now_without_additional_licenses " ,
} ,
)
2020-08-13 13:20:18 +02:00
m . assert_called_once_with ( get_realm ( " zulip " ) )
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
[ " zulip downgraded without creating additional invoices " ] , result
)
2020-08-13 13:20:18 +02:00
with mock . patch ( " analytics.views.downgrade_now_without_creating_additional_invoices " ) as m1 :
with mock . patch ( " analytics.views.void_all_open_invoices " , return_value = 1 ) as m2 :
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " ,
{
" realm_id " : f " { iago . realm_id } " ,
" downgrade_method " : " downgrade_now_void_open_invoices " ,
} ,
)
2020-08-13 13:20:18 +02:00
m1 . assert_called_once_with ( get_realm ( " zulip " ) )
m2 . assert_called_once_with ( get_realm ( " zulip " ) )
2021-02-12 08:19:30 +01:00
self . assert_in_success_response (
[ " zulip downgraded and voided 1 open invoices " ] , result
)
2020-08-13 13:20:18 +02:00
2019-04-19 18:17:41 +02:00
def test_scrub_realm ( self ) - > None :
2021-02-12 08:20:45 +01:00
cordelia = self . example_user ( " cordelia " )
lear_realm = get_realm ( " lear " )
2020-03-06 18:40:46 +01:00
self . login_user ( cordelia )
2019-04-19 18:17:41 +02:00
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { lear_realm . id } " , " discount " : " 25 " }
)
2019-04-19 18:17:41 +02:00
self . assertEqual ( result . status_code , 302 )
self . assertEqual ( result [ " Location " ] , " /login/ " )
2021-02-12 08:20:45 +01:00
self . login ( " iago " )
2019-04-19 18:17:41 +02:00
with mock . patch ( " analytics.views.do_scrub_realm " ) as m :
2021-02-12 08:19:30 +01:00
result = self . client_post (
" /activity/support " , { " realm_id " : f " { lear_realm . id } " , " scrub_realm " : " scrub_realm " }
)
2020-06-29 12:28:21 +02:00
m . assert_called_once_with ( lear_realm , acting_user = self . example_user ( " iago " ) )
2020-08-18 14:10:53 +02:00
self . assert_in_success_response ( [ " lear scrubbed " ] , result )
2019-04-19 18:17:41 +02:00
with mock . patch ( " analytics.views.do_scrub_realm " ) as m :
2020-07-27 20:21:41 +02:00
result = self . client_post ( " /activity/support " , { " realm_id " : f " { lear_realm . id } " } )
self . assert_json_error ( result , " Invalid parameters " )
2019-04-19 18:17:41 +02:00
m . assert_not_called ( )
2021-02-12 08:19:30 +01:00
2017-02-10 21:52:14 +01:00
class TestGetChartDataHelpers ( ZulipTestCase ) :
2017-11-05 06:54:00 +01:00
def test_sort_by_totals ( self ) - > None :
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
empty : List [ int ] = [ ]
2021-02-12 08:20:45 +01:00
value_arrays = { " c " : [ 0 , 1 ] , " a " : [ 9 ] , " b " : [ 1 , 1 , 1 ] , " d " : empty }
self . assertEqual ( sort_by_totals ( value_arrays ) , [ " a " , " b " , " c " , " d " ] )
2017-02-10 21:52:14 +01:00
2017-11-05 06:54:00 +01:00
def test_sort_client_labels ( self ) - > None :
2021-02-12 08:19:30 +01:00
data = {
2021-02-12 08:20:45 +01:00
" everyone " : { " a " : [ 16 ] , " c " : [ 15 ] , " b " : [ 14 ] , " e " : [ 13 ] , " d " : [ 12 ] , " h " : [ 11 ] } ,
" user " : { " a " : [ 6 ] , " b " : [ 5 ] , " d " : [ 4 ] , " e " : [ 3 ] , " f " : [ 2 ] , " g " : [ 1 ] } ,
2021-02-12 08:19:30 +01:00
}
2021-02-12 08:20:45 +01:00
self . assertEqual ( sort_client_labels ( data ) , [ " a " , " b " , " c " , " d " , " e " , " f " , " g " , " h " ] )
2016-12-20 02:30:08 +01:00
2021-02-12 08:19:30 +01:00
2016-12-20 02:30:08 +01:00
class TestTimeRange ( ZulipTestCase ) :
2017-11-05 06:54:00 +01:00
def test_time_range ( self ) - > None :
2016-12-20 02:30:08 +01:00
HOUR = timedelta ( hours = 1 )
DAY = timedelta ( days = 1 )
2020-06-05 06:55:20 +02:00
a_time = datetime ( 2016 , 3 , 14 , 22 , 59 , tzinfo = timezone . utc )
floor_hour = datetime ( 2016 , 3 , 14 , 22 , tzinfo = timezone . utc )
floor_day = datetime ( 2016 , 3 , 14 , tzinfo = timezone . utc )
2016-12-20 02:30:08 +01:00
# test start == end
2017-02-02 01:29:58 +01:00
self . assertEqual ( time_range ( a_time , a_time , CountStat . HOUR , None ) , [ ] )
self . assertEqual ( time_range ( a_time , a_time , CountStat . DAY , None ) , [ ] )
2016-12-20 02:30:08 +01:00
# test start == end == boundary, and min_length == 0
2017-02-02 01:29:58 +01:00
self . assertEqual ( time_range ( floor_hour , floor_hour , CountStat . HOUR , 0 ) , [ floor_hour ] )
self . assertEqual ( time_range ( floor_day , floor_day , CountStat . DAY , 0 ) , [ floor_day ] )
2016-12-20 02:30:08 +01:00
# test start and end on different boundaries
2021-02-12 08:19:30 +01:00
self . assertEqual (
time_range ( floor_hour , floor_hour + HOUR , CountStat . HOUR , None ) ,
[ floor_hour , floor_hour + HOUR ] ,
)
self . assertEqual (
time_range ( floor_day , floor_day + DAY , CountStat . DAY , None ) ,
[ floor_day , floor_day + DAY ] ,
)
2016-12-20 02:30:08 +01:00
# test min_length
2021-02-12 08:19:30 +01:00
self . assertEqual (
time_range ( floor_hour , floor_hour + HOUR , CountStat . HOUR , 4 ) ,
[ floor_hour - 2 * HOUR , floor_hour - HOUR , floor_hour , floor_hour + HOUR ] ,
)
self . assertEqual (
time_range ( floor_day , floor_day + DAY , CountStat . DAY , 4 ) ,
[ floor_day - 2 * DAY , floor_day - DAY , floor_day , floor_day + DAY ] ,
)
2017-02-06 01:17:31 +01:00
class TestMapArrays ( ZulipTestCase ) :
2017-11-05 06:54:00 +01:00
def test_map_arrays ( self ) - > None :
2021-02-12 08:19:30 +01:00
a = {
2021-02-12 08:20:45 +01:00
" desktop app 1.0 " : [ 1 , 2 , 3 ] ,
" desktop app 2.0 " : [ 10 , 12 , 13 ] ,
" desktop app 3.0 " : [ 21 , 22 , 23 ] ,
" website " : [ 1 , 2 , 3 ] ,
" ZulipiOS " : [ 1 , 2 , 3 ] ,
" ZulipElectron " : [ 2 , 5 , 7 ] ,
" ZulipMobile " : [ 1 , 5 , 7 ] ,
" ZulipPython " : [ 1 , 2 , 3 ] ,
" API: Python " : [ 1 , 2 , 3 ] ,
" SomethingRandom " : [ 4 , 5 , 6 ] ,
" ZulipGitHubWebhook " : [ 7 , 7 , 9 ] ,
" ZulipAndroid " : [ 64 , 63 , 65 ] ,
2021-02-12 08:19:30 +01:00
}
2017-02-06 01:17:31 +01:00
result = rewrite_client_arrays ( a )
2021-02-12 08:19:30 +01:00
self . assertEqual (
result ,
{
2021-02-12 08:20:45 +01:00
" Old desktop app " : [ 32 , 36 , 39 ] ,
" Old iOS app " : [ 1 , 2 , 3 ] ,
" Desktop app " : [ 2 , 5 , 7 ] ,
" Mobile app " : [ 1 , 5 , 7 ] ,
" Website " : [ 1 , 2 , 3 ] ,
" Python API " : [ 2 , 4 , 6 ] ,
" SomethingRandom " : [ 4 , 5 , 6 ] ,
" GitHub webhook " : [ 7 , 7 , 9 ] ,
" Old Android app " : [ 64 , 63 , 65 ] ,
2021-02-12 08:19:30 +01:00
} ,
)