Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

frontend/templatetags: Add handling for missing status #1107

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion squad/frontend/templatetags/squad.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ def download_build_attachments_url(group_slug, project_slug, build_version, test
@register_global_function
def project_status(project):
if project.latest_build is not None:
return project.latest_build.status
try:
return project.latest_build.status
except Build.status.RelatedObjectDoesNotExist:
return None
chaws marked this conversation as resolved.
Show resolved Hide resolved
return None


Expand Down
5 changes: 5 additions & 0 deletions test/frontend/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ def test_group_404(self):
def test_project(self):
self.hit('/mygroup/myproject/')

def test_project_list_no_status(self):
self.build = self.project.builds.create(version="mybuild")
self.build.status.delete()
self.hit('/mygroup/?all_projects=1')

def test_project_badge(self):
self.hit('/mygroup/myproject/badge')

Expand Down
28 changes: 27 additions & 1 deletion test/frontend/test_template_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from allauth.socialaccount.providers.github.provider import GitHubProvider
from allauth.socialaccount.providers.gitlab.provider import GitLabProvider

from squad.frontend.templatetags.squad import get_page_url, strip_get_parameters, update_get_parameters, to_json, socialaccount_providers
from squad.core import models
from squad.frontend.templatetags.squad import get_page_url, project_status, strip_get_parameters, update_get_parameters, to_json, socialaccount_providers


class FakeRequest():
Expand Down Expand Up @@ -133,3 +134,28 @@ def test_socialaccount_providers_gitlab(self):
social_providers = socialaccount_providers(context)
self.assertEqual(1, len(social_providers.keys()))
self.assertEqual(GitLabProvider, list(social_providers)[0].__class__)

def test_catch_error_when_status_missing(self):
# Test that if the status for a build gets deleted, that this is
# handled appropriately, rather than causing a crash.

# create the group, project and build
self.group = models.Group.objects.create(slug="mygroup")
self.project = self.group.projects.create(slug="myproject")
self.build = self.project.builds.create(version="mybuild")
self.project.latest_build = self.build

# Set build status to None
self.build.status = None

# Try to call project_status when status is None
missing_project_status_error = False
try:
status = project_status(self.project)
except models.Build.status.RelatedObjectDoesNotExist:
chaws marked this conversation as resolved.
Show resolved Hide resolved
missing_project_status_error = True

# Check call to project_status doesn't crash
self.assertFalse(missing_project_status_error)
# Check status returns None as expected
self.assertEqual(status, None)
Loading