def send_message_after_validation(validated_by: int, mapped_by: int, task_id: int, project_id: int): """ Sends mapper a thank you, after their task has been marked as valid """ if validated_by == mapped_by: return # No need to send a thankyou to yourself text_template = get_template('validation_message_en.txt') task_link = MessageService.get_task_link(project_id, task_id) user = UserService.get_user_by_id(mapped_by) text_template = text_template.replace('[USERNAME]', user.username) text_template = text_template.replace('[TASK_LINK]', task_link) validation_message = Message() validation_message.from_user_id = validated_by validation_message.to_user_id = mapped_by validation_message.subject = f'Your mapping on {task_link} has just been validated' validation_message.message = text_template validation_message.add_message() SMTPService.send_email_alert(user.email_address, user.username)
def test_send_alert_message_limits(self): if self.skip_tests: return if os.getenv('TM_SMTP_HOST') is None: return # If SMTP not setup there's no value attempting the integration tests for x in range(0, 50): self.assertTrue( SMTPService.send_email_alert('*****@*****.**', 'Iain Hunter'))
def send_message_to_all_contributors(project_id: int, message_dto: MessageDTO): """ Sends supplied message to all contributors on specified project """ contributors = Message.get_all_contributors(project_id) project_link = MessageService.get_project_link(project_id) message_dto.message = f'{project_link}<br/><br/>' + message_dto.message # Append project link to end of message msg_count = 0 for contributor in contributors: message = Message.from_dto(contributor[0], message_dto) message.save() user = UserService.get_user_by_id(contributor[0]) SMTPService.send_email_alert(user.email_address, user.username) msg_count += 1 if msg_count == 5: time.sleep( 0.5 ) # Sleep for 0.5 seconds to avoid hitting AWS rate limits every 5 messages msg_count = 0
def send_message_after_comment(comment_from: int, comment: str, task_id: int, project_id :int): """ Will send a canned message to anyone @'d in a comment """ usernames = MessageService._parse_comment_for_username(comment) if len(usernames) == 0: return # Nobody @'d so return link = MessageService.get_task_link(project_id, task_id) for username in usernames: try: user = UserService.get_user_by_username(username) except NotFound: continue # If we can't find the user, keep going no need to fail message = Message() message.from_user_id = comment_from message.to_user_id = user.id message.subject = f'You were mentioned in a comment on {link}' message.message = comment message.add_message() SMTPService.send_email_alert(user.email_address, user.username)
def test_email_verification_url_generated_correctly(self): # Arrange test_user = '******' # Act url = SMTPService._generate_email_verification_url('*****@*****.**', test_user) parsed_url = urlparse(url) query = parse_qs(parsed_url.query) self.assertEqual(parsed_url.path, '/api/auth/email') self.assertEqual(query['username'], [test_user]) self.assertTrue(query['token']) # Token random every time so just check we have something
def test_can_parse_email_verification_token(self): # Arrange - Generate valid email verification url test_email = '*****@*****.**' auth_url = SMTPService._generate_email_verification_url( test_email, 'mrtest') parsed_url = urlparse(auth_url) query = parse_qs(parsed_url.query) # Arrange is_valid, email_address = AuthenticationService.is_valid_token( query['token'][0], 86400) # Assert self.assertTrue(is_valid) self.assertEqual(email_address, test_email)
def test_alert_not_sent_if_email_not_supplied(self): self.assertFalse(SMTPService.send_email_alert('', 'Iain Hunter'))
def resend_email_validation(user_id: int): """ Resends the email validation email to the logged in user """ user = UserService.get_user_by_id(user_id) SMTPService.send_verification_email(user.email_address, user.username)
def test_send_alert(self): if self.skip_tests: return self.assertTrue(SMTPService.send_email_alert('*****@*****.**', 'Iain Hunter'))
def test_send_verification_mail(self): if self.skip_tests: return self.assertTrue(SMTPService.send_verification_email('*****@*****.**', 'mrtest'))