-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcoffee_tests.py
115 lines (97 loc) · 3.92 KB
/
coffee_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
import coffee
import unittest
from mongoengine import connect
from database import User, Transaction, Consumption
from flask import g
class CoffeeTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
coffee.app.config['TESTING'] = True
coffee.app.config['DB_NAME'] = 'coffeedb-test'
coffee.app.config['WTF_CSRF_ENABLED'] = False
db = connect(coffee.app.config['DB_NAME'],
host=coffee.app.config['DB_HOST'],
port=coffee.app.config['DB_PORT'])
@classmethod
def tearDownClass(cls):
db = connect(coffee.app.config['DB_NAME'],
host=coffee.app.config['DB_HOST'],
port=coffee.app.config['DB_PORT'])
db.drop_database(coffee.app.config['DB_NAME'])
def setUp(self):
testuser = User(username='testuser', email='[email protected]')
testuser.save()
self.testuser = testuser
User(username='admin', email='[email protected]', admin=True).save()
testpayment = Transaction(diff=1000, user=testuser)
testpayment.save()
testexpense = Transaction(diff=-500)
testexpense.save()
testcons = Consumption(units=1, price_per_unit=50, user=testuser)
testcons.save()
self.app = coffee.app.test_client()
def tearDown(self):
User.drop_collection()
Transaction.drop_collection()
Consumption.drop_collection()
def test_index_redirect(self):
rv = self.app.get('/')
assert(b'Redirecting...' in rv.data)
def test_login_at_index(self):
rv = self.app.get('/', follow_redirects=True)
assert(b'LDAP Login' in rv.data)
def login(self, username, password):
return self.app.post('/login',
data=dict(
username=username,
password=password,
remember='y',
),
follow_redirects=True,
)
def logout(self):
return self.app.get('/logout', follow_redirects=True)
def test_login_logout(self):
rv = self.login('testuser', 'foobar')
assert(b'Actual Budget' in rv.data)
rv = self.logout()
assert(b'LDAP Login' in rv.data)
rv = self.login('testuser', 'foobar-wrong')
assert(b'Login failed' in rv.data)
def test_login_new_user(self):
rv = self.login('newuser', 'foobar')
assert(b'Actual Budget' in rv.data)
def test_guest_login(self):
rv = self.login('guest', 'pw')
assert(b'Login failed' in rv.data)
def test_admin_access(self):
rv = self.login('admin', 'foobar')
assert(b'Actual Budget' in rv.data)
rv = self.app.get('/admin/')
assert(b'Adminpanel' in rv.data)
def test_admin_access_unauthorized(self):
rv = self.login('testuser', 'foobar')
assert(b'Actual Budget' in rv.data)
rv = self.app.get('/admin/')
assert(rv.status_code == 403)
def test_admin_access_db(self):
rv = self.login('admin', 'foobar')
assert(rv.status_code == 200)
assert(b'Actual Budget' in rv.data)
rv = self.app.get('/admin/db/consumption/')
assert(rv.status_code == 200)
def test_admin_access_db_unauthorized(self):
rv = self.login('testuser', 'foobar')
assert(rv.status_code == 200)
assert(b'Actual Budget' in rv.data)
rv = self.app.get('/admin/db/consumption/')
assert(rv.status_code == 403)
def test_consumption_list(self):
consumption_list = self.testuser.consumption_list()
assert(len(consumption_list) == 2)
assert(consumption_list[0]['amount'] == -50)
assert(consumption_list[1]['amount'] == 1000)
def test_last_service(self):
assert(self.testuser.last_service is None)
if __name__ == '__main__':
unittest.main()