Set global config to enable/disable feature
This commit is contained in:
parent
19e30eaf0a
commit
caff70ea38
|
@ -26,6 +26,19 @@ from app.log import LOG
|
||||||
from app.models import Alias, Contact, EmailLog, User
|
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():
|
def email_validator():
|
||||||
"""validate email address. Handle both only email and email with name:
|
"""validate email address. Handle both only email and email with name:
|
||||||
- ab@cd.com
|
- ab@cd.com
|
||||||
|
@ -68,7 +81,9 @@ def create_contact(
|
||||||
raise ErrContactAlreadyExists(contact)
|
raise ErrContactAlreadyExists(contact)
|
||||||
return 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()
|
raise ErrContactErrorUpgradeNeeded()
|
||||||
|
|
||||||
contact = Contact.create(
|
contact = Contact.create(
|
||||||
|
|
|
@ -2,6 +2,9 @@ from flask import url_for
|
||||||
import arrow
|
import arrow
|
||||||
|
|
||||||
from app.config import PAGE_LIMIT
|
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.db import Session
|
||||||
from app.email_utils import is_reverse_alias
|
from app.email_utils import is_reverse_alias
|
||||||
from app.models import User, ApiKey, Alias, Contact, EmailLog, Mailbox
|
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
|
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
|
user.flags = User.FLAG_FREE_DISABLE_CREATE_ALIAS
|
||||||
Session.commit()
|
Session.commit()
|
||||||
r = flask_client.post(
|
r = flask_client.post(
|
||||||
|
@ -576,7 +579,17 @@ def test_create_contact_route_free_users(flask_client):
|
||||||
headers={"Authentication": api_key.code},
|
headers={"Authentication": api_key.code},
|
||||||
json={"contact": f"First Last <first@{random_domain()}>"},
|
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
|
assert r.status_code == 403
|
||||||
|
allow_free_users_to_create_contacts.set(True)
|
||||||
|
|
||||||
|
|
||||||
def test_create_contact_route_empty_contact_address(flask_client):
|
def test_create_contact_route_empty_contact_address(flask_client):
|
||||||
|
|
Loading…
Reference in a new issue