-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
80 lines (64 loc) · 2.77 KB
/
tests.py
File metadata and controls
80 lines (64 loc) · 2.77 KB
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
import base64
import json
import unittest
from app import app
class BasicRestTestClass(unittest.TestCase):
# initialization logic for the test suite declared in the test module
# code that is executed before all tests in one test run
@classmethod
def setUpClass(cls):
pass
# clean up logic for the test suite declared in the test module
# code that is executed after all tests in one test run
@classmethod
def tearDownClass(cls):
pass
# initialization logic
# code that is executed before each test
def setUp(self):
# creates a test client
self.app = app.test_client()
# propagate the exceptions to the test client
self.app.testing = True
# code that is executed after each test
def tearDown(self):
pass
# test method
def open_with_auth(self, url, method, username, password):
return self.app.open(url,
method=method,
headers={
'Authorization': 'Basic ' + base64.b64encode(
bytes(username + ":" + password, 'ascii')).decode('ascii')
}
)
def test_auth(self):
result = self.open_with_auth('/', 'GET', 'ahad',
'bokhari')
self.assertEqual(result.status_code, 200)
def test_index(self):
res = self.open_with_auth('/', 'GET', 'ahad',
'bokhari')
self.assertEqual(res.status_code, 200)
self.assertEqual(res.data, b'{"hello":"world"}\n')
def test_get_tasks(self):
result = self.open_with_auth('/todo/tasks', 'GET', 'ahad',
'bokhari')
self.assertEqual(result.status_code, 200)
data = json.loads(result.get_data(as_text=True))
learn_python = {'id': 1, 'title': 'Learn Python',
'description': 'A clever way to learn Python is to start with the basics', 'done': False}
learn_flask = {'title': 'Learn Flask', 'description': 'Flask is one awesome package that is un-opinonated',
'id': 2, 'done': False}
self.assertEqual(data["tasks"][0], learn_python)
self.assertEqual(data["tasks"][1], learn_flask)
self.assertEqual(data["tasks"][2]["done"], True)
def test_get_task_by_id(self):
result = self.open_with_auth('/todo/tasks/3', 'GET', 'ahad',
'bokhari')
self.assertEqual(result.status_code, 200)
data = json.loads(result.get_data(as_text=True))
self.assertEqual(data["task"]["done"], True)
# runs the unit tests in the module
if __name__ == '__main__':
unittest.main()