graphqlconsole_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package console
  11. import (
  12. "bytes"
  13. "testing"
  14. "devt.de/krotik/eliasdb/config"
  15. )
  16. func TestGraphQLConsole(t *testing.T) {
  17. var out bytes.Buffer
  18. ResetDB()
  19. credGiver.Reset()
  20. createSongGraph()
  21. // Dummy test
  22. graphqlc := &GraphQLConsole{}
  23. graphqlc.Commands()
  24. // Enable access control
  25. config.Config[config.EnableAccessControl] = true
  26. defer func() {
  27. config.Config[config.EnableAccessControl] = false
  28. }()
  29. c := NewConsole("http://localhost"+TESTPORT, &out, credGiver.GetCredentials,
  30. func() string { return "***pass***" },
  31. func(args []string, e *bytes.Buffer) error {
  32. return nil
  33. })
  34. out.Reset()
  35. credGiver.UserQueue = []string{"elias"}
  36. credGiver.PassQueue = []string{"elias"}
  37. if ok, err := c.Run("users"); !ok || err != nil {
  38. t.Error(ok, err)
  39. return
  40. }
  41. if res := out.String(); res != `
  42. Login as user elias
  43. ┌─────────┬─────────────┐
  44. │Username │Groups │
  45. ├─────────┼─────────────┤
  46. │elias │admin/public │
  47. │johndoe │public │
  48. └─────────┴─────────────┘
  49. `[1:] {
  50. t.Error("Unexpected result:", res)
  51. return
  52. }
  53. out.Reset()
  54. if ok, err := c.Run("{ Song { key, name, ranking }}"); !ok || err != nil {
  55. t.Error(ok, err)
  56. return
  57. }
  58. if res := out.String(); res != `
  59. {
  60. "data": {
  61. "Song": [
  62. {
  63. "key": "StrangeSong1",
  64. "name": "StrangeSong1",
  65. "ranking": 5
  66. },
  67. {
  68. "key": "FightSong4",
  69. "name": "FightSong4",
  70. "ranking": 3
  71. },
  72. {
  73. "key": "DeadSong2",
  74. "name": "DeadSong2",
  75. "ranking": 6
  76. },
  77. {
  78. "key": "LoveSong3",
  79. "name": "LoveSong3",
  80. "ranking": 1
  81. },
  82. {
  83. "key": "MyOnlySong3",
  84. "name": "MyOnlySong3",
  85. "ranking": 19
  86. },
  87. {
  88. "key": "Aria1",
  89. "name": "Aria1",
  90. "ranking": 8
  91. },
  92. {
  93. "key": "Aria2",
  94. "name": "Aria2",
  95. "ranking": 2
  96. },
  97. {
  98. "key": "Aria3",
  99. "name": "Aria3",
  100. "ranking": 4
  101. },
  102. {
  103. "key": "Aria4",
  104. "name": "Aria4",
  105. "ranking": 18
  106. }
  107. ]
  108. }
  109. }`[1:] {
  110. t.Error("Unexpected result:", res)
  111. return
  112. }
  113. out.Reset()
  114. }