diff options
-rwxr-xr-x | Post/test.py | 111 | ||||
-rw-r--r-- | test-data/test-payload.json | 18 |
2 files changed, 129 insertions, 0 deletions
diff --git a/Post/test.py b/Post/test.py new file mode 100755 index 0000000..1eae5f6 --- /dev/null +++ b/Post/test.py @@ -0,0 +1,111 @@ +import unittest +import urllib +import json +from django.test import Client +from Post.models import BuildFailure + +class SimpleTest(unittest.TestCase): + def setUp(self): + self.client = Client(HTTP_HOST="testhost") + + def test_links(self): + response = self.client.get('/Errors/Latest/') + self.assertEqual(response.status_code, 200) + + response = self.client.get('/Statistics/') + self.assertEqual(response.status_code, 200) + + # Opening test-payload.json and submitting it to server + # expecting json response 1st item inserted to db + def test_submission(self): + + with open("test-data/test-payload.json") as f: + data = f.read() + + + data = urllib.urlencode({'data': data}) + + response = self.client.post("/ClientPost/", + data, + "application/json") + + self.assertEqual(response.status_code, 200) + self.assertEqual("/Build/1" in response.content, True) + + # Now let's see if the data entered the db + data_ob = BuildFailure.objects.get(id=1) + self.assertEqual("tester" in data_ob.BUILD.NAME, True) + + + # Opening test-payload.json and submitting it to server + # expecting json response 2nd item inserted to db + def test_submission_ret_json(self): + + with open("test-data/test-payload.json") as f: + data = f.read() + + + data = urllib.urlencode({'data': data}) + + response = self.client.post("/ClientPost/JSON/", + data, + "application/json") + + self.assertEqual(response.status_code, 200) + + ret = json.loads(response.content) + + self.assertEqual(ret['build_id'], 2) + fails = ret['failures'] + + self.assertEqual(fails[0]['id'], 2) + + # Now let's see if the data entered the db + data_ob = BuildFailure.objects.get(id=2) + self.assertEqual("tester" in data_ob.BUILD.NAME, True) + + + + # Submitting invalid json to server expecting Invalid json in response + def test_invalid_json(self): + response = self.client.post("/ClientPost/", + "data=woeifjopeijefowiejfoweijfo", + "application/json") + + + self.assertEqual("Invalid json" in response.content, True) + + + # Submitting invalid json to server expecting Invalid json in + # a json response + def test_invalid_json_ret_json(self): + response = self.client.post("/ClientPost/JSON/", + "data=woeifjopeijefowiejfoweijfo", + "application/json") + + + ret = json.loads(response.content) + + self.assertEqual(response.status_code, 500) + self.assertEqual("Invalid json" in ret['error'], True) + + + # Submitting valid json to server with fields missing + def test_missing_fields_ret_json(self): + + response = self.client.post("/ClientPost/JSON/", + "data={}", + "application/json") + + + ret = json.loads(response.content) + + self.assertEqual(response.status_code, 500) + self.assertEqual("Payload missing required fields" in ret['error'], + True) + + + + + + diff --git a/test-data/test-payload.json b/test-data/test-payload.json new file mode 100644 index 0000000..0428a16 --- /dev/null +++ b/test-data/test-payload.json @@ -0,0 +1,18 @@ +{ + "branch_commit": "(master-test): 736b49449233c936e3099b314a58e91ad17f9774", + "build_sys": "x86_64-linux", + "component": "base-passwd", + "distro": "poky", + "username" : "tester", + "email" : "tester@tester", + "failures": [ + { + "log": "DEBUG: SITE files ['endian-little', 'bit-32', 'ix86-common', 'common-linux', 'common-glibc', 'i586-linux', 'common']\nDEBUG: Executing shell function do_install\ninstall: missing destination file operand after 'wefwefweffwe'\nTry 'install --help' for more information.\nWARNING: exit code 1 from a shell command.\nERROR: Function failed: do_install (wefe.do_install.36921)\n", + "package": "base-passwd-3.5.29-r0", + "task": "do_install" + } + ], + "machine": "qemux86", + "nativelsb": "Ubuntu-14.04", + "target_sys": "i586-poky-linux" +} |