You've already forked hotpocket
This commit is contained in:
0
services/backend/tests/ui/views/saves/__init__.py
Normal file
0
services/backend/tests/ui/views/saves/__init__.py
Normal file
327
services/backend/tests/ui/views/saves/test_create.py
Normal file
327
services/backend/tests/ui/views/saves/test_create.py
Normal file
@@ -0,0 +1,327 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# type: ignore
|
||||
from __future__ import annotations
|
||||
|
||||
import http
|
||||
from unittest import mock
|
||||
import uuid
|
||||
|
||||
from django.test import Client
|
||||
from django.urls import reverse
|
||||
import pytest
|
||||
from pytest_django import asserts
|
||||
import pytest_mock
|
||||
|
||||
from hotpocket_backend_testing.services.saves import (
|
||||
AssociationsTestingService,
|
||||
SaveProcessorTestingService,
|
||||
SavesTestingService,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_saves_process_save_task_apply_async(mocker: pytest_mock.MockerFixture,
|
||||
async_result,
|
||||
) -> mock.Mock:
|
||||
return SaveProcessorTestingService().mock_process_save_task_apply_async(
|
||||
mocker=mocker, async_result=async_result,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def payload():
|
||||
return {
|
||||
'url': 'https://www.ziomek.dog/',
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_ok(authenticated_client: Client,
|
||||
payload,
|
||||
account,
|
||||
mock_saves_process_save_task_apply_async: mock.Mock,
|
||||
):
|
||||
# When
|
||||
result = authenticated_client.post(
|
||||
reverse('ui.saves.create'),
|
||||
data=payload,
|
||||
)
|
||||
|
||||
# Then
|
||||
asserts.assertRedirects(
|
||||
result,
|
||||
reverse('ui.associations.browse'),
|
||||
fetch_redirect_response=False,
|
||||
)
|
||||
assert 'X-HotPocket-Testing-Save-PK' in result.headers
|
||||
assert 'X-HotPocket-Testing-Association-PK' in result.headers
|
||||
|
||||
save_pk = uuid.UUID(result.headers['X-HotPocket-Testing-Save-PK'])
|
||||
association_pk = uuid.UUID(result.headers['X-HotPocket-Testing-Association-PK'])
|
||||
|
||||
AssociationsTestingService().assert_created(
|
||||
pk=association_pk,
|
||||
account_uuid=account.pk,
|
||||
target_uuid=save_pk,
|
||||
)
|
||||
|
||||
SavesTestingService().assert_created(
|
||||
pk=save_pk,
|
||||
account_uuid=account.pk,
|
||||
url=payload['url'],
|
||||
is_netloc_banned=False,
|
||||
)
|
||||
|
||||
mock_saves_process_save_task_apply_async.assert_called_once_with(
|
||||
kwargs={
|
||||
'pk': save_pk,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_ok_netloc_banned(authenticated_client: Client,
|
||||
payload,
|
||||
account,
|
||||
mock_saves_process_save_task_apply_async: mock.Mock,
|
||||
):
|
||||
# Given
|
||||
payload['url'] = 'https://youtube.com/'
|
||||
|
||||
# When
|
||||
result = authenticated_client.post(
|
||||
reverse('ui.saves.create'),
|
||||
data=payload,
|
||||
)
|
||||
|
||||
# Then
|
||||
save_pk = uuid.UUID(result.headers['X-HotPocket-Testing-Save-PK'])
|
||||
association_pk = uuid.UUID(result.headers['X-HotPocket-Testing-Association-PK'])
|
||||
|
||||
asserts.assertRedirects(
|
||||
result,
|
||||
reverse('ui.associations.post_save', args=(association_pk,)),
|
||||
fetch_redirect_response=False,
|
||||
)
|
||||
|
||||
SavesTestingService().assert_created(
|
||||
pk=save_pk,
|
||||
account_uuid=account.pk,
|
||||
url=payload['url'],
|
||||
is_netloc_banned=True,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_ok_reuse_save(authenticated_client: Client,
|
||||
payload,
|
||||
save_out,
|
||||
account,
|
||||
mock_saves_process_save_task_apply_async: mock.Mock,
|
||||
):
|
||||
# Given
|
||||
payload['url'] = save_out.url
|
||||
|
||||
# When
|
||||
result = authenticated_client.post(
|
||||
reverse('ui.saves.create'),
|
||||
data=payload,
|
||||
)
|
||||
|
||||
# Then
|
||||
asserts.assertRedirects(
|
||||
result,
|
||||
reverse('ui.associations.browse'),
|
||||
fetch_redirect_response=False,
|
||||
)
|
||||
assert 'X-HotPocket-Testing-Save-PK' in result.headers
|
||||
assert 'X-HotPocket-Testing-Association-PK' in result.headers
|
||||
|
||||
save_pk = uuid.UUID(result.headers['X-HotPocket-Testing-Save-PK'])
|
||||
association_pk = uuid.UUID(result.headers['X-HotPocket-Testing-Association-PK'])
|
||||
|
||||
AssociationsTestingService().assert_created(
|
||||
pk=association_pk,
|
||||
account_uuid=account.pk,
|
||||
target_uuid=save_pk,
|
||||
)
|
||||
|
||||
SavesTestingService().assert_reused(
|
||||
pk=save_pk,
|
||||
reference=save_out,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_ok_reuse_association(authenticated_client: Client,
|
||||
payload,
|
||||
save_out,
|
||||
account,
|
||||
association_out,
|
||||
mock_saves_process_save_task_apply_async: mock.Mock,
|
||||
):
|
||||
# Given
|
||||
payload['url'] = save_out.url
|
||||
|
||||
# When
|
||||
result = authenticated_client.post(
|
||||
reverse('ui.saves.create'),
|
||||
data=payload,
|
||||
)
|
||||
|
||||
# Then
|
||||
association_pk = uuid.UUID(result.headers['X-HotPocket-Testing-Association-PK'])
|
||||
|
||||
AssociationsTestingService().assert_reused(
|
||||
pk=association_pk,
|
||||
reference=association_out,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_ok_reuse_other_account_save(authenticated_client: Client,
|
||||
payload,
|
||||
other_account_save_out,
|
||||
account,
|
||||
mock_saves_process_save_task_apply_async: mock.Mock,
|
||||
):
|
||||
# Given
|
||||
payload['url'] = other_account_save_out.url
|
||||
|
||||
# When
|
||||
result = authenticated_client.post(
|
||||
reverse('ui.saves.create'),
|
||||
data=payload,
|
||||
)
|
||||
|
||||
# Then
|
||||
save_pk = uuid.UUID(result.headers['X-HotPocket-Testing-Save-PK'])
|
||||
association_pk = uuid.UUID(result.headers['X-HotPocket-Testing-Association-PK'])
|
||||
|
||||
AssociationsTestingService().assert_created(
|
||||
pk=association_pk,
|
||||
account_uuid=account.pk,
|
||||
target_uuid=save_pk,
|
||||
)
|
||||
|
||||
SavesTestingService().assert_reused(
|
||||
pk=save_pk,
|
||||
reference=other_account_save_out,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_ok_dont_process_reused_processed_save(authenticated_client: Client,
|
||||
payload,
|
||||
processed_save_out,
|
||||
account,
|
||||
mock_saves_process_save_task_apply_async: mock.Mock,
|
||||
):
|
||||
# Given
|
||||
payload['url'] = processed_save_out.url
|
||||
|
||||
# When
|
||||
_ = authenticated_client.post(
|
||||
reverse('ui.saves.create'),
|
||||
data=payload,
|
||||
)
|
||||
|
||||
# Then
|
||||
mock_saves_process_save_task_apply_async.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_invalid_all_empty(authenticated_client: Client,
|
||||
payload,
|
||||
mock_saves_process_save_task_apply_async: mock.Mock,
|
||||
):
|
||||
# Given
|
||||
effective_payload = {
|
||||
key: ''
|
||||
for key
|
||||
in payload.keys()
|
||||
}
|
||||
|
||||
# When
|
||||
result = authenticated_client.post(
|
||||
reverse('ui.saves.create'),
|
||||
data=effective_payload,
|
||||
)
|
||||
|
||||
# Then
|
||||
assert result.status_code == http.HTTPStatus.OK
|
||||
assert 'X-HotPocket-Testing-Save-PK' not in result.headers
|
||||
assert 'X-HotPocket-Testing-Association-PK' not in result.headers
|
||||
|
||||
mock_saves_process_save_task_apply_async.assert_not_called()
|
||||
|
||||
assert 'url' in result.context['form'].errors
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_invalid_all_missing(authenticated_client: Client,
|
||||
mock_saves_process_save_task_apply_async: mock.Mock,
|
||||
):
|
||||
# Given
|
||||
effective_payload = {}
|
||||
|
||||
# When
|
||||
result = authenticated_client.post(
|
||||
reverse('ui.saves.create'),
|
||||
data=effective_payload,
|
||||
)
|
||||
|
||||
# Then
|
||||
assert result.status_code == http.HTTPStatus.OK
|
||||
assert 'X-HotPocket-Testing-Save-PK' not in result.headers
|
||||
assert 'X-HotPocket-Testing-Association-PK' not in result.headers
|
||||
|
||||
mock_saves_process_save_task_apply_async.assert_not_called()
|
||||
|
||||
assert 'url' in result.context['form'].errors
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_inactive_account(inactive_account_client: Client,
|
||||
payload,
|
||||
):
|
||||
# When
|
||||
result = inactive_account_client.post(
|
||||
reverse('ui.saves.create'),
|
||||
data=payload,
|
||||
)
|
||||
|
||||
# Then
|
||||
asserts.assertRedirects(
|
||||
result,
|
||||
reverse(
|
||||
'ui.accounts.login',
|
||||
query=[
|
||||
('next', reverse('ui.saves.create')),
|
||||
],
|
||||
),
|
||||
fetch_redirect_response=False,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_anonymous(client: Client,
|
||||
payload,
|
||||
):
|
||||
# When
|
||||
result = client.post(
|
||||
reverse('ui.saves.create'),
|
||||
data=payload,
|
||||
)
|
||||
|
||||
# Then
|
||||
asserts.assertRedirects(
|
||||
result,
|
||||
reverse(
|
||||
'ui.accounts.login',
|
||||
query=[
|
||||
('next', reverse('ui.saves.create')),
|
||||
],
|
||||
),
|
||||
fetch_redirect_response=False,
|
||||
)
|
||||
111
services/backend/tests/ui/views/saves/test_embed.py
Normal file
111
services/backend/tests/ui/views/saves/test_embed.py
Normal file
@@ -0,0 +1,111 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# type: ignore
|
||||
from __future__ import annotations
|
||||
|
||||
import http
|
||||
|
||||
from django.test import Client
|
||||
from django.urls import reverse
|
||||
import pytest
|
||||
from pytest_django import asserts
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def payload(save_out):
|
||||
return {
|
||||
'pk': str(save_out.pk),
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_ok(client: Client, payload, save_out):
|
||||
# When
|
||||
result = client.post(
|
||||
reverse('ui.saves.embed'),
|
||||
headers={
|
||||
'HX-Request': 'true',
|
||||
},
|
||||
data=payload,
|
||||
)
|
||||
|
||||
# Then
|
||||
assert result.status_code == http.HTTPStatus.OK
|
||||
|
||||
asserts.assertTemplateUsed(result, 'ui/saves/partials/embed.html')
|
||||
assert result.context['save'] == save_out
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_bad_request_get(client: Client, payload):
|
||||
# When
|
||||
result = client.get(
|
||||
reverse('ui.saves.embed'),
|
||||
headers={
|
||||
'HX-Request': 'true',
|
||||
},
|
||||
data=payload,
|
||||
)
|
||||
|
||||
# Then
|
||||
assert result.status_code == http.HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_bad_request_missing_pk(client: Client):
|
||||
# When
|
||||
result = client.post(
|
||||
reverse('ui.saves.embed'),
|
||||
headers={
|
||||
'HX-Request': 'true',
|
||||
},
|
||||
data={},
|
||||
)
|
||||
|
||||
# Then
|
||||
assert result.status_code == http.HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_bad_request_not_htmx(client: Client, payload):
|
||||
# When
|
||||
result = client.post(
|
||||
reverse('ui.saves.embed'),
|
||||
data=payload,
|
||||
)
|
||||
|
||||
# Then
|
||||
assert result.status_code == http.HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_not_found(client: Client, null_uuid):
|
||||
# When
|
||||
result = client.post(
|
||||
reverse('ui.saves.embed'),
|
||||
headers={
|
||||
'HX-Request': 'true',
|
||||
},
|
||||
data={
|
||||
'pk': str(null_uuid),
|
||||
},
|
||||
)
|
||||
|
||||
# Then
|
||||
assert result.status_code == http.HTTPStatus.NOT_FOUND
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_deleted(client: Client, deleted_save_out):
|
||||
# When
|
||||
result = client.post(
|
||||
reverse('ui.saves.embed'),
|
||||
headers={
|
||||
'HX-Request': 'true',
|
||||
},
|
||||
data={
|
||||
'pk': str(deleted_save_out.pk),
|
||||
},
|
||||
)
|
||||
|
||||
# Then
|
||||
assert result.status_code == http.HTTPStatus.NOT_FOUND
|
||||
Reference in New Issue
Block a user