|
| 1 | +from collections.abc import Callable |
| 2 | + |
| 3 | +from django.contrib.auth.models import User |
| 4 | +from rest_framework.reverse import reverse |
| 5 | +from rest_framework.test import APIClient |
| 6 | + |
| 7 | +from shared.models.linkage import CVEDerivationClusterProposal |
| 8 | + |
| 9 | + |
| 10 | +def url(id: int) -> str: |
| 11 | + return reverse("cvederivationclusterproposal-comment", kwargs={"pk": id}) |
| 12 | + |
| 13 | + |
| 14 | +def test_get_comment_anonymous_no_comment( |
| 15 | + cached_suggestion: CVEDerivationClusterProposal, |
| 16 | +) -> None: |
| 17 | + client = APIClient() |
| 18 | + response = client.get(url(cached_suggestion.pk)) |
| 19 | + assert response.status_code == 200 |
| 20 | + assert response.data == {"comment": None} |
| 21 | + |
| 22 | + |
| 23 | +def test_get_comment_anonymous_with_comment( |
| 24 | + make_cached_suggestion: Callable[..., CVEDerivationClusterProposal], |
| 25 | +) -> None: |
| 26 | + suggestion = make_cached_suggestion(comment="foo") |
| 27 | + client = APIClient() |
| 28 | + response = client.get(url(suggestion.pk)) |
| 29 | + assert response.status_code == 200 |
| 30 | + assert response.data == {"comment": "foo"} |
| 31 | + |
| 32 | + |
| 33 | +def test_get_comment_not_found(cached_suggestion: CVEDerivationClusterProposal) -> None: |
| 34 | + client = APIClient() |
| 35 | + response = client.get(url(cached_suggestion.pk + 1)) |
| 36 | + assert response.status_code == 404 |
| 37 | + |
| 38 | + |
| 39 | +def test_patch_comment_unauthenticated( |
| 40 | + cached_suggestion: CVEDerivationClusterProposal, |
| 41 | +) -> None: |
| 42 | + client = APIClient() |
| 43 | + response = client.patch( |
| 44 | + url(cached_suggestion.pk), {"comment": "foo"}, format="json" |
| 45 | + ) |
| 46 | + assert response.status_code == 401 |
| 47 | + |
| 48 | + |
| 49 | +def test_patch_comment_non_comitter( |
| 50 | + cached_suggestion: CVEDerivationClusterProposal, |
| 51 | + user: User, |
| 52 | +) -> None: |
| 53 | + client = APIClient() |
| 54 | + client.force_login(user) |
| 55 | + response = client.patch( |
| 56 | + url(cached_suggestion.pk), {"comment": "foo"}, format="json" |
| 57 | + ) |
| 58 | + assert response.status_code == 403 |
| 59 | + |
| 60 | + |
| 61 | +def test_patch_comment_sets_value( |
| 62 | + cached_suggestion: CVEDerivationClusterProposal, |
| 63 | + committer: User, |
| 64 | +) -> None: |
| 65 | + client = APIClient() |
| 66 | + client.force_login(committer) |
| 67 | + response = client.patch( |
| 68 | + url(cached_suggestion.pk), {"comment": "foo"}, format="json" |
| 69 | + ) |
| 70 | + assert response.status_code == 200 |
| 71 | + assert response.data == {"comment": "foo"} |
| 72 | + response = client.get(url(cached_suggestion.pk)) |
| 73 | + assert response.status_code == 200 |
| 74 | + assert response.data == {"comment": "foo"} |
| 75 | + |
| 76 | + |
| 77 | +def test_patch_comment_clears_with_empty_string( |
| 78 | + make_cached_suggestion: Callable[..., CVEDerivationClusterProposal], |
| 79 | + committer: User, |
| 80 | +) -> None: |
| 81 | + suggestion = make_cached_suggestion(comment="foo") |
| 82 | + client = APIClient() |
| 83 | + client.force_login(committer) |
| 84 | + response = client.patch(url(suggestion.pk), {"comment": ""}, format="json") |
| 85 | + assert response.status_code == 200 |
| 86 | + assert response.data == {"comment": None} |
| 87 | + response = client.get(url(suggestion.pk)) |
| 88 | + assert response.status_code == 200 |
| 89 | + assert response.data == {"comment": None} |
| 90 | + |
| 91 | + |
| 92 | +def test_patch_comment_not_found( |
| 93 | + cached_suggestion: CVEDerivationClusterProposal, committer: User |
| 94 | +) -> None: |
| 95 | + client = APIClient() |
| 96 | + client.force_login(committer) |
| 97 | + response = client.patch( |
| 98 | + url(cached_suggestion.pk + 1), {"comment": "foo"}, format="json" |
| 99 | + ) |
| 100 | + assert response.status_code == 404 |
0 commit comments