From caff70ea38fe73e09ef4836c0c05e6d9c3b1df78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Casaj=C3=BAs?= Date: Thu, 12 May 2022 16:35:51 +0200 Subject: [PATCH] Set global config to enable/disable feature --- app/dashboard/views/alias_contact_manager.py | 17 ++++++++++++++++- tests/api/test_alias.py | 15 ++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/dashboard/views/alias_contact_manager.py b/app/dashboard/views/alias_contact_manager.py index dbe78ea1..14e139bb 100644 --- a/app/dashboard/views/alias_contact_manager.py +++ b/app/dashboard/views/alias_contact_manager.py @@ -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( diff --git a/tests/api/test_alias.py b/tests/api/test_alias.py index 1d770baf..068c5973 100644 --- a/tests/api/test_alias.py +++ b/tests/api/test_alias.py @@ -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 "}, ) + 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 "}, + ) assert r.status_code == 403 + allow_free_users_to_create_contacts.set(True) def test_create_contact_route_empty_contact_address(flask_client):