mirror of
https://github.com/simple-login/app.git
synced 2024-11-13 07:31:12 +01:00
Merge pull request #900 from simple-login/refactor-test
refactor test: no hardcode a@b.c, make sure each test has a different user
This commit is contained in:
commit
6f7ab01487
21 changed files with 131 additions and 415 deletions
|
@ -4,6 +4,7 @@ from app.config import PAGE_LIMIT
|
|||
from app.db import Session
|
||||
from app.email_utils import is_reverse_alias
|
||||
from app.models import User, ApiKey, Alias, Contact, EmailLog, Mailbox
|
||||
from tests.api.utils import get_new_user_and_api_key
|
||||
from tests.utils import login
|
||||
|
||||
|
||||
|
@ -209,14 +210,7 @@ def test_delete_alias(flask_client):
|
|||
|
||||
|
||||
def test_toggle_alias(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
|
||||
# create api_key
|
||||
api_key = ApiKey.create(user.id, "for test")
|
||||
Session.commit()
|
||||
user, api_key = get_new_user_and_api_key()
|
||||
|
||||
alias = Alias.create_new_random(user)
|
||||
Session.commit()
|
||||
|
@ -231,14 +225,7 @@ def test_toggle_alias(flask_client):
|
|||
|
||||
|
||||
def test_alias_activities(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
|
||||
# create api_key
|
||||
api_key = ApiKey.create(user.id, "for test")
|
||||
Session.commit()
|
||||
user, api_key = get_new_user_and_api_key()
|
||||
|
||||
alias = Alias.create_new_random(user)
|
||||
Session.commit()
|
||||
|
@ -292,14 +279,7 @@ def test_alias_activities(flask_client):
|
|||
|
||||
|
||||
def test_update_alias(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
|
||||
# create api_key
|
||||
api_key = ApiKey.create(user.id, "for test")
|
||||
Session.commit()
|
||||
user, api_key = get_new_user_and_api_key()
|
||||
|
||||
alias = Alias.create_new_random(user)
|
||||
Session.commit()
|
||||
|
@ -314,17 +294,10 @@ def test_update_alias(flask_client):
|
|||
|
||||
|
||||
def test_update_alias_mailbox(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
user, api_key = get_new_user_and_api_key()
|
||||
|
||||
mb = Mailbox.create(user_id=user.id, email="ab@cd.com", verified=True)
|
||||
|
||||
# create api_key
|
||||
api_key = ApiKey.create(user.id, "for test")
|
||||
Session.commit()
|
||||
|
||||
alias = Alias.create_new_random(user)
|
||||
Session.commit()
|
||||
|
||||
|
@ -346,14 +319,7 @@ def test_update_alias_mailbox(flask_client):
|
|||
|
||||
|
||||
def test_update_alias_name(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
|
||||
# create api_key
|
||||
api_key = ApiKey.create(user.id, "for test")
|
||||
Session.commit()
|
||||
user, api_key = get_new_user_and_api_key()
|
||||
|
||||
alias = Alias.create_new_random(user)
|
||||
Session.commit()
|
||||
|
@ -379,18 +345,11 @@ def test_update_alias_name(flask_client):
|
|||
|
||||
|
||||
def test_update_alias_mailboxes(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
user, api_key = get_new_user_and_api_key()
|
||||
|
||||
mb1 = Mailbox.create(user_id=user.id, email="ab1@cd.com", verified=True)
|
||||
mb2 = Mailbox.create(user_id=user.id, email="ab2@cd.com", verified=True)
|
||||
|
||||
# create api_key
|
||||
api_key = ApiKey.create(user.id, "for test")
|
||||
Session.commit()
|
||||
|
||||
alias = Alias.create_new_random(user)
|
||||
Session.commit()
|
||||
|
||||
|
@ -416,14 +375,7 @@ def test_update_alias_mailboxes(flask_client):
|
|||
|
||||
|
||||
def test_update_disable_pgp(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
|
||||
# create api_key
|
||||
api_key = ApiKey.create(user.id, "for test")
|
||||
Session.commit()
|
||||
user, api_key = get_new_user_and_api_key()
|
||||
|
||||
alias = Alias.create_new_random(user)
|
||||
Session.commit()
|
||||
|
@ -564,14 +516,7 @@ def test_create_contact_route_invalid_contact_email(flask_client):
|
|||
|
||||
|
||||
def test_delete_contact(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
|
||||
# create api_key
|
||||
api_key = ApiKey.create(user.id, "for test")
|
||||
Session.commit()
|
||||
user, api_key = get_new_user_and_api_key()
|
||||
|
||||
alias = Alias.create_new_random(user)
|
||||
Session.commit()
|
||||
|
@ -594,14 +539,7 @@ def test_delete_contact(flask_client):
|
|||
|
||||
|
||||
def test_get_alias(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
|
||||
# create api_key
|
||||
api_key = ApiKey.create(user.id, "for test")
|
||||
Session.commit()
|
||||
user, api_key = get_new_user_and_api_key()
|
||||
|
||||
# create more aliases than PAGE_LIMIT
|
||||
alias = Alias.create_new_random(user)
|
||||
|
|
|
@ -1,19 +1,13 @@
|
|||
from flask import url_for
|
||||
|
||||
from app.db import Session
|
||||
from app.models import User, ApiKey, AliasUsedOn, Alias
|
||||
from app.models import AliasUsedOn, Alias
|
||||
from tests.api.utils import get_new_user_and_api_key
|
||||
from tests.utils import login
|
||||
|
||||
|
||||
def test_different_scenarios_v4(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
|
||||
# create api_key
|
||||
api_key = ApiKey.create(user.id, "for test")
|
||||
Session.commit()
|
||||
user, api_key = get_new_user_and_api_key()
|
||||
|
||||
# <<< without hostname >>>
|
||||
r = flask_client.get(
|
||||
|
@ -51,14 +45,7 @@ def test_different_scenarios_v4(flask_client):
|
|||
|
||||
|
||||
def test_different_scenarios_v4_2(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
|
||||
# create api_key
|
||||
api_key = ApiKey.create(user.id, "for test")
|
||||
Session.commit()
|
||||
user, api_key = get_new_user_and_api_key()
|
||||
|
||||
# <<< without hostname >>>
|
||||
r = flask_client.get(
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -3,20 +3,13 @@ from flask import url_for
|
|||
from itsdangerous import Signer
|
||||
|
||||
from app.config import FLASK_SECRET
|
||||
from app.db import Session
|
||||
from app.models import User
|
||||
from tests.utils import create_new_user
|
||||
|
||||
|
||||
def test_auth_mfa_success(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
enable_otp=True,
|
||||
otp_secret="base32secret3232",
|
||||
)
|
||||
Session.commit()
|
||||
user = create_new_user()
|
||||
user.enable_otp = True
|
||||
user.otp_secret = "base32secret3232"
|
||||
|
||||
totp = pyotp.TOTP(user.otp_secret)
|
||||
s = Signer(FLASK_SECRET)
|
||||
|
@ -34,15 +27,9 @@ def test_auth_mfa_success(flask_client):
|
|||
|
||||
|
||||
def test_auth_wrong_mfa_key(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
enable_otp=True,
|
||||
otp_secret="base32secret3232",
|
||||
)
|
||||
Session.commit()
|
||||
user = create_new_user()
|
||||
user.enable_otp = True
|
||||
user.otp_secret = "base32secret3232"
|
||||
|
||||
totp = pyotp.TOTP(user.otp_secret)
|
||||
|
||||
|
|
|
@ -1,18 +1,12 @@
|
|||
from flask import url_for
|
||||
|
||||
from app.db import Session
|
||||
from app.models import User, ApiKey, Notification
|
||||
from app.models import Notification
|
||||
from tests.api.utils import get_new_user_and_api_key
|
||||
|
||||
|
||||
def test_get_notifications(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
|
||||
# create api_key
|
||||
api_key = ApiKey.create(user.id, "for test")
|
||||
Session.commit()
|
||||
user, api_key = get_new_user_and_api_key()
|
||||
|
||||
# create some notifications
|
||||
Notification.create(user_id=user.id, message="Test message 1")
|
||||
|
@ -44,14 +38,7 @@ def test_get_notifications(flask_client):
|
|||
|
||||
|
||||
def test_mark_notification_as_read(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
|
||||
# create api_key
|
||||
api_key = ApiKey.create(user.id, "for test")
|
||||
Session.commit()
|
||||
user, api_key = get_new_user_and_api_key()
|
||||
|
||||
Notification.create(id=1, user_id=user.id, message="Test message 1")
|
||||
Session.commit()
|
||||
|
|
|
@ -1,18 +1,12 @@
|
|||
from app.api.serializer import get_alias_infos_with_pagination_v3
|
||||
from app.config import PAGE_LIMIT
|
||||
from app.db import Session
|
||||
from app.models import User, Alias, Mailbox, Contact
|
||||
from tests.utils import create_random_user
|
||||
from app.models import Alias, Mailbox, Contact
|
||||
from tests.utils import create_new_user
|
||||
|
||||
|
||||
def test_get_alias_infos_with_pagination_v3(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
|
||||
# user has 1 alias that's automatically created when the account is created
|
||||
alias_infos = get_alias_infos_with_pagination_v3(user)
|
||||
|
@ -32,13 +26,7 @@ def test_get_alias_infos_with_pagination_v3(flask_client):
|
|||
|
||||
def test_get_alias_infos_with_pagination_v3_query_alias_email(flask_client):
|
||||
"""test the query on the alias email"""
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
|
||||
alias = Alias.first()
|
||||
|
||||
|
@ -51,13 +39,7 @@ def test_get_alias_infos_with_pagination_v3_query_alias_email(flask_client):
|
|||
|
||||
def test_get_alias_infos_with_pagination_v3_query_alias_mailbox(flask_client):
|
||||
"""test the query on the alias mailbox email"""
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
alias = Alias.first()
|
||||
alias_infos = get_alias_infos_with_pagination_v3(user, mailbox_id=alias.mailbox_id)
|
||||
assert len(alias_infos) == 1
|
||||
|
@ -65,13 +47,7 @@ def test_get_alias_infos_with_pagination_v3_query_alias_mailbox(flask_client):
|
|||
|
||||
def test_get_alias_infos_with_pagination_v3_query_alias_mailboxes(flask_client):
|
||||
"""test the query on the alias additional mailboxes"""
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
alias = Alias.first()
|
||||
mb = Mailbox.create(user_id=user.id, email="mb@gmail.com")
|
||||
alias._mailboxes.append(mb)
|
||||
|
@ -86,13 +62,7 @@ def test_get_alias_infos_with_pagination_v3_query_alias_mailboxes(flask_client):
|
|||
|
||||
def test_get_alias_infos_with_pagination_v3_query_alias_note(flask_client):
|
||||
"""test the query on the alias note"""
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
|
||||
alias = Alias.first()
|
||||
alias.note = "test note"
|
||||
|
@ -104,13 +74,7 @@ def test_get_alias_infos_with_pagination_v3_query_alias_note(flask_client):
|
|||
|
||||
def test_get_alias_infos_with_pagination_v3_query_alias_name(flask_client):
|
||||
"""test the query on the alias name"""
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
|
||||
alias = Alias.first()
|
||||
alias.name = "Test Name"
|
||||
|
@ -124,13 +88,7 @@ def test_get_alias_infos_with_pagination_v3_no_duplicate(flask_client):
|
|||
"""When an alias belongs to multiple mailboxes, make sure get_alias_infos_with_pagination_v3
|
||||
returns no duplicates
|
||||
"""
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
|
||||
alias = Alias.first()
|
||||
mb = Mailbox.create(user_id=user.id, email="mb@gmail.com")
|
||||
|
@ -147,7 +105,7 @@ def test_get_alias_infos_with_pagination_v3_no_duplicate_when_empty_contact(
|
|||
"""
|
||||
Make sure an alias is returned once when it has 2 contacts that have no email log activity
|
||||
"""
|
||||
user = create_random_user()
|
||||
user = create_new_user()
|
||||
alias = Alias.first()
|
||||
|
||||
Contact.create(
|
||||
|
@ -170,13 +128,7 @@ def test_get_alias_infos_with_pagination_v3_no_duplicate_when_empty_contact(
|
|||
|
||||
def test_get_alias_infos_pinned_alias(flask_client):
|
||||
"""Different scenarios with pinned alias"""
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
|
||||
# to have 3 pages: 2*PAGE_LIMIT + the alias automatically created for a new account
|
||||
for _ in range(2 * PAGE_LIMIT):
|
||||
|
|
|
@ -1,19 +1,12 @@
|
|||
from flask import url_for
|
||||
|
||||
from app.db import Session
|
||||
from app.models import User, ApiKey
|
||||
from app.models import User
|
||||
from tests.api.utils import get_new_user_and_api_key
|
||||
from tests.utils import login
|
||||
|
||||
|
||||
def test_user_in_trial(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
|
||||
# create api_key
|
||||
api_key = ApiKey.create(user.id, "for test")
|
||||
Session.commit()
|
||||
user, api_key = get_new_user_and_api_key()
|
||||
|
||||
r = flask_client.get(
|
||||
url_for("api.user_info"), headers={"Authentication": api_key.code}
|
||||
|
@ -23,7 +16,7 @@ def test_user_in_trial(flask_client):
|
|||
assert r.json == {
|
||||
"is_premium": True,
|
||||
"name": "Test User",
|
||||
"email": "a@b.c",
|
||||
"email": user.email,
|
||||
"in_trial": True,
|
||||
"profile_picture_url": None,
|
||||
}
|
||||
|
@ -40,16 +33,7 @@ def test_wrong_api_key(flask_client):
|
|||
|
||||
|
||||
def test_create_api_key(flask_client):
|
||||
# create user, user is activated
|
||||
User.create(email="a@b.c", password="password", name="Test User", activated=True)
|
||||
Session.commit()
|
||||
|
||||
# login user
|
||||
flask_client.post(
|
||||
url_for("auth.login"),
|
||||
data={"email": "a@b.c", "password": "password"},
|
||||
follow_redirects=True,
|
||||
)
|
||||
login(flask_client)
|
||||
|
||||
# create api key
|
||||
r = flask_client.post(url_for("api.create_api_key"), json={"device": "Test device"})
|
||||
|
@ -59,16 +43,7 @@ def test_create_api_key(flask_client):
|
|||
|
||||
|
||||
def test_logout(flask_client):
|
||||
# create user, user is activated
|
||||
User.create(email="a@b.c", password="password", name="Test User", activated=True)
|
||||
Session.commit()
|
||||
|
||||
# login user
|
||||
flask_client.post(
|
||||
url_for("auth.login"),
|
||||
data={"email": "a@b.c", "password": "password"},
|
||||
follow_redirects=True,
|
||||
)
|
||||
login(flask_client)
|
||||
|
||||
# logout
|
||||
r = flask_client.get(
|
||||
|
|
13
tests/api/utils.py
Normal file
13
tests/api/utils.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
from typing import Tuple
|
||||
|
||||
from app.models import User, ApiKey
|
||||
from tests.utils import create_new_user
|
||||
|
||||
|
||||
def get_new_user_and_api_key() -> Tuple[User, ApiKey]:
|
||||
user = create_new_user()
|
||||
|
||||
# create api_key
|
||||
api_key = ApiKey.create(user.id, "for test", commit=True)
|
||||
|
||||
return user, api_key
|
|
@ -1,19 +1,17 @@
|
|||
from flask import url_for
|
||||
|
||||
from app.db import Session
|
||||
from app.models import User
|
||||
from tests.utils import create_new_user
|
||||
|
||||
|
||||
def test_unactivated_user_login(flask_client):
|
||||
"""Start with a blank database."""
|
||||
|
||||
# create user, user is not activated
|
||||
User.create(email="a@b.c", password="password", name="Test User")
|
||||
user = create_new_user()
|
||||
user.activated = False
|
||||
Session.commit()
|
||||
|
||||
r = flask_client.post(
|
||||
url_for("auth.login"),
|
||||
data={"email": "a@b.c", "password": "password"},
|
||||
data={"email": user.email, "password": "password"},
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
|
@ -25,15 +23,11 @@ def test_unactivated_user_login(flask_client):
|
|||
|
||||
|
||||
def test_activated_user_login(flask_client):
|
||||
"""Start with a blank database."""
|
||||
|
||||
# create user, user is activated
|
||||
User.create(email="a@b.c", password="password", name="Test User", activated=True)
|
||||
Session.commit()
|
||||
user = create_new_user()
|
||||
|
||||
r = flask_client.post(
|
||||
url_for("auth.login"),
|
||||
data={"email": "a@b.c", "password": "password"},
|
||||
data={"email": user.email, "password": "password"},
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from flask import url_for
|
||||
|
||||
from app.db import Session
|
||||
from app.email_utils import get_email_domain_part
|
||||
from app.models import Mailbox
|
||||
from tests.utils import login
|
||||
|
||||
|
@ -28,7 +29,7 @@ def test_add_domain_same_as_user_email(flask_client):
|
|||
|
||||
r = flask_client.post(
|
||||
url_for("dashboard.custom_domain"),
|
||||
data={"form-name": "create", "domain": "b.c"}, # user email is a@b.c
|
||||
data={"form-name": "create", "domain": get_email_domain_part(user.email)},
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
|
|
|
@ -9,14 +9,12 @@ from app.email.rate_limit import (
|
|||
rate_limited_for_mailbox,
|
||||
rate_limited_reply_phase,
|
||||
)
|
||||
from app.models import User, Alias, EmailLog, Contact
|
||||
from app.models import Alias, EmailLog, Contact
|
||||
from tests.utils import create_new_user
|
||||
|
||||
|
||||
def test_rate_limited_forward_phase_for_alias(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
user = create_new_user()
|
||||
|
||||
# no rate limiting for a new alias
|
||||
alias = Alias.create_new_random(user)
|
||||
|
@ -43,10 +41,7 @@ def test_rate_limited_forward_phase_for_alias(flask_client):
|
|||
|
||||
|
||||
def test_rate_limited_forward_phase_for_mailbox(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
user = create_new_user()
|
||||
|
||||
alias = Alias.create_new_random(user)
|
||||
Session.commit()
|
||||
|
@ -88,10 +83,7 @@ def test_rate_limited_reply_phase(flask_client):
|
|||
# no rate limiting when reply_email does not exist
|
||||
assert not rate_limited_reply_phase("not-exist-reply@alias.com")
|
||||
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
user = create_new_user()
|
||||
|
||||
alias = Alias.create_new_random(user)
|
||||
Session.commit()
|
||||
|
|
|
@ -1,13 +1,7 @@
|
|||
from app.models import User
|
||||
from tests.utils import create_new_user
|
||||
|
||||
|
||||
def test_available_sl_domains(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
|
||||
assert set(user.available_sl_domains()) == {"d1.test", "d2.test", "sl.local"}
|
||||
|
|
|
@ -117,7 +117,7 @@ def test_authorize_page_login_user(flask_client):
|
|||
|
||||
html = r.get_data(as_text=True)
|
||||
assert r.status_code == 200
|
||||
assert "a@b.c (Personal Email)" in html
|
||||
assert f"{user.email} (Personal Email)" in html
|
||||
|
||||
|
||||
def test_authorize_code_flow_no_openid_scope(flask_client):
|
||||
|
|
|
@ -1,16 +1,11 @@
|
|||
from app.alias_utils import delete_alias, check_alias_prefix
|
||||
from app.db import Session
|
||||
from app.models import User, Alias, DeletedAlias
|
||||
from app.models import Alias, DeletedAlias
|
||||
from tests.utils import create_new_user
|
||||
|
||||
|
||||
def test_delete_alias(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
alias = Alias.create(
|
||||
user_id=user.id,
|
||||
email="first@d1.test",
|
||||
|
@ -26,13 +21,7 @@ def test_delete_alias(flask_client):
|
|||
|
||||
def test_delete_alias_already_in_trash(flask_client):
|
||||
"""delete an alias that's already in alias trash"""
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
alias = Alias.create(
|
||||
user_id=user.id,
|
||||
email="first@d1.test",
|
||||
|
|
|
@ -1,16 +1,12 @@
|
|||
import arrow
|
||||
|
||||
from app.models import User, CoinbaseSubscription
|
||||
from app.models import CoinbaseSubscription
|
||||
from cron import notify_manual_sub_end
|
||||
from tests.utils import create_new_user
|
||||
|
||||
|
||||
def test_notify_manual_sub_end(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
|
||||
CoinbaseSubscription.create(
|
||||
user_id=user.id, end_at=arrow.now().shift(days=13, hours=2), commit=True
|
||||
|
|
|
@ -10,7 +10,6 @@ from app.config import BOUNCE_EMAIL, EMAIL_DOMAIN, ALERT_DMARC_FAILED_REPLY_PHAS
|
|||
from app.db import Session
|
||||
from app.email import headers, status
|
||||
from app.models import (
|
||||
User,
|
||||
Alias,
|
||||
AuthorizedAddress,
|
||||
IgnoredEmail,
|
||||
|
@ -24,17 +23,11 @@ from email_handler import (
|
|||
should_ignore,
|
||||
is_automatic_out_of_office,
|
||||
)
|
||||
from tests.utils import load_eml_file, create_random_user
|
||||
from tests.utils import load_eml_file, create_new_user
|
||||
|
||||
|
||||
def test_get_mailbox_from_mail_from(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
alias = Alias.create(
|
||||
user_id=user.id,
|
||||
email="first@d1.test",
|
||||
|
@ -42,8 +35,8 @@ def test_get_mailbox_from_mail_from(flask_client):
|
|||
commit=True,
|
||||
)
|
||||
|
||||
mb = get_mailbox_from_mail_from("a@b.c", alias)
|
||||
assert mb.email == "a@b.c"
|
||||
mb = get_mailbox_from_mail_from(user.email, alias)
|
||||
assert mb.email == user.email
|
||||
|
||||
mb = get_mailbox_from_mail_from("unauthorized@gmail.com", alias)
|
||||
assert mb is None
|
||||
|
@ -56,7 +49,7 @@ def test_get_mailbox_from_mail_from(flask_client):
|
|||
commit=True,
|
||||
)
|
||||
mb = get_mailbox_from_mail_from("unauthorized@gmail.com", alias)
|
||||
assert mb.email == "a@b.c"
|
||||
assert mb.email == user.email
|
||||
|
||||
|
||||
def test_should_ignore(flask_client):
|
||||
|
@ -82,7 +75,7 @@ def test_is_automatic_out_of_office():
|
|||
|
||||
|
||||
def test_dmarc_forward_quarantine(flask_client):
|
||||
user = create_random_user()
|
||||
user = create_new_user()
|
||||
alias = Alias.create_new_random(user)
|
||||
msg = load_eml_file("dmarc_quarantine.eml", {"alias_email": alias.email})
|
||||
envelope = Envelope()
|
||||
|
@ -105,7 +98,7 @@ def test_dmarc_forward_quarantine(flask_client):
|
|||
|
||||
|
||||
def test_gmail_dmarc_softfail(flask_client):
|
||||
user = create_random_user()
|
||||
user = create_new_user()
|
||||
alias = Alias.create_new_random(user)
|
||||
msg = load_eml_file("dmarc_gmail_softfail.eml", {"alias_email": alias.email})
|
||||
envelope = Envelope()
|
||||
|
@ -119,7 +112,7 @@ def test_gmail_dmarc_softfail(flask_client):
|
|||
|
||||
|
||||
def test_prevent_5xx_from_spf(flask_client):
|
||||
user = create_random_user()
|
||||
user = create_new_user()
|
||||
alias = Alias.create_new_random(user)
|
||||
msg = load_eml_file(
|
||||
"5xx_overwrite_spf.eml",
|
||||
|
@ -133,7 +126,7 @@ def test_prevent_5xx_from_spf(flask_client):
|
|||
|
||||
|
||||
def test_preserve_5xx_with_valid_spf(flask_client):
|
||||
user = create_random_user()
|
||||
user = create_new_user()
|
||||
alias = Alias.create_new_random(user)
|
||||
msg = load_eml_file(
|
||||
"5xx_overwrite_spf.eml",
|
||||
|
@ -147,7 +140,7 @@ def test_preserve_5xx_with_valid_spf(flask_client):
|
|||
|
||||
|
||||
def test_preserve_5xx_with_no_header(flask_client):
|
||||
user = create_random_user()
|
||||
user = create_new_user()
|
||||
alias = Alias.create_new_random(user)
|
||||
msg = load_eml_file(
|
||||
"no_spamd_header.eml",
|
||||
|
@ -166,7 +159,7 @@ def generate_dmarc_result() -> List:
|
|||
|
||||
@pytest.mark.parametrize("dmarc_result", generate_dmarc_result())
|
||||
def test_dmarc_reply_quarantine(flask_client, dmarc_result):
|
||||
user = create_random_user()
|
||||
user = create_new_user()
|
||||
alias = Alias.create_new_random(user)
|
||||
Session.commit()
|
||||
contact = Contact.create(
|
||||
|
|
|
@ -38,7 +38,6 @@ from app.email_utils import (
|
|||
is_invalid_mailbox_domain,
|
||||
)
|
||||
from app.models import (
|
||||
User,
|
||||
CustomDomain,
|
||||
Alias,
|
||||
Contact,
|
||||
|
@ -48,7 +47,7 @@ from app.models import (
|
|||
)
|
||||
|
||||
# flake8: noqa: E101, W191
|
||||
from tests.utils import login, load_eml_file
|
||||
from tests.utils import login, load_eml_file, create_new_user
|
||||
|
||||
|
||||
def test_get_email_domain_part():
|
||||
|
@ -74,13 +73,7 @@ def test_can_be_used_as_personal_email(flask_client):
|
|||
assert not email_can_be_used_as_mailbox("hey@d1.test")
|
||||
|
||||
# custom domain
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
CustomDomain.create(user_id=user.id, domain="ab.cd", verified=True, commit=True)
|
||||
assert not email_can_be_used_as_mailbox("hey@ab.cd")
|
||||
|
||||
|
@ -153,10 +146,7 @@ def test_parse_full_address():
|
|||
|
||||
|
||||
def test_send_email_with_rate_control(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
user = create_new_user()
|
||||
|
||||
for _ in range(MAX_ALERT_24H):
|
||||
assert send_email_with_rate_control(
|
||||
|
@ -496,12 +486,7 @@ def test_to_bytes():
|
|||
|
||||
|
||||
def test_generate_reply_email(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
reply_email = generate_reply_email("test@example.org", user)
|
||||
assert reply_email.endswith(EMAIL_DOMAIN)
|
||||
|
||||
|
@ -511,13 +496,9 @@ def test_generate_reply_email(flask_client):
|
|||
|
||||
def test_generate_reply_email_include_sender_in_reverse_alias(flask_client):
|
||||
# user enables include_sender_in_reverse_alias
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
include_sender_in_reverse_alias=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
user.include_sender_in_reverse_alias = True
|
||||
|
||||
reply_email = generate_reply_email("test@example.org", user)
|
||||
assert reply_email.startswith("test.at.example.org")
|
||||
assert reply_email.endswith(EMAIL_DOMAIN)
|
||||
|
@ -600,13 +581,7 @@ def test_decode_text():
|
|||
|
||||
|
||||
def test_should_disable(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
include_sender_in_reverse_alias=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
alias = Alias.create_new_random(user)
|
||||
Session.commit()
|
||||
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
from app.db import Session
|
||||
from app.jose_utils import make_id_token, verify_id_token
|
||||
from app.models import ClientUser, User, Client
|
||||
from app.models import ClientUser, Client
|
||||
from tests.utils import create_new_user
|
||||
|
||||
|
||||
def test_encode_decode(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c", password="password", name="Test User", activated=True
|
||||
)
|
||||
Session.commit()
|
||||
user = create_new_user()
|
||||
|
||||
client1 = Client.create_new(name="Demo", user_id=user.id)
|
||||
client1.oauth_client_id = "client-id"
|
||||
|
|
|
@ -7,14 +7,13 @@ from app.db import Session
|
|||
from app.email_utils import parse_full_address
|
||||
from app.models import (
|
||||
generate_email,
|
||||
User,
|
||||
Alias,
|
||||
Contact,
|
||||
Mailbox,
|
||||
SenderFormatEnum,
|
||||
EnumE,
|
||||
)
|
||||
from tests.utils import login
|
||||
from tests.utils import login, create_new_user
|
||||
|
||||
|
||||
def test_generate_email(flask_client):
|
||||
|
@ -29,27 +28,15 @@ def test_generate_email(flask_client):
|
|||
|
||||
|
||||
def test_profile_picture_url(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
|
||||
assert user.profile_picture_url() == "http://sl.test/static/default-avatar.png"
|
||||
|
||||
|
||||
def test_suggested_emails_for_user_who_cannot_create_new_alias(flask_client):
|
||||
# make sure user is not in trial
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
trial_end=None,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
user.trial_end = None
|
||||
|
||||
# make sure user runs out of quota to create new email
|
||||
for _ in range(MAX_NB_EMAIL_FREE_PLAN):
|
||||
|
@ -67,26 +54,14 @@ def test_suggested_emails_for_user_who_cannot_create_new_alias(flask_client):
|
|||
|
||||
|
||||
def test_alias_create_random(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
|
||||
alias = Alias.create_new_random(user)
|
||||
assert alias.email.endswith(EMAIL_DOMAIN)
|
||||
|
||||
|
||||
def test_website_send_to(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
|
||||
alias = Alias.create_new_random(user)
|
||||
Session.commit()
|
||||
|
@ -230,13 +205,7 @@ def test_new_addr_unicode(flask_client):
|
|||
|
||||
|
||||
def test_mailbox_delete(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
|
||||
m1 = Mailbox.create(
|
||||
user_id=user.id, email="m1@example.com", verified=True, commit=True
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
import arrow
|
||||
|
||||
from app.db import Session
|
||||
from app.models import User, CoinbaseSubscription
|
||||
from app.models import CoinbaseSubscription
|
||||
from server import handle_coinbase_event
|
||||
from tests.utils import create_new_user
|
||||
|
||||
|
||||
def test_redirect_login_page(flask_client):
|
||||
|
@ -19,13 +20,7 @@ def test_coinbase_webhook(flask_client):
|
|||
|
||||
|
||||
def test_handle_coinbase_event_new_subscription(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
handle_coinbase_event(
|
||||
{"data": {"code": "AAAAAA", "metadata": {"user_id": str(user.id)}}}
|
||||
)
|
||||
|
@ -37,12 +32,7 @@ def test_handle_coinbase_event_new_subscription(flask_client):
|
|||
|
||||
|
||||
def test_handle_coinbase_event_extend_subscription(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
)
|
||||
user = create_new_user()
|
||||
user.trial_end = None
|
||||
Session.commit()
|
||||
|
||||
|
|
|
@ -11,20 +11,32 @@ from flask import url_for
|
|||
|
||||
from app.models import User
|
||||
|
||||
# keep track of the number of user
|
||||
_nb_user = 0
|
||||
|
||||
def login(flask_client) -> User:
|
||||
# create user, user is activated
|
||||
|
||||
def create_new_user() -> User:
|
||||
global _nb_user
|
||||
_nb_user += 1
|
||||
|
||||
# new user has a different email address
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
email=f"{_nb_user}@mailbox.test",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
commit=True,
|
||||
flush=True,
|
||||
)
|
||||
|
||||
return user
|
||||
|
||||
|
||||
def login(flask_client) -> User:
|
||||
user = create_new_user()
|
||||
|
||||
r = flask_client.post(
|
||||
url_for("auth.login"),
|
||||
data={"email": "a@b.c", "password": "password"},
|
||||
data={"email": user.email, "password": "password"},
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
|
@ -38,17 +50,6 @@ def random_token(length: int = 10) -> str:
|
|||
return "".join(random.choices(string.ascii_lowercase + string.digits, k=length))
|
||||
|
||||
|
||||
def create_random_user() -> User:
|
||||
random_email = "{}@{}.com".format(random_token(), random_token())
|
||||
return User.create(
|
||||
email=random_email,
|
||||
password="password",
|
||||
name="Test {}".format(random_token()),
|
||||
activated=True,
|
||||
commit=True,
|
||||
)
|
||||
|
||||
|
||||
def pretty(d):
|
||||
"""pretty print as json"""
|
||||
print(json.dumps(d, indent=2))
|
||||
|
|
Loading…
Reference in a new issue