def test_out_of_quota(flask_client): user = login(flask_client) user.trial_end = None Session.commit() # create MAX_NB_EMAIL_FREE_PLAN custom alias to run out of quota for _ in range(MAX_NB_EMAIL_FREE_PLAN): Alias.create_new(user, prefix="test") word = random_word() suffix = f".{word}@{EMAIL_DOMAIN}" signed_suffix = signer.sign(suffix).decode() r = flask_client.post( "/api/v3/alias/custom/new", json={ "alias_prefix": "prefix", "signed_suffix": signed_suffix, "note": "test note", "mailbox_ids": [user.default_mailbox_id], "name": "your name", }, ) assert r.status_code == 400 assert r.json == { "error": "You have reached the limitation of a " "free account with the maximum of 3 aliases, please upgrade your plan to create more aliases" }
def test_cannot_create_alias_in_trash(flask_client): user = login(flask_client) # create a custom domain CustomDomain.create(user_id=user.id, domain="ab.cd", verified=True, commit=True) signed_suffix = signer.sign("@ab.cd").decode() r = flask_client.post( "/api/v3/alias/custom/new", json={ "alias_prefix": "prefix", "signed_suffix": signed_suffix, "mailbox_ids": [user.default_mailbox_id], }, ) assert r.status_code == 201 assert r.json["alias"] == f"*****@*****.**" # delete alias: it's going to be moved to ab.cd trash alias = Alias.get_by(email="*****@*****.**") assert alias.custom_domain_id delete_alias(alias, user) # try to create the same alias, will fail as the alias is in trash r = flask_client.post( "/api/v3/alias/custom/new", json={ "alias_prefix": "prefix", "signed_suffix": signed_suffix, "mailbox_ids": [user.default_mailbox_id], }, ) assert r.status_code == 409
def test_add_alias_multiple_mailboxes(flask_client): user = login(flask_client) Session.commit() alias_suffix = AliasSuffix( is_custom=False, suffix=f".12345@{EMAIL_DOMAIN}", is_premium=False, domain=EMAIL_DOMAIN, ) signed_alias_suffix = signer.sign(alias_suffix.serialize()).decode() # create with a multiple mailboxes mb1 = Mailbox.create(user_id=user.id, email="*****@*****.**", verified=True) Session.commit() r = flask_client.post( url_for("dashboard.custom_alias"), data={ "prefix": "prefix", "signed-alias-suffix": signed_alias_suffix, "mailboxes": [user.default_mailbox_id, mb1.id], }, follow_redirects=True, ) assert r.status_code == 200 assert f"Alias prefix.12345@{EMAIL_DOMAIN} has been created" in str(r.data) alias = Alias.order_by(Alias.created_at.desc()).first() assert alias._mailboxes
def test_minimal_payload(flask_client): user = login(flask_client) word = random_word() suffix = f".{word}@{EMAIL_DOMAIN}" signed_suffix = signer.sign(suffix).decode() r = flask_client.post( "/api/v3/alias/custom/new", json={ "alias_prefix": "prefix", "signed_suffix": signed_suffix, "mailbox_ids": [user.default_mailbox_id], }, ) assert r.status_code == 201 assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}" res = r.json assert "id" in res assert "email" in res assert "creation_date" in res assert "creation_timestamp" in res assert "nb_forward" in res assert "nb_block" in res assert "nb_reply" in res assert "enabled" in res new_alias: Alias = Alias.get_by(email=r.json["alias"]) assert len(new_alias.mailboxes) == 1
def test_too_many_requests(flask_client): user = login(flask_client) # create a custom domain CustomDomain.create(user_id=user.id, domain="ab.cd", verified=True, commit=True) # can't create more than 5 aliases in 1 minute for i in range(7): signed_suffix = signer.sign("@ab.cd").decode() r = flask_client.post( "/api/v3/alias/custom/new", json={ "alias_prefix": f"prefix{i}", "signed_suffix": signed_suffix, "mailbox_ids": [user.default_mailbox_id], }, ) # to make flask-limiter work with unit test # https://github.com/alisaifee/flask-limiter/issues/147#issuecomment-642683820 g._rate_limiting_complete = False else: # last request assert r.status_code == 429 assert r.json == {"error": "Rate limit exceeded"}
def test_too_many_requests(flask_client): user = login(flask_client) # create a custom domain CustomDomain.create(user_id=user.id, domain="ab.cd", verified=True, commit=True) # can't create more than 5 aliases in 1 minute for i in range(7): signed_suffix = signer.sign("@ab.cd").decode() r = flask_client.post( url_for("dashboard.custom_alias"), data={ "prefix": f"prefix{i}", "suffix": signed_suffix, "mailboxes": [user.default_mailbox_id], }, follow_redirects=True, ) # to make flask-limiter work with unit test # https://github.com/alisaifee/flask-limiter/issues/147#issuecomment-642683820 g._rate_limiting_complete = False else: # last request assert r.status_code == 429 assert "Whoa, slow down there, pardner!" in str(r.data)
def test_add_alias_success(flask_client): user = login(flask_client) alias_suffix = AliasSuffix( is_custom=False, suffix=f".12345@{EMAIL_DOMAIN}", is_premium=False, domain=EMAIL_DOMAIN, ) signed_alias_suffix = signer.sign(alias_suffix.serialize()).decode() # create with a single mailbox r = flask_client.post( url_for("dashboard.custom_alias"), data={ "prefix": "prefix", "signed-alias-suffix": signed_alias_suffix, "mailboxes": [user.default_mailbox_id], }, follow_redirects=True, ) assert r.status_code == 200 assert f"Alias prefix.12345@{EMAIL_DOMAIN} has been created" in str(r.data) alias = Alias.order_by(Alias.created_at.desc()).first() assert not alias._mailboxes
def test_add_alias_multiple_mailboxes(flask_client): user = login(flask_client) db.session.commit() word = random_word() suffix = f".{word}@{EMAIL_DOMAIN}" suffix = signer.sign(suffix).decode() # create with a multiple mailboxes mb1 = Mailbox.create(user_id=user.id, email="*****@*****.**", verified=True) db.session.commit() r = flask_client.post( url_for("dashboard.custom_alias"), data={ "prefix": "prefix", "suffix": suffix, "mailboxes": [user.default_mailbox_id, mb1.id], }, follow_redirects=True, ) assert r.status_code == 200 assert f"Alias prefix.{word}@{EMAIL_DOMAIN} has been created" in str( r.data) alias = Alias.query.order_by(Alias.created_at.desc()).first() assert alias._mailboxes
def test_success_v3(flask_client): user = User.create( email="[email protected]", password="******", name="Test User", activated=True, ) db.session.commit() # create api_key api_key = ApiKey.create(user.id, "for test") db.session.commit() # create another mailbox mb = Mailbox.create(user_id=user.id, email="*****@*****.**", verified=True) db.session.commit() # create new alias with note word = random_word() suffix = f".{word}@{EMAIL_DOMAIN}" suffix = signer.sign(suffix).decode() r = flask_client.post( url_for("api.new_custom_alias_v3", hostname="www.test.com"), headers={"Authentication": api_key.code}, json={ "alias_prefix": "prefix", "signed_suffix": suffix, "note": "test note", "mailbox_ids": [user.default_mailbox_id, mb.id], "name": "your name", }, ) assert r.status_code == 201 assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}" # assert returned field res = r.json assert "id" in res assert "email" in res assert "creation_date" in res assert "creation_timestamp" in res assert "nb_forward" in res assert "nb_block" in res assert "nb_reply" in res assert "enabled" in res assert "note" in res assert res["name"] == "your name" new_alias: Alias = Alias.get_by(email=r.json["alias"]) assert new_alias.note == "test note" assert len(new_alias.mailboxes) == 2
def test_cannot_create_alias_in_trash(flask_client): user = User.create(email="[email protected]", password="******", name="Test User", activated=True) db.session.commit() # create api_key api_key = ApiKey.create(user.id, "for test") db.session.commit() # create a custom domain CustomDomain.create(user_id=user.id, domain="ab.cd", verified=True) db.session.commit() # create new alias with note suffix = "@ab.cd" suffix = signer.sign(suffix).decode() r = flask_client.post( url_for("api.new_custom_alias_v2", hostname="www.test.com"), headers={"Authentication": api_key.code}, json={ "alias_prefix": "prefix", "signed_suffix": suffix, "note": "test note", }, ) # assert alias creation is successful assert r.status_code == 201 assert r.json["alias"] == "*****@*****.**" # delete alias: it's going to be moved to ab.cd trash alias = Alias.get_by(email="*****@*****.**") assert alias.custom_domain_id delete_alias(alias, user) # try to create the same alias, will fail as the alias is in trash r = flask_client.post( url_for("api.new_custom_alias_v2", hostname="www.test.com"), headers={"Authentication": api_key.code}, json={ "alias_prefix": "prefix", "signed_suffix": suffix, "note": "test note", }, ) assert r.status_code == 409
def test_full_payload(flask_client): """Create alias with: - additional mailbox - note - name - hostname (in URL) """ user = login(flask_client) # create another mailbox mb = Mailbox.create(user_id=user.id, email="*****@*****.**", verified=True) Session.commit() word = random_word() suffix = f".{word}@{EMAIL_DOMAIN}" signed_suffix = signer.sign(suffix).decode() assert AliasUsedOn.count() == 0 r = flask_client.post( "/api/v3/alias/custom/new?hostname=example.com", json={ "alias_prefix": "prefix", "signed_suffix": signed_suffix, "note": "test note", "mailbox_ids": [user.default_mailbox_id, mb.id], "name": "your name", }, ) assert r.status_code == 201 assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}" # assert returned field res = r.json assert res["note"] == "test note" assert res["name"] == "your name" new_alias: Alias = Alias.get_by(email=r.json["alias"]) assert new_alias.note == "test note" assert len(new_alias.mailboxes) == 2 alias_used_on = AliasUsedOn.first() assert alias_used_on.alias_id == new_alias.id assert alias_used_on.hostname == "example.com"
def test_mailbox_ids_is_not_an_array(flask_client): login(flask_client) word = random_word() suffix = f".{word}@{EMAIL_DOMAIN}" signed_suffix = signer.sign(suffix).decode() r = flask_client.post( "/api/v3/alias/custom/new", json={ "alias_prefix": "prefix", "signed_suffix": signed_suffix, "mailbox_ids": "not an array", }, ) assert r.status_code == 400 assert r.json == {"error": "mailbox_ids must be an array of id"}
def test_add_alias_in_global_trash(flask_client): user = login(flask_client) Session.commit() another_user = User.create( email="[email protected]", password="******", name="Test User", activated=True, commit=True, ) word = random_word() suffix = f".{word}@{EMAIL_DOMAIN}" alias_suffix = AliasSuffix(is_custom=False, suffix=suffix, is_premium=False, domain=EMAIL_DOMAIN) signed_alias_suffix = signer.sign(alias_suffix.serialize()).decode() # delete an alias: alias should go the DeletedAlias alias = Alias.create( user_id=another_user.id, email=f"prefix{suffix}", mailbox_id=another_user.default_mailbox_id, commit=True, ) assert DeletedAlias.count() == 0 delete_alias(alias, another_user) assert DeletedAlias.count() == 1 # create the same alias, should return error r = flask_client.post( url_for("dashboard.custom_alias"), data={ "prefix": "prefix", "signed-alias-suffix": signed_alias_suffix, "mailboxes": [user.default_mailbox_id], }, follow_redirects=True, ) assert r.status_code == 200 assert f"prefix{suffix} cannot be used" in r.get_data(True)
def test_success_v2(flask_client): user = User.create(email="[email protected]", password="******", name="Test User", activated=True) db.session.commit() # create api_key api_key = ApiKey.create(user.id, "for test") db.session.commit() # create new alias with note word = random_word() suffix = f".{word}@{EMAIL_DOMAIN}" suffix = signer.sign(suffix).decode() r = flask_client.post( url_for("api.new_custom_alias_v2", hostname="www.test.com"), headers={"Authentication": api_key.code}, json={ "alias_prefix": "prefix", "signed_suffix": suffix, "note": "test note", }, ) assert r.status_code == 201 assert r.json["alias"] == f"prefix.{word}@{EMAIL_DOMAIN}" # assert returned field res = r.json assert "id" in res assert "email" in res assert "creation_date" in res assert "creation_timestamp" in res assert "nb_forward" in res assert "nb_block" in res assert "nb_reply" in res assert "enabled" in res assert "note" in res new_ge = Alias.get_by(email=r.json["alias"]) assert new_ge.note == "test note"
def test_custom_domain_alias(flask_client): user = login(flask_client) # create a custom domain CustomDomain.create(user_id=user.id, domain="ab.cd", verified=True, commit=True) signed_suffix = signer.sign("@ab.cd").decode() r = flask_client.post( "/api/v3/alias/custom/new", json={ "alias_prefix": "prefix", "signed_suffix": signed_suffix, "mailbox_ids": [user.default_mailbox_id], }, ) assert r.status_code == 201 assert r.json["alias"] == "*****@*****.**"
def test_add_alias_in_custom_domain_trash(flask_client): user = login(flask_client) custom_domain = CustomDomain.create(user_id=user.id, domain="ab.cd", ownership_verified=True, commit=True) # delete a custom-domain alias: alias should go the DomainDeletedAlias alias = Alias.create( user_id=user.id, email="*****@*****.**", custom_domain_id=custom_domain.id, mailbox_id=user.default_mailbox_id, commit=True, ) assert DomainDeletedAlias.count() == 0 delete_alias(alias, user) assert DomainDeletedAlias.count() == 1 # create the same alias, should return error suffix = "@ab.cd" alias_suffix = AliasSuffix(is_custom=False, suffix=suffix, is_premium=False, domain=EMAIL_DOMAIN) signed_alias_suffix = signer.sign(alias_suffix.serialize()).decode() r = flask_client.post( url_for("dashboard.custom_alias"), data={ "prefix": "prefix", "signed-alias-suffix": signed_alias_suffix, "mailboxes": [user.default_mailbox_id], }, follow_redirects=True, ) assert r.status_code == 200 assert "You have deleted this alias before. You can restore it on" in r.get_data( True)
def test_invalid_alias_2_consecutive_dots(flask_client): user = login(flask_client) word = random_word() suffix = f".{word}@{EMAIL_DOMAIN}" signed_suffix = signer.sign(suffix).decode() r = flask_client.post( "/api/v3/alias/custom/new", json={ "alias_prefix": "prefix.", # with the trailing dot, the alias will have 2 consecutive dots "signed_suffix": signed_suffix, "mailbox_ids": [user.default_mailbox_id], }, ) assert r.status_code == 400 assert r.json == { "error": "2 consecutive dot signs aren't allowed in an email address" }
def test_add_alias_success(flask_client): user = login(flask_client) word = random_word() suffix = f".{word}@{EMAIL_DOMAIN}" suffix = signer.sign(suffix).decode() # create with a single mailbox r = flask_client.post( url_for("dashboard.custom_alias"), data={ "prefix": "prefix", "suffix": suffix, "mailboxes": [user.default_mailbox_id], }, follow_redirects=True, ) assert r.status_code == 200 assert f"Alias prefix.{word}@{EMAIL_DOMAIN} has been created" in str( r.data) alias = Alias.query.order_by(Alias.created_at.desc()).first() assert not alias._mailboxes
def test_add_alias_in_custom_domain_trash(flask_client): user = login(flask_client) db.session.commit() custom_domain = CustomDomain.create(user_id=user.id, domain="ab.cd", verified=True, commit=True) # delete a custom-domain alias: alias should go the DomainDeletedAlias alias = Alias.create( user_id=user.id, email=f"*****@*****.**", custom_domain_id=custom_domain.id, mailbox_id=user.default_mailbox_id, commit=True, ) assert DomainDeletedAlias.query.count() == 0 delete_alias(alias, user) assert DomainDeletedAlias.query.count() == 1 # create the same alias, should return error suffix = f"@ab.cd" signed_suffix = signer.sign(suffix).decode() r = flask_client.post( url_for("dashboard.custom_alias"), data={ "prefix": "prefix", "suffix": signed_suffix, "mailboxes": [user.default_mailbox_id], }, follow_redirects=True, ) assert r.status_code == 200 assert "You have deleted this alias before. You can restore it on" in r.get_data( True)
def test_add_already_existed_alias(flask_client): user = login(flask_client) db.session.commit() another_user = User.create( email="[email protected]", password="******", name="Test User", activated=True, commit=True, ) word = random_word() suffix = f".{word}@{EMAIL_DOMAIN}" signed_suffix = signer.sign(suffix).decode() # alias already exist Alias.create( user_id=another_user.id, email=f"prefix{suffix}", mailbox_id=another_user.default_mailbox_id, commit=True, ) # create the same alias, should return error r = flask_client.post( url_for("dashboard.custom_alias"), data={ "prefix": "prefix", "suffix": signed_suffix, "mailboxes": [user.default_mailbox_id], }, follow_redirects=True, ) assert r.status_code == 200 assert f"prefix{suffix} cannot be used" in r.get_data(True)