From 5d650f07fb7e499094518e97c9fc0d82caa19cd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 3 Dec 2020 13:01:53 +0200 Subject: [PATCH] Fix db field overflow when copying a check with a long name --- CHANGELOG.md | 3 +++ hc/front/tests/test_copy.py | 10 ++++++++++ hc/front/views.py | 7 ++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3e0e108..b2358dc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ All notable changes to this project will be documented in this file. - Implement badge mode (up/down vs up/late/down) selector (#282) - Add Ping.exitstatus field, store client's reported exit status values (#455) +## Bug Fixes +- Fix db field overflow when copying a check with a long name + ## v1.17.0 - 2020-10-14 ## Improvements diff --git a/hc/front/tests/test_copy.py b/hc/front/tests/test_copy.py index 68fbad9e..b8333800 100644 --- a/hc/front/tests/test_copy.py +++ b/hc/front/tests/test_copy.py @@ -41,3 +41,13 @@ class CopyCheckTestCase(BaseTestCase): self.client.login(username="bob@example.org", password="password") r = self.client.post(self.copy_url) self.assertEqual(r.status_code, 403) + + def test_it_handles_long_check_name(self): + self.check.name = "A" * 100 + self.check.save() + + self.client.login(username="alice@example.org", password="password") + self.client.post(self.copy_url) + + q = Check.objects.filter(name="A" * 90 + "... (copy)") + self.assertTrue(q.exists()) diff --git a/hc/front/views.py b/hc/front/views.py index f7350d58..ea1b0add 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -623,8 +623,13 @@ def copy(request, code): if check.project.num_checks_available() <= 0: return HttpResponseBadRequest() + new_name = check.name + " (copy)" + # Make sure we don't exceed the 100 character db field limit: + if len(new_name) > 100: + new_name = check.name[:90] + "... (copy)" + copied = Check(project=check.project) - copied.name = check.name + " (copy)" + copied.name = new_name copied.desc, copied.tags = check.desc, check.tags copied.subject, copied.subject_fail = check.subject, check.subject_fail copied.methods = check.methods