async def test_no_permission(self): ''' Test with no permission ''' self.database.drop_testing_database() # Clear database user = self.user for cmd in [ 'help', 'start', 'user', 'admin', 'train', 'done', 'addadmin', 'adduser' ]: await user.send_message('/{}'.format(cmd)) message = await user.get_message() self.assertEqual(message, None) # Send photo with no face no_face_image = Image.new('RGB', (30, 30), color='white') no_face_photo = image_to_file(no_face_image) await user.send_photo(no_face_photo) message = await user.get_message(10) self.assertEqual(message, None)
async def test_user_permission(self): ''' Test with user permission ''' # Setup permission self.database.drop_testing_database() # Clear database if '@' not in self.user.user_id: self.user.user_id = '@' + self.user.user_id self.database.add_user(self.user.user_id, 'user') # Test forbidden commands user = self.user for cmd in ['user', 'admin', 'train', 'done', 'addadmin', 'adduser']: await user.send_message('/{}'.format(cmd)) message = await user.get_message() self.assertTrue('Permission denied' in message.text) # Send photo with no face no_face_image = Image.new('RGB', (30, 30), color='white') no_face_photo = image_to_file(no_face_image) await user.send_photo(no_face_photo) message = await user.get_message(10) self.assertTrue('No model' in message.text or 'No face found' in message.text)
def handle_predict_result(image, x_locations, predictions): ''' Handle prediction result ''' # Add label to image LOGGER.debug("Adding label to predicted image") try: image = add_label_to_image(image, x_locations, predictions) except (IOError, TypeError, ValueError) as exp: LOGGER.error("Cannot add label to image, %s", exp) raise ImageFileError # Convert image to file LOGGER.debug("Converting labelled image to file") try: file = image_to_file(image) except IOError: LOGGER.error("Cannot convert image to file") raise ImageFileError # Create prediction caption LOGGER.debug("Creating prediction caption") try: caption = predict_caption(predictions) except (TypeError, ValueError): LOGGER.error("Cannot create caption") raise CreateCaptionError return {'file': file, 'caption': caption}
async def test_train_incorrect_num_face(self): ''' Test train with incorrect number of face ''' user = self.user tag = 'test_label_1' # Run /train <tag> await user.send_message('/train {}'.format(tag)) message = await user.get_message() self.assertTrue(tag in message.text) # Send photo with no face no_face_image = Image.new('RGB', (30, 30), color='white') no_face_photo = image_to_file(no_face_image) await user.send_photo(no_face_photo) message = await user.get_message(10) self.assertEqual('No face found', message.text) # Send photo with two faces two_face_photo = open('./test/media/two_wong.png', 'rb') await user.send_photo(two_face_photo) two_face_photo.close() message = await user.get_message(10) self.assertEqual('More than one face found', message.text) # Done await user.send_message('/done') message = await user.get_message() self.assertTrue(tag in message.text)
async def test_admin_permission(self): ''' Test with admin permission ''' # Setup permission self.database.drop_testing_database() # Clear database if '@' not in self.user.user_id: self.user.user_id = '@' + self.user.user_id self.database.add_user(self.user.user_id, 'admin') # Test forbidden commands user = self.user for cmd in ['admin', 'addadmin']: await user.send_message('/{}'.format(cmd)) message = await user.get_message() self.assertTrue('Permission denied' in message.text) # Run predict no_face_image = Image.new('RGB', (30, 30), color='white') no_face_photo = image_to_file(no_face_image) await user.send_photo(no_face_photo) message = await user.get_message(10) self.assertTrue('No model' in message.text or 'No face found' in message.text) # Run /train with label tag = 'testlabel1' await user.send_message('/train {}'.format(tag)) message = await user.get_message() self.assertTrue(tag in message.text) # Send photo with one face one_face_photo = open('./test/media/wong_1.jpg', 'rb') await user.send_photo(one_face_photo) one_face_photo.close() # Check message message = await user.get_message() self.assertTrue(tag in message.text) self.assertTrue('more' in message.text) await user.send_message('/done') message = await user.get_message() self.assertTrue('Done' in message.text) self.assertTrue(tag in message.text) # List users await user.send_message('/user') message = await user.get_message() self.assertTrue('List of users' in message.text) # Add user if '@' not in self.user.bot_id: self.user.bot_id = '@' + self.user.bot_id await user.send_message('/adduser {}'.format(self.user.bot_id)) message = await user.get_message() self.assertTrue('Added user' in message.text) self.assertTrue(self.user.bot_id in message.text) # Cancel on removing user await user.send_message('/user') message = await user.get_message() self.assertTrue('List of users' in message.text) self.assertEqual(message.button_count, 1) self.assertTrue(self.user.bot_id in message.buttons[0][0].text) # Click on user await message.click(0) # Display Remove and cancel button message = await user.get_message(last=True) self.assertEqual(message.button_count, 2) await message.click(1) # Cancel message = await user.get_message(last=True) self.assertEqual(message.button_count, 1) self.assertTrue(self.user.bot_id in message.buttons[0][0].text) # Remove user await user.send_message('/user') message = await user.get_message() self.assertTrue('List of users' in message.text) self.assertEqual(message.button_count, 1) self.assertTrue(self.user.bot_id in message.buttons[0][0].text) # Click on user await message.click(0) # Display Remove and cancel button message = await user.get_message(last=True) self.assertEqual(message.button_count, 2) await message.click(0) # Remove message = await user.get_message(last=True) self.assertEqual(message.button_count, 0)