diff --git a/app/config.py b/app/config.py index 72eec144..fdbbe5c1 100644 --- a/app/config.py +++ b/app/config.py @@ -464,3 +464,5 @@ def setup_nameservers(): NAMESERVERS = setup_nameservers() + +DISABLE_CREATE_CONTACTS_FOR_FREE_USERS = False diff --git a/app/dashboard/views/alias_contact_manager.py b/app/dashboard/views/alias_contact_manager.py index 14e139bb..caa6488f 100644 --- a/app/dashboard/views/alias_contact_manager.py +++ b/app/dashboard/views/alias_contact_manager.py @@ -8,7 +8,7 @@ from flask_wtf import FlaskForm from sqlalchemy import and_, func, case from wtforms import StringField, validators, ValidationError -from app.config import PAGE_LIMIT +from app import config from app.dashboard.base import dashboard_bp from app.db import Session from app.email_utils import ( @@ -26,19 +26,6 @@ from app.log import LOG from app.models import Alias, Contact, EmailLog, User -# TODO: Move this to a scoped config once the global config gets scoped. -# This allows this config to be modified from tests so we can test both scenarios. -# By default allow users to create contacts -class AllowFreeUsersToCreateContacts: - allow: bool = True - - def set(self, allow: bool): - self.allow = allow - - -allow_free_users_to_create_contacts = AllowFreeUsersToCreateContacts() - - def email_validator(): """validate email address. Handle both only email and email with name: - ab@cd.com @@ -81,7 +68,7 @@ def create_contact( raise ErrContactAlreadyExists(contact) return contact - if not allow_free_users_to_create_contacts.allow and ( + if config.DISABLE_CREATE_CONTACTS_FOR_FREE_USERS and ( not user.is_premium() and user.flags & User.FLAG_FREE_DISABLE_CREATE_ALIAS > 0 ): raise ErrContactErrorUpgradeNeeded() @@ -196,7 +183,11 @@ def get_contact_infos( ], else_=Contact.created_at, ) - q = q.order_by(latest_activity.desc()).limit(PAGE_LIMIT).offset(page * PAGE_LIMIT) + q = ( + q.order_by(latest_activity.desc()) + .limit(config.PAGE_LIMIT) + .offset(page * config.PAGE_LIMIT) + ) ret = [] for contact, latest_email_log, nb_reply, nb_forward in q: @@ -293,7 +284,7 @@ def alias_contact_manager(alias_id): ) contact_infos = get_contact_infos(alias, page, query=query) - last_page = len(contact_infos) < PAGE_LIMIT + last_page = len(contact_infos) < config.PAGE_LIMIT nb_contact = Contact.filter(Contact.alias_id == alias.id).count() # if highlighted contact isn't included, fetch it diff --git a/tests/api/test_alias.py b/tests/api/test_alias.py index 068c5973..b120c00d 100644 --- a/tests/api/test_alias.py +++ b/tests/api/test_alias.py @@ -1,10 +1,7 @@ from flask import url_for import arrow -from app.config import PAGE_LIMIT -from app.dashboard.views.alias_contact_manager import ( - allow_free_users_to_create_contacts, -) +from app import config from app.db import Session from app.email_utils import is_reverse_alias from app.models import User, ApiKey, Alias, Contact, EmailLog, Mailbox @@ -46,17 +43,17 @@ def test_get_aliases_with_pagination(flask_client): api_key = ApiKey.create(user.id, "for test") Session.commit() - # create more aliases than PAGE_LIMIT - for _ in range(PAGE_LIMIT + 1): + # create more aliases than config.PAGE_LIMIT + for _ in range(config.PAGE_LIMIT + 1): Alias.create_new_random(user) Session.commit() - # get aliases on the 1st page, should return PAGE_LIMIT aliases + # get aliases on the 1st page, should return config.PAGE_LIMIT aliases r = flask_client.get( url_for("api.get_aliases", page_id=0), headers={"Authentication": api_key.code} ) assert r.status_code == 200 - assert len(r.json["aliases"]) == PAGE_LIMIT + assert len(r.json["aliases"]) == config.PAGE_LIMIT # assert returned field for a in r.json["aliases"]: @@ -71,7 +68,7 @@ def test_get_aliases_with_pagination(flask_client): assert "note" in a # get aliases on the 2nd page, should return 2 aliases - # as the total number of aliases is PAGE_LIMIT +2 + # as the total number of aliases is config.PAGE_LIMIT +2 # 1 alias is created when user is created r = flask_client.get( url_for("api.get_aliases", page_id=1), headers={"Authentication": api_key.code} @@ -90,7 +87,7 @@ def test_get_aliases_query(flask_client): api_key = ApiKey.create(user.id, "for test") Session.commit() - # create more aliases than PAGE_LIMIT + # create more aliases than config.PAGE_LIMIT Alias.create_new(user, "prefix1") Alias.create_new(user, "prefix2") Session.commit() @@ -281,7 +278,7 @@ def test_alias_activities(flask_client): ) Session.commit() - for _ in range(int(PAGE_LIMIT / 2)): + for _ in range(int(config.PAGE_LIMIT / 2)): EmailLog.create( contact_id=contact.id, is_reply=True, @@ -289,7 +286,7 @@ def test_alias_activities(flask_client): alias_id=contact.alias_id, ) - for _ in range(int(PAGE_LIMIT / 2) + 2): + for _ in range(int(config.PAGE_LIMIT / 2) + 2): EmailLog.create( contact_id=contact.id, blocked=True, @@ -303,7 +300,7 @@ def test_alias_activities(flask_client): ) assert r.status_code == 200 - assert len(r.json["activities"]) == PAGE_LIMIT + assert len(r.json["activities"]) == config.PAGE_LIMIT for ac in r.json["activities"]: assert ac["from"] assert ac["to"] @@ -456,7 +453,7 @@ def test_alias_contacts(flask_client): Session.commit() # create some alias log - for i in range(PAGE_LIMIT + 1): + for i in range(config.PAGE_LIMIT + 1): contact = Contact.create( website_email=f"marketing-{i}@example.com", reply_email=f"reply-{i}@a.b", @@ -476,7 +473,7 @@ def test_alias_contacts(flask_client): r = flask_client.get(f"/api/aliases/{alias.id}/contacts?page_id=0") assert r.status_code == 200 - assert len(r.json["contacts"]) == PAGE_LIMIT + assert len(r.json["contacts"]) == config.PAGE_LIMIT for ac in r.json["contacts"]: assert ac["creation_date"] assert ac["creation_timestamp"] @@ -582,14 +579,14 @@ def test_create_contact_route_free_users(flask_client): assert r.status_code == 201 # Set the global config to disable free users from create contacts - allow_free_users_to_create_contacts.set(False) + config.DISABLE_CREATE_CONTACTS_FOR_FREE_USERS = True r = flask_client.post( url_for("api.create_contact_route", alias_id=alias.id), headers={"Authentication": api_key.code}, json={"contact": f"First Last "}, ) assert r.status_code == 403 - allow_free_users_to_create_contacts.set(True) + config.DISABLE_CREATE_CONTACTS_FOR_FREE_USERS = False def test_create_contact_route_empty_contact_address(flask_client): @@ -644,11 +641,11 @@ def test_delete_contact(flask_client): def test_get_alias(flask_client): user, api_key = get_new_user_and_api_key() - # create more aliases than PAGE_LIMIT + # create more aliases than config.PAGE_LIMIT alias = Alias.create_new_random(user) Session.commit() - # get aliases on the 1st page, should return PAGE_LIMIT aliases + # get aliases on the 1st page, should return config.PAGE_LIMIT aliases r = flask_client.get( url_for("api.get_alias", alias_id=alias.id), headers={"Authentication": api_key.code},