refactor verify_prefix_suffix: remove user_custom_domains param

This commit is contained in:
Son NK 2020-05-02 12:27:54 +02:00
parent db92003e5f
commit 0edcc25289
4 changed files with 21 additions and 10 deletions

View file

@ -51,7 +51,7 @@ def new_custom_alias():
note = data.get("note")
alias_prefix = convert_to_id(alias_prefix)
if not verify_prefix_suffix(user, alias_prefix, alias_suffix, user_custom_domains):
if not verify_prefix_suffix(user, alias_prefix, alias_suffix):
return jsonify(error="wrong alias prefix or suffix"), 400
full_alias = alias_prefix + alias_suffix

View file

@ -71,9 +71,7 @@ def custom_alias():
flash("Unknown error, refresh the page", "error")
return redirect(url_for("dashboard.custom_alias"))
if verify_prefix_suffix(
current_user, alias_prefix, alias_suffix, user_custom_domains
):
if verify_prefix_suffix(current_user, alias_prefix, alias_suffix):
full_alias = alias_prefix + alias_suffix
if Alias.get_by(email=full_alias) or DeletedAlias.get_by(email=full_alias):
@ -110,11 +108,12 @@ def custom_alias():
return render_template("dashboard/custom_alias.html", **locals())
def verify_prefix_suffix(user, alias_prefix, alias_suffix, user_custom_domains) -> bool:
def verify_prefix_suffix(user, alias_prefix, alias_suffix) -> bool:
"""verify if user could create an alias with the given prefix and suffix"""
if not alias_prefix or not alias_suffix: # should be caught on frontend
return False
user_custom_domains = [cd.domain for cd in user.verified_custom_domains()]
alias_prefix = alias_prefix.strip()
alias_prefix = convert_to_id(alias_prefix)

View file

@ -171,9 +171,7 @@ def authorize():
from app.dashboard.views.custom_alias import verify_prefix_suffix
if verify_prefix_suffix(
current_user, alias_prefix, alias_suffix, user_custom_domains
):
if verify_prefix_suffix(current_user, alias_prefix, alias_suffix):
full_alias = alias_prefix + alias_suffix
if Alias.get_by(email=full_alias) or DeletedAlias.get_by(

View file

@ -1,9 +1,9 @@
from flask import url_for
from app.config import EMAIL_DOMAIN
from app.dashboard.views.custom_alias import signer
from app.dashboard.views.custom_alias import signer, verify_prefix_suffix
from app.extensions import db
from app.models import Mailbox
from app.models import Mailbox, CustomDomain
from app.utils import random_word
from tests.utils import login
@ -39,3 +39,17 @@ def test_not_show_unverified_mailbox(flask_client):
assert "m1@example.com" in str(r.data)
assert "m2@example.com" not in str(r.data)
def test_verify_prefix_suffix(flask_client):
user = login(flask_client)
db.session.commit()
CustomDomain.create(user_id=user.id, domain="test.com", verified=True)
assert verify_prefix_suffix(user, "prefix", "@test.com")
assert not verify_prefix_suffix(user, "prefix", "@abcd.com")
word = random_word()
suffix = f".{word}@{EMAIL_DOMAIN}"
assert verify_prefix_suffix(user, "prefix", suffix)