-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdminControllerSpec.scala
943 lines (825 loc) · 40.1 KB
/
AdminControllerSpec.scala
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
package controllers
import com.dimafeng.testcontainers.PostgreSQLContainer
import controllers.common.PlayPostgresSpec
import net.wiringbits.spra.admin.AppRouter
import net.wiringbits.spra.admin.config.{DataExplorerConfig, PrimaryKeyDataType, TableSettings}
import net.wiringbits.spra.admin.controllers.{AdminController, ImagesController}
import net.wiringbits.spra.api.models.{AdminCreateTable, AdminUpdateTable}
import org.apache.commons.lang3.StringUtils
import play.api.inject
import play.api.inject.guice.GuiceApplicationBuilder
import utils.AdminUtils
import java.util.UUID
import java.util.regex.Pattern
import scala.util.Random
class AdminControllerSpec extends PlayPostgresSpec with AdminUtils {
val dataExplorerConfigTables: List[TableSettings] = List(
TableSettings("users", "user_id"), // "UUID" default
TableSettings("user_logs", "user_log_id", Some("users")), // "UUID" default
TableSettings(
tableName = "uuid_table",
primaryKeyField = "id",
primaryKeyDataType = PrimaryKeyDataType.UUID
), // explicit default
TableSettings(tableName = "serial_table", primaryKeyField = "id", primaryKeyDataType = PrimaryKeyDataType.Serial),
TableSettings(
tableName = "big_serial_table",
primaryKeyField = "id",
primaryKeyDataType = PrimaryKeyDataType.BigSerial
),
TableSettings(
tableName = "serial_table_overflow",
primaryKeyField = "id",
primaryKeyDataType = PrimaryKeyDataType.Serial
),
TableSettings(
tableName = "big_serial_table_overflow",
primaryKeyField = "id",
primaryKeyDataType = PrimaryKeyDataType.BigSerial
),
TableSettings(
tableName = "bytea_table",
primaryKeyField = "id"
)
)
val dataExplorerConfig: DataExplorerConfig = DataExplorerConfig("http://localhost:9000", dataExplorerConfigTables)
def usersSettings: TableSettings = dataExplorerConfig.tablesSettings.headOption.value
// TODO: loop through dataExplorerSettings for each table instead of defining usersSettings, uuidSettings
def userLogsSettings: TableSettings = dataExplorerConfig.tablesSettings(1)
def uuidSettings: TableSettings = dataExplorerConfig.tablesSettings(2)
def serialSettings: TableSettings = dataExplorerConfig.tablesSettings(3)
def bigSerialSettings: TableSettings = dataExplorerConfig.tablesSettings(4)
def serialOverflowSettings: TableSettings = dataExplorerConfig.tablesSettings(5)
def bigSerialOverflowSettings: TableSettings = dataExplorerConfig.tablesSettings(6)
def byteaSettings: TableSettings = dataExplorerConfig.tablesSettings(7)
def isValidUUID(str: String): Boolean = {
if (str == null) return false
Pattern.compile("^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$").matcher(str).matches()
}
override def guiceApplicationBuilder(container: PostgreSQLContainer): GuiceApplicationBuilder = {
val appBuilder = super
.guiceApplicationBuilder(container)
.overrides(inject.bind[DataExplorerConfig].to(dataExplorerConfig))
val adminController = appBuilder.injector().instanceOf[AdminController]
val imagesController = appBuilder.injector().instanceOf[ImagesController]
val appRouter = new AppRouter(adminController, imagesController)
appBuilder.router(appRouter)
}
"GET /admin/tables" should {
"return tables from modules" in withApiClient { client =>
val response = client.getTables.futureValue
response.data.map(_.name) match
case List(users, userLogs, uuidTable, serialTable, bigSerialTable, _, _, byteaTable) =>
users must be(usersSettings.tableName)
userLogs must be(userLogsSettings.tableName)
uuidTable must be(uuidSettings.tableName)
serialTable must be(serialSettings.tableName)
bigSerialTable must be(bigSerialSettings.tableName)
byteaTable must be(byteaSettings.tableName)
case list => fail(s"Unexpected response: ${list.mkString(", ")}")
}
"return extra config from module" in withApiClient { client =>
val response = client.getTables.futureValue
val head = response.data.headOption.value
head.primaryKeyName must be(usersSettings.primaryKeyField)
usersSettings.referenceField must be(None)
usersSettings.hiddenColumns must be(List.empty)
usersSettings.nonEditableColumns must be(List.empty)
usersSettings.canBeDeleted must be(true)
usersSettings.columnTypeOverrides must be(Map.empty)
usersSettings.filterableColumns must be(List.empty)
usersSettings.createSettings.nonRequiredColumns must be(List.empty)
usersSettings.createSettings.requiredColumns must be(List.empty)
val head2 = response.data(1)
head2.primaryKeyName must be(userLogsSettings.primaryKeyField)
userLogsSettings.referenceField must be(Some("users"))
userLogsSettings.hiddenColumns must be(List.empty)
userLogsSettings.nonEditableColumns must be(List.empty)
userLogsSettings.canBeDeleted must be(true)
userLogsSettings.columnTypeOverrides must be(Map.empty)
userLogsSettings.filterableColumns must be(List.empty)
userLogsSettings.createSettings.nonRequiredColumns must be(List.empty)
userLogsSettings.createSettings.requiredColumns must be(List.empty)
val head3 = response.data(2)
head3.primaryKeyName must be(uuidSettings.primaryKeyField)
uuidSettings.referenceField must be(None)
uuidSettings.hiddenColumns must be(List.empty)
uuidSettings.nonEditableColumns must be(List.empty)
uuidSettings.canBeDeleted must be(true)
uuidSettings.columnTypeOverrides must be(Map.empty)
uuidSettings.filterableColumns must be(List.empty)
uuidSettings.createSettings.nonRequiredColumns must be(List.empty)
uuidSettings.createSettings.requiredColumns must be(List.empty)
val head4 = response.data(3)
head4.primaryKeyName must be(serialSettings.primaryKeyField)
serialSettings.referenceField must be(None)
serialSettings.hiddenColumns must be(List.empty)
serialSettings.nonEditableColumns must be(List.empty)
serialSettings.canBeDeleted must be(true)
serialSettings.columnTypeOverrides must be(Map.empty)
serialSettings.filterableColumns must be(List.empty)
serialSettings.createSettings.nonRequiredColumns must be(List.empty)
serialSettings.createSettings.requiredColumns must be(List.empty)
val head5 = response.data(4)
head5.primaryKeyName must be(bigSerialSettings.primaryKeyField)
bigSerialSettings.referenceField must be(None)
bigSerialSettings.hiddenColumns must be(List.empty)
bigSerialSettings.nonEditableColumns must be(List.empty)
bigSerialSettings.canBeDeleted must be(true)
bigSerialSettings.columnTypeOverrides must be(Map.empty)
bigSerialSettings.filterableColumns must be(List.empty)
bigSerialSettings.createSettings.nonRequiredColumns must be(List.empty)
bigSerialSettings.createSettings.requiredColumns must be(List.empty)
val head6 = response.data(5)
head6.primaryKeyName must be(byteaSettings.primaryKeyField)
byteaSettings.referenceField must be(None)
byteaSettings.hiddenColumns must be(List.empty)
byteaSettings.nonEditableColumns must be(List.empty)
byteaSettings.canBeDeleted must be(true)
byteaSettings.columnTypeOverrides must be(Map.empty)
byteaSettings.filterableColumns must be(List.empty)
byteaSettings.createSettings.nonRequiredColumns must be(List.empty)
byteaSettings.createSettings.requiredColumns must be(List.empty)
}
}
"GET /admin/tables/:tableName" should {
"return data" in withApiClient { client =>
val name = "wiringbits"
val email = "[email protected]"
val request = AdminCreateTable.Request(
Map("name" -> name, "email" -> email, "password" -> "wiringbits")
)
client.createItem(usersSettings.tableName, request).futureValue
val response = client.getTableMetadata(usersSettings.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
val head = response.headOption.value
// TODO: Find a better way to do this
val nameValue = head.find(_._1 == "name").value._2
val emailValue = head.find(_._1 == "email").value._2
response.size must be(1)
name must be(nameValue)
email must be(emailValue)
}
"return data from user logs table" in withApiClient { implicit client =>
val user = createUser.futureValue
val userLog = createUserLog(user.userId).futureValue
val response =
client.getTableMetadata(userLogsSettings.tableName, List("message", "ASC"), List(0, 9), "{}").futureValue.head
response("message") mustBe userLog.message
response("user_id") mustBe user.userId
response("id") mustBe userLog.userLogId
}
"return data from uuid table" in withApiClient { client =>
val name = "wiringbits"
// val uuid_id = UUID.randomUUID().toString
val request = AdminCreateTable.Request(
Map("name" -> name)
)
client.createItem(uuidSettings.tableName, request).futureValue
val response = client.getTableMetadata(uuidSettings.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
val head = response.headOption.value
// TODO: Find a better way to do this
val uuidValue = head.find(_._1 == "id").value._2
val nameValue = head.find(_._1 == "name").value._2
response.size must be(1)
name must be(nameValue)
isValidUUID(uuidValue) must be(true)
}
"return data from serial table" in withApiClient { client =>
val name = "wiringbits"
val request = AdminCreateTable.Request(Map("name" -> name))
client.createItem(serialSettings.tableName, request).futureValue
val response =
client.getTableMetadata(serialSettings.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
val head = response.headOption.value
// TODO: Find a better way to do this
val intValue = head.find(_._1 == "id").value._2
val nameValue = head.find(_._1 == "name").value._2
response.size must be(1)
intValue must be("1")
name must be(nameValue)
}
"return data from big serial table" in withApiClient { client =>
val name = "wiringbits"
val request = AdminCreateTable.Request(Map("name" -> name))
client.createItem(bigSerialSettings.tableName, request).futureValue
val response =
client.getTableMetadata(bigSerialSettings.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
val head = response.headOption.value
// TODO: Find a better way to do this
val intValue = head.find(_._1 == "id").value._2
val nameValue = head.find(_._1 == "name").value._2
response.size must be(1)
intValue must be("1")
name must be(nameValue)
}
"return a empty map if there isn't any user" in withApiClient { client =>
val response = client.getTableMetadata(usersSettings.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
response.size must be(0)
}
"return an empty map if tables are empty" in withApiClient { client =>
val response = client.getTableMetadata(uuidSettings.tableName, List("id", "ASC"), List(0, 9), "{}").futureValue
response.size must be(0)
val response2 = client.getTableMetadata(serialSettings.tableName, List("id", "ASC"), List(0, 9), "{}").futureValue
response2.size must be(0)
val response3 =
client.getTableMetadata(bigSerialSettings.tableName, List("id", "ASC"), List(0, 9), "{}").futureValue
response3.size must be(0)
}
"return a empty map if start and end is the same" in withApiClient { client =>
val request = AdminCreateTable.Request(
Map("name" -> "wiringbits", "email" -> "[email protected]", "password" -> "wiringbits")
)
client.createItem(usersSettings.tableName, request).futureValue
val response = client.getTableMetadata(usersSettings.tableName, List("name", "ASC"), List(0, 0), "{}").futureValue
response.size must be(0)
}
"return an empty map for range of zero length" in withApiClient { client =>
val name = "wiringbits"
val request = AdminCreateTable.Request(
Map("name" -> name)
) // can use this for all 3 of the simple tables UUID, SERIAL, BIGSERIAL
client.createItem(uuidSettings.tableName, request).futureValue // 1
val response1 = client.getTableMetadata(uuidSettings.tableName, List("id", "ASC"), List(0, 0), "{}").futureValue
response1.size must be(0)
client.createItem(serialSettings.tableName, request).futureValue // 2
val response2 = client.getTableMetadata(serialSettings.tableName, List("id", "ASC"), List(0, 0), "{}").futureValue
response2.size must be(0)
client.createItem(bigSerialSettings.tableName, request).futureValue // 2
val response3 =
client.getTableMetadata(bigSerialSettings.tableName, List("id", "ASC"), List(0, 0), "{}").futureValue
response3.size must be(0)
}
"only return the end minus start elements" in withApiClient { client =>
Range.apply(0, 4).foreach { i =>
val data = Map("name" -> "wiringbits", "email" -> s"test@wiringbits$i.net", "password" -> "wiringbits")
val request = AdminCreateTable.Request(data)
client.createItem(usersSettings.tableName, request).futureValue
}
val end = 2
val start = 1
val returnedElements = end - start
val response =
client.getTableMetadata(usersSettings.tableName, List("name", "ASC"), List(start, end), "{}").futureValue
response.size must be(returnedElements)
}
"only return the range size of elements" in withApiClient { client =>
val end = 2
val start = 1
val returnedElements = end - start
// val data = Map() // data for these tables is always nothing. BIG/SERIAL autogenerated.
val name = "wiringbits"
val tables = List(uuidSettings, serialSettings, bigSerialSettings)
// this could just be a for loop instead of for comprehension
for (table <- tables)
yield {
Range.apply(0, 4).foreach { _ =>
val request = AdminCreateTable.Request(
Map("name" -> name)
) // tried using data and got error. I thought val data was in scope but perhaps not
client.createItem(table.tableName, request).futureValue
}
}
for (table <- tables)
yield {
val response = client.getTableMetadata(table.tableName, List("id", "ASC"), List(start, end), "{}").futureValue
response.size must be(returnedElements)
}
}
"return the elements in ascending order" in withApiClient { client =>
// should really be 5, since 5 users are created. Or Range.apply should start at 1.
val createdUsers = 4
val nameLength = 7
Range.apply(0, createdUsers).foreach { i =>
val letter = Character.valueOf(('A' + i).toChar)
val name = StringUtils.repeat(letter, nameLength)
val data = Map("name" -> name, "email" -> s"test@wiringbits$i.net", "password" -> "wiringbits")
val request = AdminCreateTable.Request(data)
client.createItem(usersSettings.tableName, request).futureValue
}
val response =
client.getTableMetadata(usersSettings.tableName, List("name", "ASC"), List(0, createdUsers), "{}").futureValue
val head = response.headOption.value
val name = head.find(_._1 == "name").value._2
response.size must be(createdUsers)
name must be(StringUtils.repeat('A', nameLength))
}
// TODO: add uuidSettings table to tests below
"return the elements of all tables in ascending order" in withApiClient { client =>
val createdRows = 4
val tables = List(serialSettings, bigSerialSettings)
val name = "wiringbits"
val expectedName = name + "0"
// insert rows
for (table <- tables)
yield {
Range.apply(0, createdRows).foreach { i =>
val request = AdminCreateTable.Request(Map("name" -> s"$name$i"))
client.createItem(table.tableName, request).futureValue
}
}
for (table <- tables)
yield {
val response =
client.getTableMetadata(table.tableName, List("name", "ASC"), List(0, createdRows), "{}").futureValue
val head = response.headOption.value
val id = head.find(_._1 == "id").value._2 // id from response
val nameValue = head.find(_._1 == "name").value._2 // name from response
response.size must be(createdRows)
id must be("1")
nameValue must be(expectedName)
}
}
"return the elements in descending order" in withApiClient { client =>
val createdUsers = 4
val nameLength = 7
Range.apply(0, createdUsers).foreach { i =>
val letter = Character.valueOf(('A' + i).toChar)
val name = StringUtils.repeat(letter, nameLength)
val data = Map("name" -> name, "email" -> s"test@wiringbits$i.net", "password" -> "wiringbits")
val request = AdminCreateTable.Request(data)
client.createItem(usersSettings.tableName, request).futureValue
}
val response =
client.getTableMetadata(usersSettings.tableName, List("name", "DESC"), List(0, createdUsers), "{}").futureValue
val head = response.headOption.value
val name = head.find(_._1 == "name").value._2
response.size must be(createdUsers)
name must be(StringUtils.repeat('D', nameLength))
}
"return the elements of all tables in descending order" in withApiClient { client =>
val createdRows = 4
// removed uuidSettings because ordering is random
val tables = List(serialSettings, bigSerialSettings)
val name = "Johnny"
val expectedNameValue = name + (createdRows - 1).toString
// insert rows
for (table <- tables)
yield {
Range.apply(0, createdRows).foreach { i =>
val request = AdminCreateTable.Request(Map("name" -> s"$name$i"))
client.createItem(table.tableName, request).futureValue
}
}
for (table <- tables)
yield {
val response =
client.getTableMetadata(table.tableName, List("name", "DESC"), List(0, createdRows), "{}").futureValue
val head = response.headOption.value
val id = head.find(_._1 == "id").value._2 // id from response
val nameValue = head.find(_._1 == "name").value._2 // name from response
response.size must be(createdRows)
id must be(createdRows.toString) // toString
expectedNameValue must be(nameValue)
}
}
"return filtered elements" in withApiClient { client =>
val createdUsers = 4
Range.apply(0, createdUsers).foreach { i =>
val data = Map("name" -> "wiringbits", "email" -> s"test@wiringbits$i.net", "password" -> "wiringbits")
val request = AdminCreateTable.Request(data)
client.createItem(usersSettings.tableName, request).futureValue
}
val expectedEmail = "[email protected]"
val response =
client
.getTableMetadata(
usersSettings.tableName,
List("name", "ASC"),
List(0, createdUsers),
s"""{"email":"$expectedEmail"}"""
)
.futureValue
val head = response.headOption.value
val email = head.find(_._1 == "email").value._2
response.size must be(1)
email must be(expectedEmail)
}
"return filtered elements for all tables" in withApiClient { client =>
val createdRows = 4
val tables = List(serialSettings, bigSerialSettings)
for (table <- tables) {
Range.apply(0, createdRows).foreach { i =>
val data = Map("name" -> s"wiringbits$i")
val request = AdminCreateTable.Request(data)
client.createItem(table.tableName, request).futureValue
}
}
val expectedName = "wiringbits0"
for (table <- tables) {
val response =
client
.getTableMetadata(
table.tableName,
List("name", "ASC"),
List(0, createdRows),
s"""{"name":"$expectedName"}"""
)
.futureValue
val head = response.headOption.value
val name = head.find(_._1 == "name").value._2
response.size must be(1)
name must be(expectedName)
}
}
"fail when table doesn't exists" in withApiClient { client =>
val invalidTableName = "aaaaaaaaaaa"
val error = client.getTableMetadata(invalidTableName, List("name", "ASC"), List(0, 9), "{}").expectError
error must be(s"Unexpected error because the DB table wasn't found: $invalidTableName")
}
"return data with partial match" in withApiClient { client =>
val name = "wiringbits"
val email = "[email protected]"
val request = AdminCreateTable.Request(
Map("name" -> name, "email" -> email, "password" -> "wiringbits")
)
client.createItem(usersSettings.tableName, request).futureValue
val response = client
.getTableMetadata(usersSettings.tableName, List("name", "ASC"), List(0, 9), """{"name":"irin"}""")
.futureValue
val head = response.headOption.value
val nameValue = head.find(_._1 == "name").value._2
val emailValue = head.find(_._1 == "email").value._2
response.size must be(1)
name must be(nameValue)
email must be(emailValue)
}
"not return data due to partial match" in withApiClient { client =>
val name = "wiringbits"
val email = "[email protected]"
val request = AdminCreateTable.Request(
Map("name" -> name, "email" -> email, "password" -> "wiringbits")
)
client.createItem(usersSettings.tableName, request).futureValue
val response = client
.getTableMetadata(usersSettings.tableName, List("name", "ASC"), List(0, 9), """{"name":"yyy"}""")
.futureValue
response.headOption.isEmpty must be(true)
}
"return data with two filters" in withApiClient { client =>
val name = "wiringbits"
val email = "[email protected]"
val request = AdminCreateTable.Request(
Map("name" -> name, "email" -> email, "password" -> "wiringbits")
)
client.createItem(usersSettings.tableName, request).futureValue
val response = client
.getTableMetadata(
usersSettings.tableName,
List("name", "ASC"),
List(0, 9),
"""{"name":"irin","email":"test"}"""
)
.futureValue
val head = response.headOption.value
val nameValue = head.find(_._1 == "name").value._2
val emailValue = head.find(_._1 == "email").value._2
response.size must be(1)
name must be(nameValue)
email must be(emailValue)
}
"not return with a valid filter and a non valid filter" in withApiClient { client =>
val name = "wiringbits"
val email = "[email protected]"
val request = AdminCreateTable.Request(
Map("name" -> name, "email" -> email, "password" -> "wiringbits")
)
client.createItem(usersSettings.tableName, request).futureValue
val response = client
.getTableMetadata(
usersSettings.tableName,
List("name", "ASC"),
List(0, 9),
"""{"name":"irin","email":"yyyy"}"""
)
.futureValue
response.headOption.isEmpty must be(true)
}
}
"GET /admin/tables/:tableName/:primaryKey" should {
"return table row" in withApiClient { client =>
val name = "wiringbits"
val email = "[email protected]"
val password = "wiringbits"
val request = AdminCreateTable.Request(Map("name" -> name, "email" -> email, "password" -> password))
client.createItem(usersSettings.tableName, request).futureValue
val users = client.getTableMetadata(usersSettings.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
val userId = users.headOption.value.find(_._1 == "id").value._2
val response = client.viewItem(usersSettings.tableName, userId).futureValue
response.find(_._1 == "name").value._2 must be(name)
response.find(_._1 == "email").value._2 must be(email)
response.find(_._1 == "password").value._2 must be(password)
response.find(_._1 == "id").value._2 must be(userId)
}
"return table row for all tables" in withApiClient { client =>
val tables = List(serialSettings, bigSerialSettings)
val name = "wiringbits"
val request = AdminCreateTable.Request(Map("name" -> name))
for (table <- tables) {
client.createItem(table.tableName, request).futureValue
val rows = client.getTableMetadata(table.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
val rowId = rows.headOption.value.find(_._1 == "id").value._2
val response = client.viewItem(table.tableName, rowId).futureValue
response.find(_._1 == "name").value._2 must be(name)
response.find(_._1 == "id").value._2 must be(rowId)
}
}
"fail if the table row doesn't exists" in withApiClient { client =>
val userId = UUID.randomUUID().toString
val error = client.viewItem(usersSettings.tableName, userId).expectError
error must be(s"Cannot find item in users with id $userId")
}
"fail if the table row doesn't exists for all tables" in withApiClient { client =>
val tables = List(serialSettings, bigSerialSettings)
val rand = new Random()
val id = rand.nextInt(1000) + 100
for (table <- tables) {
val tableName = table.tableName
val error = client.viewItem(table.tableName, id.toString).expectError
error must be(s"Cannot find item in $tableName with id $id")
}
}
}
"GET /admin/tables/:tableName/:primaryKeys" should {
"return table rows" in withApiClient { client =>
val tableName = "users"
val numOfCreatedUsers = 3
Range.apply(0, numOfCreatedUsers).foreach { i =>
val request = AdminCreateTable
.Request(Map("name" -> "wiringbits", "email" -> s"test@wiringbits$i.net", "password" -> "wiringbits"))
client.createItem(tableName, request).futureValue
}
val users = client.getTableMetadata(tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
val userIds = users.map(_.find(_._1 == "id").value._2)
val response = client.viewItems(tableName, userIds).futureValue
val sameName = response.flatMap(_.find(_._1 == "name")).forall(_._2 == "wiringbits")
val samePassword = response.flatMap(_.find(_._1 == "password")).forall(_._2 == "wiringbits")
response.size must be(userIds.length)
response.size must be(numOfCreatedUsers)
sameName must be(true)
samePassword must be(true)
}
"return table rows for all tables" in withApiClient { client =>
val tables = List(serialSettings, bigSerialSettings)
val numOfCreatedRows = 3
for (table <- tables) {
Range.apply(0, numOfCreatedRows).foreach { i =>
val request = AdminCreateTable
.Request(Map("name" -> s"wiringbits$i"))
client.createItem(table.tableName, request).futureValue
}
val rows = client.getTableMetadata(table.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
val ids = rows.map(_.find(_._1 == "id").value._2)
val response = client.viewItems(table.tableName, ids).futureValue
// TODO: check the names
// val sameName = response.flatMap(_.find(_._1 == "name")).forall(_._2 == "wiringbits")
response.size must be(ids.length)
response.size must be(numOfCreatedRows)
/*sameName must be(true)
samePassword must be(true)*/
}
}
"fail if the table row doesn't exists" in withApiClient { client =>
val userIds = List(UUID.randomUUID().toString)
val error = client.viewItems("users", userIds).expectError
error must be(s"Cannot find item in users with id ${userIds.headOption.value}")
}
"fail if the table row doesn't exists for all tables" in withApiClient { client =>
val tables = List(serialSettings, bigSerialSettings)
val rand = new Random()
val ids = List((rand.nextInt(1000) + 100).toString)
for (table <- tables) {
val tableName = table.tableName
val error = client.viewItems(tableName, ids).expectError
error must be(s"Cannot find item in $tableName with id ${ids.headOption.value}")
}
}
}
"POST /admin/tables/:tableName" should {
"create a new user" in withApiClient { client =>
val name = "wiringbits"
val email = "[email protected]"
val password = "wiringbits"
val request = AdminCreateTable.Request(Map("name" -> name, "email" -> email, "password" -> password))
val response = client.createItem("users", request).futureValue
response.id.nonEmpty mustBe true
}
"create a new row for all tables" in withApiClient { client =>
val tables = List(serialSettings, bigSerialSettings)
val name = "wiringbits"
for (table <- tables) {
val request = AdminCreateTable.Request(Map("name" -> name))
val response = client.createItem(table.tableName, request).futureValue
response.id.nonEmpty mustBe true
}
}
"don't fail if we send a null in an optional parameter" in withApiClient { client =>
val json = """{"name":"wiringbits","last_name":null,"email":"[email protected]","password":"wiringbits"}"""
val path = s"/admin/tables/${usersSettings.tableName}"
val response = POST(path, json).futureValue
response.header.status mustBe 200
val responseMetadata =
client.getTableMetadata(usersSettings.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
responseMetadata.head.nonEmpty mustBe true
responseMetadata.head("last_name") mustBe ""
}
"return new user id" in withApiClient { implicit client =>
val user = createUser.futureValue
val response = client.getTableMetadata(usersSettings.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
val userId = response.head("id")
userId mustBe user.userId
}
"create a new user log" in withApiClient { implicit client =>
val user = createUser.futureValue
val request = AdminCreateTable.Request(Map("user_id" -> user.userId, "message" -> "Wiringbits"))
val response = client.createItem("user_logs", request).futureValue
response.id.nonEmpty mustBe true
}
"fail when a mandatory field is not sent" in withApiClient { client =>
val name = "wiringbits"
val request = AdminCreateTable.Request(Map("name" -> name))
val error = client.createItem("users", request).expectError
error must be(s"There are missing fields: email, password")
}
"fail when a mandatory field is not sent for all tables" in withApiClient { client =>
val tables = List(serialSettings, bigSerialSettings)
// val name = "wiringbits"
for (table <- tables) {
val request = AdminCreateTable.Request(Map())
val error = client.createItem(table.tableName, request).expectError
error must be(s"There are missing fields: name")
}
}
"fail when overlow max int value" in withApiClient { client =>
val name = "wiringbits"
val table = serialOverflowSettings
val request = AdminCreateTable.Request(Map("name" -> name))
val ignore = client.createItem(table.tableName, request).futureValue
ignore.id.nonEmpty mustBe true
val request2 = AdminCreateTable.Request(Map("name" -> s"asdf"))
val error = client.createItem(table.tableName, request2).expectError
error must be(s"ERROR: integer out of range")
}
"fail when overlow max bigint value" in withApiClient { client =>
val name = "wiringbits"
val table = bigSerialOverflowSettings
val request = AdminCreateTable.Request(Map("name" -> name))
val ignore = client.createItem(table.tableName, request).futureValue
ignore.id.nonEmpty mustBe true
val request2 = AdminCreateTable.Request(Map("name" -> s"asdf"))
val error = client.createItem(table.tableName, request2).expectError
error must be(
s"ERROR: nextval: reached maximum value of sequence \"big_serial_table_overflow_seq\" (9223372036854775807)"
)
}
"create a new bytea" in withApiClient { implicit client =>
val stringBytea = "[0, 10, 20, 30]"
// The response returns the bytea as Hex; this would be its equivalent in Hex.
val correctValue = "\\x000a141e"
val request = AdminCreateTable.Request(Map("data" -> stringBytea))
val byteaId = client.createItem(byteaSettings.tableName, request).futureValue.id
val response = client.viewItem(byteaSettings.tableName, byteaId).futureValue
val dataResponse = response.find(_._1 == "data").value._2
dataResponse must be(correctValue)
}
}
"fail when field in request doesn't exists" in withApiClient { client =>
val name = "wiringbits"
val nonExistentField = "nonExistentField"
val request = AdminCreateTable.Request(Map("name" -> name, "nonExistentField" -> nonExistentField))
val error = client.createItem("users", request).expectError
error must be(s"A field doesn't correspond to this table schema")
}
"fail when field in request doesn't exists for all tables" in withApiClient { client =>
val tables = List(serialSettings, bigSerialSettings)
val name = "wiringbits"
val nonExistentField = "nonExistentField"
for (table <- tables) {
val request = AdminCreateTable.Request(Map("name" -> name, "nonExistentField" -> nonExistentField))
val error = client.createItem(table.tableName, request).expectError
error must be(s"A field doesn't correspond to this table schema")
}
}
"PUT /admin/tables/:tableName" should {
"update a new user" in withApiClient { client =>
val request = AdminCreateTable.Request(
Map("name" -> "wiringbits", "email" -> "[email protected]", "password" -> "wiringbits")
)
client.createItem(usersSettings.tableName, request).futureValue
val response = client.getTableMetadata(usersSettings.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
val userId = response.headOption.value.find(_._1 == "id").value._2
val email = "[email protected]"
val updateRequest = AdminUpdateTable.Request(Map("email" -> email))
val updateResponse = client.updateItem(usersSettings.tableName, userId, updateRequest).futureValue
val newResponse = client.viewItem(usersSettings.tableName, userId).futureValue
val emailResponse = newResponse.find(_._1 == "email").value._2
updateResponse.id must be(userId)
emailResponse must be(email)
}
"update a new bytea" in withApiClient { client =>
val request = AdminCreateTable.Request(Map("data" -> "[10, 10, 10, 10]"))
val byteaId = client.createItem(byteaSettings.tableName, request).futureValue.id
val stringBytea = "[0, 10, 20, 30]"
// The response returns the bytea as Hex; this would be its equivalent in Hex.
val correctValue = "\\x000a141e"
val updateRequest = AdminUpdateTable.Request(Map("data" -> stringBytea))
val updateResponse = client.updateItem(byteaSettings.tableName, byteaId, updateRequest).futureValue
val newResponse = client.viewItem(byteaSettings.tableName, byteaId).futureValue
val dataResponse = newResponse.find(_._1 == "data").value._2
updateResponse.id must be(byteaId)
dataResponse must be(correctValue)
}
"update a new row for all tables" in withApiClient { client =>
val tables = List(serialSettings, bigSerialSettings)
for (table <- tables) {
val request = AdminCreateTable.Request(
Map("name" -> "wiringbits")
)
client.createItem(table.tableName, request).futureValue
val response = client.getTableMetadata(table.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
val id = response.headOption.value.find(_._1 == "id").value._2
val name = "wiringbitsbitsbits"
val updateRequest = AdminUpdateTable.Request(Map("name" -> name))
val updateResponse = client.updateItem(table.tableName, id, updateRequest).futureValue
val newResponse = client.viewItem(table.tableName, id).futureValue
val nameResponse = newResponse.find(_._1 == "name").value._2
updateResponse.id must be(id)
nameResponse must be(name)
}
}
"don't fail if we send a null in an optional parameter" in withApiClient { client =>
val name = "wiringbits"
val lastName = "test"
val email = "[email protected]"
val password = "wiringbits"
val request =
AdminCreateTable.Request(Map("name" -> name, "last_name" -> lastName, "email" -> email, "password" -> password))
client.createItem("users", request).futureValue
val responseMetadata1 =
client.getTableMetadata(usersSettings.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
val id = responseMetadata1.head("id")
val json = """{"last_name":"null"}"""
val path = s"/admin/tables/${usersSettings.tableName}/$id"
val response = PUT(path, json).futureValue
response.header.status mustBe 200
val responseMetadata2 =
client.getTableMetadata(usersSettings.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
responseMetadata2.head("last_name") mustBe ""
}
"fail if the field in body doesn't exists" in withApiClient { client =>
val request = AdminCreateTable.Request(
Map("name" -> "wiringbits", "email" -> "[email protected]", "password" -> "wiringbits")
)
client.createItem("users", request).futureValue
val response = client.getTableMetadata("users", List("user_id", "ASC"), List(0, 9), "{}").futureValue
val userId = response.headOption.value.find(_._1 == "id").value._2
val email = "[email protected]"
val updateRequest = AdminUpdateTable.Request(Map("nonExistentField" -> email))
val error = client.updateItem("users", userId, updateRequest).expectError
error must be("A field doesn't correspond to this table schema")
}
"fail if the field in body doesn't exists for all tables" in withApiClient { client =>
val tables = List(serialSettings, bigSerialSettings)
for (table <- tables) {
val request = AdminCreateTable.Request(
Map("name" -> "test")
)
client.createItem(table.tableName, request).futureValue
val response = client.getTableMetadata(table.tableName, List("id", "ASC"), List(0, 9), "{}").futureValue
val id = response.headOption.value.find(_._1 == "id").value._2
val name = "wiringbits"
val updateRequest = AdminUpdateTable.Request(Map("nonExistentField" -> name))
val error = client.updateItem(table.tableName, id, updateRequest).expectError
error must be("A field doesn't correspond to this table schema")
}
}
}
"DELETE /admin/tables/users" should {
"delete a new user" in withApiClient { client =>
val name = "wiringbits"
val email = "[email protected]"
val password = "wiringbits"
val request = AdminCreateTable.Request(Map("name" -> name, "email" -> email, "password" -> password))
client.createItem("users", request).futureValue
val response = client.getTableMetadata("users", List("name", "ASC"), List(0, 9), "{}").futureValue
val userId = response.headOption.value.find(_._1 == "id").value._2
client.deleteItem("users", userId).futureValue
val newResponse = client.getTableMetadata("users", List("name", "ASC"), List(0, 9), "{}").futureValue
newResponse.isEmpty must be(true)
}
}
"DELETE /admin/tables/:tableName" should {
"delete a new row in all tables" in withApiClient { client =>
val tables = List(serialSettings, bigSerialSettings)
val name = "wiringbits"
val request = AdminCreateTable.Request(Map("name" -> name))
for (table <- tables) {
client.createItem(table.tableName, request).futureValue
val response = client.getTableMetadata(table.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
val id = response.headOption.value.find(_._1 == "id").value._2
client.deleteItem(table.tableName, id).futureValue
val newResponse = client.getTableMetadata(table.tableName, List("name", "ASC"), List(0, 9), "{}").futureValue
newResponse.isEmpty must be(true)
}
}
}
}