From 867e177c90f61b1ca1c14a3e001ae72cfbce66ef Mon Sep 17 00:00:00 2001 From: sqozz Date: Sat, 7 Apr 2018 21:31:29 +0200 Subject: [PATCH 1/8] Add simple function tests --- schort_functional_tests.py | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 schort_functional_tests.py diff --git a/schort_functional_tests.py b/schort_functional_tests.py new file mode 100755 index 0000000..c9ae47e --- /dev/null +++ b/schort_functional_tests.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +import os +import unittest +import requests + +BASE_URL="http://localhost:5000" + +class SchortFunctionalTestCase(unittest.TestCase): + def setUp(self): + # maybe later + pass + + def tearDown(self): + # maybe later + pass + + def assertPostReq(self, url, data = {}): + req = requests.post(url, data=data) + self.assertEqual(req.status_code, 200) + return req + + def assertGetReq(self, url, params = {}): + req = requests.get(url, params=params) + self.assertEqual(req.status_code, 200) + return req + + def test_entry_page(self): + req = self.assertGetReq(BASE_URL + "/") + + def test_custom_creation(self): + req = self.assertPostReq(BASE_URL + "/", data={"url" : "https://github.com/sqozz/schort", "wishId" : "custom_user_supplied_url"}) + + def test_easy_api(self): + ''' + Test if the api is intuitive/easy to implement + + While creating the custom_creation test, out of pure laziness, I left out the whishId parameter. + For intuitive use of the API from a script, this shouldn't be neccessary. + ''' + req = self.assertPostReq("http://localhost:5000/", data={"url" : "https://github.com/sqozz/schort"}) + + +if __name__ == '__main__': + unittest.main() + +# vim: noexpandtab:ts=2:sw=2:sts=2 From 9d9b591cff1fb0cf8fafb791647782014ba60c64 Mon Sep 17 00:00:00 2001 From: sqozz Date: Sat, 7 Apr 2018 22:11:35 +0200 Subject: [PATCH 2/8] Extend basic entry page test --- schort_functional_tests.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/schort_functional_tests.py b/schort_functional_tests.py index c9ae47e..f0ee115 100755 --- a/schort_functional_tests.py +++ b/schort_functional_tests.py @@ -26,6 +26,10 @@ class SchortFunctionalTestCase(unittest.TestCase): def test_entry_page(self): req = self.assertGetReq(BASE_URL + "/") + content = req.text + self.assertNotEqual(len(content), 0, msg="Get request content was empty.") + self.assertRegex(content, ".*\ Date: Sat, 7 Apr 2018 22:22:10 +0200 Subject: [PATCH 3/8] Extend wish-URL test --- schort_functional_tests.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/schort_functional_tests.py b/schort_functional_tests.py index f0ee115..0f730b5 100755 --- a/schort_functional_tests.py +++ b/schort_functional_tests.py @@ -32,7 +32,11 @@ class SchortFunctionalTestCase(unittest.TestCase): self.assertRegex(content, ".*\ Date: Sat, 7 Apr 2018 23:07:43 +0200 Subject: [PATCH 4/8] Split tests by cases --- schort_functional_tests.py | 42 ++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/schort_functional_tests.py b/schort_functional_tests.py index 0f730b5..52dcef9 100755 --- a/schort_functional_tests.py +++ b/schort_functional_tests.py @@ -5,14 +5,8 @@ import requests BASE_URL="http://localhost:5000" -class SchortFunctionalTestCase(unittest.TestCase): - def setUp(self): - # maybe later - pass - - def tearDown(self): - # maybe later - pass +class TestCase(unittest.TestCase): + pass def assertPostReq(self, url, data = {}): req = requests.post(url, data=data) @@ -24,31 +18,43 @@ class SchortFunctionalTestCase(unittest.TestCase): self.assertEqual(req.status_code, 200) return req + +class SchortBasicTests(TestCase): def test_entry_page(self): + """HTML exists in root page""" req = self.assertGetReq(BASE_URL + "/") content = req.text self.assertNotEqual(len(content), 0, msg="Get request content was empty.") self.assertRegex(content, ".*\ Date: Sun, 8 Apr 2018 15:15:05 +0200 Subject: [PATCH 5/8] Fix tests to base on one common test --- schort_functional_tests.py | 77 +++++++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 18 deletions(-) diff --git a/schort_functional_tests.py b/schort_functional_tests.py index 52dcef9..2919730 100755 --- a/schort_functional_tests.py +++ b/schort_functional_tests.py @@ -2,12 +2,11 @@ import os import unittest import requests +from urllib import parse BASE_URL="http://localhost:5000" -class TestCase(unittest.TestCase): - pass - +class WebTestCase(object): def assertPostReq(self, url, data = {}): req = requests.post(url, data=data) self.assertEqual(req.status_code, 200) @@ -18,8 +17,13 @@ class TestCase(unittest.TestCase): self.assertEqual(req.status_code, 200) return req + def assertGetStatusReq(self, expected_status, url, params = {}): + req = requests.get(url, params=params, allow_redirects=False) + self.assertEqual(req.status_code, expected_status) + return req -class SchortBasicTests(TestCase): + +class SchortBasicTests(unittest.TestCase, WebTestCase): def test_entry_page(self): """HTML exists in root page""" req = self.assertGetReq(BASE_URL + "/") @@ -28,33 +32,70 @@ class SchortBasicTests(TestCase): self.assertRegex(content, ".*\)({url})()".format(url=self.shortDest), msg="Returned HTML does not match the regex") + + +class SchortCustomIdTests(unittest.TestCase, SchortShortLinkCase, WebTestCase): + def setUp(self): + self.shortID = "custom_user_supplied_url" + self.shortDest = "https://github.com/sqozz/schort" + self.req = requests.post(BASE_URL + "/", data={"url" : self.shortDest, "wishId" : self.shortID}) + + def test_create(self): + """Test short link creation with a custom supplied wish-id""" + short_url = self.req.text + self.assertEqual(short_url, BASE_URL + "/" + self.shortID) + +class SchortRandomIdTests(unittest.TestCase, SchortShortLinkCase, WebTestCase): + def setUp(self): + """Set up a short url with a randomly assigned id""" + self.shortDest = "https://github.com/sqozz/schort" + req = requests.post(BASE_URL + "/", data={"url" : self.shortDest}) + aquiredId = parse.urlparse(req.text) + aquiredId = aquiredId.path.replace("/", "", 1) + self.shortID = aquiredId + + def test_create(self): + """Test short link creation with a randomly assigned id""" + self.assertNotEqual(len(self.shortID), 0) + if __name__ == '__main__': suite = unittest.TestSuite() suite.addTests(unittest.TestLoader().loadTestsFromTestCase(SchortBasicTests)) - suite.addTests(unittest.TestLoader().loadTestsFromTestCase(SchortRandomIdTests)) + suite.addTests(unittest.TestLoader().loadTestsFromTestCase(SchortRegressionTests)) suite.addTests(unittest.TestLoader().loadTestsFromTestCase(SchortCustomIdTests)) + suite.addTests(unittest.TestLoader().loadTestsFromTestCase(SchortRandomIdTests)) unittest.TextTestRunner(verbosity=2).run(suite) # vim: noexpandtab:ts=2:sw=2:sts=2 From da3f6f5d023c2bac40f5e04729bde59103f99cfa Mon Sep 17 00:00:00 2001 From: sqozz Date: Sun, 8 Apr 2018 15:53:43 +0200 Subject: [PATCH 6/8] Add test messages on fail --- .../functional_tests.py | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) rename schort_functional_tests.py => tests/functional_tests.py (81%) diff --git a/schort_functional_tests.py b/tests/functional_tests.py similarity index 81% rename from schort_functional_tests.py rename to tests/functional_tests.py index 2919730..e60d076 100755 --- a/schort_functional_tests.py +++ b/tests/functional_tests.py @@ -9,17 +9,18 @@ BASE_URL="http://localhost:5000" class WebTestCase(object): def assertPostReq(self, url, data = {}): req = requests.post(url, data=data) - self.assertEqual(req.status_code, 200) + self.assertEqual(req.status_code, 200, msg="Post request unsuccessful") return req def assertGetReq(self, url, params = {}): req = requests.get(url, params=params) - self.assertEqual(req.status_code, 200) + self.assertEqual(req.status_code, 200, msg="Get request unsuccessful") return req def assertGetStatusReq(self, expected_status, url, params = {}): req = requests.get(url, params=params, allow_redirects=False) - self.assertEqual(req.status_code, expected_status) + self.assertEqual(req.status_code, expected_status, msg="Returned status code does not match the expected one") + return req @@ -39,11 +40,17 @@ class SchortRegressionTests(unittest.TestCase, WebTestCase): short_url = req.text self.assertNotEqual(short_url, BASE_URL + "/", msg="Created short link cannot be equal to the root URL") + def test_empty_wish_id(self): + """Test a request with no wishId as all""" + req = self.assertPostReq(BASE_URL + "/", data={"url" : "https://github.com/sqozz/schort"}) + self.assertEqual(req.status_code, 200, msg="Could not handle a request without wishId in the parameter-list") + class SchortShortLinkCase(object): pass shortID = "" shortDest = "" + req = None def test_resolve(self): """Test basic resolving capabilites of schort""" @@ -74,20 +81,21 @@ class SchortCustomIdTests(unittest.TestCase, SchortShortLinkCase, WebTestCase): """Test short link creation with a custom supplied wish-id""" short_url = self.req.text self.assertEqual(short_url, BASE_URL + "/" + self.shortID) + self.assertEqual(self.req.status_code, 200) class SchortRandomIdTests(unittest.TestCase, SchortShortLinkCase, WebTestCase): def setUp(self): """Set up a short url with a randomly assigned id""" self.shortDest = "https://github.com/sqozz/schort" - req = requests.post(BASE_URL + "/", data={"url" : self.shortDest}) - aquiredId = parse.urlparse(req.text) + self.req = requests.post(BASE_URL + "/", data={"url" : self.shortDest}) + aquiredId = parse.urlparse(self.req.text) aquiredId = aquiredId.path.replace("/", "", 1) self.shortID = aquiredId def test_create(self): """Test short link creation with a randomly assigned id""" self.assertNotEqual(len(self.shortID), 0) - + self.assertEqual(self.req.status_code, 200, msg="Link creation was unsuccessful") if __name__ == '__main__': From 51e8e32deef9f64df02c6f430772c70d167b2f37 Mon Sep 17 00:00:00 2001 From: sqozz Date: Sun, 8 Apr 2018 16:00:19 +0200 Subject: [PATCH 7/8] Add resolve test --- tests/functional_tests.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/functional_tests.py b/tests/functional_tests.py index e60d076..a91c4a4 100755 --- a/tests/functional_tests.py +++ b/tests/functional_tests.py @@ -33,6 +33,7 @@ class SchortBasicTests(unittest.TestCase, WebTestCase): self.assertRegex(content, ".*\ Date: Tue, 1 May 2018 03:51:57 +0200 Subject: [PATCH 8/8] Add test for empty urls --- tests/functional_tests.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/functional_tests.py b/tests/functional_tests.py index a91c4a4..1c04079 100755 --- a/tests/functional_tests.py +++ b/tests/functional_tests.py @@ -46,6 +46,13 @@ class SchortRegressionTests(unittest.TestCase, WebTestCase): req = self.assertPostReq(BASE_URL + "/", data={"url" : "https://github.com/sqozz/schort"}) self.assertEqual(req.status_code, 200, msg="Could not handle a request without wishId in the parameter-list") + def test_empty_url(self): + """Test an empty user supplied URL""" + req = requests.post(BASE_URL + "/", data={"url": ""}) + self.assertEqual(req.status_code, 400, msg="Could not handle a request with empty url") + req = requests.post(BASE_URL + "/", data={}) + self.assertEqual(req.status_code, 400, msg="Could not handle a request with url at all") + class SchortShortLinkCase(object): pass