Pull out email validation into validator class
This commit is contained in:
parent
ee4f5ed5d6
commit
00eceb7ed5
2 changed files with 27 additions and 18 deletions
|
@ -9,7 +9,6 @@ import ldap
|
|||
import peewee
|
||||
|
||||
from flask import Flask
|
||||
from flask_mail import Mail, Message
|
||||
|
||||
from app import app, config_provider, get_app_url, OVERRIDE_CONFIG_DIRECTORY
|
||||
from auth.auth_context import get_authenticated_user
|
||||
|
@ -32,6 +31,7 @@ from util.security.ssl import load_certificate, CertInvalidException, KeyInvalid
|
|||
from util.config.validators.database import DatabaseValidator
|
||||
from util.config.validators.buildlogredis import RedisValidator
|
||||
from util.config.validators.registrystorage import StorageValidator
|
||||
from util.config.validators.email import EmailValidator
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
@ -82,22 +82,6 @@ def _validate_database(config, user_obj, _):
|
|||
raise ex
|
||||
|
||||
|
||||
def _validate_mailing(config, user_obj, _):
|
||||
""" Validates sending email. """
|
||||
test_app = Flask("mail-test-app")
|
||||
test_app.config.update(config)
|
||||
test_app.config.update({
|
||||
'MAIL_FAIL_SILENTLY': False,
|
||||
'TESTING': False
|
||||
})
|
||||
|
||||
test_mail = Mail(test_app)
|
||||
test_msg = Message("Test e-mail from %s" % app.config['REGISTRY_TITLE'],
|
||||
sender=config.get('MAIL_DEFAULT_SENDER'))
|
||||
test_msg.add_recipient(user_obj.email)
|
||||
test_mail.send(test_msg)
|
||||
|
||||
|
||||
def _validate_gitlab(config, user_obj, _):
|
||||
""" Validates the OAuth credentials and API endpoint for a GitLab service. """
|
||||
github_config = config.get('GITLAB_TRIGGER_CONFIG')
|
||||
|
@ -482,7 +466,7 @@ VALIDATORS = {
|
|||
DatabaseValidator.name: DatabaseValidator.validate,
|
||||
RedisValidator.name: RedisValidator.validate,
|
||||
StorageValidator.name: StorageValidator.validate,
|
||||
'mail': _validate_mailing,
|
||||
EmailValidator.name: EmailValidator.validate,
|
||||
'github-login': _validate_github('GITHUB_LOGIN_CONFIG'),
|
||||
'github-trigger': _validate_github('GITHUB_TRIGGER_CONFIG'),
|
||||
'gitlab-trigger': _validate_gitlab,
|
||||
|
|
25
util/config/validators/email.py
Normal file
25
util/config/validators/email.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
from flask import Flask
|
||||
from flask_mail import Mail, Message
|
||||
|
||||
from app import app
|
||||
from util.config.validators import BaseValidator
|
||||
|
||||
class EmailValidator(BaseValidator):
|
||||
name = "mail"
|
||||
|
||||
@classmethod
|
||||
def validate(cls, config, user, user_password):
|
||||
""" Validates sending email. """
|
||||
with app.app_context():
|
||||
test_app = Flask("mail-test-app")
|
||||
test_app.config.update(config)
|
||||
test_app.config.update({
|
||||
'MAIL_FAIL_SILENTLY': False,
|
||||
'TESTING': False
|
||||
})
|
||||
|
||||
test_mail = Mail(test_app)
|
||||
test_msg = Message("Test e-mail from %s" % app.config['REGISTRY_TITLE'],
|
||||
sender=config.get('MAIL_DEFAULT_SENDER'))
|
||||
test_msg.add_recipient(user.email)
|
||||
test_mail.send(test_msg)
|
Reference in a new issue