/
tests.py
171 lines (134 loc) · 5.13 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
169
170
171
import os
import unittest
from flask import abort, url_for
from flask_testing import TestCase
from app import create_app, db
from app.models import User, Asset, Inventory, Location, Ticket, TicketType
class TestBase(TestCase):
def create_app(self):
# pass in test configuration
config_name = 'testing'
app = create_app(config_name)
app.config.update(
SQLALCHEMY_DATABASE_URI='mysql://fc_admin:fc_admin@localhost/fc_test'
)
return app
def setUp(self):
"""
Will be called before every test
"""
db.create_all()
# create test admin user
user = User(name="Rohit", password="admin2020",position="Stuccan",department="controls")
# save users to database
db.session.add(user)
db.session.commit()
#create static data
if User.query.count()==1:
user_id = user.id
location = Location(description="ABQ-First")
db.session.add(location)
db.session.commit()
inventory = Inventory(inventory_type="Fist Inv",purchased_by=user_id,location=location.id)
db.session.add(inventory)
db.session.commit()
ticket_types = TicketType(description="First ticket_type")
db.session.add(ticket_types)
db.session.commit()
ticket = Ticket(ticket_type=ticket_types.id,opened_by=user_id,updated_by=user_id,description="First Ticket")
db.session.add(ticket)
db.session.commit()
def tearDown(self):
"""
Will be called after every test
"""
db.session.remove()
db.drop_all()
class TestModels(TestBase):
def test_user_model(self):
"""
Test number of records in Employee table
"""
self.assertEqual(User.query.count(), 1)
def test_asset_model(self):
"""
Test number of records in asssets table
"""
# create test department
asset = Asset(comments="test comments for asset",
inventory_id=Inventory.query.first().id,
location=Location.query.first().id,
managed_by=User.query.first().id,
assigned_to=User.query.first().id,
certified_by=User.query.first().id,
)
# save department to database
db.session.add(asset)
db.session.commit()
self.assertEqual(Asset.query.count(), 1)
class TestViews(TestBase):
def test_homepage_view(self):
"""
Test that homepage is accessible without login
"""
response = self.client.get(url_for('home.homepage'))
self.assertEqual(response.status_code, 200)
def test_login_view(self):
"""
Test that login page is accessible without login
"""
response = self.client.get(url_for('auth.login'))
self.assertEqual(response.status_code, 200)
def test_logout_view(self):
"""
Test that logout link is inaccessible without login
and redirects to login page then to logout
"""
target_url = url_for('auth.logout')
redirect_url = url_for('auth.login', next=target_url)
response = self.client.get(target_url)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, redirect_url)
def test_dashboard_view(self):
"""
Test that dashboard is inaccessible without login
and redirects to login page then to dashboard
"""
target_url = url_for('home.dashboard')
redirect_url = url_for('auth.login', next=target_url)
response = self.client.get(target_url)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, redirect_url)
def test_assets_view(self):
"""
Test that departments page is inaccessible without login
and redirects to login page then to departments page
"""
target_url = url_for('assets.list_assets')
redirect_url = url_for('auth.login', next=target_url)
response = self.client.get(target_url)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, redirect_url)
class TestErrorPages(TestBase):
def test_403_forbidden(self):
# create route to abort the request with the 403 Error
@self.app.route('/403')
def forbidden_error():
abort(403)
response = self.client.get('/403')
self.assertEqual(response.status_code, 403)
self.assertTrue("403 Error" in response.data)
def test_404_not_found(self):
response = self.client.get('/nothinghere')
self.assertEqual(response.status_code, 404)
self.assertTrue("404 Error" in response.data)
def test_500_internal_server_error(self):
# create route to abort the request with the 500 Error
@self.app.route('/500')
def internal_server_error():
abort(500)
response = self.client.get('/500')
self.assertEqual(response.status_code, 500)
self.assertTrue("500 Error" in response.data)
if __name__ == '__main__':
unittest.main()