-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
139 lines (119 loc) · 5.11 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
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
import os
import unittest
import json
from flask_sqlalchemy import SQLAlchemy
from app import app, db
from models import Building, Unit
admin_token = os.environ['AUTH0_ADMIN_TOKEN']
owner_token = os.environ['AUTH0_OWNER_TOKEN']
class EstateTestCase(unittest.TestCase):
def setUp(self):
app.config.from_object('config.TestingConfig')
app.config['SQLALCHEMY_DATABASE_URI'] = '\
postgres://aaltwaim@localhost:5432/estate'
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
self.client = app.test_client()
self.headers = {'Content-Type': 'application/json'}
db.drop_all()
db.create_all()
def tearDown(self):
"""Executed after reach test"""
pass
def test_visitor_get_buildings(self):
res = self.client.get('/buildings', headers=self.headers)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
def test_admin_get_buildings(self):
self.headers.update({'Authorization': 'Bearer ' + str(admin_token)})
res = self.client.get('/buildings', headers=self.headers)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
def test_visitor_get_building_by_id(self):
res = self.client.get('/buildings/2', headers=self.headers)
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
def test_admin_get_building_by_id(self):
self.headers.update({'Authorization': 'Bearer ' + str(admin_token)})
res = self.client.get('/buildings/2', headers=self.headers)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
def test_visitor_add_building(self):
new_building = {
"address": "Alyasmin",
"description": "3 floors building",
"name": "Tlal",
"number_of_units": 9,
"ownerID": 1234567890,
"building_image": "https://dangerwordfilm.\
files.wordpress.com/2014/04/coming-soon.png"
}
res = self.client.post('/buildings', json=new_building)
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
def test_admin_add_building(self):
new_building = {
"address": "Alyasmin",
"description": "3 floors building",
"name": "Tlal",
"number_of_units": 9,
"ownerID": 1234567890,
"building_image": "https://dangerwordfilm.files.\
wordpress.com/2014/04/coming-soon.png"
}
self.headers.update({'Authorization': 'Bearer ' + str(admin_token)})
res = self.client.post('\
/buildings', json=new_building, headers=self.headers)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
def test_visitor_update_building(self):
updated_building = {
"address": "Almraba",
}
res = self.client.patch('/buildings/2', json=updated_building)
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
def test_admin_update_building(self):
building = Building(
address="Alyasmin", description="3 floors building",
name="Tlal", number_of_units=9, ownerID=1234567890,
building_image="https://dangerwordfilm.files.\
wordpress.com/2014/04/coming-soon.png")
building.insert()
building_id = building.id
updated_building = {
"address": "Almraba",
}
self.headers.update({'Authorization': 'Bearer ' + str(admin_token)})
res = self.client.patch(f'/buildings/\
{building_id}', json=updated_building, headers=self.headers)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
def test_visitor_delete_building(self):
res = self.client.delete('/buildings/3')
data = json.loads(res.data)
self.assertEqual(res.status_code, 401)
def test_admin_delete_building(self):
building = Building(
address="Alyasmin", description="3 floors building",
name="Tlal", number_of_units=9, ownerID=1234567890,
building_image="https://dangerwordfilm.files.\
wordpress.com/2014/04/coming-soon.png")
building.insert()
building_id = building.id
self.headers.update({'Authorization': 'Bearer ' + str(admin_token)})
res = self.client.delete(f'/buildings/\
{building_id}', headers=self.headers)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
def test_visitor_get_units_based_on_building(self):
res = self.client.get('/buildings/2/units', headers=self.headers)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
def test_admin_get_units_based_on_building(self):
self.headers.update({'Authorization': 'Bearer ' + str(admin_token)})
res = self.client.get('/buildings/2/units', headers=self.headers)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
# Make the tests conveniently executable
if __name__ == "__main__":
unittest.main()