This repository has been archived by the owner on Jun 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
executable file
·123 lines (97 loc) · 4.85 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
#! /usr/bin/env python3
import os
import opensplit
import unittest
import tempfile
TEST_USER1 = {"EMAIL_ADDR": "[email protected]", "NAME": "John Doe"}
TEST_USER2 = {"EMAIL_ADDR": "[email protected]", "NAME": "Mark Ham"}
TEST_USER3 = {"EMAIL_ADDR": "[email protected]", "NAME": "Luke Sky"}
GROUP_NAME = "Testgroup"
TEST_EXPENSE1 = {"DESCRIPTION": "Testexpense", "AMOUNT": 13.37, "PAID_BY": 1, "SPLIT_AMONGST": [1]}
TEST_EXPENSE2 = {"DESCRIPTION": "Testexpense", "AMOUNT": 42, "PAID_BY": 1, "SPLIT_AMONGST": [1, 2]}
TEST_EXPENSE3 = {"DESCRIPTION": "Testexpense", "AMOUNT": 12, "PAID_BY": 2, "SPLIT_AMONGST": [1, 2]}
TEST_EXPENSE4 = {"DESCRIPTION": "Testexpense", "AMOUNT": 23, "PAID_BY": 3, "SPLIT_AMONGST": [1, 2, 3]}
class OpenSplitTestCase(unittest.TestCase):
def setUp(self):
self.db_filehandle, self.db_filename = tempfile.mkstemp()
opensplit.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///{}'.format(
self.db_filename)
opensplit.app.testing = True
self.app = opensplit.app.test_client()
with opensplit.app.app_context():
opensplit.db.create_all()
def tearDown(self):
os.close(self.db_filehandle)
def create_user(self, user):
return self.app.post('/users', data=dict(
email=user["EMAIL_ADDR"],
name=user["NAME"]))
def test_create_user(self):
r = self.create_user(TEST_USER1)
self.assertEqual(r.status, "201 CREATED")
def test_user_login(self):
self.create_user(TEST_USER1)
rv = self.app.get('/login/{}'.format(TEST_USER1["EMAIL_ADDR"]))
self.assertEqual(rv.status, "200 OK")
# Generate our own login token because we won't receive emails
user = opensplit.db.session.query(opensplit.models.User).first()
login_token = user.generate_login_token()
rv = self.app.get('/session/{}'.format(login_token.decode()))
self.assertEqual(rv.status, "200 OK")
self.assertIsNotNone(rv.json.get("session_key"))
def login_user(self, user):
self.create_user(user)
# Generate our own login token because we won't receive emails
user = opensplit.db.session.query(opensplit.models.User).first()
login_token = user.generate_login_token()
rv = self.app.get('/session/{}'.format(login_token.decode()))
return rv.json.get("session_key")
def create_group(self, session_key):
r = self.app.post('/groups',
data={"name": GROUP_NAME},
headers={"Authorization": session_key})
self.assertEqual(r.status, "200 OK")
return opensplit.db.session.query(opensplit.models.Group).first().id
def join_group(self, group_token, session_key):
r = self.app.post('/groups/join/{}'.format(group_token),
headers={"Authorization": session_key})
self.assertEqual(r.status, "200 OK")
return r
def get_group_token(self, group_id, session_key):
r = self.app.get('/groups/{}/generateToken'.format(group_id),
headers={"Authorization": session_key})
self.assertEqual(r.status, "200 OK")
return r.json
def test_session_key(self):
session_key = self.login_user(TEST_USER1)
r = self.app.get('/users',
headers={"Authorization": session_key})
self.assertEqual(r.status, "200 OK")
self.assertEqual(r.json.get("name"), TEST_USER1["NAME"])
self.assertEqual(r.json.get("email"), TEST_USER1["EMAIL_ADDR"])
def create_expense(self, group_id, session_key, expense):
r = self.app.post('/groups/{}/transactions'.format(group_id),
data={"description": expense["DESCRIPTION"],
"amount": expense["AMOUNT"],
"group_id": group_id,
"paid_by": expense["PAID_BY"],
"split_amongst": expense["SPLIT_AMONGST"]},
headers={"Authorization": session_key})
self.assertEqual(r.status, "200 OK")
return r
def test_add_and_get_expense(self):
session_key = self.login_user(TEST_USER1)
group_id = self.create_group(session_key)
r = self.create_expense(group_id, session_key, TEST_EXPENSE1)
self.assertEqual(r.status, "200 OK")
r = self.app.get('/groups/{}/transactions'.format(group_id),
headers={"Authorization": session_key})
data = r.json
testexpense = data[0]
self.assertEqual(len(data), 1)
self.assertEqual(testexpense["amount"], TEST_EXPENSE1["AMOUNT"])
self.assertEqual(testexpense["description"], TEST_EXPENSE1["DESCRIPTION"])
self.assertEqual(testexpense["group_id"], group_id)
self.assertEqual(len(testexpense["split_amongst"]), 1)
if __name__ == '__main__':
unittest.main()