-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtest_all.py
More file actions
213 lines (183 loc) · 7.63 KB
/
Copy pathtest_all.py
File metadata and controls
213 lines (183 loc) · 7.63 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import os
TEST_DB="sqlite:///test_db"
os.environ['DATABASE_URL'] = TEST_DB # sets DB URL for this process
import hardwarecheckout
from hardwarecheckout.config import SECRET
import unittest
from jose import jws
import tempfile
import random
import string
from utils import *
from hardwarecheckout.models import db
from hardwarecheckout.models.user import User
from hardwarecheckout.models.inventory_entry import InventoryEntry
from hardwarecheckout.models.inventory_entry import ItemType
from hardwarecheckout.models.request import Request
from hardwarecheckout.models.request import RequestStatus
from hardwarecheckout.models.request_item import RequestItem
from flask import url_for, json
import pytest
@pytest.fixture
def app():
hardwarecheckout.app.config['TESTING'] = True
hardwarecheckout.app.config['DEBUG'] = False
app = hardwarecheckout.app.test_client()
with hardwarecheckout.app.app_context():
db.drop_all()
db.create_all()
db.app = hardwarecheckout.app
db.init_app(hardwarecheckout.app)
ctx = hardwarecheckout.app.test_request_context()
ctx.push()
yield app
ctx.pop()
db.session.remove()
db.drop_all()
@pytest.fixture
def user(app):
quill_id = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10))
token = jws.sign(quill_id, SECRET, algorithm='HS256')
user = User(quill_id, 'alyssap@hacker.org', False)
db.session.add(user)
db.session.commit()
app.set_cookie('localhost:5000', 'jwt', token)
return user
@pytest.fixture
def admin(app, user):
user.is_admin = True
db.session.commit()
return user
@pytest.fixture
def item(app):
item = InventoryEntry('Item', 'Wow lick my socks',
'http://test.co', 'Item', [], '', 3)
db.session.add(item)
db.session.commit()
return item
def test_home(app):
with captured_templates(hardwarecheckout.app) as templates:
rv = app.get('/', follow_redirects = True)
assert rv.status_code == 200
assert len(templates) == 1
template, context = templates[0]
assert template.name == 'pages/inventory.html'
assert len(context['lottery_items']) == 0
assert len(context['checkout_items']) == 0
assert len(context['free_items']) == 0
def test_quill_login(app):
with captured_templates(hardwarecheckout.app) as templates:
rv = quill_login(app, 'admin@example.com', 'party')
assert rv.status_code == 200
assert len(templates) == 1
template, context = templates[0]
assert template.name == 'pages/inventory.html'
with captured_templates(hardwarecheckout.app) as templates:
rv = quill_login(app, 'admin@example.com', 'prty')
assert rv.status_code == 200
assert len(templates) == 1
template, context = templates[0]
assert template.name == 'pages/login.html'
assert "That's not the right password." in context['error']
with captured_templates(hardwarecheckout.app) as templates:
rv = quill_login(app, 'admin@example.co', 'party')
assert rv.status_code == 200
assert len(templates) == 1
template, context = templates[0]
assert template.name == 'pages/login.html'
assert "We couldn't find you!" in context['error']
def test_request_item(app, user, item):
update_user(app)
rv = request_item(app, 0)
response = json.loads(rv.data)
assert response['success'] == False
rv = request_item(app, 1)
response = json.loads(rv.data)
assert response['success'] == True
def test_update_user(app, user):
rv = update_user(app)
response = json.loads(rv.data)
assert response['success'] == True
def test_add_delete_item(app, admin):
rv = add_item(app)
assert json.loads(rv.data)['success'] == True
rv = app.get('/inventory/delete/1')
# TODO: add back check
def test_view_request(app, admin):
with captured_templates(hardwarecheckout.app) as templates:
rv = app.get('/request', follow_redirects=True)
template, context = templates[0]
assert rv.status_code == 200
assert template.name == 'pages/admin.html'
assert len(context['submitted_requests']) == 0
def test_run_lottery(app, admin, item):
item.item_type = ItemType.LOTTERY
for _ in range(item.quantity + 3):
request_item = RequestItem(item, 1)
request = Request([request_item], admin.id, proposal='Test')
db.session.add(request_item)
db.session.add(request)
db.session.commit()
rv = app.post(url_for('run_lottery', id=item.id))
assert json.loads(rv.data)['success'] == True
assert RequestItem.query.filter_by(entry_id=item.id) \
.join(Request).filter_by(status=RequestStatus.APPROVED).count() == 3
def test_run_all_lotteries(app, admin):
for _ in range(3):
item = InventoryEntry('Item' + str(_), 'Wow lick my socks',
'http://test.co', 'Item', [], '', 3, item_type=ItemType.LOTTERY)
db.session.add(item)
for _ in range(item.quantity + 3):
request_item = RequestItem(item, 1)
request = Request([request_item], admin.id, proposal='Test')
db.session.add(request_item)
db.session.add(request)
db.session.commit()
rv = app.post(url_for('run_all_lotteries'))
assert json.loads(rv.data)['success'] == True
items = InventoryEntry.query.all()
for item in items:
assert RequestItem.query.filter_by(entry_id=item.id) \
.join(Request).filter_by(status=RequestStatus.APPROVED).count() == 3
def test_quantities_correct(app, admin):
def quantities_correct(expected_counts):
""" Checks that loading the index page produces expected quantities.
expected_counts - a list of tuples with form
(inventory_entry_name, count)
"""
with captured_templates(hardwarecheckout.app) as templates:
rv = app.get('/', follow_redirects=True)
counts = templates[0][1]["counts"]
for (name, num) in expected_counts:
id_ = InventoryEntry.query.filter_by(name=name).one().id
actual_quant = counts.get(id_, 0)
if actual_quant != num:
print("Unexpected quantity {} for item {}".format(actual_quant,
name))
return False
return True
# add item with 1, item with 0 and item with 5 to the database
item_0_quant = InventoryEntry('Item0', 'Wow lick my socks',
'http://test.co', 'Item', [], '', 0)
item_1_quant = InventoryEntry('Item1', 'Wow lick my socks',
'http://test.co', 'Item', [], '', 1)
item_5_quant = InventoryEntry('Item5', 'Wow lick my socks',
'http://test.co', 'Item', [], '', 5)
db.session.add(item_0_quant)
db.session.add(item_1_quant)
db.session.add(item_5_quant)
# Confirm on index page that quantities are correct
expected_initial_quantities = [('Item0', 0), ('Item1', 1), ('Item5', 5)]
assert quantities_correct(expected_initial_quantities)
# needed to request_items
update_user(app)
# request and approve each item
ids = [entry.id for entry in InventoryEntry.query.all()]
for id_ in ids:
request_item(app, id_)
ids = [req.id for req in Request.query.all()]
for id_ in ids:
approve_request(app, id_)
# confirm updated quantities correct
expected_post_quantities = [('Item0', 0), ('Item1', 0), ('Item5', 4)]
assert quantities_correct(expected_post_quantities)