-
-
Notifications
You must be signed in to change notification settings - Fork 328
/
tests.py
168 lines (144 loc) · 5.06 KB
/
tests.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
# -*- coding: utf_8 -*-
"""Basic tests."""
from datetime import datetime
import unittest
from nodejsscan.app import app
from nodejsscan.models import db
from nodejsscan import utils
from werkzeug.datastructures import FileStorage
INV_HASH = '4b3ce8526462b31de7cda339b121b10b299fcb42f4f5a7be48a08797b545f711'
VALID_HASH = 'a6c706242fe1040fb94b53625c98b7d36c94cfffd3f40981faf0d04e1796db1f'
def _tim():
"""Patch timestamp for test."""
return datetime.now()
utils.get_timestamp = _tim
class Tests(unittest.TestCase):
"""Tests."""
def setUp(self):
"""Executed prior to each test."""
app.config['TESTING'] = True
app.config['DEBUG'] = True
app.app_context().push()
db.create_all()
self.app = app.test_client()
self.assertEqual(app.debug, True)
def tearDown(self):
"""Executed after each test."""
pass
def test_index(self):
"""Index."""
response = self.app.get('/', follow_redirects=True)
self.assertEqual(response.status_code, 200)
def test_scans(self):
"""Scans."""
response = self.app.get('/scans', follow_redirects=True)
self.assertEqual(response.status_code, 200)
def test_invalid_hash_scan(self):
"""Scan with invalid hash."""
response = self.app.get('/scan/blaa', follow_redirects=True)
self.assertEqual(response.status_code, 404)
def test_no_hash_scan(self):
"""Scan with non existing hash."""
response = self.app.get(
f'/scan/{INV_HASH}',
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.is_json, True)
self.assertEqual(response.json, {
'message': 'Scan hash not found',
'status': 'failed'})
def test_invalid_hash_search(self):
"""Search with non existing hash."""
dat = {
'q': 'js',
'scan_hash': INV_HASH,
}
response = self.app.post(
'/search',
data=dat,
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.is_json, True)
self.assertEqual(response.json, {
'message': 'Scan hash not found',
'status': 'failed'})
def test_a_scan(self):
"""Test file upload and scan."""
testzip = FileStorage(
stream=open('static/tests/test_assets.zip', 'rb'),
filename='test_assets.zip',
content_type='application/zip',
)
response = self.app.post(
'/upload/',
content_type='multipart/form-data',
data={'file': testzip},
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.is_json, True)
self.assertEqual(response.json, {
'status': 'success',
'url': f'scan/{VALID_HASH}'})
def test_search(self):
"""Test search."""
test_str = b'test_assets/js/express_bodyparser_dos.js'
dat = {
'q': 'body',
'scan_hash': VALID_HASH,
}
response = self.app.post(
'/search',
data=dat,
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.is_json, False)
self.assertEqual(test_str in response.data, True)
def test_view(self):
"""Test view file."""
vfile = 'test_assets/js/express_bodyparser_dos.js'
dat = {
'path': f'{VALID_HASH}/{vfile}',
'scan_hash': VALID_HASH,
}
response = self.app.post(
'/view_file',
data=dat,
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.is_json, True)
self.assertEqual('contents' in response.json, True)
def test_view_path_traversal(self):
"""Attempt path traversal."""
dat = {
'path': '../../../../etc/passwd',
'scan_hash': VALID_HASH,
}
response = self.app.post(
'/view_file',
data=dat,
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.is_json, True)
self.assertEqual(response.json, {
'contents': 'Path Traversal Detected!',
'status': 'failed'})
def test_view_scan(self):
"""Scan with invalid hash."""
response = self.app.get(f'/scan/{VALID_HASH}', follow_redirects=True)
self.assertEqual(response.status_code, 200)
def test_z_delete_scan(self):
"""Test delete scan."""
dat = {
'scan_hash': VALID_HASH,
}
response = self.app.post(
'/delete_scan',
data=dat,
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.is_json, True)
self.assertEqual(response.json, {
'status': 'ok'})
if __name__ == '__main__':
unittest.main()