-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.rb
333 lines (268 loc) · 12.3 KB
/
test.rb
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
327
328
329
330
331
332
333
ENV['RACK_ENV'] = 'test'
require 'routes'
require 'test/unit'
require 'rack/test'
require "base64"
class RoutesTest < Test::Unit::TestCase
include Rack::Test::Methods
def app
Sinatra::Application
end
def test_without_authentication
get '/accounts/User_133'
assert_equal 401, last_response.status
end
def test_with_bad_credentials
get '/accounts/User_145', {}, {'HTTP_AUTHORIZATION' => encode_credentials('go', 'away')}
assert_equal 401, last_response.status
end
def test_with_proper_credentials
get '/accounts/User_133', {}, cred(133)
assert_equal 200, last_response.status
end
def test_acct_creation
i = rand(7000)+1000
uid = "User_#{i}"
post '/accounts', {:user => uid, :secret => "pw"}, {}
assert_equal 200, last_response.status
assert_equal uid, JSON.parse(last_response.body)['user']
assert_equal 8, JSON.parse(last_response.body)['depth']
get "/accounts/#{uid}", {}, cred(i)
assert_equal 200, last_response.status
assert_equal uid, JSON.parse(last_response.body)['user']
assert_equal 8, JSON.parse(last_response.body)['depth']
end
def test_acct_noncreation
post '/accounts', {:user => "User_200", :secret => "pw"}, {}
assert_equal 403, last_response.status
end
def test_acct_creation_depth
i = rand(7000)+1000
uid = "User_#{i}"
get "/accounts/#{uid}", {}, cred(i)
assert_equal 401, last_response.status
post '/accounts', {:user => uid, :secret => "pw", :depth => 5}, {}
assert_equal 200, last_response.status
assert_equal uid, JSON.parse(last_response.body)['user']
assert_equal 5, JSON.parse(last_response.body)['depth']
get "/accounts/#{uid}", {}, cred(i)
assert_equal 200, last_response.status
assert_equal uid, JSON.parse(last_response.body)['user']
assert_equal 5, JSON.parse(last_response.body)['depth']
#Could also test that correct URLs are returned.
end
def test_credit_extension_parsefailure_nonnumeric
post '/credits/User_133', {:to => "User_134", :amount => "dinosaur"}, cred(133)
assert_equal 400, last_response.status
end
def test_credit_extension_parsefailure_tomissing
post '/credits/User_133', {:user_id => "User_134", :amount => "4.5"}, cred(133)
assert_equal 400, last_response.status
end
def test_credit_extension_parsefailure_amountmissing
post '/credits/User_133', {:user_id => "User_134"}, cred(133)
assert_equal 400, last_response.status
end
def test_credit_ext_parse_nonnegative
post '/credits/User_383/', {:to => "User_326", :amount => "-1.5"}, cred(383)
assert_equal 400, last_response.status
end
def test_credit_ext_parse_positive
post '/credits/User_383/', {:to => "User_326", :amount => "0"}, cred(383)
assert_equal 400, last_response.status
end
def test_credit_extension_auth_failure
post '/credits/User_133', {:to => "User_134", :amount => 5.4}, cred(167)
assert_equal 401, last_response.status
end
def test_credit_extension
amt = rand(23) + 0.1
post '/credits/User_133', {:to => "User_134", :amount => amt}, cred(133)
assert_equal 200, last_response.status
assert_equal amt, JSON.parse(last_response.body)['credit_offered']
end
def test_credit_receipt_parsefailure
post '/credits/User_133', {:to => "User_134", :amount => "dinosaur"}, cred(134)
assert_equal 400, last_response.status
end
def test_credit_receipt
amt = rand(23) + 0.1
post '/credits/User_133', {:to => "User_134", :amount => amt}, cred(134)
assert_equal 200, last_response.status
assert_equal amt, JSON.parse(last_response.body)['credit_accepted']
end
def test_payment_auth_failure
post '/transactions/User_433', {:to => "User_446", :amount => 4.0}, cred(446)
assert_equal 401, last_response.status
end
def test_payment_auth_failure2
post '/transactions/User_433', {:to => "User_446", :amount => 4.0}, bad_cred()
assert_equal 401, last_response.status
end
def test_payment_parsefailure_nonnumeric
post '/transactions/User_433', {:to => "User_446", :amount => "dinosaur"}, cred(433)
assert_equal 400, last_response.status
end
def test_payment_parsefailure_tomissing
post '/transactions/User_433', {:user_id => "User_446", :amount => "4.5"}, cred(433)
assert_equal 400, last_response.status
end
def test_payment_parsefailure_amountmissing
post '/transactions/User_433', {:user_id => "User_446"}, cred(433)
assert_equal 400, last_response.status
end
def test_payment_parse_nonnegative
post '/transactions/User_67', {:to => "User_326", :amount => "-1.5"}, cred(67)
assert_equal 400, last_response.status
end
def test_payment_parse_positive
post '/transactions/User_34', {:to => "User_326", :amount => "0"}, cred(34)
assert_equal 400, last_response.status
end
def test_hold_auth_failure
post '/transactions/User_233/held', {:to => "User_246", :amount => 1.0}, bad_cred()
assert_equal 401, last_response.status
end
def test_hold_success
end
def test_hold_success2
end
def test_hold_parse_nonnumeric
post '/transactions/User_183/held', {:to => "User_326", :amount => "dinosaur"}, cred(183)
assert_equal 400, last_response.status
end
def test_hold_parse_tomissing
post '/transactions/User_183/held', {:amount => "3.5"}, cred(183)
assert_equal 400, last_response.status
end
def test_hold_parse_amountmissing
post '/transactions/User_183/held', {:to => "User_326"}, cred(183)
assert_equal 400, last_response.status
end
def test_hold_parse_nonnegative
post '/transactions/User_183/held', {:to => "User_326", :amount => "-1.5"}, cred(183)
assert_equal 400, last_response.status
end
def test_hold_parse_positive
post '/transactions/User_183/held', {:to => "User_326", :amount => "0"}, cred(183)
assert_equal 400, last_response.status
end
def test_comprehensive
src_id = rand(100000) + 10000
dest_id = src_id + 1
#Create accounts
post '/accounts', {:user => "User_#{src_id}", :secret => "pw"}, {}
assert_equal 200, last_response.status
post '/accounts', {:user => "User_#{dest_id}", :secret => "pw"}, {}
assert_equal 200, last_response.status
#Create unaccepted credit.
amt = 10.0
post "/credits/User_#{src_id}", {:to => "User_#{dest_id}", :amount => amt}, cred(src_id)
assert_equal 200, last_response.status
assert_equal amt, JSON.parse(last_response.body)['credit_offered']
get "/credits/User_#{src_id}", {:to => "User_#{dest_id}"}, cred(src_id)
assert_equal 200, last_response.status
assert_equal amt, JSON.parse(last_response.body)['credit_offered']
get "/credits/User_#{src_id}", {:to => "User_#{dest_id}"}, cred(dest_id)
assert_equal 200, last_response.status
assert_equal amt, JSON.parse(last_response.body)['credit_offered']
#Verify inability to for src to grant a credit of 5.0 to dest.
amt = 5.0
post "/transactions/User_#{src_id}", {:to => "User_#{dest_id}", :amount => amt}, cred(src_id)
assert_equal 403, last_response.status
#Accept partial credit.
amt = 8.0
post "/credits/User_#{src_id}", {:to => "User_#{dest_id}", :amount => amt}, cred(dest_id)
assert_equal 200, last_response.status
assert_equal amt, JSON.parse(last_response.body)['credit_accepted']
get "/credits/User_#{src_id}", {:to => "User_#{dest_id}"}, cred(src_id)
assert_equal 200, last_response.status
assert_equal amt, JSON.parse(last_response.body)['credit_accepted']
get "/credits/User_#{src_id}", {:to => "User_#{dest_id}"}, cred(dest_id)
assert_equal 200, last_response.status
assert_equal amt, JSON.parse(last_response.body)['credit_accepted']
get "/credits/User_#{src_id}", {:to => "User_#{dest_id}"}, cred(79)
assert_equal 401, last_response.status
#Use part of the credit line.
amt = 4.0
post "/transactions/User_#{dest_id}", {:to => "User_#{src_id}", :amount => amt}, cred(dest_id)
assert_equal 200, last_response.status
#puts last_response.body
assert_equal 4.0, JSON.parse(last_response.body)['max_credit_line']
assert_equal 4.0, JSON.parse(last_response.body)['max_debit_line']
assert_equal 0.0, JSON.parse(last_response.body)['debit_held']
assert_equal 0.0, JSON.parse(last_response.body)['credit_held']
#Check balances for each user.
get "/accounts/User_#{src_id}", {}, cred(src_id)
assert_equal 200, last_response.status
assert_equal 4.0, JSON.parse(last_response.body)['balance']
get "/accounts/User_#{dest_id}", {}, cred(dest_id)
assert_equal 200, last_response.status
assert_equal -4.0, JSON.parse(last_response.body)['balance']
#Accept full credit.
amt = 10.0
post "/credits/User_#{src_id}", {:to => "User_#{dest_id}", :amount => amt}, cred(dest_id)
assert_equal 200, last_response.status
assert_equal amt, JSON.parse(last_response.body)['credit_accepted']
assert_equal 6.0, JSON.parse(last_response.body)['max_credit_line']
assert_equal 0.0, JSON.parse(last_response.body)['debit_held']
assert_equal 0.0, JSON.parse(last_response.body)['credit_held']
get "/credits/User_#{src_id}", {:to => "User_#{dest_id}"}, cred(dest_id)
assert_equal 200, last_response.status
assert_equal amt, JSON.parse(last_response.body)['credit_accepted']
assert_equal 6.0, JSON.parse(last_response.body)['max_credit_line']
assert_equal 0.0, JSON.parse(last_response.body)['debit_held']
assert_equal 0.0, JSON.parse(last_response.body)['credit_held']
get "/credits/User_#{src_id}", {:to => "User_#{dest_id}"}, cred(src_id)
assert_equal 200, last_response.status
assert_equal amt, JSON.parse(last_response.body)['credit_accepted']
assert_equal 6.0, JSON.parse(last_response.body)['max_credit_line']
assert_equal 0.0, JSON.parse(last_response.body)['debit_held']
assert_equal 0.0, JSON.parse(last_response.body)['credit_held']
#Give back that 4.0 of credit.
amt = 4.0
post "/transactions/User_#{src_id}", {:to => "User_#{dest_id}", :amount => amt}, cred(src_id)
assert_equal 200, last_response.status
assert_equal 10.0, JSON.parse(last_response.body)['max_debit_line']
assert_equal 0.0, JSON.parse(last_response.body)['max_credit_line']
assert_equal 0.0, JSON.parse(last_response.body)['debit_held']
assert_equal 0.0, JSON.parse(last_response.body)['credit_held']
get "/credits/User_#{dest_id}", {:to => "User_#{src_id}"}, cred(src_id)
assert_equal 200, last_response.status
assert_equal 10.0, JSON.parse(last_response.body)['max_debit_line']
assert_equal 0.0, JSON.parse(last_response.body)['max_credit_line']
assert_equal 0.0, JSON.parse(last_response.body)['debit_held']
assert_equal 0.0, JSON.parse(last_response.body)['credit_held']
get "/credits/User_#{dest_id}", {:to => "User_#{src_id}"}, cred(dest_id)
assert_equal 200, last_response.status
assert_equal 10.0, JSON.parse(last_response.body)['max_debit_line']
assert_equal 0.0, JSON.parse(last_response.body)['max_credit_line']
assert_equal 0.0, JSON.parse(last_response.body)['debit_held']
assert_equal 0.0, JSON.parse(last_response.body)['credit_held']
#Reserve some of that there credit.
amt = 3.0
post "/transactions/User_#{dest_id}/held", {:to => "User_#{src_id}", :amount => amt}, cred(dest_id)
assert_equal 200, last_response.status
puts last_response.body
#assert_equal 0.0, JSON.parse(last_response.body)['max_debit_line']
assert_equal 7.0, JSON.parse(last_response.body)['max_credit_line']
#assert_equal 0.0, JSON.parse(last_response.body)['debit_held']
#assert_equal 0.0, JSON.parse(last_response.body)['credit_held']
# Should fail
post "/transactions/User_#{dest_id}", {:to => "User_#{src_id}", :amount => 8.0}, cred(dest_id)
assert_equal 403, last_response.status
post "/transactions/User_#{dest_id}", {:to => "User_#{src_id}", :amount => 2.0}, cred(dest_id)
#puts last_response.body
#assert_equal 403, last_response.status
end
private
def encode_credentials(username, password)
"Basic " + Base64.encode64("#{username}:#{password}")
end
def cred(uid)
return {'HTTP_AUTHORIZATION'=> encode_credentials("User_#{uid}", 'pw')}
end
def bad_cred
{'HTTP_AUTHORIZATION'=> encode_credentials("go", 'away')}
end
end