-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities_test.go
More file actions
578 lines (551 loc) · 22.9 KB
/
utilities_test.go
File metadata and controls
578 lines (551 loc) · 22.9 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package main
import (
"fmt"
"io/ioutil"
"net/http/httptest"
"testing"
"github.com/jinzhu/copier"
)
func TestEncrypt(t *testing.T) {
// <setup code>
// <test-code>
t.Run(fmt.Sprintf("Test correct encryption"), func(t *testing.T) {
have, _ := Encrypt("r")
want := "4dc7c9ec434ed06502767136789763ec11d2c4b7"
if have != want {
t.Error("Failed correct encryption")
}
})
t.Run(fmt.Sprintf("Test encrypting empty string"), func(t *testing.T) {
_, err := Encrypt("")
if err.Error() != "Empty string" {
t.Error("TestEncrypt: empty string")
}
})
// <tear-down code>
}
func TestCheckRelation(t *testing.T) {
// <setup code>
Db.Exec(
`INSERT INTO users (wallet, username, privacy, createdat, updatedat) VALUES
('0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A', 'usera', 'all', current_timestamp, current_timestamp),
('0x29D7d1dd5B6f9C864d9db560D72a247c178aE86B', 'userb', 'all', current_timestamp, current_timestamp),
('0x29D7d1dd5B6f9C864d9db560D72a247c178aE86C', 'userd', 'all', current_timestamp, current_timestamp);`)
Db.Exec(
`INSERT INTO followers (followfrom, followto, createdat) VALUES
('0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A', '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86B', current_timestamp);`)
Db.Exec(
`INSERT INTO subscribers (subscribefrom, subscribeto, createdat) VALUES
('0x29D7d1dd5B6f9C864d9db560D72a247c178aE86B', '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86C', current_timestamp);`)
// <test code>
t.Run(fmt.Sprintf("Test user not follow when userid is null"), func(t *testing.T) {
observed, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86B")
observer := User{Wallet: ""}
user_connection := Connection{
Observer: observer,
Observed: observed,
}
user_connection.CheckConnection()
if user_connection.IsFollower || user_connection.IsSubscriber {
t.Fatal("Failed user not follow when userid is null")
}
})
t.Run(fmt.Sprintf("Test usera follows userb"), func(t *testing.T) {
observed, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86B")
observer := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A"}
user_connection := Connection{
Observer: observer,
Observed: observed,
}
user_connection.CheckConnection()
if !user_connection.IsFollower {
t.Fatal("Failed test usera follows userb")
}
})
t.Run(fmt.Sprintf("Test userc does not follows userb"), func(t *testing.T) {
observed, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86B")
observer := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86C"}
user_connection := Connection{
Observer: observer,
Observed: observed,
}
user_connection.CheckConnection()
if user_connection.IsFollower {
t.Fatal("Failed test userc does not follows userb")
}
})
t.Run(fmt.Sprintf("Test userb is subscribed to userc"), func(t *testing.T) {
observed, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86C")
observer := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86B"}
user_connection := Connection{
Observer: observer,
Observed: observed,
}
user_connection.CheckConnection()
if !user_connection.IsSubscriber {
t.Fatal("Failed test userb is subscibed to userc")
}
})
t.Run(fmt.Sprintf("Test usera is not subscribed to userc"), func(t *testing.T) {
observed, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86C")
observer := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A"}
user_connection := Connection{
Observer: observer,
Observed: observed,
}
user_connection.CheckConnection()
if user_connection.IsSubscriber {
t.Fatal("Failed test usera is not subscribed to userc")
}
})
// <tear-down code>
Db.Exec(`DELETE FROM users WHERE 1 = 1;`)
}
func TestCheckPrivacy(t *testing.T) {
// <setup code>
Db.Exec(
`INSERT INTO users (wallet, username, privacy, createdat, updatedat) VALUES
('0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A', 'usera', 'all', current_timestamp, current_timestamp),
('0x29D7d1dd5B6f9C864d9db560D72a247c178aE86B', 'userb', 'private', current_timestamp, current_timestamp),
('0x29D7d1dd5B6f9C864d9db560D72a247c178aE86C', 'userc', 'followers', current_timestamp, current_timestamp),
('0x29D7d1dd5B6f9C864d9db560D72a247c178aE86D', 'userd', 'subscribers', current_timestamp, current_timestamp);`)
// <test code>
t.Run(fmt.Sprintf("Test user with privacy ALL is fully visibile"), func(t *testing.T) {
observed, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A")
observer := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86B"}
user_connection := Connection{
Observer: observer,
Observed: observed,
}
user_connection.CheckConnection()
user_connection.CheckPrivacy()
if user_connection.Privacy.Status != "OK" {
t.Fatal("Failed test user with privacy ALL is fully visibile")
}
})
t.Run(fmt.Sprintf("Test user not authenticated try to access not ALL users"), func(t *testing.T) {
observed, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86B")
observer := User{Wallet: ""}
user_connection := Connection{
Observer: observer,
Observed: observed,
}
user_connection.CheckConnection()
user_connection.CheckPrivacy()
if user_connection.Privacy.Status != "KO" {
t.Fatal("Failed user not authenticated try to access not ALL users")
}
})
t.Run(fmt.Sprintf("Test user PRIVATE always able to see its profile if authenticated"), func(t *testing.T) {
observed, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86B")
observer := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86B"}
user_connection := Connection{
Observer: observer,
Observed: observed,
}
user_connection.CheckConnection()
user_connection.CheckPrivacy()
if user_connection.Privacy.Status != "OK" {
t.Fatal("Failed user PRIVATE always able to see its profile if authenticated")
}
})
t.Run(fmt.Sprintf("Test user FOLLOWERS always able to see its profile if authenticated"), func(t *testing.T) {
observed, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86C")
observer := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86C"}
user_connection := Connection{
Observer: observer,
Observed: observed,
}
user_connection.CheckConnection()
user_connection.CheckPrivacy()
if user_connection.Privacy.Status != "OK" {
t.Fatal("Failed user FOLLOWERS always able to see its profile if authenticated")
}
})
t.Run(fmt.Sprintf("Test user SUBSCRIBERS always able to see its profile if authenticated"), func(t *testing.T) {
observed, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86D")
observer := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86D"}
user_connection := Connection{
Observer: observer,
Observed: observed,
}
user_connection.CheckConnection()
user_connection.CheckPrivacy()
if user_connection.Privacy.Status != "OK" {
t.Fatal("Failed user SUBSCRIBERS always able to see its profile if authenticated")
}
})
t.Run(fmt.Sprintf("Test user cannot access other user when PRIVATE"), func(t *testing.T) {
observed, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86B")
observer := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A"}
user_connection := Connection{
Observer: observer,
Observed: observed,
}
user_connection.CheckConnection()
user_connection.CheckPrivacy()
if user_connection.Privacy.Reason != "private" {
t.Fatal("Failed user cannot access other user when PRIVATE")
}
})
t.Run(fmt.Sprintf("Test user cannot access other user when FOLLOWERS and not following"), func(t *testing.T) {
observed, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86C")
observer := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A"}
user_connection := Connection{
Observer: observer,
Observed: observed,
}
user_connection.CheckConnection()
user_connection.CheckPrivacy()
if user_connection.Privacy.Reason != "user is not follower" {
t.Fatal("Failed user cannot access other user when FOLLOWERS and not following")
}
})
t.Run(fmt.Sprintf("Test user can access other user when FOLLOWERS and yes following"), func(t *testing.T) {
observed, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86C")
Db.Exec(`
INSERT INTO followers (followfrom, followto, createdat)
VALUES ('0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A', '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86C', current_timestamp);`)
observer := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A"}
user_connection := Connection{
Observer: observer,
Observed: observed,
}
user_connection.CheckConnection()
user_connection.CheckPrivacy()
if user_connection.Privacy.Status != "OK" {
t.Fatal("Failed user can access other user when FOLLOWERS and yes following")
}
})
t.Run(fmt.Sprintf("Test user cannot access other user when SUBSCRIBERS and not subscribers"), func(t *testing.T) {
observed, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86D")
observer := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A"}
user_connection := Connection{
Observer: observer,
Observed: observed,
}
user_connection.CheckConnection()
user_connection.CheckPrivacy()
if user_connection.Privacy.Reason != "user is not subscriber" {
t.Fatal("Failed user cannot access other user when SUBSCRIBERS and not subscribers")
}
})
t.Run(fmt.Sprintf("Test user can access other user when SUBSCRIBERS and yes subscriber"), func(t *testing.T) {
observed, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86D")
Db.Exec(`
INSERT INTO subscribers (subscribefrom, subscribeto, createdat)
VALUES ('0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A', '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86D', current_timestamp);`)
observer := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A"}
user_connection := Connection{
Observer: observer,
Observed: observed,
}
user_connection.CheckConnection()
user_connection.CheckPrivacy()
if user_connection.Privacy.Status != "OK" {
t.Fatal("Failed user can access other user when SUBSCRIBERS and yes subscriber")
}
})
t.Run(fmt.Sprintf("Test user with privacy not legit"), func(t *testing.T) {
observed, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A")
observer := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86B"}
observed.Privacy = "random"
user_connection := Connection{
Observer: observer,
Observed: observed,
}
user_connection.CheckConnection()
user_connection.CheckPrivacy()
if user_connection.Privacy.Reason != "unknown reason" {
t.Fatal("Failed user with privacy not legit")
}
})
// <tear-down code>
Db.Exec(`DELETE FROM users WHERE 1 = 1;`)
}
func TestCheckVisibility(t *testing.T) {
// <setup code>
Db.Exec(
`INSERT INTO users (wallet, username, privacy, createdat, updatedat) VALUES
('0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A', 'userd', 'all', current_timestamp, current_timestamp);`)
Db.Exec(
`INSERT INTO visibilities (wallet, totalcounttrades, totalportfolio,
totalreturn, totalroi, tradeqtyavailable, tradevalue, tradereturn,
traderoi, subtradesall, subtradereasons, subtradequantity, subtradeavgprice, subtradetotal)
VALUES (
'0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A', TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE ,TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);`)
Db.Exec(`
INSERT INTO coins (
coinid, name, symbol, slug)
VALUES
(1, 'Bitcoin', 'BTC', 'Bitcoin'),
(1000, 'USDC', 'USDC', 'usdc');`)
Db.Exec(`
INSERT INTO lastprices (
updatedat, coinid, price)
VALUES
(current_timestamp, 1, 45000),
(current_timestamp, 1000, 1);`)
Db.Exec(`
INSERT INTO trades(
code, userwallet, createdat, updatedat,
firstpair, secondpair)
VALUES
('useratr1', '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A', current_timestamp, current_timestamp, 1000, 1);`)
Db.Exec(`
INSERT INTO subtrades(
code, userwallet, tradecode, createdat, updatedat,
type, quantity, avgprice, total, reason)
VALUES ('userasub1', '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A', 'useratr1', current_timestamp, current_timestamp, 'BUY', 1, 65000, 65000, 'TESTART');`)
user, _ := SelectUser("wallet", "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A")
initial_snapshot := user.GetSnapshot()
// <test code>
t.Run(fmt.Sprintf("Test totalcounttrades is equals to 0"), func(t *testing.T) {
Db.Exec(`
UPDATE visibilities
SET totalcounttrades = FALSE
WHERE wallet = '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A';`)
temp_snapshot := TradesSnapshot{}
copier.CopyWithOption(&temp_snapshot, &initial_snapshot, copier.Option{IgnoreEmpty: true, DeepCopy: true})
user.CheckVisibility(&temp_snapshot)
if initial_snapshot.CountTrades-temp_snapshot.CountTrades != 1 {
t.Fatal("Failed totalcounttrades is equals to 0")
}
})
t.Run(fmt.Sprintf("Test totalportfolio is equals to 0"), func(t *testing.T) {
Db.Exec(`
UPDATE visibilities
SET totalportfolio = FALSE
WHERE wallet = '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A';`)
temp_snapshot := TradesSnapshot{}
copier.CopyWithOption(&temp_snapshot, &initial_snapshot, copier.Option{IgnoreEmpty: true, DeepCopy: true})
user.CheckVisibility(&temp_snapshot)
if initial_snapshot.TotalPortfolioUsd != "45,000" || temp_snapshot.TotalPortfolioUsd != "0" {
t.Fatal("Failed totalportfolio is equals to 0")
}
})
t.Run(fmt.Sprintf("Test totalreturn is equals to 0"), func(t *testing.T) {
Db.Exec(`
UPDATE visibilities
SET totalreturn = FALSE
WHERE wallet = '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A';`)
temp_snapshot := TradesSnapshot{}
copier.CopyWithOption(&temp_snapshot, &initial_snapshot, copier.Option{IgnoreEmpty: true, DeepCopy: true})
user.CheckVisibility(&temp_snapshot)
if initial_snapshot.TotalReturnUsd != "-20,000" || temp_snapshot.TotalReturnUsd != "0" {
t.Fatal("Failed totalreturn is equals to 0")
}
if initial_snapshot.TotalReturnBtc != "-0.44" || temp_snapshot.TotalReturnBtc != "0" {
t.Fatal("Failed totalreturn is equals to 0")
}
})
t.Run(fmt.Sprintf("Test totalroi is equals to 0"), func(t *testing.T) {
Db.Exec(`
UPDATE visibilities
SET totalroi = FALSE
WHERE wallet = '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A';`)
temp_snapshot := TradesSnapshot{}
copier.CopyWithOption(&temp_snapshot, &initial_snapshot, copier.Option{IgnoreEmpty: true, DeepCopy: true})
user.CheckVisibility(&temp_snapshot)
if initial_snapshot.Roi != -30.77 || temp_snapshot.Roi != 0 {
t.Fatal("Failed totalreturn is equals to 0")
}
})
t.Run(fmt.Sprintf("Test tradeqtyavailable is equals to 0"), func(t *testing.T) {
Db.Exec(`
UPDATE visibilities
SET tradeqtyavailable = FALSE
WHERE wallet = '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A';`)
temp_snapshot := TradesSnapshot{}
copier.CopyWithOption(&temp_snapshot, &initial_snapshot, copier.Option{IgnoreEmpty: true, DeepCopy: true})
user.CheckVisibility(&temp_snapshot)
if initial_snapshot.Trades[0].QtyAvailable != "1" && temp_snapshot.Trades[0].QtyAvailable != "0" {
t.Fatal("Failed tradeqtyavailable is equals to 0")
}
})
t.Run(fmt.Sprintf("Test tradevalue is equals to 0"), func(t *testing.T) {
Db.Exec(`
UPDATE visibilities
SET tradevalue = FALSE
WHERE wallet = '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A';`)
temp_snapshot := TradesSnapshot{}
copier.CopyWithOption(&temp_snapshot, &initial_snapshot, copier.Option{IgnoreEmpty: true, DeepCopy: true})
user.CheckVisibility(&temp_snapshot)
if initial_snapshot.Trades[0].TotalValueUsd != 45000 || temp_snapshot.Trades[0].TotalValueUsd != 0 {
t.Fatal("Failed tradevalue is equals to 0")
}
if initial_snapshot.Trades[0].TotalValueUsdS != "45,000" || temp_snapshot.Trades[0].TotalValueUsdS != "0" {
t.Fatal("Failed tradevalue is equals to 0")
}
})
t.Run(fmt.Sprintf("Test tradereturn is equals to 0"), func(t *testing.T) {
Db.Exec(`
UPDATE visibilities
SET tradereturn = FALSE
WHERE wallet = '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A';`)
temp_snapshot := TradesSnapshot{}
copier.CopyWithOption(&temp_snapshot, &initial_snapshot, copier.Option{IgnoreEmpty: true, DeepCopy: true})
user.CheckVisibility(&temp_snapshot)
if initial_snapshot.Trades[0].TotalReturnUsd != 20000 && temp_snapshot.Trades[0].TotalReturnUsd != 0 {
t.Fatal("Failed tradereturn is equals to 0")
}
if initial_snapshot.Trades[0].TotalReturnBtc != -0.4444444444444444 || temp_snapshot.Trades[0].TotalReturnBtc != 0 {
t.Fatal("Failed tradereturn is equals to 0")
}
})
t.Run(fmt.Sprintf("Test traderoi is equals to 0"), func(t *testing.T) {
Db.Exec(`
UPDATE visibilities
SET traderoi = FALSE
WHERE wallet = '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A';`)
temp_snapshot := TradesSnapshot{}
copier.CopyWithOption(&temp_snapshot, &initial_snapshot, copier.Option{IgnoreEmpty: true, DeepCopy: true})
user.CheckVisibility(&temp_snapshot)
if initial_snapshot.Trades[0].Roi != -30.8 || temp_snapshot.Trades[0].Roi != 0 {
t.Fatal("Failed traderoi is equals to 0")
}
})
t.Run(fmt.Sprintf("Test subtradereasons is equals to null"), func(t *testing.T) {
Db.Exec(`
UPDATE visibilities
SET subtradereasons = FALSE
WHERE wallet = '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A';`)
temp_snapshot := TradesSnapshot{}
copier.CopyWithOption(&temp_snapshot, &initial_snapshot, copier.Option{IgnoreEmpty: true, DeepCopy: true})
user.CheckVisibility(&temp_snapshot)
if initial_snapshot.Trades[0].Subtrades[0].Reason != "TESTART" || temp_snapshot.Trades[0].Subtrades[0].Reason != "" {
t.Fatal("Failed subtradereasons is equals to null")
}
})
t.Run(fmt.Sprintf("Test subtradequantity is equals to 0"), func(t *testing.T) {
Db.Exec(`
UPDATE visibilities
SET subtradequantity = FALSE
WHERE wallet = '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A';`)
temp_snapshot := TradesSnapshot{}
copier.CopyWithOption(&temp_snapshot, &initial_snapshot, copier.Option{IgnoreEmpty: true, DeepCopy: true})
user.CheckVisibility(&temp_snapshot)
if initial_snapshot.Trades[0].Subtrades[0].Quantity != 1 || temp_snapshot.Trades[0].Subtrades[0].Quantity != 0 {
t.Fatal("Failed subtradereasons is equals to 0")
}
})
t.Run(fmt.Sprintf("Test subtradeavgprice is equals to 0"), func(t *testing.T) {
Db.Exec(`
UPDATE visibilities
SET subtradeavgprice = FALSE
WHERE wallet = '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A';`)
temp_snapshot := TradesSnapshot{}
copier.CopyWithOption(&temp_snapshot, &initial_snapshot, copier.Option{IgnoreEmpty: true, DeepCopy: true})
user.CheckVisibility(&temp_snapshot)
if initial_snapshot.Trades[0].Subtrades[0].AvgPrice != 65000 || temp_snapshot.Trades[0].Subtrades[0].AvgPrice != 0 {
t.Fatal("Failed subtradeavgprice is equals to 0")
}
})
t.Run(fmt.Sprintf("Test subtradetotal is equals to 0"), func(t *testing.T) {
Db.Exec(`
UPDATE visibilities
SET subtradetotal = FALSE
WHERE wallet = '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A';`)
temp_snapshot := TradesSnapshot{}
copier.CopyWithOption(&temp_snapshot, &initial_snapshot, copier.Option{IgnoreEmpty: true, DeepCopy: true})
user.CheckVisibility(&temp_snapshot)
if initial_snapshot.Trades[0].Subtrades[0].Total != 65000 || temp_snapshot.Trades[0].Subtrades[0].Total != 0 {
t.Fatal("Failed subtradetotal is equals to 0")
}
})
t.Run(fmt.Sprintf("Test subtradesall is empty"), func(t *testing.T) {
Db.Exec(`
UPDATE visibilities
SET subtradesall = FALSE
WHERE wallet = '0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A';`)
temp_snapshot := TradesSnapshot{}
copier.CopyWithOption(&temp_snapshot, &initial_snapshot, copier.Option{IgnoreEmpty: true, DeepCopy: true})
user.CheckVisibility(&temp_snapshot)
if len(initial_snapshot.Trades[0].Subtrades) != 1 || len(temp_snapshot.Trades[0].Subtrades) != 0 {
t.Fatal("Failed subtradesall is empty")
}
})
// <tear-down code>
Db.Exec(`DELETE FROM users WHERE 1 = 1;`)
}
func TestGenerateApiToken(t *testing.T) {
// <setup code>
Db.Exec(
`INSERT INTO users (wallet, username, privacy, createdat, updatedat) VALUES
('0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A', 'userd', 'all', current_timestamp, current_timestamp);`)
Db.Exec(
`INSERT INTO visibilities (wallet, totalcounttrades, totalportfolio,
totalreturn, totalroi, tradeqtyavailable, tradevalue, tradereturn,
traderoi, subtradesall, subtradereasons, subtradequantity, subtradeavgprice, subtradetotal)
VALUES (
'0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A', TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE ,TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);`)
// <test code>
t.Run(fmt.Sprintf("Test wrong header"), func(t *testing.T) {
req := httptest.NewRequest("GET", "/generate_api_token", nil)
req.Header.Set("Authorization", "Bearer sessionId=")
w := httptest.NewRecorder()
GenerateApiToken(w, req)
res := w.Result()
if res.StatusCode != 401 {
t.Fatal("Failed test generate APi token, wrong header")
}
})
t.Run(fmt.Sprintf("Test wrong origin"), func(t *testing.T) {
user := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A"}
session, _ := user.InsertSession("api", "Europe|Berlin")
req := httptest.NewRequest("GET", "/generate_api_token", nil)
req.Header.Set("Authorization", "Bearer sessionId="+session.Code)
w := httptest.NewRecorder()
GenerateApiToken(w, req)
res := w.Result()
if res.StatusCode != 400 {
t.Fatal("Failed test generate APi token, wrong origin")
}
})
t.Run(fmt.Sprintf("Test deleting previous session"), func(t *testing.T) {
user := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A"}
session, _ := user.InsertSession("web", "Europe|Berlin")
_, _ = user.InsertSession("api", "Europe|Berlin")
_, _ = user.InsertSession("api", "Europe|Berlin")
req := httptest.NewRequest("GET", "/generate_api_token", nil)
req.Header.Set("Authorization", "Bearer sessionId="+session.Code)
w := httptest.NewRecorder()
GenerateApiToken(w, req)
var count_row int
_ = Db.QueryRow(`
SELECT
COUNT(*)
FROM sessions
WHERE userwallet = $1
AND origin = 'api';`, "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A").Scan(&count_row)
if count_row != 1 {
t.Fatal("Failed test generate APi token, deleting previous row")
}
})
t.Run(fmt.Sprintf("Succesfully create session"), func(t *testing.T) {
user := User{Wallet: "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A"}
session, _ := user.InsertSession("web", "Europe|Berlin")
_, _ = user.InsertSession("api", "Europe|Berlin")
_, _ = user.InsertSession("api", "Europe|Berlin")
req := httptest.NewRequest("GET", "/generate_api_token", nil)
req.Header.Set("Authorization", "Bearer sessionId="+session.Code)
w := httptest.NewRecorder()
GenerateApiToken(w, req)
session_received, _ := ioutil.ReadAll(w.Body)
session_received_s := string(session_received)
var session_code string
_ = Db.QueryRow(`
SELECT
code
FROM sessions
WHERE userwallet = $1
AND origin = 'api';`, "0x29D7d1dd5B6f9C864d9db560D72a247c178aE86A").Scan(&session_code)
if session_received_s != session_code {
t.Fatal("Failed test generate APi token, successfully create session")
}
})
// <tear-down code>
Db.Exec(`DELETE FROM users WHERE 1 = 1;`)
}