user-groups: Generate test data of user groups.

This commit is contained in:
Umair Khan 2017-11-13 11:49:01 +05:00
parent 50eb3be0c2
commit ad8f5650a3
2 changed files with 36 additions and 25 deletions

View File

@ -28,24 +28,25 @@ class UserGroupTestCase(ZulipTestCase):
def test_user_groups_in_realm(self): def test_user_groups_in_realm(self):
# type: () -> None # type: () -> None
realm = get_realm('zulip') realm = get_realm('zulip')
self.assertEqual(len(user_groups_in_realm(realm)), 0) self.assertEqual(len(user_groups_in_realm(realm)), 1)
self.create_user_group_for_test('support') self.create_user_group_for_test('support')
user_groups = user_groups_in_realm(realm) user_groups = user_groups_in_realm(realm)
self.assertEqual(len(user_groups), 1) self.assertEqual(len(user_groups), 2)
self.assertEqual(user_groups[0].name, 'support') names = set([ug.name for ug in user_groups])
self.assertEqual(names, set(['hamletcharacters', 'support']))
def test_user_groups_in_realm_serialized(self): def test_user_groups_in_realm_serialized(self):
# type: () -> None # type: () -> None
realm = get_realm('zulip') realm = get_realm('zulip')
user_group = self.create_user_group_for_test('support') user_group = UserGroup.objects.first()
hamlet = self.example_user('hamlet') membership = UserGroupMembership.objects.filter(user_group=user_group)
check_add_user_to_user_group(hamlet, user_group) membership = membership.values_list('user_profile_id', flat=True)
members = user_groups_in_realm_serialized(realm) user_groups = user_groups_in_realm_serialized(realm)
self.assertEqual(len(members), 1) self.assertEqual(len(user_groups), 1)
self.assertEqual(members[0]['id'], user_group.id) self.assertEqual(user_groups[0]['id'], user_group.id)
self.assertEqual(members[0]['name'], user_group.name) self.assertEqual(user_groups[0]['name'], 'hamletcharacters')
self.assertEqual(members[0]['description'], '') self.assertEqual(user_groups[0]['description'], 'Characters of Hamlet')
self.assertEqual(members[0]['members'], [4, 6]) self.assertEqual(set(user_groups[0]['members']), set(membership))
def test_get_user_groups(self): def test_get_user_groups(self):
# type: () -> None # type: () -> None
@ -87,7 +88,7 @@ class UserGroupAPITestCase(ZulipTestCase):
} }
result = self.client_post('/json/user_groups/create', info=params) result = self.client_post('/json/user_groups/create', info=params)
self.assert_json_success(result) self.assert_json_success(result)
self.assert_length(UserGroup.objects.all(), 1) self.assert_length(UserGroup.objects.all(), 2)
# Test invalid member error # Test invalid member error
params = { params = {
@ -97,7 +98,7 @@ class UserGroupAPITestCase(ZulipTestCase):
} }
result = self.client_post('/json/user_groups/create', info=params) result = self.client_post('/json/user_groups/create', info=params)
self.assert_json_error(result, "Invalid user ID: 1111") self.assert_json_error(result, "Invalid user ID: 1111")
self.assert_length(UserGroup.objects.all(), 1) self.assert_length(UserGroup.objects.all(), 2)
# Test we cannot add hamlet again # Test we cannot add hamlet again
params = { params = {
@ -107,7 +108,7 @@ class UserGroupAPITestCase(ZulipTestCase):
} }
result = self.client_post('/json/user_groups/create', info=params) result = self.client_post('/json/user_groups/create', info=params)
self.assert_json_error(result, "User group 'support' already exists.") self.assert_json_error(result, "User group 'support' already exists.")
self.assert_length(UserGroup.objects.all(), 1) self.assert_length(UserGroup.objects.all(), 2)
def test_user_group_update(self): def test_user_group_update(self):
# type: () -> None # type: () -> None
@ -150,15 +151,15 @@ class UserGroupAPITestCase(ZulipTestCase):
'description': 'Support team', 'description': 'Support team',
} }
self.client_post('/json/user_groups/create', info=params) self.client_post('/json/user_groups/create', info=params)
user_group = UserGroup.objects.first() user_group = UserGroup.objects.get(name='support')
# Test success # Test success
self.assertEqual(UserGroup.objects.count(), 1) self.assertEqual(UserGroup.objects.count(), 2)
self.assertEqual(UserGroupMembership.objects.count(), 1) self.assertEqual(UserGroupMembership.objects.count(), 3)
result = self.client_delete('/json/user_groups/{}'.format(user_group.id)) result = self.client_delete('/json/user_groups/{}'.format(user_group.id))
self.assert_json_success(result) self.assert_json_success(result)
self.assertEqual(UserGroup.objects.count(), 0) self.assertEqual(UserGroup.objects.count(), 1)
self.assertEqual(UserGroupMembership.objects.count(), 0) self.assertEqual(UserGroupMembership.objects.count(), 2)
# Test when invalid user group is supplied # Test when invalid user group is supplied
result = self.client_delete('/json/user_groups/1111') result = self.client_delete('/json/user_groups/1111')
@ -177,14 +178,14 @@ class UserGroupAPITestCase(ZulipTestCase):
user_group = UserGroup.objects.first() user_group = UserGroup.objects.first()
# Test add members # Test add members
self.assertEqual(UserGroupMembership.objects.count(), 1) self.assertEqual(UserGroupMembership.objects.count(), 3)
othello = self.example_user('othello') othello = self.example_user('othello')
add = [othello.id] add = [othello.id]
params = {'add': ujson.dumps(add)} params = {'add': ujson.dumps(add)}
result = self.client_post('/json/user_groups/{}/members'.format(user_group.id), result = self.client_post('/json/user_groups/{}/members'.format(user_group.id),
info=params) info=params)
self.assert_json_success(result) self.assert_json_success(result)
self.assertEqual(UserGroupMembership.objects.count(), 2) self.assertEqual(UserGroupMembership.objects.count(), 4)
members = get_memberships_of_users(user_group, [hamlet, othello]) members = get_memberships_of_users(user_group, [hamlet, othello])
self.assertEqual(len(members), 2) self.assertEqual(len(members), 2)
@ -192,7 +193,7 @@ class UserGroupAPITestCase(ZulipTestCase):
result = self.client_post('/json/user_groups/{}/members'.format(user_group.id), result = self.client_post('/json/user_groups/{}/members'.format(user_group.id),
info=params) info=params)
self.assert_json_error(result, "User 6 is already a member of this group") self.assert_json_error(result, "User 6 is already a member of this group")
self.assertEqual(UserGroupMembership.objects.count(), 2) self.assertEqual(UserGroupMembership.objects.count(), 4)
members = get_memberships_of_users(user_group, [hamlet, othello]) members = get_memberships_of_users(user_group, [hamlet, othello])
self.assertEqual(len(members), 2) self.assertEqual(len(members), 2)
@ -201,7 +202,7 @@ class UserGroupAPITestCase(ZulipTestCase):
result = self.client_post('/json/user_groups/{}/members'.format(user_group.id), result = self.client_post('/json/user_groups/{}/members'.format(user_group.id),
info=params) info=params)
self.assert_json_success(result) self.assert_json_success(result)
self.assertEqual(UserGroupMembership.objects.count(), 0) self.assertEqual(UserGroupMembership.objects.count(), 2)
members = get_memberships_of_users(user_group, [hamlet, othello]) members = get_memberships_of_users(user_group, [hamlet, othello])
self.assertEqual(len(members), 0) self.assertEqual(len(members), 0)
@ -210,7 +211,7 @@ class UserGroupAPITestCase(ZulipTestCase):
result = self.client_post('/json/user_groups/{}/members'.format(user_group.id), result = self.client_post('/json/user_groups/{}/members'.format(user_group.id),
info=params) info=params)
self.assert_json_success(result) self.assert_json_success(result)
self.assertEqual(UserGroupMembership.objects.count(), 0) self.assertEqual(UserGroupMembership.objects.count(), 2)
members = get_memberships_of_users(user_group, [hamlet, othello]) members = get_memberships_of_users(user_group, [hamlet, othello])
self.assertEqual(len(members), 0) self.assertEqual(len(members), 0)

View File

@ -14,6 +14,7 @@ from django.conf import settings
from zerver.lib.bulk_create import bulk_create_streams, bulk_create_users from zerver.lib.bulk_create import bulk_create_streams, bulk_create_users
from zerver.lib.generate_test_data import create_test_data from zerver.lib.generate_test_data import create_test_data
from zerver.lib.upload import upload_backend from zerver.lib.upload import upload_backend
from zerver.lib.user_groups import create_user_group
import random import random
@ -393,6 +394,7 @@ class Command(BaseCommand):
UserProfile.objects.filter(id=user['user_profile_id']).update( UserProfile.objects.filter(id=user['user_profile_id']).update(
pointer=user['pointer']) pointer=user['pointer'])
create_user_groups()
self.stdout.write("Successfully populated test database.\n") self.stdout.write("Successfully populated test database.\n")
recipient_hash = {} # type: Dict[int, Recipient] recipient_hash = {} # type: Dict[int, Recipient]
@ -512,3 +514,11 @@ def create_user_presences(user_profiles):
client=client, client=client,
timestamp=date, timestamp=date,
status=status) status=status)
def create_user_groups():
# type: () -> None
zulip = get_realm('zulip')
members = [get_user('cordelia@zulip.com', zulip),
get_user('hamlet@zulip.com', zulip)]
create_user_group("hamletcharacters", members, zulip,
description="Characters of Hamlet")