def test_check_user_is_member(self): cr = chatter_classes.Chatroom(2, db) u1 = chatter_classes.User(3, db) u2 = chatter_classes.User(4, db) self.assertTrue(cr.user_is_member(u1)) self.assertTrue(cr.user_is_member(u2))
def test_constructor_existing_user(self): # Instantiate a User object from user id 1 in the test database u = chatter_classes.User(1, db) # Assert that the properties for the instantiated User matches our test data self.assertEqual(u.username, "TestUser1") self.assertFalse(u.is_admin) self.assertTrue(u.is_active)
def test_contructor_existing_message(self): m = chatter_classes.Message(1, db) self.assertEqual( m.content, "This is the first message in TestRoom1, sent by TestUser1. It has two attachments (a picture of Donald Trump and another of Gary Barlow)." ) self.assertEqual(m.chatroom.name, chatter_classes.Chatroom(1, db).name) self.assertEqual(m.sender.username, chatter_classes.User(1, db).username) self.assertEqual(m.timestamp.year, 2021)
def test_user_send_message(self): cr = chatter_classes.Chatroom(1, db) message_count = cr.get_message_count() u = chatter_classes.User(1, db) u.send_message("Sent by test_user_send_message()", cr.chatroomid) self.assertEqual(message_count + 1, chatter_classes.Chatroom(1, db).get_message_count())
def test_delete_user_by_admin(self): u = chatter_classes.User(5, db) # As well as deleting the user, we need to ensure that their messages have been reassigned users_messages = chatter_classes.Message.get_messages_for_user( 5, None, db) users_message_ids = [message.messageid for message in users_messages] u.delete(chatter_classes.User(6, db)) # User 6 is TestAdmin for message_id in users_message_ids: m = chatter_classes.Message(message_id, db) self.assertEqual( m.senderid, 0) # assert message's sender Id updated to DeletedUser 0 # Check it's no longer possible to instantiate the deleted user self.assertRaises(chatter_classes.UserNotFoundError, chatter_classes.User, 5, db)
def test_update_user(self): u = chatter_classes.User(1, db) u.update(username="******", password="******", last_login_ts=0, active=False, admin=True) v = chatter_classes.User(1, db) self.assertEqual(v.username, "UpdatedUser") self.assertEqual(v.last_login_ts, datetime.datetime.fromtimestamp(0)) self.assertTrue(v.is_admin) self.assertFalse(v.is_active) # Restore Test User 1's details u.update(username="******", password="******", last_login_ts=0, active=True, admin=False)
def test_add_new_message(self): text = "This is a test message generated in the unit test test_add_new_message()" message = chatter_classes.Message.add(text, 1, 1, db) self.assertIsInstance(message, chatter_classes.Message) self.assertEqual(message.content, text) self.assertEqual(message.sender.username, chatter_classes.User(1, db).username) self.assertEqual(message.chatroom.name, chatter_classes.Chatroom(1, db).name)
def test_check_user_is_owner(self): cr = chatter_classes.Chatroom(2, db) u = chatter_classes.User(2, db) self.assertTrue(cr.user_is_owner(u))
def test_user_json(self): u = chatter_classes.User(1, db) js = u.json print(js) self.assertNotEqual(0, len(js))
def test_user_memberships(self): u = chatter_classes.User(2, db) rooms = u.get_chatrooms() self.assertEqual(2, len(rooms['owner'])) self.assertEqual(1, len(rooms['member']))
def test_delete_user_non_admin_fails(self): u = chatter_classes.User(4, db) self.assertRaises(chatter_classes.UserPermissionError, u.delete, chatter_classes.User( 1, db)) # User 1 is a standard user