-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
60 lines (53 loc) · 2.43 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
from unittest import TestCase
from flask import url_for
from app import app
import jwt
import string
import random
import base64
class RootTests(TestCase):
def setUp(self):
self.app = app
self.app.testing = True
self.app_context = self.app.test_request_context()
self.app_context.push()
self.client = self.app.test_client()
self.username = "test_user"
self.password = "1234567890"
self.token = jwt.encode({}, app.config['SECRET_KEY'], algorithm='HS256')
def test_root_must_return_hello_world(self):
request = self.client.get(url_for('hello_world'))
self.assertEqual('Hello, World!', request.data.decode())
def test_root__must_code_200(self):
request = self.client.get(url_for('hello_world'))
self.assertEqual(200, request.status_code)
def test_a_registration(self):
expected = 200
request = self.client.post(url_for('create_user'),
json={"username": self.username, "password": self.password},
headers={'x-access-token': self.token},
content_type='application/json'
)
self.assertEqual(expected, request.status_code)
def test_b_login(self):
expected = 200
data = self.username + ":" + self.password
base = base64.urlsafe_b64encode(data.encode('UTF-8')).decode('ascii')
request = self.client.get(url_for('login'),
json={},
headers={'x-access-token': self.token,
'Authorization': 'Basic ' + base},
content_type='application/json'
)
self.token = request.data.decode('UTF-8')[10:-3]
self.assertEqual(expected, request.status_code)
def test_create_person(self):
expected = 200
token = jwt.encode({}, app.config['SECRET_KEY'], algorithm='HS256')
request = self.client.post(url_for('create_person'),
json={"name": "Chandler",
"surname": "Bing"},
headers={'x-access-token': token},
content_type='application/json'
)
self.assertEqual(expected, request.status_code)