Set global config to enable/disable feature

This commit is contained in:
Adrià Casajús 2022-05-12 16:35:51 +02:00
parent 19e30eaf0a
commit caff70ea38
No known key found for this signature in database
GPG key ID: F0033226A5AFC9B9
2 changed files with 30 additions and 2 deletions

View file

@ -26,6 +26,19 @@ 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
@ -68,7 +81,9 @@ def create_contact(
raise ErrContactAlreadyExists(contact)
return contact
if not user.is_premium() and user.flags & User.FLAG_FREE_DISABLE_CREATE_ALIAS > 0:
if not allow_free_users_to_create_contacts.allow and (
not user.is_premium() and user.flags & User.FLAG_FREE_DISABLE_CREATE_ALIAS > 0
):
raise ErrContactErrorUpgradeNeeded()
contact = Contact.create(

View file

@ -2,6 +2,9 @@ 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.db import Session
from app.email_utils import is_reverse_alias
from app.models import User, ApiKey, Alias, Contact, EmailLog, Mailbox
@ -568,7 +571,7 @@ def test_create_contact_route_free_users(flask_client):
)
assert r.status_code == 201
# End trial and disallow for new free users
# End trial and disallow for new free users. Config should allow it
user.flags = User.FLAG_FREE_DISABLE_CREATE_ALIAS
Session.commit()
r = flask_client.post(
@ -576,7 +579,17 @@ def test_create_contact_route_free_users(flask_client):
headers={"Authentication": api_key.code},
json={"contact": f"First Last <first@{random_domain()}>"},
)
assert r.status_code == 201
# Set the global config to disable free users from create contacts
allow_free_users_to_create_contacts.set(False)
r = flask_client.post(
url_for("api.create_contact_route", alias_id=alias.id),
headers={"Authentication": api_key.code},
json={"contact": f"First Last <first@{random_domain()}>"},
)
assert r.status_code == 403
allow_free_users_to_create_contacts.set(True)
def test_create_contact_route_empty_contact_address(flask_client):