Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Maintenance/reduce num unittests #2525

821 changes: 239 additions & 582 deletions .github/workflows/ci-testing-deploy.yml

Large diffs are not rendered by default.

19 changes: 11 additions & 8 deletions services/web/server/src/simcore_service_webserver/groups_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,18 @@ async def auto_add_user_to_groups(app: web.Application, user_id: int) -> None:
possible_gids.add(row[groups.c.gid])

# now add the user to these groups if possible
for gid in possible_gids:
await conn.execute(
# pylint: disable=no-value-for-parameter
insert(user_to_groups)
.values(
uid=user_id, gid=gid, access_rights=DEFAULT_GROUP_READ_ACCESS_RIGHTS
async with conn.begin():
for gid in possible_gids:
await conn.execute(
# pylint: disable=no-value-for-parameter
insert(user_to_groups)
.values(
uid=user_id,
gid=gid,
access_rights=DEFAULT_GROUP_READ_ACCESS_RIGHTS,
)
.on_conflict_do_nothing() # in case the user was already added
)
.on_conflict_do_nothing() # in case the user was already added
)


async def add_user_in_group(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
@permission_required("groups.read")
async def list_groups(request: web.Request):
user_id = request[RQT_USERID_KEY]
print("user_id received: ", user_id)
primary_group, user_groups, all_group = await groups_api.list_user_groups(
request.app, user_id
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,64 @@ def inject_tables(postgres_db: sa.engine.Engine):
with postgres_db.connect() as conn:
conn.execute(stmt_create_services)
conn.execute(stmt_create_services_consume_filetypes)
yield
# cleanup
with postgres_db.connect() as conn:
conn.execute(
text(
"DELETE FROM services_consume_filetypes WHERE service_key='simcore/services/dynamic/bio-formats-web' AND service_version='1.0.1' AND filetype='PNG'"
)
)
conn.execute(
text(
"DELETE FROM services_consume_filetypes WHERE service_key='simcore/services/dynamic/raw-graphs' AND service_version='2.11.1' AND filetype='CSV'"
)
)
conn.execute(
text(
"DELETE FROM services_consume_filetypes WHERE service_key='simcore/services/dynamic/bio-formats-web' AND service_version='1.0.1' AND filetype='JPEG'"
)
)
conn.execute(
text(
"DELETE FROM services_consume_filetypes WHERE service_key='simcore/services/dynamic/raw-graphs' AND service_version='2.11.1' AND filetype='TSV'"
)
)
conn.execute(
text(
"DELETE FROM services_consume_filetypes WHERE service_key='simcore/services/dynamic/raw-graphs' AND service_version='2.11.1' AND filetype='XSLX'"
)
)
conn.execute(
text(
"DELETE FROM services_consume_filetypes WHERE service_key='simcore/services/dynamic/raw-graphs' AND service_version='2.11.1' AND filetype='JSON'"
)
)
conn.execute(
text(
"DELETE FROM services_consume_filetypes WHERE service_key='simcore/services/dynamic/jupyter-octave-python-math' AND service_version='1.6.9' AND filetype='PY'"
)
)
conn.execute(
text(
"DELETE FROM services_consume_filetypes WHERE service_key='simcore/services/dynamic/jupyter-octave-python-math' AND service_version='1.6.9' AND filetype='IPYNB'"
)
)
conn.execute(
text(
"DELETE FROM services_meta_data WHERE key='simcore/services/dynamic/raw-graphs' AND version='2.11.1'"
)
)
conn.execute(
text(
"DELETE FROM services_meta_data WHERE key='simcore/services/dynamic/bio-formats-web' AND version='1.0.1'"
)
)
conn.execute(
text(
"DELETE FROM services_meta_data WHERE key='simcore/services/dynamic/jupyter-octave-python-math' AND version='1.6.9'"
)
)


FAKE_VIEWS_LIST = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pytest_simcore.helpers.utils_assert import assert_status
from pytest_simcore.helpers.utils_login import LoggedUser, create_user, log_client_in
from servicelib.aiohttp.application import create_safe_application
from simcore_postgres_database.models.users import users
from simcore_service_webserver.db import setup_db
from simcore_service_webserver.groups import setup_groups
from simcore_service_webserver.groups_api import (
Expand Down Expand Up @@ -522,7 +523,7 @@ async def test_group_access_rights(

@pytest.mark.parametrize(*standard_role_response())
async def test_add_user_gets_added_to_group(
client, standard_groups: List[Dict[str, str]], user_role, expected
client, standard_groups: List[Dict[str, str]], user_role, expected, postgres_db
):
emails = [
"[email protected]",
Expand All @@ -537,8 +538,9 @@ async def test_add_user_gets_added_to_group(
user_data={"role": user_role.name, "email": email},
enable_check=user_role != UserRole.ANONYMOUS,
)
await auto_add_user_to_groups(client.app, user["id"])
print("user_id: ", user["id"])

await auto_add_user_to_groups(client.app, user["id"])
url = client.app.router["list_groups"].url_for()
assert str(url) == f"{PREFIX}"

Expand All @@ -548,3 +550,9 @@ async def test_add_user_gets_added_to_group(
)
if not error:
assert len(data["organizations"]) == (0 if "bad" in email else 1)

# cleanup users
with postgres_db.begin() as conn:
for email in emails:
# pylint: disable=no-value-for-parameter
conn.execute(users.delete().where(users.c.email == email))
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ async def lock_manager(loop, redis_service: URL):
await lm.destroy()



async def test_redlocks_features(lock_manager: Aioredlock):
# Originally https://github.com/joanvila/aioredlock#readme

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@
# pylint:disable=redefined-outer-name
import pytest
from aiohttp import web

from faker import Faker
from pytest_simcore.helpers.utils_assert import assert_error, assert_status
from pytest_simcore.helpers.utils_login import NewInvitation, NewUser, parse_link
from servicelib.aiohttp.rest_responses import unwrap_envelope
from simcore_service_webserver.db_models import ConfirmationAction, UserStatus
from simcore_service_webserver.login.cfg import cfg, get_storage
from simcore_service_webserver.login.registration import get_confirmation_info

EMAIL, PASSWORD = "[email protected]", "password"


async def test_regitration_availibility(client):
async def test_regitration_availibility(client, faker: Faker):
url = client.app.router["auth_register"].url_for()
password = faker.numerify(text="#" * 5)
r = await client.post(
url,
json={
"email": EMAIL,
"password": PASSWORD,
"confirm": PASSWORD,
"email": faker.email(),
"password": password,
"confirm": password,
},
)

Expand Down Expand Up @@ -74,34 +73,45 @@ async def test_registration_with_expired_confirmation(client, monkeypatch):
await assert_error(r, web.HTTPConflict, cfg.MSG_EMAIL_EXISTS)


async def test_registration_without_confirmation(client, monkeypatch):
async def test_registration_without_confirmation(client, monkeypatch, faker: Faker):
monkeypatch.setitem(cfg, "REGISTRATION_CONFIRMATION_REQUIRED", False)
registration_data = {
"password": faker.numerify(text="#" * 5),
"email": faker.email(),
}

db = get_storage(client.app)
url = client.app.router["auth_register"].url_for()
r = await client.post(
url, json={"email": EMAIL, "password": PASSWORD, "confirm": PASSWORD}
url, json={**registration_data, "confirm": registration_data["password"]}
)
data, error = unwrap_envelope(await r.json())

assert r.status == 200, (data, error)
assert cfg.MSG_LOGGED_IN in data["message"]

user = await db.get_user({"email": EMAIL})
user = await db.get_user({"email": registration_data["email"]})
assert user
await db.delete_user(user)


async def test_registration_with_confirmation(client, capsys, monkeypatch):
async def test_registration_with_confirmation(
client, capsys, monkeypatch, faker: Faker
):
monkeypatch.setitem(cfg, "REGISTRATION_CONFIRMATION_REQUIRED", True)
db = get_storage(client.app)
url = client.app.router["auth_register"].url_for()
registration_data = {
"password": faker.numerify(text="#" * 5),
"email": faker.email(),
}
r = await client.post(
url, json={"email": EMAIL, "password": PASSWORD, "confirm": PASSWORD}
url, json={**registration_data, "confirm": registration_data["password"]}
)
data, error = unwrap_envelope(await r.json())
assert r.status == 200, (data, error)

user = await db.get_user({"email": EMAIL})
user = await db.get_user({"email": registration_data["email"]})
assert user["status"] == UserStatus.CONFIRMATION_PENDING.name

assert "verification link" in data["message"]
Expand All @@ -119,7 +129,7 @@ async def test_registration_with_confirmation(client, capsys, monkeypatch):
)
assert resp.status == 200

user = await db.get_user({"email": EMAIL})
user = await db.get_user({"email": registration_data["email"]})
assert user["status"] == UserStatus.ACTIVE.name
await db.delete_user(user)

Expand All @@ -134,7 +144,11 @@ async def test_registration_with_confirmation(client, capsys, monkeypatch):
],
)
async def test_registration_with_invitation(
client, is_invitation_required, has_valid_invitation, expected_response
client,
is_invitation_required,
has_valid_invitation,
expected_response,
faker: Faker,
):
from servicelib.aiohttp.application_keys import APP_CONFIG_KEY
from simcore_service_webserver.login.config import CONFIG_SECTION_NAME
Expand All @@ -150,6 +164,11 @@ async def test_registration_with_invitation(
#
# Front end then creates the following request
#
registration_data = {
"password": faker.numerify(text="#" * 5),
"email": faker.email(),
}

async with NewInvitation(client) as confirmation:
print(get_confirmation_info(confirmation))

Expand All @@ -158,9 +177,8 @@ async def test_registration_with_invitation(
r = await client.post(
url,
json={
"email": EMAIL,
"password": PASSWORD,
"confirm": PASSWORD,
**registration_data,
"confirm": registration_data["password"],
"invitation": confirmation["code"]
if has_valid_invitation
else "WRONG_CODE",
Expand All @@ -171,7 +189,11 @@ async def test_registration_with_invitation(
# check optional fields in body
if not has_valid_invitation or not is_invitation_required:
r = await client.post(
url, json={"email": "new-user" + EMAIL, "password": PASSWORD}
url,
json={
"email": "new-user" + registration_data["email"],
"password": registration_data["password"],
},
)
await assert_status(r, expected_response)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

import pytest
from aiohttp import web
from yarl import URL

from pytest_simcore.helpers.utils_assert import assert_status
from pytest_simcore.helpers.utils_login import NewUser, parse_link, parse_test_marks
from simcore_service_webserver.db_models import ConfirmationAction, UserStatus
from simcore_service_webserver.login.cfg import APP_LOGIN_CONFIG
from simcore_service_webserver.login.utils import get_random_string
from yarl import URL

EMAIL, PASSWORD = "[email protected]", "password"

Expand All @@ -23,7 +22,12 @@ def cfg(client):
async def test_unknown_email(client, capsys, cfg):
reset_url = client.app.router["auth_reset_password"].url_for()

rp = await client.post(reset_url, json={"email": EMAIL,})
rp = await client.post(
reset_url,
json={
"email": EMAIL,
},
)
payload = await rp.text()

assert rp.url_obj.path == reset_url.path
Expand All @@ -37,7 +41,12 @@ async def test_banned_user(client, capsys, cfg):
reset_url = client.app.router["auth_reset_password"].url_for()

async with NewUser({"status": UserStatus.BANNED.name}) as user:
rp = await client.post(reset_url, json={"email": user["email"],})
rp = await client.post(
reset_url,
json={
"email": user["email"],
},
)

assert rp.url_obj.path == reset_url.path
await assert_status(rp, web.HTTPOk, cfg.MSG_EMAIL_SENT.format(**user))
Expand All @@ -50,7 +59,12 @@ async def test_inactive_user(client, capsys, cfg):
reset_url = client.app.router["auth_reset_password"].url_for()

async with NewUser({"status": UserStatus.CONFIRMATION_PENDING.name}) as user:
rp = await client.post(reset_url, json={"email": user["email"],})
rp = await client.post(
reset_url,
json={
"email": user["email"],
},
)

assert rp.url_obj.path == reset_url.path
await assert_status(rp, web.HTTPOk, cfg.MSG_EMAIL_SENT.format(**user))
Expand All @@ -69,7 +83,12 @@ async def test_too_often(client, capsys, cfg):
confirmation = await db.create_confirmation(
user, ConfirmationAction.RESET_PASSWORD.name
)
rp = await client.post(reset_url, json={"email": user["email"],})
rp = await client.post(
reset_url,
json={
"email": user["email"],
},
)
await db.delete_confirmation(confirmation)

assert rp.url_obj.path == reset_url.path
Expand All @@ -82,7 +101,12 @@ async def test_too_often(client, capsys, cfg):
async def test_reset_and_confirm(client, capsys, cfg):
async with NewUser() as user:
reset_url = client.app.router["auth_reset_password"].url_for()
rp = await client.post(reset_url, json={"email": user["email"],})
rp = await client.post(
reset_url,
json={
"email": user["email"],
},
)
assert rp.url_obj.path == reset_url.path
await assert_status(rp, web.HTTPOk, cfg.MSG_EMAIL_SENT.format(**user))

Expand All @@ -106,7 +130,11 @@ async def test_reset_and_confirm(client, capsys, cfg):
)
new_password = get_random_string(5, 10)
rp = await client.post(
reset_allowed_url, json={"password": new_password, "confirm": new_password,}
reset_allowed_url,
json={
"password": new_password,
"confirm": new_password,
},
)
payload = await rp.json()
assert rp.status == 200, payload
Expand All @@ -122,7 +150,11 @@ async def test_reset_and_confirm(client, capsys, cfg):

login_url = client.app.router["auth_login"].url_for()
rp = await client.post(
login_url, json={"email": user["email"], "password": new_password,}
login_url,
json={
"email": user["email"],
"password": new_password,
},
)
assert rp.url_obj.path == login_url.path
await assert_status(rp, web.HTTPOk, cfg.MSG_LOGGED_IN)
Expand Down
Loading