cmd_graph_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 TestGraphCommands(t *testing.T) {
  17. var out bytes.Buffer
  18. ResetDB()
  19. credGiver.Reset()
  20. createSongGraph()
  21. // Enable access control
  22. config.Config[config.EnableAccessControl] = true
  23. defer func() {
  24. config.Config[config.EnableAccessControl] = false
  25. }()
  26. c := NewConsole("http://localhost"+TESTPORT, &out, credGiver.GetCredentials,
  27. func() string { return "***pass***" },
  28. func(args []string, e *bytes.Buffer) error {
  29. return nil
  30. })
  31. // Now force the login - we should get one failed login
  32. out.Reset()
  33. credGiver.UserQueue = []string{"elias"}
  34. credGiver.PassQueue = []string{"elias"}
  35. if ok, err := c.Run("users"); !ok || err != nil {
  36. t.Error(ok, err)
  37. return
  38. }
  39. if res := out.String(); res != `
  40. Login as user elias
  41. ┌─────────┬─────────────┐
  42. │Username │Groups │
  43. ├─────────┼─────────────┤
  44. │elias │admin/public │
  45. │johndoe │public │
  46. └─────────┴─────────────┘
  47. `[1:] {
  48. t.Error("Unexpected result:", res)
  49. return
  50. }
  51. out.Reset()
  52. if ok, err := c.Run("info"); !ok || err != nil {
  53. t.Error(ok, err)
  54. return
  55. }
  56. if res := out.String(); res != `
  57. ┌─────────┬───────────┐
  58. │Kind │Count │
  59. ├─────────┼───────────┤
  60. │Author │ 2 │
  61. │Producer │ 1 │
  62. │Song │ 9 │
  63. │Spam │ 21 │
  64. │Writer │ 1 │
  65. └─────────┴───────────┘
  66. `[1:] {
  67. t.Error("Unexpected result:", res)
  68. return
  69. }
  70. out.Reset()
  71. if ok, err := c.Run("part"); !ok || err != nil {
  72. t.Error(ok, err)
  73. return
  74. }
  75. if res := out.String(); res != `
  76. main
  77. `[1:] {
  78. t.Error("Unexpected result:", res)
  79. return
  80. }
  81. out.Reset()
  82. if ok, err := c.Run("part foo"); !ok || err != nil {
  83. t.Error(ok, err)
  84. return
  85. }
  86. if res := out.String(); res != `
  87. Current partition is: foo
  88. `[1:] {
  89. t.Error("Unexpected result:", res)
  90. return
  91. }
  92. out.Reset()
  93. if ok, err := c.Run("part"); !ok || err != nil {
  94. t.Error(ok, err)
  95. return
  96. }
  97. if res := out.String(); res != `
  98. foo
  99. `[1:] {
  100. t.Error("Unexpected result:", res)
  101. return
  102. }
  103. out.Reset()
  104. if ok, err := c.Run("find"); ok || err == nil || err.Error() != "Please specify a search phrase" {
  105. t.Error(ok, err)
  106. return
  107. }
  108. out.Reset()
  109. if ok, err := c.Run("find artist"); !ok || err != nil {
  110. t.Error(ok, err)
  111. return
  112. }
  113. if res := out.String(); res != `
  114. Partition main
  115. Kind Author
  116. ┌───────────────────┬────┬───────┬─────┐
  117. │desc │key │kind │name │
  118. ├───────────────────┼────┼───────┼─────┤
  119. │A lonely artisT │000 │Author │John │
  120. │An annoying artist │123 │Author │Mike │
  121. └───────────────────┴────┴───────┴─────┘
  122. Partition main
  123. Kind Writer
  124. ┌────┬───────┬─────┬────────────────────────────┐
  125. │key │kind │name │text │
  126. ├────┼───────┼─────┼────────────────────────────┤
  127. │456 │Writer │Hans │A song writer for an artist │
  128. └────┴───────┴─────┴────────────────────────────┘
  129. Partition second
  130. Kind Producer
  131. ┌────┬─────────┬─────┬────────────────────────┐
  132. │key │kind │name │occupation │
  133. ├────┼─────────┼─────┼────────────────────────┤
  134. │123 │Producer │Jack │A producer of an aRtIsT │
  135. └────┴─────────┴─────┴────────────────────────┘
  136. `[1:] && res != `
  137. Partition main
  138. Kind Author
  139. ┌───────────────────┬────┬───────┬─────┐
  140. │desc │key │kind │name │
  141. ├───────────────────┼────┼───────┼─────┤
  142. │An annoying artist │123 │Author │Mike │
  143. │A lonely artisT │000 │Author │John │
  144. └───────────────────┴────┴───────┴─────┘
  145. Partition main
  146. Kind Writer
  147. ┌────┬───────┬─────┬────────────────────────────┐
  148. │key │kind │name │text │
  149. ├────┼───────┼─────┼────────────────────────────┤
  150. │456 │Writer │Hans │A song writer for an artist │
  151. └────┴───────┴─────┴────────────────────────────┘
  152. Partition second
  153. Kind Producer
  154. ┌────┬─────────┬─────┬────────────────────────┐
  155. │key │kind │name │occupation │
  156. ├────┼─────────┼─────┼────────────────────────┤
  157. │123 │Producer │Jack │A producer of an aRtIsT │
  158. └────┴─────────┴─────┴────────────────────────┘
  159. `[1:] {
  160. t.Error("Unexpected result:", res)
  161. return
  162. }
  163. out.Reset()
  164. }