schort/tests/functional_tests.py

124 lines
4.8 KiB
Python
Raw Normal View History

2018-04-07 21:31:29 +02:00
#!/usr/bin/env python3
import os
import unittest
import requests
2018-04-08 15:15:05 +02:00
from urllib import parse
2018-04-07 21:31:29 +02:00
BASE_URL="http://localhost:5000"
2018-04-08 15:15:05 +02:00
class WebTestCase(object):
2018-04-07 21:31:29 +02:00
def assertPostReq(self, url, data = {}):
req = requests.post(url, data=data)
2018-04-08 15:53:43 +02:00
self.assertEqual(req.status_code, 200, msg="Post request unsuccessful")
2018-04-07 21:31:29 +02:00
return req
def assertGetReq(self, url, params = {}):
req = requests.get(url, params=params)
2018-04-08 15:53:43 +02:00
self.assertEqual(req.status_code, 200, msg="Get request unsuccessful")
2018-04-07 21:31:29 +02:00
return req
2018-04-08 15:15:05 +02:00
def assertGetStatusReq(self, expected_status, url, params = {}):
req = requests.get(url, params=params, allow_redirects=False)
2018-04-08 15:53:43 +02:00
self.assertEqual(req.status_code, expected_status, msg="Returned status code does not match the expected one")
2018-04-08 15:15:05 +02:00
return req
2018-04-07 23:07:43 +02:00
2018-04-08 15:15:05 +02:00
class SchortBasicTests(unittest.TestCase, WebTestCase):
2018-04-07 21:31:29 +02:00
def test_entry_page(self):
2018-04-07 23:07:43 +02:00
"""HTML exists in root page"""
2018-04-07 21:31:29 +02:00
req = self.assertGetReq(BASE_URL + "/")
2018-04-07 22:11:35 +02:00
content = req.text
self.assertNotEqual(len(content), 0, msg="Get request content was empty.")
self.assertRegex(content, ".*\<html.*", msg="Didn't find an opening <html tag in the response.")
self.assertRegex(content, ".*\<div.*", msg="Didn't find any opening <div tag in the response.")
2018-04-07 21:31:29 +02:00
2018-04-08 16:00:19 +02:00
2018-04-08 15:15:05 +02:00
class SchortRegressionTests(unittest.TestCase, WebTestCase):
2018-04-07 23:07:43 +02:00
def test_empty_wish_id(self):
"""Test a request with an empty wish-URL"""
req = self.assertPostReq(BASE_URL + "/", data={"url" : "https://github.com/sqozz/schort", "wishId" : ""})
short_url = req.text
self.assertNotEqual(short_url, BASE_URL + "/", msg="Created short link cannot be equal to the root URL")
2018-04-08 15:53:43 +02:00
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")
2018-05-01 03:51:57 +02:00
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={})
2018-06-09 21:56:47 +02:00
self.assertEqual(req.status_code, 400, msg="Could not handle a request without a url at all")
2018-05-01 03:51:57 +02:00
2018-04-07 23:07:43 +02:00
2018-04-08 15:15:05 +02:00
class SchortShortLinkCase(object):
pass
shortID = ""
shortDest = ""
2018-04-08 15:53:43 +02:00
req = None
2018-04-08 15:15:05 +02:00
2018-04-08 16:00:19 +02:00
def test_redirect(self):
"""Test basic redirecting capabilites of schort"""
2018-04-08 15:15:05 +02:00
self.assertNotEqual(len(self.shortID), 0)
req = self.assertGetStatusReq(301, BASE_URL + "/" + self.shortID)
loc = req.headers.get("location")
self.assertEqual(loc, self.shortDest)
2018-04-08 16:00:19 +02:00
def test_redirect_follow(self):
2018-04-08 15:15:05 +02:00
"""Test if the requests-lib can follow the redirect"""
req = requests.get(BASE_URL + "/" + self.shortID, allow_redirects=True)
req.url = self.shortDest
2018-04-08 16:00:19 +02:00
def test_resolve(self):
"""Test the resolve parameter"""
req = self.assertGetReq(BASE_URL + "/" + self.shortID, params = {"resolve" : ""})
self.assertEqual(req.text, self.shortDest)
2018-04-08 15:15:05 +02:00
def test_HTMLresolve(self):
"""Test HTML displaying of the shortened URL"""
HTML_keyword = "+"
req = self.assertGetReq(BASE_URL + "/" + self.shortID + HTML_keyword)
self.assertRegex(req.text, "(<a href=)({url})(>)({url})(</a>)".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)
2018-04-08 15:53:43 +02:00
self.assertEqual(self.req.status_code, 200)
2018-04-08 15:15:05 +02:00
2018-04-08 16:00:19 +02:00
2018-04-08 15:15:05 +02:00
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"
2018-04-08 15:53:43 +02:00
self.req = requests.post(BASE_URL + "/", data={"url" : self.shortDest})
aquiredId = parse.urlparse(self.req.text)
2018-04-08 15:15:05 +02:00
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)
2018-04-08 15:53:43 +02:00
self.assertEqual(self.req.status_code, 200, msg="Link creation was unsuccessful")
2018-04-07 21:31:29 +02:00
if __name__ == '__main__':
2018-04-07 23:07:43 +02:00
suite = unittest.TestSuite()
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(SchortBasicTests))
2018-04-08 15:15:05 +02:00
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(SchortRegressionTests))
2018-04-07 23:07:43 +02:00
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(SchortCustomIdTests))
2018-04-08 15:15:05 +02:00
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(SchortRandomIdTests))
2018-04-07 23:07:43 +02:00
unittest.TextTestRunner(verbosity=2).run(suite)
2018-04-07 21:31:29 +02:00
# vim: noexpandtab:ts=2:sw=2:sts=2