Skip to content

Commit

Permalink
404 if tags don't have resources assigned
Browse files Browse the repository at this point in the history
  • Loading branch information
mathjazz committed Feb 23, 2024
1 parent fb25c53 commit 2ffb809
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
30 changes: 22 additions & 8 deletions pontoon/tags/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,37 @@
from django.urls import reverse

from pontoon.base.models import Priority
from pontoon.test.factories import TagFactory, TranslatedResourceFactory


@pytest.mark.django_db
def test_view_project_tag_locales(client, project_a, tag_a):
def test_view_project_tag_locales(client, locale_a, project_a, resource_a):
TranslatedResourceFactory.create(resource=resource_a, locale=locale_a)

tag = TagFactory.create(
slug="tag",
name="Tag",
)

url = reverse(
"pontoon.tags.project.tag",
kwargs=dict(project=project_a.slug, tag=tag_a.slug),
kwargs=dict(project=project_a.slug, tag=tag.slug),
)

# tag is not associated with project
project_a.tag_set.remove(tag_a)
# tag is not associated with a project
response = client.get(url)
assert response.status_code == 404

project_a.tag_set.add(tag_a)
tag_a.priority = Priority.NORMAL
tag_a.save()
tag.project = project_a
tag.save()

# tag is not associated with a resource
response = client.get(url)
assert response.status_code == 404

tag.resources.add(resource_a)
tag.save()

response = client.get(url)
assert response.status_code == 200

Expand All @@ -31,7 +45,7 @@ def test_view_project_tag_locales(client, project_a, tag_a):
assert response.context_data["project"] == project_a

res_tag = response.context_data["tag"]
assert res_tag == tag_a
assert res_tag == tag


@pytest.mark.django_db
Expand Down
4 changes: 3 additions & 1 deletion pontoon/tags/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def __init__(self, **kwargs):
self.project = kwargs.get("project")
self.locale = kwargs.get("locale")
self.slug = kwargs.get("slug")
self.tag = Tag.objects.filter(project=self.project, slug=self.slug).first()
self.tag = Tag.objects.filter(
project=self.project, slug=self.slug, resources__isnull=False
).first()

def get(self):
tags = (
Expand Down

0 comments on commit 2ffb809

Please sign in to comment.