Release v1.0.0
Some checks failed
CI / Checks (push) Failing after 13m2s

This commit is contained in:
2025-08-20 21:00:50 +02:00
commit b4338e2769
401 changed files with 23576 additions and 0 deletions

View File

@@ -0,0 +1,166 @@
# -*- 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
from hotpocket_backend_testing.services.saves import AssociationsTestingService
@pytest.mark.django_db
def test_ok(authenticated_client: Client,
association_out,
):
# When
result = authenticated_client.post(
reverse('ui.associations.archive', args=(association_out.pk,)),
data={
'canhazconfirm': 'hai',
},
)
# Then
asserts.assertRedirects(
result,
reverse('ui.associations.browse'),
fetch_redirect_response=False,
)
AssociationsTestingService().assert_archived(
pk=association_out.pk, reference=association_out,
)
@pytest.mark.django_db
def test_ok_htmx(authenticated_client: Client,
association_out,
):
# When
result = authenticated_client.post(
reverse('ui.associations.archive', args=(association_out.pk,)),
headers={
'HX-Request': 'true',
},
data={
'canhazconfirm': 'hai',
},
)
# Then
assert result.status_code == http.HTTPStatus.OK
expected_payload = {
'status': 'ok',
'result': True,
}
assert result.json() == expected_payload
@pytest.mark.django_db
def test_invalid_all_missing(authenticated_client: Client,
association_out,
):
# When
result = authenticated_client.post(
reverse('ui.associations.archive', args=(association_out.pk,)),
data={
},
)
# Then
assert result.status_code == http.HTTPStatus.OK
AssociationsTestingService().assert_not_archived(pk=association_out.pk)
assert 'canhazconfirm' in result.context['form'].errors
@pytest.mark.django_db
def test_invalid_all_empty(authenticated_client: Client,
association_out,
):
# When
result = authenticated_client.post(
reverse('ui.associations.archive', args=(association_out.pk,)),
data={
'canhazconfirm': '',
},
)
# Then
assert result.status_code == http.HTTPStatus.OK
AssociationsTestingService().assert_not_archived(pk=association_out.pk)
assert 'canhazconfirm' in result.context['form'].errors
@pytest.mark.django_db
def test_other_account_association(authenticated_client: Client,
other_account_association_out,
):
# When
result = authenticated_client.post(
reverse('ui.associations.archive', args=(other_account_association_out.pk,)),
data={
'canhazconfirm': 'hai',
},
)
# Then
assert result.status_code == http.HTTPStatus.FORBIDDEN
@pytest.mark.django_db
def test_inactive_account(inactive_account_client: Client,
association_out,
):
# When
result = inactive_account_client.post(
reverse('ui.associations.archive', args=(association_out.pk,)),
data={
'canhazconfirm': 'hai',
},
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[
('next', reverse('ui.associations.archive', args=(association_out.pk,))),
],
),
fetch_redirect_response=False,
)
@pytest.mark.django_db
def test_anonymous(client: Client,
association_out,
):
# When
result = client.post(
reverse('ui.associations.archive', args=(association_out.pk,)),
data={
'canhazconfirm': 'hai',
},
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[
('next', reverse('ui.associations.archive', args=(association_out.pk,))),
],
),
fetch_redirect_response=False,
)

View File

@@ -0,0 +1,201 @@
# -*- 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
from hotpocket_common.constants import AssociationsSearchMode
def assert_default_mode_response_context(response, associations):
expected_association_ids = list(sorted(
[obj.pk for obj in associations],
reverse=True,
))
assert len(response.context['associations']) == 2
assert response.context['associations'][0].pk == expected_association_ids[0]
assert response.context['associations'][1].pk == expected_association_ids[1]
assert response.context['params'].mode == AssociationsSearchMode.DEFAULT
assert response.context['before'] is None
assert response.context['next_url'] is None
@pytest.mark.django_db
def test_ok(browsable_association_outs,
authenticated_client: Client,
):
# When
result = authenticated_client.get(
reverse('ui.associations.browse'),
)
# Then
assert result.status_code == http.HTTPStatus.OK
asserts.assertTemplateUsed(result, 'ui/associations/browse.html')
assert_default_mode_response_context(result, browsable_association_outs)
@pytest.mark.django_db
def test_ok_htmx(browsable_association_outs,
authenticated_client: Client,
):
# When
result = authenticated_client.get(
reverse('ui.associations.browse'),
headers={
'HX-Request': 'true',
},
)
# Then
assert result.status_code == http.HTTPStatus.OK
asserts.assertTemplateNotUsed(result, 'ui/associations/browse.html')
asserts.assertTemplateUsed(result, 'ui/associations/partials/associations.html')
assert_default_mode_response_context(result, browsable_association_outs)
@pytest.mark.django_db
def test_starred_mode(browsable_association_outs,
authenticated_client: Client,
starred_association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.browse'),
data={
'mode': AssociationsSearchMode.STARRED.value,
},
)
# Then
assert result.status_code == http.HTTPStatus.OK
assert len(result.context['associations']) == 1
assert result.context['associations'][0].pk == starred_association_out.pk
assert result.context['params'].mode == AssociationsSearchMode.STARRED
@pytest.mark.django_db
def test_archived_mode(browsable_association_outs,
authenticated_client: Client,
archived_association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.browse'),
data={
'mode': AssociationsSearchMode.ARCHIVED.value,
},
)
# Then
assert result.status_code == http.HTTPStatus.OK
assert len(result.context['associations']) == 1
assert result.context['associations'][0].pk == archived_association_out.pk
assert result.context['params'].mode == AssociationsSearchMode.ARCHIVED
@pytest.mark.parametrize(
'query_before_index,expected_before_index,expected_length,first_index,last_index',
[
(None, 11, 12, 0, 11),
(11, None, 2, 12, 13),
],
)
@pytest.mark.django_db
def test_pagination(query_before_index,
expected_before_index,
expected_length,
first_index,
last_index,
paginatable_association_outs,
authenticated_client: Client,
):
# Given
request_data = {}
if query_before_index is not None:
request_data['before'] = str(
paginatable_association_outs[query_before_index].pk,
)
# When
result = authenticated_client.get(
reverse('ui.associations.browse'),
data=request_data,
)
# Then
assert result.status_code == http.HTTPStatus.OK
expected_before = None
expected_next_url = None
if expected_before_index:
expected_before = paginatable_association_outs[expected_before_index].pk
expected_next_url = reverse(
'ui.associations.browse',
query=[
('before', expected_before),
('search', ''),
('limit', 12),
('mode', AssociationsSearchMode.DEFAULT.value),
],
)
assert len(result.context['associations']) == expected_length
assert result.context['associations'][0].pk == paginatable_association_outs[first_index].pk
assert result.context['associations'][-1].pk == paginatable_association_outs[last_index].pk
assert result.context['before'] == expected_before
assert result.context['next_url'] == expected_next_url
@pytest.mark.django_db
def test_inactive_account(inactive_account_client: Client):
# When
result = inactive_account_client.get(
reverse('ui.associations.browse'),
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[
('next', reverse('ui.associations.browse')),
],
),
fetch_redirect_response=False,
)
@pytest.mark.django_db
def test_anonymous(client: Client):
# When
result = client.get(
reverse('ui.associations.browse'),
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[
('next', reverse('ui.associations.browse')),
],
),
fetch_redirect_response=False,
)

View File

@@ -0,0 +1,174 @@
# -*- 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
from hotpocket_backend.apps.saves.models import Association
from hotpocket_common.constants import AssociationsSearchMode
@pytest.mark.django_db
def test_ok(authenticated_client: Client,
association_out,
):
# When
result = authenticated_client.post(
reverse('ui.associations.delete', args=(association_out.pk,)),
data={
'canhazconfirm': 'hai',
},
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.associations.browse',
query=[('mode', AssociationsSearchMode.ARCHIVED.value)],
),
fetch_redirect_response=False,
)
association_object = Association.objects.get(pk=association_out.pk)
assert association_object.updated_at > association_out.updated_at
assert association_object.deleted_at is not None
@pytest.mark.django_db
def test_ok_htmx(authenticated_client: Client,
association_out,
):
# When
result = authenticated_client.post(
reverse('ui.associations.delete', args=(association_out.pk,)),
headers={
'HX-Request': 'true',
},
data={
'canhazconfirm': 'hai',
},
)
# Then
assert result.status_code == http.HTTPStatus.OK
expected_payload = {
'status': 'ok',
'result': True,
}
assert result.json() == expected_payload
@pytest.mark.django_db
def test_invalid_all_missing(authenticated_client: Client,
association_out,
):
# When
result = authenticated_client.post(
reverse('ui.associations.delete', args=(association_out.pk,)),
data={
},
)
# Then
assert result.status_code == http.HTTPStatus.OK
association_object = Association.objects.get(pk=association_out.pk)
assert association_object.updated_at == association_out.updated_at
assert association_object.deleted_at is None
assert 'canhazconfirm' in result.context['form'].errors
@pytest.mark.django_db
def test_invalid_all_empty(authenticated_client: Client,
association_out,
):
# When
result = authenticated_client.post(
reverse('ui.associations.delete', args=(association_out.pk,)),
data={
'canhazconfirm': '',
},
)
# Then
assert result.status_code == http.HTTPStatus.OK
association_object = Association.objects.get(pk=association_out.pk)
assert association_object.updated_at == association_out.updated_at
assert association_object.deleted_at is None
assert 'canhazconfirm' in result.context['form'].errors
@pytest.mark.django_db
def test_other_account_association(authenticated_client: Client,
other_account_association_out,
):
# When
result = authenticated_client.post(
reverse('ui.associations.delete', args=(other_account_association_out.pk,)),
data={
'canhazconfirm': 'hai',
},
)
# Then
assert result.status_code == http.HTTPStatus.FORBIDDEN
@pytest.mark.django_db
def test_inactive_account(inactive_account_client: Client,
association_out,
):
# When
result = inactive_account_client.post(
reverse('ui.associations.delete', args=(association_out.pk,)),
data={
'canhazconfirm': 'hai',
},
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[
('next', reverse('ui.associations.delete', args=(association_out.pk,))),
],
),
fetch_redirect_response=False,
)
@pytest.mark.django_db
def test_anonymous(client: Client,
association_out,
):
# When
result = client.post(
reverse('ui.associations.delete', args=(association_out.pk,)),
data={
'canhazconfirm': 'hai',
},
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[
('next', reverse('ui.associations.delete', args=(association_out.pk,))),
],
),
fetch_redirect_response=False,
)

View File

@@ -0,0 +1,162 @@
# -*- 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
from hotpocket_backend_testing.services.saves import AssociationsTestingService
@pytest.fixture
def payload():
return {
'url': 'https://ziomek.dog/',
'target_title': 'New Target Title',
'target_description': 'New Target Description',
}
@pytest.mark.django_db
def test_ok(authenticated_client: Client,
association_out,
payload,
):
# When
result = authenticated_client.post(
reverse('ui.associations.edit', args=(association_out.pk,)),
data=payload,
)
# Then
asserts.assertRedirects(
result,
reverse('ui.associations.view', args=(association_out.pk,)),
fetch_redirect_response=False,
)
AssociationsTestingService().assert_edited(
pk=association_out.pk,
update=payload,
reference=association_out,
)
@pytest.mark.django_db
def test_invalid_all_empty(authenticated_client: Client,
association_out,
payload,
):
# Given
effective_payload = {
key: ''
for key
in payload.keys()
}
# When
result = authenticated_client.post(
reverse('ui.associations.edit', args=(association_out.pk,)),
data=effective_payload,
)
# Then
asserts.assertRedirects(
result,
reverse('ui.associations.view', args=(association_out.pk,)),
fetch_redirect_response=False,
)
AssociationsTestingService().assert_edited(
pk=association_out.pk,
update=effective_payload,
reference=association_out,
)
@pytest.mark.django_db
def test_invalid_all_missing(authenticated_client: Client,
association_out,
):
# Given
effective_payload = {}
# When
result = authenticated_client.post(
reverse('ui.associations.edit', args=(association_out.pk,)),
data=effective_payload,
)
# Then
asserts.assertRedirects(
result,
reverse('ui.associations.view', args=(association_out.pk,)),
fetch_redirect_response=False,
)
AssociationsTestingService().assert_edited(
pk=association_out.pk,
update=effective_payload,
reference=association_out,
)
@pytest.mark.django_db
def test_other_account_association(authenticated_client: Client,
other_account_association_out,
):
# When
result = authenticated_client.post(
reverse('ui.associations.edit', args=(other_account_association_out.pk,)),
)
# Then
assert result.status_code == http.HTTPStatus.FORBIDDEN
@pytest.mark.django_db
def test_inactive_account(inactive_account_client: Client,
association_out,
):
# When
result = inactive_account_client.post(
reverse('ui.associations.edit', args=(association_out.pk,)),
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[
('next', reverse('ui.associations.edit', args=(association_out.pk,))),
],
),
fetch_redirect_response=False,
)
@pytest.mark.django_db
def test_anonymous(client: Client,
association_out,
):
# When
result = client.post(
reverse('ui.associations.edit', args=(association_out.pk,)),
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[
('next', reverse('ui.associations.edit', args=(association_out.pk,))),
],
),
fetch_redirect_response=False,
)

View File

@@ -0,0 +1,59 @@
# -*- coding: utf-8 -*-
# type: ignore
from __future__ import annotations
from django.test import Client
from django.urls import reverse
import pytest
from pytest_django import asserts
@pytest.mark.django_db
def test_ok(authenticated_client: Client):
# When
result = authenticated_client.get(
reverse('ui.associations.index'),
)
# Then
asserts.assertRedirects(
result,
reverse('ui.associations.browse'),
fetch_redirect_response=False,
)
@pytest.mark.django_db
def test_inactive_account(inactive_account_client: Client):
# When
result = inactive_account_client.get(
reverse('ui.associations.index'),
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[('next', reverse('ui.associations.index'))],
),
fetch_redirect_response=False,
)
@pytest.mark.django_db
def test_anonymous(client: Client):
# When
result = client.get(
reverse('ui.associations.index'),
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[('next', reverse('ui.associations.index'))],
),
fetch_redirect_response=False,
)

View File

@@ -0,0 +1,124 @@
# -*- 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.mark.django_db
def test_ok(authenticated_client: Client,
association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.post_save', args=(association_out.pk,)),
)
# Then
assert result.status_code == http.HTTPStatus.OK
asserts.assertTemplateUsed('ui/associations/view.html')
assert result.context['association'].pk == association_out.pk
assert hasattr(result.context['association'], 'target') is True
assert result.context['association'].target.pk == association_out.target.pk
@pytest.mark.django_db
def test_archived(authenticated_client: Client,
archived_association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.post_save', args=(archived_association_out.pk,)),
)
# Then
assert result.status_code == http.HTTPStatus.NOT_FOUND
@pytest.mark.django_db
def test_deleted(authenticated_client: Client,
deleted_association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.post_save', args=(deleted_association_out.pk,)),
)
# Then
assert result.status_code == http.HTTPStatus.NOT_FOUND
@pytest.mark.django_db
def test_not_found(authenticated_client: Client,
null_uuid,
):
# When
result = authenticated_client.get(
reverse('ui.associations.post_save', args=(null_uuid,)),
)
# Then
assert result.status_code == http.HTTPStatus.NOT_FOUND
@pytest.mark.django_db
def test_other_account_association(authenticated_client: Client,
other_account_association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.post_save', args=(other_account_association_out.pk,)),
)
# Then
assert result.status_code == http.HTTPStatus.FORBIDDEN
@pytest.mark.django_db
def test_inactive_account(inactive_account_client: Client,
association_out,
):
# When
result = inactive_account_client.get(
reverse('ui.associations.post_save', args=(association_out.pk,)),
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[
('next', reverse('ui.associations.post_save', args=(association_out.pk,))),
],
),
fetch_redirect_response=False,
)
@pytest.mark.django_db
def test_anonymous(client: Client,
association_out,
):
# When
result = client.get(
reverse('ui.associations.post_save', args=(association_out.pk,)),
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[
('next', reverse('ui.associations.post_save', args=(association_out.pk,))),
],
),
fetch_redirect_response=False,
)

View File

@@ -0,0 +1,194 @@
# -*- coding: utf-8 -*-
# type: ignore
from __future__ import annotations
import http
from unittest import mock
from celery.result import AsyncResult
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 SaveProcessorTestingService
@pytest.fixture
def async_result(null_uuid) -> mock.Mock:
result = mock.Mock(spec=AsyncResult)
result.id = str(null_uuid)
return result
@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.mark.django_db
def test_ok(authenticated_client: Client,
association_out,
mock_saves_process_save_task_apply_async: mock.Mock,
save_out,
):
# When
result = authenticated_client.post(
reverse('ui.associations.refresh', args=(association_out.pk,)),
data={
'canhazconfirm': 'hai',
},
)
# Then
asserts.assertRedirects(
result,
reverse('ui.associations.browse'),
fetch_redirect_response=False,
)
mock_saves_process_save_task_apply_async.assert_called_once_with(
kwargs={
'pk': save_out.pk,
},
)
@pytest.mark.django_db
def test_ok_htmx(mock_saves_process_save_task_apply_async: mock.Mock,
authenticated_client: Client,
association_out,
async_result,
):
# When
result = authenticated_client.post(
reverse('ui.associations.refresh', args=(association_out.pk,)),
headers={
'HX-Request': 'true',
},
data={
'canhazconfirm': 'hai',
},
)
# Then
assert result.status_code == http.HTTPStatus.OK
expected_payload = {
'status': 'ok',
'result': async_result.id,
}
assert result.json() == expected_payload
@pytest.mark.django_db
def test_invalid_all_missing(authenticated_client: Client,
association_out,
mock_saves_process_save_task_apply_async: mock.Mock,
):
# When
result = authenticated_client.post(
reverse('ui.associations.refresh', args=(association_out.pk,)),
data={
},
)
# Then
assert result.status_code == http.HTTPStatus.OK
mock_saves_process_save_task_apply_async.assert_not_called()
assert 'canhazconfirm' in result.context['form'].errors
@pytest.mark.django_db
def test_invalid_all_empty(authenticated_client: Client,
association_out,
mock_saves_process_save_task_apply_async: mock.Mock,
):
# When
result = authenticated_client.post(
reverse('ui.associations.refresh', args=(association_out.pk,)),
data={
'canhazconfirm': '',
},
)
# Then
assert result.status_code == http.HTTPStatus.OK
mock_saves_process_save_task_apply_async.assert_not_called()
assert 'canhazconfirm' in result.context['form'].errors
@pytest.mark.django_db
def test_other_account_association(authenticated_client: Client,
other_account_association_out,
):
# When
result = authenticated_client.post(
reverse('ui.associations.refresh', args=(other_account_association_out.pk,)),
data={
'canhazconfirm': 'hai',
},
)
# Then
assert result.status_code == http.HTTPStatus.FORBIDDEN
@pytest.mark.django_db
def test_inactive_account(inactive_account_client: Client,
association_out,
):
# When
result = inactive_account_client.post(
reverse('ui.associations.refresh', args=(association_out.pk,)),
data={
'canhazconfirm': 'hai',
},
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[
('next', reverse('ui.associations.refresh', args=(association_out.pk,))),
],
),
fetch_redirect_response=False,
)
@pytest.mark.django_db
def test_anonymous(client: Client,
association_out,
):
# When
result = client.post(
reverse('ui.associations.refresh', args=(association_out.pk,)),
data={
'canhazconfirm': 'hai',
},
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[
('next', reverse('ui.associations.refresh', args=(association_out.pk,))),
],
),
fetch_redirect_response=False,
)

View 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
from hotpocket_backend_testing.services.saves import AssociationsTestingService
@pytest.mark.django_db
def test_ok(authenticated_client: Client,
association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.star', args=(association_out.pk,)),
)
# Then
asserts.assertRedirects(
result,
reverse('ui.associations.browse'),
fetch_redirect_response=False,
)
AssociationsTestingService().assert_starred(
pk=association_out.pk, reference=association_out,
)
@pytest.mark.django_db
def test_ok_htmx(authenticated_client: Client,
association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.star', args=(association_out.pk,)),
headers={
'HX-Request': 'true',
},
)
# Then
assert result.status_code == http.HTTPStatus.OK
asserts.assertTemplateUsed('ui/associations/association_card.html')
assert result.context['association'].pk == association_out.pk
assert hasattr(result.context['association'], 'target') is True
assert result.context['association'].target.pk == association_out.target.pk
@pytest.mark.django_db
def test_other_account_association(authenticated_client: Client,
other_account_association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.star', args=(other_account_association_out.pk,)),
)
# Then
assert result.status_code == http.HTTPStatus.FORBIDDEN
@pytest.mark.django_db
def test_inactive_account(inactive_account_client: Client,
association_out,
):
# When
result = inactive_account_client.get(
reverse('ui.associations.star', args=(association_out.pk,)),
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[
('next', reverse('ui.associations.star', args=(association_out.pk,))),
],
),
fetch_redirect_response=False,
)
@pytest.mark.django_db
def test_anonymous(client: Client,
association_out,
):
# When
result = client.get(
reverse('ui.associations.star', args=(association_out.pk,)),
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[
('next', reverse('ui.associations.star', args=(association_out.pk,))),
],
),
fetch_redirect_response=False,
)

View 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
from hotpocket_backend_testing.services.saves import AssociationsTestingService
@pytest.mark.django_db
def test_ok(authenticated_client: Client,
starred_association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.unstar', args=(starred_association_out.pk,)),
)
# Then
asserts.assertRedirects(
result,
reverse('ui.associations.browse'),
fetch_redirect_response=False,
)
AssociationsTestingService().assert_not_starred(
pk=starred_association_out.pk, reference=starred_association_out,
)
@pytest.mark.django_db
def test_ok_htmx(authenticated_client: Client,
starred_association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.unstar', args=(starred_association_out.pk,)),
headers={
'HX-Request': 'true',
},
)
# Then
assert result.status_code == http.HTTPStatus.OK
asserts.assertTemplateUsed('ui/associations/association_card.html')
assert result.context['association'].pk == starred_association_out.pk
assert hasattr(result.context['association'], 'target') is True
assert result.context['association'].target.pk == starred_association_out.target.pk
@pytest.mark.django_db
def test_other_account_association(authenticated_client: Client,
other_account_starred_association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.unstar', args=(other_account_starred_association_out.pk,)),
)
# Then
assert result.status_code == http.HTTPStatus.FORBIDDEN
@pytest.mark.django_db
def test_inactive_account(inactive_account_client: Client,
association_out,
):
# When
result = inactive_account_client.get(
reverse('ui.associations.unstar', args=(association_out.pk,)),
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[
('next', reverse('ui.associations.unstar', args=(association_out.pk,))),
],
),
fetch_redirect_response=False,
)
@pytest.mark.django_db
def test_anonymous(client: Client,
association_out,
):
# When
result = client.get(
reverse('ui.associations.unstar', args=(association_out.pk,)),
)
# Then
asserts.assertRedirects(
result,
reverse(
'ui.accounts.login',
query=[
('next', reverse('ui.associations.unstar', args=(association_out.pk,))),
],
),
fetch_redirect_response=False,
)

View File

@@ -0,0 +1,274 @@
# -*- 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.mark.django_db
def test_authenticated_ok(authenticated_client: Client,
association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.view', args=(association_out.pk,)),
)
# Then
assert result.status_code == http.HTTPStatus.OK
asserts.assertTemplateUsed('ui/associations/view.html')
assert result.context['association'].pk == association_out.pk
assert hasattr(result.context['association'], 'target') is True
assert result.context['association'].target.pk == association_out.target.pk
assert result.context['show_controls'] is True
@pytest.mark.django_db
def test_authenticated_archived(authenticated_client: Client,
archived_association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.view', args=(archived_association_out.pk,)),
)
# Then
assert result.status_code == http.HTTPStatus.OK
asserts.assertTemplateUsed('ui/associations/view.html')
assert result.context['show_controls'] is False
@pytest.mark.django_db
def test_authenticated_deleted(authenticated_client: Client,
deleted_association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.view', args=(deleted_association_out.pk,)),
)
# Then
assert result.status_code == http.HTTPStatus.NOT_FOUND
@pytest.mark.django_db
def test_authenticated_not_found(authenticated_client: Client,
null_uuid,
):
# When
result = authenticated_client.get(
reverse('ui.associations.view', args=(null_uuid,)),
)
# Then
assert result.status_code == http.HTTPStatus.NOT_FOUND
@pytest.mark.django_db
def test_authenticated_other_account_association(authenticated_client: Client,
other_account_association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.view', args=(other_account_association_out.pk,)),
)
# Then
assert result.status_code == http.HTTPStatus.FORBIDDEN
@pytest.mark.django_db
def test_authenticated_share_ok(authenticated_client: Client,
other_account_association_out,
):
# When
result = authenticated_client.get(
reverse(
'ui.associations.view',
args=(other_account_association_out.pk,),
query=[('share', 'true')],
),
)
# Then
assert result.status_code == http.HTTPStatus.OK
asserts.assertTemplateUsed('ui/associations/view.html')
assert result.context['association'].pk == other_account_association_out.pk
assert hasattr(result.context['association'], 'target') is True
assert result.context['association'].target.pk == other_account_association_out.target.pk
assert result.context['show_controls'] is False
@pytest.mark.django_db
def test_authenticated_share_owner_ok(authenticated_client: Client,
association_out,
):
# When
result = authenticated_client.get(
reverse(
'ui.associations.view',
args=(association_out.pk,),
query=[('share', 'true')],
),
)
# Then
assert result.status_code == http.HTTPStatus.OK
asserts.assertTemplateUsed('ui/associations/view.html')
assert result.context['show_controls'] is False
@pytest.mark.django_db
def test_authenticated_share_archived(authenticated_client: Client,
other_account_archived_association_out,
):
# When
result = authenticated_client.get(
reverse(
'ui.associations.view',
args=(other_account_archived_association_out.pk,),
query=[('share', 'true')],
),
)
# Then
assert result.status_code == http.HTTPStatus.NOT_FOUND
@pytest.mark.django_db
def test_authenticated_share_deleted(authenticated_client: Client,
other_account_deleted_association_out,
):
# When
result = authenticated_client.get(
reverse(
'ui.associations.view',
args=(other_account_deleted_association_out.pk,),
query=[('share', 'true')],
),
)
# Then
assert result.status_code == http.HTTPStatus.NOT_FOUND
@pytest.mark.django_db
def test_authenticated_share_not_found(authenticated_client: Client,
null_uuid,
):
# When
result = authenticated_client.get(
reverse(
'ui.associations.view', args=(null_uuid,),
query=[('share', 'true')],
),
)
# Then
assert result.status_code == http.HTTPStatus.NOT_FOUND
@pytest.mark.django_db
def test_anonymous_share_ok(client: Client,
association_out,
):
# When
result = client.get(
reverse(
'ui.associations.view',
args=(association_out.pk,),
query=[('share', 'true')],
),
)
# Then
assert result.status_code == http.HTTPStatus.OK
asserts.assertTemplateUsed('ui/associations/view.html')
assert result.context['association'].pk == association_out.pk
assert result.context['show_controls'] is False
@pytest.mark.django_db
def test_anonymous_share_archived(client: Client,
archived_association_out,
):
# When
result = client.get(
reverse(
'ui.associations.view',
args=(archived_association_out.pk,),
query=[('share', 'true')],
),
)
# Then
assert result.status_code == http.HTTPStatus.NOT_FOUND
@pytest.mark.django_db
def test_anonymous_share_deleted(client: Client,
deleted_association_out,
):
# When
result = client.get(
reverse(
'ui.associations.view',
args=(deleted_association_out.pk,),
query=[('share', 'true')],
),
)
# Then
assert result.status_code == http.HTTPStatus.NOT_FOUND
@pytest.mark.django_db
def test_anonymous_share_not_found(client: Client,
null_uuid,
):
# When
result = client.get(
reverse(
'ui.associations.view', args=(null_uuid,),
query=[('share', 'true')],
),
)
# Then
assert result.status_code == http.HTTPStatus.NOT_FOUND
@pytest.mark.django_db
def test_inactive_account(inactive_account_client: Client,
association_out,
):
# When
result = inactive_account_client.get(
reverse('ui.associations.view', args=(association_out.pk,)),
)
# Then
assert result.status_code == http.HTTPStatus.FORBIDDEN
@pytest.mark.django_db
def test_anonymous(client: Client,
association_out,
):
# When
result = client.get(
reverse('ui.associations.view', args=(association_out.pk,)),
)
# Then
assert result.status_code == http.HTTPStatus.FORBIDDEN