-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_server.py
179 lines (105 loc) · 4.53 KB
/
test_server.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
172
173
174
175
176
177
178
179
import unittest
from server import app
from model import db, connect_to_db, test_data
class TestServerUser(unittest.TestCase):
def setUp(self):
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'test_key'
self.client = app.test_client()
with self.client as c:
with c.session_transaction() as sess:
sess['user_id'] = 1
def test_logout(self):
result = self.client.get('/logout', follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('login', result.data)
def test_process_song(self):
data = {'melody': 'C4'}
result = self.client.post('/process_song', data=data)
self.assertEqual(result.status_code, 200)
class TestServerNoUser(unittest.TestCase):
def setUp(self):
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'test_key'
self.client = app.test_client()
def test_index_no_user(self):
result = self.client.get('/')
self.assertEqual(result.status_code, 200)
self.assertIn('Name it before you save it', result.data)
self.assertIn('login', result.data)
def test_profile_no_user(self):
result = self.client.get('/profile', follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertNotIn('Your saved songs', result.data)
self.assertIn('Name it before you save it', result.data)
def test_save_no_user(self):
result = self.client.get('/save')
self.assertEqual(result.status_code, 405)
class TestServerWithDB(unittest.TestCase):
def setUp(self):
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'test_key'
self.client = app.test_client()
connect_to_db(app, 'postgresql:///testdb')
db.create_all()
def tearDown(self):
db.session.close()
db.drop_all()
def test_signin(self):
data = {'username': 'my name', 'password': 'my pword'}
result = self.client.post('/signin.json', data=data, follow_redirects=True)
self.assertIn('Username or password incorrect', result.data)
def test_signup(self):
data = {'username': 'user', 'password': '1234'}
result = self.client.post('/signup.json', data=data)
self.assertEqual(result.status_code, 200)
class TestServerWithUserAndDB(unittest.TestCase):
def setUp(self):
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'test_key'
self.client = app.test_client()
with self.client as c:
with c.session_transaction() as sess:
sess['user_id'] = 1
connect_to_db(app, 'postgresql:///testdb')
db.create_all()
test_data()
def tearDown(self):
db.session.close()
db.drop_all()
def test_index_user(self):
result = self.client.get('/')
self.assertEqual(result.status_code, 200)
self.assertIn('Name it before you save it', result.data)
self.assertIn('logout', result.data)
def test_profile_user(self):
result = self.client.get('/profile', follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('Your saved songs', result.data)
def test_process_song(self):
data = {'melody': 'C4'}
result = self.client.post('/process_song', data=data)
self.assertEqual(result.status_code, 200)
def test_save_song(self):
sample_image_string = 'data:image/png;base64,iVBORw='
data = {'name': 'song', 'image': sample_image_string}
result = self.client.post('/save', data=data, follow_redirects=True)
self.assertEqual(result.status_code, 200)
def test_delete_song(self):
data = {'song_id': 'song1'}
result = self.client.post('/delete_song', data=data, follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertNotIn('static/music/song1.wav', result.data)
def test_signin(self):
data = {'username': 'username', 'password': 'password'}
result = self.client.post('/signin.json', data=data)
self.assertEqual(result.status_code, 200)
def test_signup(self):
data = {'username': 'username', 'password': 'password'}
result = self.client.post('/signup.json', data=data)
self.assertIn('message', result.data)
def test_delete_profile(self):
result = self.client.get('/delete_profile')
self.assertEqual(result.status_code, 200)
if __name__ == '__main__':
unittest.main()