class ChatModelTestCase(unittest.TestCase): def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() database.create_all() Role.insert_roles() self.bob = User(username='******', password='******', email='*****@*****.**', confirmed=True) self.arthur = User(username='******', password='******', email='*****@*****.**', confirmed=True) self.clair = User(username='******', password='******', email='*****@*****.**', confirmed=True) self.morgana = User(username='******', password='******', email='*****@*****.**', confirmed=True) self.ophelia = User(username='******', password='******', email='*****@*****.**', confirmed=True) self.artorias = User(username='******', password='******', email='*****@*****.**', confirmed=True) self.chat_bob_arthur = Chat() self.chat_bob_artorias = Chat() self.chat_bob_clair = Chat() self.chat_morgana_arthur = Chat() self.chat_morgana_bob = Chat(name='chat_morgana_bob_') self.chat_bob_arthur.add_users([self.bob, self.arthur]) self.chat_bob_artorias.add_users([self.bob, self.artorias]) self.chat_bob_clair.add_users([self.bob, self.clair]) self.chat_morgana_arthur.add_users([self.arthur, self.morgana]) self.chat_morgana_bob.add_users([self.bob, self.morgana]) database.session.add_all([ self.bob, self.arthur, self.ophelia, self.artorias, self.clair, self.morgana, self.chat_bob_arthur, self.chat_bob_artorias, self.chat_bob_clair, self.chat_morgana_arthur, self.chat_morgana_bob ]) database.session.commit() def tearDown(self): database.session.remove() database.drop_all() self.app_context.pop() def test_add_users(self): chat = Chat() chat.add_users([self.bob, self.arthur]) self.assertIn(self.bob, chat.users.all()) self.assertIn(self.arthur, chat.users.all()) self.assertEqual(chat.users.count(), 2) def test_remove_users(self): chat = Chat() chat.add_users([self.bob, self.clair, self.morgana]) self.assertEqual(chat.users.count(), 3) self.assertIn(self.bob, chat.users.all()) chat.remove_users([self.bob]) self.assertEqual(chat.users.count(), 2) self.assertNotIn(self.bob, chat.users.all()) chat.remove_users([self.morgana, self.clair]) self.assertEqual(chat.users.count(), 0) def test_get_chat(self): self.assertEqual(Chat.get_chat([self.bob, self.arthur]), self.chat_bob_arthur) self.assertEqual(Chat.get_chat([self.clair, self.bob]), self.chat_bob_clair) self.assertEqual(Chat.get_chat([self.arthur, self.morgana]), self.chat_morgana_arthur) def test_get_name(self): self.assertEqual(self.chat_morgana_bob.get_name(self.bob), self.chat_morgana_bob.name) self.assertEqual(self.chat_morgana_bob.get_name(self.morgana), self.chat_morgana_bob.name) self.assertEqual(self.chat_bob_arthur.get_name(self.bob), self.arthur.username) self.assertEqual(self.chat_bob_arthur.get_name(self.arthur), self.bob.username) def test_search_chats_query(self): chats = Chat.search_chats_query('mor', self.bob).all() self.assertIn(self.chat_morgana_bob, chats) self.assertEqual(len(chats), 1) self.chat_morgana_bob.name = 'chat' chats = Chat.search_chats_query('chat', self.bob).all() self.assertIn(self.chat_morgana_bob, chats) self.assertEqual(len(chats), 1) chats = Chat.search_chats_query('wrong', self.bob).all() self.assertEqual(len(chats), 0) chats = Chat.search_chats_query('art', self.bob).all() self.assertIn(self.chat_bob_arthur, chats) self.assertIn(self.chat_bob_artorias, chats) self.assertEqual(len(chats), 2) def test_from_json(self): json_chat = {'chat_name': None, 'users': ['morgana']} chat = Chat.from_json(json_chat, self.clair) self.assertEqual(chat.name, None) self.assertEqual(chat.get_name(self.clair), self.morgana.username) self.assertEqual(chat.get_name(self.morgana), self.clair.username) self.assertIn(self.clair, chat.users.all()) self.assertIn(self.morgana, chat.users.all()) json_chat = {'chat_name': None, 'users': ['morgana', 'bob']} with self.assertRaises(ValidationError): Chat.from_json(json_chat, self.clair) def test_to_json(self): chat = self.chat_bob_arthur self.app.config['SERVER_NAME'] = 'localhost' with self.app.app_context(): json_chat = chat.to_json(self.bob) self.assertEqual( json_chat, { 'chat_name': self.arthur.username, 'is_group_chat': chat.is_group_chat, 'date_created': chat.date_created, 'date_modified': chat.date_modified, 'messages': url_for( 'api.get_messages', chat_id=chat.id, _external=True) }) del self.app.config['SERVER_NAME']
class UserModelTestCase(unittest.TestCase): def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() database.create_all() Role.insert_roles() self.bob = User(username='******', password='******', email='*****@*****.**', confirmed=True) self.arthur = User(username='******', password='******', email='*****@*****.**', confirmed=True) self.clair = User(username='******', password='******', email='*****@*****.**', confirmed=True) self.morgana = User(username='******', password='******', email='*****@*****.**', confirmed=True) self.ophelia = User(username='******', password='******', email='*****@*****.**', confirmed=True) self.chat_bob_arthur = Chat() self.chat_bob_clair = Chat() self.chat_morgana_arthur = Chat() self.chat_morgana_bob = Chat() self.chat_bob_arthur.add_users([self.bob, self.arthur]) self.chat_bob_clair.add_users([self.bob, self.clair]) self.chat_morgana_arthur.add_users([self.arthur, self.morgana]) self.chat_morgana_bob.add_users([self.bob, self.morgana]) self.bob.add_contacts([self.clair]) self.bob.add_contacts([self.morgana]) database.session.add_all([ self.bob, self.arthur, self.ophelia, self.clair, self.morgana, self.chat_bob_arthur, self.chat_bob_clair, self.chat_morgana_arthur, self.chat_morgana_bob ]) database.session.commit() def tearDown(self): database.session.remove() database.drop_all() self.app_context.pop() def test_get_chat_query(self): query = self.bob.get_chat_query( [self.clair.id, self.ophelia.id, self.arthur.id]) self.assertEqual(query.all(), [self.chat_bob_arthur, self.chat_bob_clair]) def test_get_removed_chats_query(self): self.bob.mark_chats_as_removed([self.chat_bob_arthur]) self.bob.mark_chats_as_removed([self.chat_morgana_bob]) self.assertEqual(RemovedChat.query.count(), 2) removed = self.bob.get_removed_chats_query( [self.bob.id, self.clair.id, self.arthur.id, self.morgana.id]) self.assertEqual(set(removed.all()), set([self.chat_bob_arthur, self.chat_morgana_bob])) def test_mark_chats_as_removed(self): self.bob.mark_chats_as_removed([self.chat_bob_arthur]) self.assertEqual(RemovedChat.query.count(), 1) self.assertEqual(RemovedChat.query.first().chat, self.chat_bob_arthur) self.assertEqual(RemovedChat.query.first().user, self.bob) def test_get_removed_query(self): self.bob.mark_chats_as_removed( [self.chat_bob_clair, self.chat_morgana_bob]) query = self.bob.get_removed_query() self.assertIn((RemovedChat.query.filter_by( chat_id=self.chat_bob_clair.id)).first(), query.all()) self.assertIn((RemovedChat.query.filter_by( chat_id=self.chat_morgana_bob.id)).first(), query.all()) self.assertEqual(query.count(), 2) def test_expired_confirmation_token(self): user = User(password='******', email='*****@*****.**', username='******') database.session.add(user) database.session.commit() token = user.generate_confirmation_token(1) time.sleep(2) self.assertFalse(user.confirm(token)) def test_password_setter(self): self.assertTrue(self.bob.password_hash is not None) def test_no_password_getter(self): user = User(username='******', email='*****@*****.**', password='******') with self.assertRaises(AttributeError): user.password def test_password_verification(self): self.assertTrue(self.bob.verify_password('bob')) self.assertFalse(self.bob.verify_password('wrong')) def test_passwords_salts_are_random(self): self.assertTrue(self.bob.password_hash != self.clair.password_hash) def test_add_contacts(self): self.assertEqual(self.clair, (self.bob.contacts.filter_by( contact_id=self.clair.id).first().contact)) self.assertTrue(self.bob.has_contact(self.clair)) self.assertFalse(self.clair.has_contact(self.bob)) self.assertTrue(self.clair.is_contacted_by(self.bob)) self.assertTrue(self.bob.contacts.count() == 2) self.assertTrue(self.clair.contacted.count() == 1) self.assertTrue(Contact.query.count() == 2) def test_has_contact(self): self.assertTrue(self.bob.has_contact(self.clair)) self.assertFalse(self.bob.has_contact(self.arthur)) def test_is_contacted_by(self): self.assertTrue(self.clair.is_contacted_by(self.bob)) self.assertFalse(self.arthur.is_contacted_by(self.bob)) def test_delete_contacts(self): self.bob.delete_contacts([self.clair]) database.session.commit() self.assertTrue(Contact.query.count() == 1) self.assertTrue(self.bob.contacts.count() == 1) self.assertTrue(self.clair.contacted.count() == 0) self.assertFalse(self.bob.has_contact(self.clair)) self.assertFalse(self.clair.is_contacted_by(self.bob)) def test_confirm_user(self): user1 = User(username='******', email='*****@*****.**', password='******') user2 = User(username='******', email='*****@*****.**', password='******') user1.id = 1 user2.id = 2 token1 = user1.generate_confirmation_token() token2 = user2.generate_confirmation_token() self.assertFalse(user1.confirm(token2)) self.assertTrue(user1.confirm(token1)) def test_roles(self): Role.insert_roles() user = User(username='******', email='*****@*****.**', password='******') admin = User(username='******', email=current_app.config['ADMIN_MAIL'], password='******') self.assertEqual(user.role, Role.query.filter_by(is_default=True).first()) self.assertEqual(user.role, Role.query.filter_by(name='User').first()) self.assertEqual(admin.role, Role.query.filter_by(name='Admin').first()) def test_permissions(self): Role.insert_roles() user = User(username='******', email='*****@*****.**', password='******') admin = User(username='******', email=current_app.config['ADMIN_MAIL'], password='******') self.assertFalse(user.has_permission(Permission.ADMINISTRATION)) self.assertTrue(admin.has_permission(Permission.ADMINISTRATION)) def test_chat_two_users_1(self): message1 = Message(text='hi there1', sender=self.arthur, recipient=self.bob, was_read=True, chat=self.chat_bob_arthur) message2 = Message(text='hi there2', sender=self.arthur, recipient=self.bob, was_read=True, chat=self.chat_bob_arthur) message3 = Message(text='hi there3', sender=self.arthur, recipient=self.bob, chat=self.chat_bob_arthur) message4 = Message(text='hi there4', sender=self.arthur, recipient=self.bob, chat=self.chat_bob_arthur) database.session.add_all([message1, message2, message3, message4]) database.session.commit() user_chats = (database.session.query(Chat).join( UserChatTable, (UserChatTable.c.user_id == self.bob.id) & (UserChatTable.c.chat_id == Chat.id)).subquery()) found_chat = (database.session.query(Chat).join( UserChatTable, (UserChatTable.c.user_id == self.arthur.id) & (UserChatTable.c.chat_id == Chat.id)).join(user_chats).first()) self.assertEqual(self.chat_bob_arthur, found_chat) def test_chat_two_users_2(self): message1 = Message(text='hi there1', sender=self.arthur, recipient=self.bob, was_read=True, chat=self.chat_bob_arthur) message2 = Message(text='hi there2', sender=self.arthur, recipient=self.bob, was_read=True, chat=self.chat_bob_arthur) message3 = Message(text='hi there3', sender=self.arthur, recipient=self.bob, chat=self.chat_bob_arthur) message4 = Message(text='hi there4', sender=self.arthur, recipient=self.bob, chat=self.chat_bob_arthur) message5 = Message(text='hi there5', sender=self.morgana, recipient=self.arthur, chat=self.chat_bob_clair) message6 = Message(text='hi there6', sender=self.bob, recipient=self.clair, chat=self.chat_morgana_arthur) database.session.add_all( [message1, message2, message3, message4, message5, message6]) database.session.commit() user_chats = (database.session.query(Chat).join( UserChatTable, (UserChatTable.c.user_id == self.bob.id) & (UserChatTable.c.chat_id == Chat.id)).subquery()) found_chat = (database.session.query(Chat).join( UserChatTable, (UserChatTable.c.user_id == self.arthur.id) & (UserChatTable.c.chat_id == Chat.id)).join(user_chats).first()) self.assertEqual(set(found_chat.messages), set([message1, message2, message3, message4])) self.assertEqual(self.chat_bob_arthur, found_chat) def test_get_other_users_query(self): bob_other_users = self.bob.get_other_users_query().all() self.assertIn(self.clair, bob_other_users) self.assertIn(self.morgana, bob_other_users) self.assertIn(self.arthur, bob_other_users) self.assertEqual(len(bob_other_users), User.query.count() - 1) def test_get_available_chats_query(self): chats = self.bob.get_available_chats_query().all() self.assertEqual( set(chats), set((Chat.query.filter(Chat.users.contains(self.bob)).all()))) self.bob.mark_chats_as_removed([self.chat_bob_arthur]) chats = self.bob.get_available_chats_query().all() self.assertNotEqual( chats, (Chat.query.filter(Chat.users.contains(self.bob)).all())) self.assertNotIn(self.chat_bob_arthur, chats) self.assertEqual( len(chats), (Chat.query.filter(Chat.users.contains(self.bob)).count() - 1)) def test_get_messages(self): message1 = Message(text='hi arthur', sender=self.bob, recipient=self.arthur, chat=self.chat_bob_arthur, was_read=True) message2 = Message(text='hi bob', sender=self.arthur, recipient=self.bob, chat=self.chat_bob_arthur) message3 = Message(text='what\'s up', sender=self.arthur, recipient=self.bob, chat=self.chat_bob_arthur) database.session.add_all([message1, message2, message3]) database.session.commit() message_dict_1 = { 'text': message1.text, 'date_created': message1.date_created.isoformat(), 'sender_username': message1.sender.username, 'recipient_username': message1.recipient.username } message_dict_2 = { 'text': message2.text, 'date_created': message2.date_created.isoformat(), 'sender_username': message2.sender.username, 'recipient_username': message2.recipient.username } message_dict_3 = { 'text': message3.text, 'date_created': message3.date_created.isoformat(), 'sender_username': message3.sender.username, 'recipient_username': message3.recipient.username } messages = self.bob.get_messages(self.chat_bob_arthur) self.assertIn(message_dict_1, messages) self.assertIn(message_dict_2, messages) self.assertIn(message_dict_3, messages) self.assertEqual(len(messages), 3) def test_get_unread_messages_query(self): message_1 = Message(text='hi arthur', sender=self.bob, recipient=self.arthur, chat=self.chat_bob_arthur, was_read=True) message_2 = Message(text='hi bob', sender=self.arthur, recipient=self.bob, chat=self.chat_bob_arthur) message_3 = Message(text='what\'s up', sender=self.arthur, recipient=self.bob, chat=self.chat_bob_arthur) message_4 = Message(text='see you', sender=self.arthur, recipient=self.bob, chat=self.chat_bob_arthur, was_read=True) database.session.add_all([message_1, message_2, message_3, message_4]) database.session.commit() messages = self.bob.get_unread_messages_query(self.chat_bob_arthur) self.assertIn(message_2, messages.all()) self.assertIn(message_3, messages.all()) self.assertEqual(len(messages.all()), 2) def test_search_users_query(self): other_users_query = self.bob.get_other_users_query() artorias = User(username='******', password='******', email='*****@*****.**', confirmed=True) self.assertEqual( self.bob.search_users_query('bob', other_users_query).all(), []) self.assertIn( self.morgana, self.bob.search_users_query('morgana', other_users_query).all()) self.assertIn( self.arthur, self.bob.search_users_query('art', other_users_query).all()) self.assertIn( artorias, self.bob.search_users_query('art', other_users_query).all()) self.assertEqual( self.bob.search_users_query('art', other_users_query).count(), 2)
def test_add_users(self): chat = Chat() chat.add_users([self.bob, self.arthur]) self.assertIn(self.bob, chat.users.all()) self.assertIn(self.arthur, chat.users.all()) self.assertEqual(chat.users.count(), 2)
class ClientTestCase(unittest.TestCase): def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() database.create_all() Role.insert_roles() self.client = self.app.test_client(use_cookies=True) self.bob = User(username='******', password='******', email='*****@*****.**', confirmed=True) self.arthur = User(username='******', password='******', email='*****@*****.**', confirmed=True) self.clair = User(username='******', password='******', email='*****@*****.**', confirmed=True) self.chat_bob_arthur = Chat() self.chat_bob_arthur.add_users([self.bob, self.arthur]) database.session.add_all( [self.bob, self.arthur, self.clair, self.chat_bob_arthur]) database.session.commit() def test_index(self): response = self.client.post('/auth/login', data={ 'email': '*****@*****.**', 'password': '******' }, follow_redirects=True) self.assertIn('Simple Messenger - bob', response.get_data(as_text=True)) def test_register_and_login(self): response = self.client.post('/auth/signup', data={ 'email': '*****@*****.**', 'username': '******', 'password': '******', 'password_confirmation': 'pass' }) self.assertEqual(response.status_code, 302) user = User.query.filter_by(username='******').first() response = self.client.post('/auth/login', data={ 'email': '*****@*****.**', 'password': '******' }, follow_redirects=True) self.assertIn('Invalid', response.get_data(as_text=True)) self.assertEqual(response.status_code, 200) response = self.client.post('/auth/login', data={ 'email': '*****@*****.**', 'password': '******' }, follow_redirects=True) self.assertIn('Hello', response.get_data(as_text=True)) self.assertEqual(response.status_code, 200) user = User(username='******', password='******', email='*****@*****.**', confirmed=True) database.session.add(user) database.session.commit() response = self.client.post('/auth/login', data={ 'email': '*****@*****.**', 'password': '******' }, follow_redirects=True) self.assertIn(f'Simple Messenger - {user.username}', response.get_data(as_text=True)) def tearDown(self): database.session.remove() database.drop_all() self.app_context.pop()