hotpocket/services/backend/tests/ui/views/associations/test_unstar.py
Tomek Wójcik 8b86145519 BTHLABS-61: Service layer refactoring
A journey to fix `ValidationError` in Pocket imports turned service
layer refactoring :D
2025-10-12 20:54:00 +02:00

151 lines
3.9 KiB
Python

# -*- 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_archived(authenticated_client: Client,
archived_association_out,
):
# When
result = authenticated_client.get(
reverse('ui.associations.unstar', 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.unstar', 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.unstar', 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_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,
)