forked from Laksh47/segarage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
executable file
·164 lines (134 loc) · 5.3 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
import os
import io
import unittest
from config import basedir
from app import app, db, mail
from app.models import Paper
from app.utils import get_email_token
# Run command `nose2 -v tests` to run the unit tests
# Run command `coverage run tests.py` to run unittest and measure code coverage!
# Run command `coverage html app/*.py` to get code coverage report!
class TestCase(unittest.TestCase):
def setUp(self):
"""
initial unit testing setup
disabling WTF_CSRF for form submission requests
test db creation
"""
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.db')
app.config['S3_BUCKET'] = 'segarage_test'
Paper.__tablename__ = 'papers_test'
self.app = app.test_client()
db.create_all()
# Disable send email functionality for unit testing
mail.init_app(app)
self.assertEqual(app.debug, False)
def tearDown(self):
"""
Destroying the test session
"""
db.session.remove()
db.drop_all()
def test_main_page(self):
"""
Testing the index page
"""
response = self.app.get('/', follow_redirects=True)
self.assertEqual(response.status_code, 200)
def test_request_upload(self):
"""
Testing the request upload route function
parameters => request_upload_form params: author email and paper name
"""
form_data = { 'authoremail': 'scrawler16.1@gmail.com', 'papername': 'Test paper' }
response = self.app.post('/request_upload', content_type='multipart/form-data', data=form_data, follow_redirects=True)
# print(response.__dict__)
self.assertEqual(response.status, "200 OK")
def test_search_papers_post(self):
"""
Searching papers (elastic search test case) body params
"""
search_form_data = { 'q': 's3' }
response = self.app.post('/search', content_type='multipart/form-data', data=search_form_data, follow_redirects=True)
# print(response.__dict__)
self.assertEqual(response.status, "200 OK")
def test_search_papers_get(self):
"""
Searching papers (elastic search test case) query params
"""
response = self.app.get('/search?q=s3', follow_redirects=True)
# print(response.__dict__)
self.assertEqual(response.status, "200 OK")
def test_search_papers_fail(self):
"""
Search when query is None
"""
response = self.app.get('/search?q=', follow_redirects=True)
self.assertEqual(response.status, "404 NOT FOUND")
def test_upload_tool(self):
"""
Actual upload paper functionality
"""
payload = { 'authoremail': 'test@test.com', 'papername': 'Testing Paper' }
token = get_email_token(payload)
form_data = { 'papername': 'Test Paper', 'authoremail': 'test@test.com', 'linktoarchive': 'http://test.com', 'description': 'This paper is for unit testing', 'tags': 'test,paper', 'useragreement': True, 'dropdown_choices': 'Binary','file_types': 'Binary', 'bibtex': 'Test' }
form_data['all_files'] = [(io.BytesIO(b"abcdef"), 'test.pdf')]
response = self.app.post('/tool_upload/{}'.format(token), content_type='multipart/form-data', data=form_data)
# print(response.get_data())
self.assertEqual(response.status, "302 FOUND")
def test_update_tool(self):
"""
Testing the editing functionality
"""
paper = self.create_paper()
payload = { 'paper_id': paper.id }
token = get_email_token(payload)
response = self.app.get('/update_tool/{}'.format(token))
self.assertEqual(response.status, "200 OK")
def test_update_tool_submit(self):
"""
Testing the editing functionality
"""
paper = self.create_paper()
payload = { 'paper_id': paper.id }
token = get_email_token(payload)
form_data = { 'papername': 'Test Paper', 'authoremail': 'test@test.com', 'linktoarchive': 'http://test.com', 'description': 'This paper is for unit testing', 'tags': 'test,paper', 'useragreement': True, 'dropdown_choices': 'Binary','file_types': 'Binary', 'bibtex': 'Test' }
form_data['all_files'] = [(io.BytesIO(b"abcdef"), 'test.pdf')]
response = self.app.post('/update_tool/{}'.format(token), content_type='multipart/form-data', data=form_data)
self.assertEqual(response.status, "200 OK")
def test_papers(self):
"""
Papers html page
"""
response = self.app.get('/papers')
self.assertEqual(response.status, "200 OK")
# def test_specific_papers(self):
# """
# Specific paper html page rendering
# """
# paper = self.create_paper()
# response = self.app.get('/papers/{}'.format(paper.id))
# self.assertEqual(response.status, "200 OK")
def test_specific_papers_not_exists(self):
"""
Specific paper html page rendering failure
"""
response = self.app.get('/papers/4')
self.assertEqual(response.status, "404 NOT FOUND")
def test_download_file(self):
"""
Trying to download a test file from test S3 bucket
"""
paper = self.create_paper()
response = self.app.get('/downloads/{}/test.pdf'.format(paper.id), follow_redirects=True)
self.assertEqual(response.status, "200 OK")
#### Helper methods for test cases
def create_paper(self):
paper = Paper(paper_name='Test paper', author_email='test@gmail.com')
db.session.add(paper)
db.session.commit()
return paper
if __name__ == '__main__':
unittest.main()