def test_can_get_correct_permission(self): """Test correct permission can be returend.""" room = Room.objects.create(title='Test Chat', slug='test-chat', author=self.user) user0 = UserFactory.build(username='******') user1 = UserFactory.build(username='******') user2 = UserFactory.build(username='******') user3 = UserFactory.build(username='******') user0.save() user1.save() user2.save() user3.save() room.add_member(user0, permission=Role.ADMIN) room.add_member(user1, permission=Role.MEMBER) room.add_member(user2, permission=Role.VIEWER) self.assertTrue(room.is_viewable(user0), 'Admin user can view the room') self.assertTrue(room.is_viewable(user1), 'Member user can view the room') self.assertTrue(room.is_viewable(user2), 'Viewer user can view the room') self.assertFalse(room.is_viewable(user3), 'Not member can not view the room') self.assertTrue(room.is_writable(user0), 'Admin user can write to the room') self.assertTrue(room.is_writable(user1), 'Member user can write to the room') self.assertFalse(room.is_writable(user2), 'Viewer user can not write to the room') self.assertFalse(room.is_writable(user3), 'Not member can not write to the room') self.assertTrue(room.is_administrable(user0), 'Admin user can manage the room') self.assertFalse(room.is_administrable(user1), 'Member user can not manage the room') self.assertFalse(room.is_administrable(user2), 'Viewer user can not manage the room') self.assertFalse(room.is_administrable(user3), 'Not member can not manage the room')
def test_can_add_member(self): """Test user can be added""" room = Room.objects.create(title='Test Chat', slug='test-chat', author=self.user) user1 = UserFactory.build(username='******') user1.save() self.assertTrue(room.add_member(user1), 'add_member returns true') user2 = UserFactory.build(username='******') user2.save() self.assertTrue(room.add_member(user2, permission=Role.VIEWER), 'add_member returns true') self.assertEqual(room.members.count(), 3, 'the count of members is correct') role = Role.objects.get(room=room, user=user1) self.assertEqual(role.permission, Role.MEMBER, 'the default permission is correct') role1 = Role.objects.get(room=room, user=user2) self.assertEqual(role1.permission, Role.VIEWER, 'the member of permission is correct')
def test_can_get_suitable_administrators(self): """Test administrators property can get suitable users""" room = Room.objects.create(title='Test Chat', slug='test-chat', author=self.user) user1 = UserFactory.build(username='******') user2 = UserFactory.build(username='******') user3 = UserFactory.build(username='******') user1.save() user2.save() user3.save() room.add_member(user1, permission=Role.ADMIN) room.add_member(user2, permission=Role.VIEWER) room.add_member(user3, permission=Role.MEMBER) self.assertEqual(room.administrators.count(), 2, 'the count of administrators is correct') self.assertEqual(room.administrators[0].username, 'kawaztan') self.assertEqual(room.administrators[1].username, 'mario')
def test_can_not_add_multiple_members(self): """Test multiple members can not be added to the room.""" room = Room.objects.create(title='Test Chat', slug='test-chat', author=self.user) user1 = UserFactory.build(username='******') user1.save() self.assertTrue(room.add_member(user1), 'add_member returns true') self.assertFalse(room.add_member(user1), 'add_member returns true')
def test_can_remove_member(self): room = Room.objects.create(title='Test Chat', slug='test-chat', author=self.user) user1 = UserFactory.build(username='******') user1.save() room.add_member(user1) self.assertTrue(room.remove_member(user1), 'remove_member returns false') self.assertEqual(room.members.count(), 1, 'the count of members is correct')
def test_has_member(self): """Test is_member returns correct value""" room = Room.objects.create(title='Test Chat', slug='test-chat', author=self.user) user1 = UserFactory.build(username='******') user1.save() self.assertFalse(room.is_member(user1), 'is_member returns false') room.add_member(user1) self.assertTrue(room.is_member(user1), 'is_member returns true')
def test_is_joinable_returns_correct_value(self): """Test is_joinable returns correct value""" room = Room.objects.create(title='Test Chat', slug='test-chat', author=self.user) user0 = UserFactory.build(username='******') self.assertTrue(room.is_joinable(user0), 'Everyone can join to a public room') room1 = RoomFactory.build(scope='private') self.assertFalse(room1.is_joinable(user0), 'Anyone can not join to a public room')
def test_can_not_remove_non_member(self): """Test the member who not join to the room can not be removed""" room = Room.objects.create(title='Test Chat', slug='test-chat', author=self.user) user1 = UserFactory.build(username='******') user1.save() self.assertFalse(room.remove_member(user1), 'remove_member returns false')
def test_raise_error_when_create_multiple_role(self): """Test the error is raised when multiple roles are created""" room = Room.objects.create(title='Test Chat', slug='test-chat', author=self.user) user1 = UserFactory.build(username='******') user1.save() role = Role.objects.create(room=room, user=user1) def create_role_multiple(): Role.objects.create(room=room, user=user1, permission=Role.ADMIN) self.assertRaises(IntegrityError, create_role_multiple)