From e5079e48dacdad21dfcdcdf07fef46f55748094c Mon Sep 17 00:00:00 2001 From: Son NK Date: Sun, 15 Dec 2019 09:57:24 +0200 Subject: [PATCH] remove can_create_new_random_alias() --- app/api/views/alias_options.py | 1 - app/api/views/new_random_alias.py | 45 ------------------------------- app/models.py | 9 ------- cron.py | 3 ++- tests/test_models.py | 19 ------------- 5 files changed, 2 insertions(+), 75 deletions(-) delete mode 100644 app/api/views/new_random_alias.py diff --git a/app/api/views/alias_options.py b/app/api/views/alias_options.py index aba138c1..8178d90d 100644 --- a/app/api/views/alias_options.py +++ b/app/api/views/alias_options.py @@ -33,7 +33,6 @@ def options(): ret = { "existing": [ge.email for ge in GenEmail.query.filter_by(user_id=user.id)], "can_create_custom": user.can_create_new_custom_alias(), - "can_create_random": user.can_create_new_random_alias(), } # recommendation alias if exist diff --git a/app/api/views/new_random_alias.py b/app/api/views/new_random_alias.py deleted file mode 100644 index b02b37cd..00000000 --- a/app/api/views/new_random_alias.py +++ /dev/null @@ -1,45 +0,0 @@ -from flask import g -from flask import jsonify, request -from flask_cors import cross_origin - -from app.api.base import api_bp, verify_api_key -from app.extensions import db -from app.log import LOG -from app.models import GenEmail, AliasUsedOn - - -# OBSOLETE -@api_bp.route("/alias/random/new", methods=["POST"]) -@cross_origin() -@verify_api_key -def new_random_alias(): - """ - Create a new random alias - Input: - optional "hostname" in args - Output: - 201 if success - 409 if alias already exists - - """ - LOG.error("/api/alias/new is obsolete! Called by %s", g.user) - - user = g.user - if not user.can_create_new_random_alias(): - LOG.d("user %s cannot create random alias", user) - return ( - jsonify( - error="You have created 3 random aliases, please upgrade to create more" - ), - 400, - ) - - hostname = request.args.get("hostname") - gen_email = GenEmail.create_new_gen_email(user_id=user.id) - db.session.commit() - - if hostname: - AliasUsedOn.create(gen_email_id=gen_email.id, hostname=hostname) - db.session.commit() - - return jsonify(alias=gen_email.email), 201 diff --git a/app/models.py b/app/models.py index be7c5c1d..164cae30 100644 --- a/app/models.py +++ b/app/models.py @@ -143,15 +143,6 @@ class User(db.Model, ModelMixin, UserMixin): < MAX_NB_EMAIL_FREE_PLAN ) - def can_create_new_random_alias(self): - if self.is_premium(): - return True - else: - return ( - GenEmail.filter_by(user_id=self.id, custom=False).count() - < MAX_NB_EMAIL_FREE_PLAN - ) - def set_password(self, password): salt = bcrypt.gensalt() password_hash = bcrypt.hashpw(password.encode(), salt).decode() diff --git a/cron.py b/cron.py index 04e727d9..2d083e3b 100644 --- a/cron.py +++ b/cron.py @@ -10,7 +10,8 @@ from app.models import ( ForwardEmailLog, ForwardEmail, CustomDomain, - Client) + Client, +) from server import create_app diff --git a/tests/test_models.py b/tests/test_models.py index b62903d3..b3497c22 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -19,25 +19,6 @@ def test_profile_picture_url(flask_client): assert user.profile_picture_url() == "http://sl.test/static/default-avatar.png" -def test_suggested_emails_for_user_who_can_create_new_random_alias(flask_client): - user = User.create( - email="a@b.c", password="password", name="Test User", activated=True - ) - db.session.commit() - - suggested_email, other_emails = user.suggested_emails(website_name="test") - assert suggested_email - assert len(other_emails) == 1 - - db.session.rollback() - - # the suggested email is new and not exist in GenEmail - assert GenEmail.get_by(email=suggested_email) is None - - # all other emails are generated emails - assert GenEmail.get_by(email=other_emails[0]) - - def test_suggested_emails_for_user_who_cannot_create_new_email(flask_client): user = User.create( email="a@b.c", password="password", name="Test User", activated=True