-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
326 lines (266 loc) · 11 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# 소개원실 HW1 테스트케이스 - (c) 2019 이동민
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from datetime import datetime, timedelta, timezone
from django.test import TestCase
from django.contrib.auth import get_user_model
from rest_framework.fields import DateTimeField
from rest_framework.test import APITestCase
from rest_framework import status
# 간단한 테스트 케이스들입니다.
# 문제가 있으면 Github에 이슈를 남겨주세요.
class 여러가지테스트(APITestCase):
User = get_user_model()
base_time = datetime.now(timezone.utc).replace(
minute = 0, second = 0, microsecond = 0
)
ids = ('t1', 't2')
passwords = ('HomeworkTest1', 'HomeworkTest2')
def setUp(self):
self.users = (
self.User.objects.create_user(
username = self.ids[0],
password = self.passwords[0]
),
self.User.objects.create_user(
username = self.ids[1],
password = self.passwords[1]
)
)
# Helper functions
def client_login(self, no):
self.user = self.users[no]
return self.client.login(
username = self.ids[no],
password = self.passwords[no]
)
def get_time(self, t):
return self.base_time + timedelta(hours = t)
def check_time(self, iso, t):
# Compare equality of iso 8601 formatted string and datetime
self.assertEqual(DateTimeField().to_internal_value(iso), t)
def meeting(self, since, til):
# Assemble a meeting object
return {
'sinceWhen': self.get_time(since),
'tilWhen': self.get_time(til)
}
# post/get/put/delete helpers
def post_meeting(self, since, til):
return self.client.post('/meetings/', self.meeting(since, til))
def get_meeting(self, id):
return self.client.get("/meetings/{0}/".format(id))
def delete_meeting(self, id):
return self.client.delete("/meetings/{0}/".format(id))
def put_meeting(self, id, since, til):
return self.client.put("/meetings/{0}/".format(id),
self.meeting(since, til))
def check_meeting_success(self, response, since, til, user_id):
# Check whether post/put meeting succeeded
self.assertTrue(status.is_success(response.status_code))
# check response itself
self.check_time(response.data['sinceWhen'], self.get_time(since))
self.check_time(response.data['tilWhen'], self.get_time(til))
self.assertEqual(response.data['user'], user_id)
# check /meetings/id/
response = self.get_meeting(response.data['id'])
self.assertTrue(status.is_success(response.status_code))
self.check_time(response.data['sinceWhen'], self.get_time(since))
self.check_time(response.data['tilWhen'], self.get_time(til))
self.assertEqual(response.data['user'], user_id)
# check /meetings/
all = self.client.get('/meetings/')
self.assertTrue(status.is_success(response.status_code))
self.assertIn(response.data, all.data)
def check_meeting_failure(self, response):
# todo: better checking?
self.assertTrue(status.is_client_error(response.status_code))
def check_delete_success(self, response, id):
self.assertTrue(status.is_success(response.status_code))
response = self.get_meeting(id)
self.assertTrue(status.is_client_error(response.status_code))
response = self.client.get('/meetings/')
for meeting in response.data:
self.assertNotEqual(meeting['id'], id)
def check_delete_failure(self, response, id):
self.assertTrue(status.is_client_error(response.status_code))
# Shorthand functions
def post_expect_success(self, since, til):
prev = self.client.get('/meetings/').data
resp = self.post_meeting(since, til)
self.check_meeting_success(resp, since, til, self.user.id)
curr = self.client.get('/meetings/').data
for meeting in prev:
self.assertIn(meeting, curr)
return resp
def post_expect_failure(self, since, til):
all = self.client.get('/meetings/')
resp = self.post_meeting(since, til)
self.check_meeting_failure(resp)
self.assertEqual(all.data, self.client.get('/meetings/').data)
return resp
def put_expect_success(self, id, since, til):
prev = self.client.get('/meetings/').data
resp = self.put_meeting(id, since, til)
self.check_meeting_success(resp, since, til, self.user.id)
self.assertEqual(resp.data['id'], id)
curr = self.client.get('/meetings/').data
for meeting in prev:
if meeting['id'] != id:
self.assertIn(meeting, curr)
return resp
def put_expect_failure(self, id, since, til):
all = self.client.get('/meetings/')
resp = self.put_meeting(id, since, til)
self.check_meeting_failure(resp)
self.assertEqual(all.data, self.client.get('/meetings/').data)
return resp
def delete_expect_success(self, id):
prev = self.client.get('/meetings/').data
resp = self.delete_meeting(id)
self.check_delete_success(resp, id)
curr = self.client.get('/meetings/').data
for meeting in prev:
if meeting['id'] != id:
self.assertIn(meeting, curr)
return resp
def delete_expect_failure(self, id):
all = self.client.get('/meetings/')
resp = self.delete_meeting(id)
self.check_delete_failure(resp, id)
self.assertEqual(all.data, self.client.get('/meetings/').data)
return resp
def test_empty(self):
response = self.client.get('/meetings/')
self.assertEqual(response.data, [])
def test_post_auth(self):
response = self.client.post('/meetings/', self.meeting(1, 2))
self.assertTrue(status.is_client_error(response.status_code))
def test_post_then_delete(self):
login = self.client_login(0)
response = self.post_expect_success(1, 2)
self.delete_expect_success(response.data['id'])
self.client.logout()
self.test_empty()
def test_overlap(self):
# 겹치는 시간 처리
login = self.client_login(0)
response1 = self.post_expect_success(6, 8)
# overlap
self.post_expect_failure(5, 7)
self.post_expect_failure(7, 9)
self.post_expect_failure(5, 9)
# wrong order
self.post_expect_failure(2, 1)
self.post_expect_failure(1, 1)
response2 = self.post_expect_success(4, 6)
response3 = self.post_expect_success(8, 9)
response4 = self.post_expect_success(14, 16)
self.delete_expect_success(response1.data['id'])
response5 = self.post_expect_success(6, 8)
# cleanup
self.delete_expect_success(response2.data['id'])
self.delete_expect_success(response3.data['id'])
self.delete_expect_success(response4.data['id'])
self.delete_expect_success(response5.data['id'])
self.client.logout()
self.test_empty()
def test_detail_auth(self):
# 다른 유저의 예약 권한
login = self.client_login(0)
post_id = self.post_expect_success(4, 7).data['id']
self.client.logout()
login = self.client_login(1)
# cannot put/delete/get from other users
self.put_expect_failure(post_id, 5, 8)
self.delete_expect_failure(post_id)
get = self.get_meeting(post_id)
self.assertTrue(status.is_client_error(get.status_code))
self.client.logout()
# login as original user to cleanup
login = self.client_login(0)
self.delete_expect_success(post_id)
self.client.logout()
self.test_empty()
def test_put_overlap(self):
# 회의 수정 시 겹침
login = self.client_login(0)
resp1 = self.post_expect_success(1, 2)
resp2 = self.post_expect_success(5, 6)
resp3 = self.post_expect_success(8, 9)
id = resp2.data['id']
# overlap
self.put_expect_failure(id, 1, 3)
self.put_expect_failure(id, 7, 10)
# wrong order
self.put_expect_failure(id, 6, 5)
self.put_expect_failure(id, 6, 6)
self.put_expect_success(id, 3, 8)
self.put_expect_success(id, 2, 7)
self.put_expect_success(id, 5, 6)
# cleanup
self.delete_expect_success(id)
self.delete_expect_success(resp1.data['id'])
self.delete_expect_success(resp3.data['id'])
self.client.logout()
self.test_empty()
def test_users(self):
resp = [[], []]
login = self.client_login(0)
to_delete = self.post_expect_success(10, 11).data['id']
resp[0] = [
self.post_expect_success(1, 2),
self.post_expect_success(3, 4),
self.post_expect_success(5, 6)
]
self.delete_expect_success(to_delete)
self.client.logout()
login = self.client_login(1)
to_delete = self.post_expect_success(10, 11).data['id']
resp[1] = [
self.post_expect_success(2, 3),
self.post_expect_success(4, 5),
self.post_expect_success(6, 7)
]
self.delete_expect_success(to_delete)
self.client.logout()
# Test /users/ - account for possible order differences
all = self.client.get("/users/")
self.assertEqual(len(all.data), 2)
for userdat in all.data:
for i in [0, 1]:
if (userdat['id'] == self.users[i].id):
self.assertEqual(
userdat['username'],
self.users[i].username
)
self.assertSequenceEqual(
userdat['meetings'],
[x.data['id'] for x in resp[i]]
)
# Test /users/id/
# No need to be precise, most of the checking has already been done
for i in [1, 2]:
self.assertIn(
self.client.get("/users/{0}/".format(i)).data,
all.data
)
# Cleanup
for i in [0, 1]:
login = self.client_login(i)
for r in resp[i]:
self.delete_expect_success(r.data['id'])
self.client.logout()
self.test_empty()
return