From 76a9d2f26800e3e43e1550654ae51037fb04813c Mon Sep 17 00:00:00 2001 From: sqozz Date: Sun, 8 Apr 2018 15:15:05 +0200 Subject: [PATCH] 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