Skip to content

Commit

Permalink
Fix tests and add fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
iwankrshkin committed Feb 12, 2024
1 parent 818eea7 commit d983484
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 118 deletions.
36 changes: 34 additions & 2 deletions src/apps/answers/fixtures/arbitrary_server_answers.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"note": {
"plain_workspace_name": "Admin"
}
},
{
},{
"table": "applets",
"fields": {
"id": "92917a56-d586-4613-b7aa-991f2c4b15b8",
Expand Down Expand Up @@ -760,5 +759,38 @@
"last_name": "USpl8mH9r0f1/ffBdCO/jw==",
"creator_id": "6cde911e-8a57-47c0-b6b2-685b3664f418"
}
},
{
"table": "subjects",
"fields": {
"id": "303d2bc5-5f07-4f7d-b1f9-db3996abf69d",
"created_at": "2023-01-05T15:49:51.752113",
"updated_at": "2023-01-05T15:49:51.752113",
"applet_id": "92917a56-d586-4613-b7aa-991f2c4b15b8",
"user_id": "7484f34a-3acc-4ee6-8a94-fd7299502fa1",
"language": "en",
"nickname": "hFywashKw+KlcDPazIy5QHz4AdkTOYkD28Q8+dpeDDA=",
"secret_user_id": "f0dd4996-e0eb-461f-b2f8-ba873a674788",
"email": "mWykF1OYv6Z/KkYLEA8lAbt7GQqBX7Xy57II/r9DqhM=",
"first_name": "PWrlro9d6WB767UVp43dNg==",
"last_name": "USpl8mH9r0f1/ffBdCO/jw==",
"creator_id": "6cde911e-8a57-47c0-b6b2-685b3664f418"
}
},{
"table": "subjects",
"fields": {
"id": "7484f34a-3acc-4ee6-8a94-fd7299502fa0",
"created_at": "2023-01-05T15:49:51.752113",
"updated_at": "2023-01-05T15:49:51.752113",
"applet_id": "92917a56-d586-4613-b7aa-991f2c4b15b8",
"user_id": "7484f34a-3acc-4ee6-8a94-fd7299502fa7",
"language": "en",
"nickname": "hFywashKw+KlcDPazIy5QHz4AdkTOYkD28Q8+dpeDDA=",
"secret_user_id": "f0dd4996-e0eb-461f-b2f8-ba873a674788",
"email": "mWykF1OYv6Z/KkYLEA8lAbt7GQqBX7Xy57II/r9DqhM=",
"first_name": "PWrlro9d6WB767UVp43dNg==",
"last_name": "USpl8mH9r0f1/ffBdCO/jw==",
"creator_id": "6cde911e-8a57-47c0-b6b2-685b3664f418"
}
}
]
7 changes: 7 additions & 0 deletions src/apps/answers/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ async def user_reviewer_applet_one(user: UserSchema, session: AsyncSession, mock
return_value={"respondents": ["7484f34a-3acc-4ee6-8a94-fd7299502fa6"]},
)
await srv.add_role(user.id, Role.REVIEWER)


@pytest.fixture
async def editor_user_reviewer_applet_one(user: UserSchema, session: AsyncSession, mocker: MockerFixture):
applet_id = uuid.UUID("92917a56-d586-4613-b7aa-991f2c4b15b1")
srv = UserAppletAccessService(session, user.id, applet_id)
await srv.add_role(user.id, Role.EDITOR)
6 changes: 2 additions & 4 deletions src/apps/answers/tests/test_answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class TestAnswerActivityItems(BaseTest):
fixtures = [
"folders/fixtures/folders.json",
# "folders/fixtures/folders.json",
"applets/fixtures/applets.json",
"applets/fixtures/applet_user_accesses.json",
"applets/fixtures/applet_histories.json",
Expand Down Expand Up @@ -158,7 +158,7 @@ async def test_get_latest_summary(self, mock_report_server_response, mock_kiq_re
)
assert response.status_code == 200

async def test_public_answer_activity_items_create_for_respondent(self, mock_kiq_report, client, tom):
async def test_public_answer_activity_items_create_for_respondent(self, mock_kiq_report, client):
create_data = dict(
submit_id="270d86e0-2158-4d18-befd-86b3ce0122ae",
applet_id="92917a56-d586-4613-b7aa-991f2c4b15b1",
Expand Down Expand Up @@ -189,9 +189,7 @@ async def test_public_answer_activity_items_create_for_respondent(self, mock_kiq
height=1080,
),
)

response = await client.post(self.public_answer_url, data=create_data)

assert response.status_code == 201, response.json()

async def test_answer_skippable_activity_items_create_for_respondent(self, mock_kiq_report, client, tom):
Expand Down
66 changes: 9 additions & 57 deletions src/apps/applets/fixtures/applet_user_accesses.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,6 @@
"meta": {}
}
},
{
"table": "user_applet_accesses",
"fields": {
"id": "f0dd4996-e0eb-461f-b2f8-ba873a674767",
"created_at": "2023-01-05T15:49:51.752119",
"updated_at": "2023-01-05T15:49:51.752113",
"is_deleted": false,
"is_pinned": false,
"user_id": "7484f34a-3acc-4ee6-8a94-fd7299502fa6",
"owner_id": "7484f34a-3acc-4ee6-8a94-fd7299502fa1",
"applet_id": "92917a56-d586-4613-b7aa-991f2c4b15b1",
"invitor_id": "7484f34a-3acc-4ee6-8a94-fd7299502fa1",
"role": "editor",
"meta": {}
}
},
{
"table": "user_applet_accesses",
"fields": {
Expand Down Expand Up @@ -248,26 +232,6 @@
"meta": {}
}
},
{
"table": "user_applet_accesses",
"fields": {
"id": "92917a56-d586-4613-b7aa-991f2c4b15b8",
"created_at": "2023-01-05T15:49:51.752112",
"updated_at": "2023-01-05T15:49:51.752113",
"is_deleted": false,
"is_pinned": false,
"applet_id": "92917a56-d586-4613-b7aa-991f2c4b15b1",
"user_id": "7484f34a-3acc-4ee6-8a94-fd7299502f00",
"owner_id": "7484f34a-3acc-4ee6-8a94-fd7299502fa1",
"invitor_id": "7484f34a-3acc-4ee6-8a94-fd7299502fa1",
"role": "reviewer",
"meta": {
"subjects": [
"7484f34a-3acc-4ee6-8a94-fd7299502fa6"
]
}
}
},
{
"table": "user_applet_accesses",
"fields": {
Expand Down Expand Up @@ -318,35 +282,23 @@
"meta": {},
"nickname": "hFywashKw+KlcDPazIy5QHz4AdkTOYkD28Q8+dpeDDA="
}
},{
}, {
"table": "user_applet_accesses",
"fields": {
"id": "4a315044-5aa5-40be-88dd-677c96b3da05",
"created_at": "2023-01-05T15:49:51.752113",
"id": "d6af0ab2-8b91-42ea-9208-114cab938061",
"created_at": "2023-01-05T15:49:51.752114",
"updated_at": "2023-01-05T15:49:51.752113",
"is_deleted": false,
"is_pinned": false,
"user_id": "965f1d93-e64a-4f67-b76b-8427f033a864",
"applet_id": "92917a56-d586-4613-b7aa-991f2c4b15b1",
"user_id": "7484f34a-3acc-4ee6-8a94-fd7299502f00",
"owner_id": "7484f34a-3acc-4ee6-8a94-fd7299502fa1",
"invitor_id": "7484f34a-3acc-4ee6-8a94-fd7299502fa1",
"role": "editor",
"meta": {}
}
},{
"table": "user_applet_accesses",
"fields": {
"id": "9eaf090b-caa7-4b3e-9789-220542753082",
"created_at": "2023-01-05T15:49:51.752113",
"updated_at": "2023-01-05T15:49:51.752113",
"is_deleted": false,
"is_pinned": false,
"user_id": "965f1d93-e64a-4f67-b76b-8427f033a864",
"applet_id": "92917a56-d586-4613-b7aa-991f2c4b15b1",
"owner_id": "7484f34a-3acc-4ee6-8a94-fd7299502fa1",
"invitor_id": "7484f34a-3acc-4ee6-8a94-fd7299502fa1",
"role": "respondent",
"meta": {}
"role": "reviewer",
"meta": {
"subjects": ["89ba6774-4f48-4ff1-9d34-0e6efd24f03f"]
},
"nickname": "hFywashKw+KlcDPazIy5QHz4AdkTOYkD28Q8+dpeDDA="
}
}
]
2 changes: 1 addition & 1 deletion src/apps/file/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TestAnswerActivityItems(BaseTest):
file_id = "1693560380000/c60859c4-6f5f-4390-a572-da85fcd59709"

@mock.patch("infrastructure.utility.cdn_arbitrary.ArbitraryS3CdnClient.upload")
async def test_arbitrary_upload_to_s3_aws(self, mock_client: mock.MagicMock, session, client, **kwargs):
async def test_arbitrary_upload_to_s3_aws(self, mock_client: mock.MagicMock, session, client, tom):
await client.login(self.login_url, "[email protected]", "Test1234!")
await set_storage_type(StorageType.AWS, session)

Expand Down
2 changes: 1 addition & 1 deletion src/apps/invitations/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ async def _verify_applet_subjects(
).reduce_applet_subject_ids(applet_id, subject_ids)

if len(existing_subject_ids) != len(subject_ids):
raise ValidationError("Subject does not exist in applet")
raise ValidationError("Subject does not exist in applet.")

async def accept(self, key: uuid.UUID) -> None:
self._user.email_encrypted = cast(str, self._user.email_encrypted)
Expand Down
23 changes: 10 additions & 13 deletions src/apps/invitations/test_invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from apps.mailing.services import TestMail
from apps.shared.test import BaseTest
from apps.subjects.crud import SubjectsCrud
from apps.users import UserSchema
from apps.users.domain import UserCreate, UserCreateRequest


Expand Down Expand Up @@ -821,15 +822,13 @@ async def test_invite_and_accept_invitation_as_respondent(
assert subjects_on_applet2 == subjects_on_applet1

async def test_invite_and_accept_invitation_as_manager(
self, client, session, invitation_manager_data
self, client, session, invitation_manager_data, tom: UserSchema, user: UserSchema
):
subject_crud = SubjectsCrud(session)
applet_id = uuid.UUID("92917a56-d586-4613-b7aa-991f2c4b15b1")
mike_email = "[email protected]"
mike_id = uuid.UUID("6a180cd9-db2b-4195-a5ac-30a8733dfb06")
# Create invitation to Mike
await client.login(self.login_url, "[email protected]", "Test1234!")
invitation_manager_data.email = mike_email
# Create invitation to User
await client.login(self.login_url, tom.email_encrypted, "Test1234!")
invitation_manager_data.email = user.email_encrypted
subjects_on_applet0 = await subject_crud.count(applet_id=applet_id)
response = await client.post(
self.invite_manager_url.format(applet_id=applet_id),
Expand All @@ -840,23 +839,21 @@ async def test_invite_and_accept_invitation_as_manager(
assert subjects_on_applet1 == subjects_on_applet0
invitation = response.json()["result"]
# Login as Mike and accept invitation
await client.login(self.login_url, mike_email, "Test1234!")
await client.login(self.login_url, user.email_encrypted, "Test1234!")
url_accept = self.accept_url.format(key=invitation["key"])
response = await client.post(url_accept)
assert response.status_code == http.HTTPStatus.OK
subject = await subject_crud.get(mike_id, applet_id)
subject = await subject_crud.get(user.id, applet_id)
assert subject
subjects_on_applet2 = await subject_crud.count(applet_id=applet_id)
assert subjects_on_applet2 == (subjects_on_applet1 + 1)

async def test_private_invitation_accept_crate_subject(
self, client, session
self, client, session, user: UserSchema
):
subject_crud = SubjectsCrud(session)
mike_id = uuid.UUID("6a180cd9-db2b-4195-a5ac-30a8733dfb06")
mike_email = "[email protected]"
applet_id = uuid.UUID("92917a56-d586-4613-b7aa-991f2c4b15b3")
await client.login(self.login_url, mike_email, "Test1234!")
await client.login(self.login_url, user.email_encrypted, "Test1234!")
count0 = await subject_crud.count(applet_id=applet_id)
response = await client.post(
self.accept_private_url.format(
Expand All @@ -866,5 +863,5 @@ async def test_private_invitation_accept_crate_subject(
assert response.status_code == http.HTTPStatus.OK
count1 = await subject_crud.count(applet_id=applet_id)
assert (count0 + 1) == count1
subject = subject_crud.get(mike_id, applet_id)
subject = subject_crud.get(user.id, applet_id)
assert subject
8 changes: 5 additions & 3 deletions src/apps/shared/test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ class BaseTest:

@pytest.fixture(scope="class", autouse=True)
async def initialize(self):
await self.populate_db()
yield
await truncate_tables()
try:
await self.populate_db()
yield
finally:
await truncate_tables()

@pytest.fixture(autouse=True)
async def clear_mails(self):
Expand Down
2 changes: 1 addition & 1 deletion src/apps/shared/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async def truncate_tables():
Session = session_manager.get_session()
async with Session() as session:
for table_name in Base.metadata.tables:
if table_name in ("users", "user_devices"):
if table_name in ("users", "user_devices", "subjects"):
continue
elif table_name == "users_workspaces":
query = text(f"delete from {table_name} where user_id != '57b63dfa-5cee-4a53-a69e-0a35407e601d'")
Expand Down
30 changes: 4 additions & 26 deletions src/apps/subjects/fixtures/subjects.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,6 @@
"plain_last_name": "Isaak"
}
},
{
"table": "subjects",
"fields": {
"id": "d552a73e-0dfe-41ee-9602-49d85994b1cb",
"created_at": "2023-01-05T15:49:51.752113",
"updated_at": "2023-01-05T15:49:51.752113",
"applet_id": "92917a56-d586-4613-b7aa-991f2c4b15b1",
"user_id": "7484f34a-3acc-4ee6-8a94-fd7299502fa7",
"language": "en",
"nickname": "hFywashKw+KlcDPazIy5QHz4AdkTOYkD28Q8+dpeDDA=",
"secret_user_id": "7d31e08b-f552-45c2-a0f3-fcac9251685b",
"email": "wR8cW30TKhQwbCCddfawUoOe1rXUZYBl4bmgF7NWDu+qbYXlU9Qjcxb+AJugpF9+",
"first_name": "xjYl6DpzZ1uq+KofwSTn7w==",
"last_name": "YRCR7GN7zM6TsUQOSLMmvw==",
"creator_id": "7484f34a-3acc-4ee6-8a94-fd7299502fa1"
},
"note": {
"plain_first_name": "first_name",
"plain_last_name": "last_name",
"plan_email": "[email protected]"
}
},
{
"table": "subjects",
"fields": {
Expand Down Expand Up @@ -110,14 +88,14 @@
},{
"table": "subjects",
"fields": {
"id": "7484f34a-3acc-4ee6-8a94-fd7299502fa6",
"id": "f07ccfd2-20b0-4fdb-8c9c-73dd90784db8",
"created_at": "2023-01-05T15:49:51.752113",
"updated_at": "2023-01-05T15:49:51.752113",
"applet_id": "92917a56-d586-4613-b7aa-991f2c4b15b1",
"user_id": "7484f34a-3acc-4ee6-8a94-fd7299502fa6",
"user_id": "7484f34a-3acc-4ee6-8a94-fd7299502fa7",
"language": "en",
"nickname": "hFywashKw+KlcDPazIy5QHz4AdkTOYkD28Q8+dpeDDA=",
"secret_user_id": "2f04d02e-9f75-4405-a17b-3004a747d079",
"secret_user_id": "0bb3032b-8bd4-48f6-8a00-92ca43d658b0",
"email": "G/fUWdjS0euNqM0EuhcsKQ==",
"first_name": "G3pm+lDvNouDFrSGQx6myg==",
"last_name": "qZ9yEVQaDanvs9mB58OQQw==",
Expand All @@ -126,7 +104,7 @@
"note": {
"plain_first_name": "first_name",
"plain_last_name": "last_name",
"plan_email": "plan_email"
"plan_email": "[email protected]"
}
}
]
1 change: 1 addition & 0 deletions src/apps/subjects/services/subjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ async def create_anonymous_subject(
Subject(
applet_id=applet_id,
creator_id=self.user_id,
user_id=anonymous_user.id,
first_name=anonymous_user.first_name,
last_name=anonymous_user.last_name,
secret_user_id=str(uuid.uuid4()),
Expand Down
10 changes: 5 additions & 5 deletions src/apps/subjects/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from apps.subjects.crud import SubjectsCrud
from apps.subjects.domain import Subject, SubjectCreateRequest, SubjectRespondentCreate
from apps.subjects.services import SubjectsService
from apps.users import UserSchema


@pytest.fixture
Expand Down Expand Up @@ -141,7 +142,6 @@ def answer_create_arbitrary_payload():

class TestSubjects(BaseTest):
fixtures = [
"users/fixtures/users.json",
"applets/fixtures/applets.json",
"applets/fixtures/applet_user_accesses.json",
"applets/fixtures/applet_histories.json",
Expand Down Expand Up @@ -365,7 +365,7 @@ async def test_successfully_delete_subject_with_answers(
# Coordinator
("[email protected]", "Test1234!", http.HTTPStatus.OK),
# Editor
("[email protected]", "Test1234!", http.HTTPStatus.FORBIDDEN),
("[email protected]", "Test1234", http.HTTPStatus.FORBIDDEN),
# Reviewer
("[email protected]", "Test1234!", http.HTTPStatus.FORBIDDEN)
))
Expand All @@ -386,12 +386,12 @@ async def test_error_try_delete_subject_by_not_owner(
assert res.status_code == expected

@pytest.mark.parametrize("subject_id,expected_code", (
("7484f34a-3acc-4ee6-8a94-fd7299502fa6", http.HTTPStatus.OK),
("89ba6774-4f48-4ff1-9d34-0e6efd24f03f", http.HTTPStatus.OK),
("ee96b767-4609-4b8b-93c5-e7b15b81c6f7", http.HTTPStatus.FORBIDDEN),
(uuid.uuid4(), http.HTTPStatus.NOT_FOUND)
))
async def test_get_subject(self, client, subject_id, expected_code):
await client.login(self.login_url, "reviewer@mail.com", "Test1234!")
async def test_get_subject(self, client, reviewer: UserSchema, subject_id, expected_code):
await client.login(self.login_url, reviewer.email_encrypted, "Test1234!")
response = await client.get(
self.subject_detail_url.format(subject_id=subject_id)
)
Expand Down
Loading

0 comments on commit d983484

Please sign in to comment.