forked from FeeiCN/Cobra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
64 lines (56 loc) · 2.05 KB
/
test.py
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
#!/usr/bin/env python
#
# Copyright 2016 Feei. All Rights Reserved
#
# Author: Feei <[email protected]>
# Homepage: https://github.com/wufeifei/cobra
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See the file 'doc/COPYING' for copying permission
#
import unittest
import requests
from utils import common, config
import json
class Test(unittest.TestCase):
domain = '{0}:{1}'.format(config.Config('cobra', 'host').value, config.Config('cobra', 'port').value)
api = 'http://' + domain + '/api/{0}'
headers = {"Content-Type": "application/json"}
key = common.md5('CobraAuthKey')
target = 'https://github.com/wufeifei/dict.git'
branch = 'master'
def test_api(self):
"""
Cobra API Test
:return:
"""
payload = json.dumps({
"key": self.key,
"target": self.target,
"branch": self.branch
})
try:
response = requests.post(self.api.format('add'), data=payload, headers=self.headers)
response_json = response.json()
code = response_json.get('code')
self.assertEqual(code, 1001)
result = response_json.get('result')
scan_id = result.get('scan_id')
print("API Add: {0}".format(result))
status_query = json.dumps({
'key': self.key,
'scan_id': scan_id
})
status_response = requests.post(self.api.format('status'), data=status_query, headers=self.headers)
status_response_json = status_response.json()
code = status_response_json.get('status')
result = status_response_json.get('result')
print("API Status: {0}".format(result))
self.assertEqual(code, 1001)
except (requests.ConnectionError, requests.HTTPError) as e:
self.fail("API Add failed: {0}".format(e))
if __name__ == '__main__':
unittest.main()