Browse Source

Fix tests to base on one common test

sqozz 1 year ago
parent
commit
76a9d2f268
1 changed files with 59 additions and 18 deletions
  1. 59
    18
      schort_functional_tests.py

+ 59
- 18
schort_functional_tests.py View File

@@ -2,12 +2,11 @@
2 2
 import os
3 3
 import unittest
4 4
 import requests
5
+from urllib import parse
5 6
 
6 7
 BASE_URL="http://localhost:5000"
7 8
 
8
-class TestCase(unittest.TestCase):
9
-	pass
10
-
9
+class WebTestCase(object):
11 10
 	def assertPostReq(self, url, data = {}):
12 11
 		req = requests.post(url, data=data)
13 12
 		self.assertEqual(req.status_code, 200)
@@ -18,8 +17,13 @@ class TestCase(unittest.TestCase):
18 17
 		self.assertEqual(req.status_code, 200)
19 18
 		return req
20 19
 
20
+	def assertGetStatusReq(self, expected_status, url, params = {}):
21
+		req = requests.get(url, params=params, allow_redirects=False)
22
+		self.assertEqual(req.status_code, expected_status)
23
+		return req
21 24
 
22
-class SchortBasicTests(TestCase):
25
+
26
+class SchortBasicTests(unittest.TestCase, WebTestCase):
23 27
 	def test_entry_page(self):
24 28
 		"""HTML exists in root page"""
25 29
 		req = self.assertGetReq(BASE_URL + "/")
@@ -28,33 +32,70 @@ class SchortBasicTests(TestCase):
28 32
 		self.assertRegex(content, ".*\<html.*", msg="Didn't find an opening <html tag in the response.")
29 33
 		self.assertRegex(content, ".*\<div.*", msg="Didn't find any opening <div tag in the response.")
30 34
 
31
-class SchortCustomIdTests(TestCase):
32
-	def test_custom_creation(self):
33
-		"""Test user supplied wish-URLs"""
34
-		wishId = "custom_user_supplied_url"
35
-		req = self.assertPostReq(BASE_URL + "/", data={"url" : "https://github.com/sqozz/schort", "wishId" : "custom_user_supplied_url"})
36
-		short_url = req.text
37
-		self.assertEqual(short_url, BASE_URL + "/" + wishId)
38
-
35
+class SchortRegressionTests(unittest.TestCase, WebTestCase):
39 36
 	def test_empty_wish_id(self):
40 37
 		"""Test a request with an empty wish-URL"""
41 38
 		req = self.assertPostReq(BASE_URL + "/", data={"url" : "https://github.com/sqozz/schort", "wishId" : ""})
42 39
 		short_url = req.text
43 40
 		self.assertNotEqual(short_url, BASE_URL + "/", msg="Created short link cannot be equal to the root URL")
44 41
 
45
-class SchortRandomIdTests(TestCase):
46
-	aquiredId = ""
47 42
 
48
-	def test_default(self):
49
-		"""Test default usage of schort"""
50
-		req = self.assertPostReq(BASE_URL + "/", data={"url" : "https://github.com/sqozz/schort", "wishId" : ""})
43
+class SchortShortLinkCase(object):
44
+	pass
45
+	shortID = ""
46
+	shortDest = ""
47
+
48
+	def test_resolve(self):
49
+		"""Test basic resolving capabilites of schort"""
50
+		self.assertNotEqual(len(self.shortID), 0)
51
+		req = self.assertGetStatusReq(301, BASE_URL + "/" + self.shortID)
52
+		loc = req.headers.get("location")
53
+		self.assertEqual(loc, self.shortDest)
54
+
55
+	def test_resolve_follow(self):
56
+		"""Test if the requests-lib can follow the redirect"""
57
+		req = requests.get(BASE_URL + "/" + self.shortID, allow_redirects=True)
58
+		req.url = self.shortDest
59
+
60
+	def test_HTMLresolve(self):
61
+		"""Test HTML displaying of the shortened URL"""
62
+		HTML_keyword = "+"
63
+		req = self.assertGetReq(BASE_URL + "/" + self.shortID + HTML_keyword)
64
+		self.assertRegex(req.text, "(<a href=)({url})(>)({url})(</a>)".format(url=self.shortDest), msg="Returned HTML does not match the regex")
65
+
66
+
67
+class SchortCustomIdTests(unittest.TestCase, SchortShortLinkCase, WebTestCase):
68
+	def setUp(self):
69
+		self.shortID = "custom_user_supplied_url"
70
+		self.shortDest = "https://github.com/sqozz/schort"
71
+		self.req = requests.post(BASE_URL + "/", data={"url" : self.shortDest, "wishId" : self.shortID})
72
+
73
+	def test_create(self):
74
+		"""Test short link creation with a custom supplied wish-id"""
75
+		short_url = self.req.text
76
+		self.assertEqual(short_url, BASE_URL + "/" + self.shortID)
77
+
78
+class SchortRandomIdTests(unittest.TestCase, SchortShortLinkCase, WebTestCase):
79
+	def setUp(self):
80
+		"""Set up a short url with a randomly assigned id"""
81
+		self.shortDest = "https://github.com/sqozz/schort"
82
+		req = requests.post(BASE_URL + "/", data={"url" : self.shortDest})
83
+		aquiredId = parse.urlparse(req.text)
84
+		aquiredId = aquiredId.path.replace("/", "", 1)
85
+		self.shortID = aquiredId
86
+
87
+	def test_create(self):
88
+		"""Test short link creation with a randomly assigned id"""
89
+		self.assertNotEqual(len(self.shortID), 0)
90
+
51 91
 
52 92
 
53 93
 if __name__ == '__main__':
54 94
 	suite = unittest.TestSuite()
55 95
 	suite.addTests(unittest.TestLoader().loadTestsFromTestCase(SchortBasicTests))
56
-	suite.addTests(unittest.TestLoader().loadTestsFromTestCase(SchortRandomIdTests))
96
+	suite.addTests(unittest.TestLoader().loadTestsFromTestCase(SchortRegressionTests))
57 97
 	suite.addTests(unittest.TestLoader().loadTestsFromTestCase(SchortCustomIdTests))
98
+	suite.addTests(unittest.TestLoader().loadTestsFromTestCase(SchortRandomIdTests))
58 99
 	unittest.TextTestRunner(verbosity=2).run(suite)
59 100
 
60 101
 # vim: noexpandtab:ts=2:sw=2:sts=2

Loading…
Cancel
Save