summaryrefslogtreecommitdiffstats
path: root/test/test_parsers.py
blob: 7a30f2eec166708b033b9a87c51167b8bb52deec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import unittest
from utils import WebhookTestCaseBase


class WebhookTestCase(WebhookTestCaseBase):

    test_case = None
    test_name = None

    def set_test_case(self, test_case, test_name):
        self.test_case = test_case
        self.test_name = test_name

    def shortDescription(self):
        return self.test_name[:-15]

    def runTest(self):
        import requests
        import json

        # GAD configuration for this test case
        config = {
            'http-port': 0,
            'https-enabled': False,
            'wss-enabled': False,
            'web-ui-require-https': False,
            'intercept-stdout': False,
            'detailed-response': True,
            'log-level': 'ERROR',
            'repositories': []
        }

        if 'url' in self.test_case['config']:
            config['repositories'].append(self.test_case['config'])

        # Start GAD instance
        self.start_gad(config)

        # Send webhook request
        session = requests.session()
        response = requests.post('http://localhost:%s/' % self.gad_port(), data=json.dumps(self.test_case['payload']), headers=self.test_case['headers'])

        # Verify response status
        self.assertEqual(response.status_code, self.test_case['expected']['status'])

        # Compare results
        #if 'data' in self.test_case['expected']:
        #    actual = json.dumps(response.json())
        #    expected = json.dumps(self.test_case['expected']['data'])
        #    self.assertEqual(actual, expected)

        # Wait for GAD to handle the request
        self.await_gad()

def suite():
    import os
    import json

    suite = unittest.TestSuite()

    # Look for test cases in samples dir
    samples_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'samples')
    for item in os.listdir(samples_dir):

        if not item[-15:] == '.test-case.json':
            continue

        file = open(os.path.join(samples_dir, item), 'r')
        test_case = json.loads(file.read())

        t = WebhookTestCase()
        t.set_test_case(test_case, item)

        suite.addTest(t)

    return suite

def main():
    from unittest import TestResult
    #result = TestResult()
    #suite().run(result)
    #unittest.main()
    #print result
    s = suite()
    result = unittest.TextTestRunner(verbosity=2).run(s)

if __name__ == '__main__':
    main()