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

Commit

Permalink
frontend/templatetags: Add handling for missing status
Browse files Browse the repository at this point in the history
If status was missing, this would previously cause a 500 error when
loading the page, due to a RelatedObjectDoesNotExist error being raised
when the project status function was called.

Now, add handling for the case where status is None, so the
project_status will return None instead of raising an error.

Signed-off-by: Katie Worton <[email protected]>
  • Loading branch information
katieworton committed Oct 16, 2023
1 parent 2167533 commit df3a171
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
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
return None


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:
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)

0 comments on commit df3a171

Please sign in to comment.