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_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_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_can_create_message(self): """Tests Message model returns correct attributes""" message = MessageFactory.create(body=u'nyanpass-') author = UserFactory(screen_name='renge') room = MessageFactory.attributes()['room'] self.assertEqual(message.body, 'nyanpass-', 'body can be returned') self.assertEqual(message.author, author, 'author can be returned') self.assertEqual(message.room, room, 'room can be returned')
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)
def test_can_create_user(self): """Tests Message model returns correct attributes""" user = UserFactory.create(twitter='@kawaztan', screen_name='Kawaz tan', username='******') self.assertEqual(user.username, 'kawaztan', 'username can be returned') self.assertEqual(user.screen_name, 'Kawaz tan', 'screen_name can be returned') self.assertEqual(user.twitter, '@kawaztan', 'twitter can be returned')
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_serialize(self): """Test serialize() returns correct dict""" user = UserFactory.create(twitter='@kawaztan', screen_name='Kawaz tan', username='******') self.assertDictEqual(user.serialize(), { 'pk': user.pk, 'username': '******', 'screen_name': 'Kawaz tan', 'twitter': '@kawaztan', 'avatar': '' }, 'serialize() returns correct dict')
def test_serialize(self): """Test serialize() returns correct dict""" user = UserFactory.create(twitter='@kawaztan', screen_name='Kawaz tan', username='******') self.assertDictEqual( user.serialize(), { 'pk': user.pk, 'username': '******', 'screen_name': 'Kawaz tan', 'twitter': '@kawaztan', 'avatar': '' }, 'serialize() returns correct dict')
def test_unicode(self): """Test __unicode__() returns correct value""" message = UserFactory.create(username='******', screen_name='Kawaz tan') self.assertEqual(unicode(message), 'Kawaz tan(kawaztan)', '__unicode__() returns correct value')
def test_unicode(self): """Test __unicode__() returns correct value""" message = MessageFactory.create(body='toxuttoxuru-', author=UserFactory.create(screen_name='mayuri'), room=RoomFactory.create(title='lab chat')) self.assertEqual(unicode(message), 'toxuttoxuru- (mayuri) at lab chat', '__unicode__() returns correct value')
def setUp(self): self.user = UserFactory.create() self.user.save()