def _push_messages(messages): if len(messages) == 0: return messages_objs = [] for i, message in enumerate(messages): user = message.get("user") obj = message.get("message") # Store message in the database only if mentions option are disabled. if ( user.mentions_notifications is False and obj.message_type == MessageType.MENTION_NOTIFICATION.value ): messages_objs.append(obj) continue if ( user.projects_notifications is False and obj.message_type == MessageType.PROJECT_ACTIVITY_NOTIFICATION.value ): continue if ( user.projects_notifications is False and obj.message_type == MessageType.BROADCAST.value ): continue if ( user.teams_notifications is False and obj.message_type == MessageType.TEAM_BROADCAST.value ): messages_objs.append(obj) continue if user.comments_notifications is False and obj.message_type in ( MessageType.TASK_COMMENT_NOTIFICATION.value, MessageType.PROJECT_CHAT_NOTIFICATION.value, ): continue if user.tasks_notifications is False and obj.message_type in ( MessageType.VALIDATION_NOTIFICATION.value, MessageType.INVALIDATION_NOTIFICATION.value, ): messages_objs.append(obj) continue messages_objs.append(obj) SMTPService.send_email_alert( user.email_address, user.username, message["message"].id, clean_html(message["message"].subject), message["message"].message, ) if i + 1 % 10 == 0: time.sleep(0.5) # Flush messages to the database. if len(messages_objs) > 0: db.session.add_all(messages_objs) db.session.flush() db.session.commit()
def update_user_details(user_id: int, user_dto: UserDTO) -> dict: """ Update user with info supplied by user, if they add or change their email address a verification mail will be sent """ user = UserService.get_user_by_id(user_id) verification_email_sent = False if ( user_dto.email_address and user.email_address != user_dto.email_address.lower() ): # Send user verification email if they are adding or changing their email address SMTPService.send_verification_email( user_dto.email_address.lower(), user.username ) user.set_email_verified_status(is_verified=False) verification_email_sent = True user.update(user_dto) user_email = UserEmail.query.filter( UserEmail.email == user_dto.email_address ).one_or_none() if user_email is not None: user_email.delete() return dict(verificationEmailSent=verification_email_sent)
def test_does_send_if_user_verified(self): self.assertTrue( SMTPService.send_email_alert("*****@*****.**", "Iain Hunter", True)) self.assertFalse( SMTPService.send_email_alert("", "Iain Hunter", 1, "test", "testing"))
def _push_messages(messages): if len(messages) == 0: return # Flush messages to get the id db.session.add_all([m["message"] for m in messages]) db.session.flush() for i, message in enumerate(messages): user = message.get("user") SMTPService.send_email_alert(user.email_address, user.username, message["message"].id) if i + 1 % 10 == 0: time.sleep(0.5) db.session.commit()
def test_send_verification_mail(self): if os.getenv("TM_SMTP_HOST") is None: return # If SMTP not setup there's no value attempting the integration tests self.assertTrue( SMTPService.send_verification_email("*****@*****.**", "mrtest"))
def test_send_alert(self): if os.getenv("TM_SMTP_HOST") is None: return # If SMTP not setup there's no value attempting the integration tests self.assertTrue( SMTPService.send_email_alert("*****@*****.**", "Iain Hunter", True))
def send_message_after_validation( status: int, validated_by: int, mapped_by: int, task_id: int, project_id: int ): """ Sends mapper a notification after their task has been marked valid or invalid """ if validated_by == mapped_by: return # No need to send a message to yourself user = UserService.get_user_by_id(mapped_by) if user.validation_message is False: return # No need to send validation message if user.projects_notifications is False: return text_template = get_template( "invalidation_message_en.txt" if status == TaskStatus.INVALIDATED else "validation_message_en.txt" ) status_text = ( "marked invalid" if status == TaskStatus.INVALIDATED else "validated" ) task_link = MessageService.get_task_link(project_id, task_id) text_template = text_template.replace("[USERNAME]", user.username) text_template = text_template.replace("[TASK_LINK]", task_link) validation_message = Message() validation_message.message_type = ( MessageType.INVALIDATION_NOTIFICATION.value if status == TaskStatus.INVALIDATED else MessageType.VALIDATION_NOTIFICATION.value ) validation_message.project_id = project_id validation_message.task_id = task_id validation_message.from_user_id = validated_by validation_message.to_user_id = mapped_by validation_message.subject = f"Your mapping in Project {project_id} on {task_link} has just been {status_text}" validation_message.message = text_template validation_message.add_message() SMTPService.send_email_alert( user.email_address, user.username, validation_message.id )
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 post(self): """ Send an email to the system admin --- tags: - system produces: - application/json parameters: - in: body name: body required: true description: JSON object with the data of the message to send to the system admin schema: properties: name: type: string default: The name of the sender email: type: string default: The email of the sender content: type: string default: The content of the message responses: 201: description: Email sent successfully 400: description: Invalid Request 500: description: A problem occurred """ try: data = request.get_json() SMTPService.send_contact_admin_email(data) return {"Success": "Email sent"}, 201 except Exception as e: error_msg = f"Application GET API - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": "Unable to fetch application keys"}, 500
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, "/verify-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 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_alert_not_sent_if_email_not_supplied(self): self.assertFalse(SMTPService.send_email_alert("", "Iain Hunter"))
def test_does_not_send_if_user_not_verified(self): self.assertFalse( SMTPService.send_email_alert("*****@*****.**", "Iain Hunter", False))