# -*- 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, )