admin_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Rufs - Remote Union File System
  3. *
  4. * Copyright 2017 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package v1
  11. import (
  12. "fmt"
  13. "testing"
  14. "devt.de/krotik/rufs"
  15. "devt.de/krotik/rufs/api"
  16. "devt.de/krotik/rufs/config"
  17. )
  18. func TestAdminQuery(t *testing.T) {
  19. queryURL := "http://localhost" + TESTPORT + EndpointAdmin
  20. defer func() {
  21. // Make sure all trees are removed
  22. api.ResetTrees()
  23. }()
  24. // In the beginning there should be no trees
  25. st, _, res := sendTestRequest(queryURL, "GET", nil)
  26. if st != "200 OK" || res != "{}" {
  27. t.Error("Unexpected response:", st, res)
  28. return
  29. }
  30. // Create a new tree
  31. st, _, res = sendTestRequest(queryURL, "POST", []byte("\"Hans1\""))
  32. if st != "200 OK" || res != "" {
  33. t.Error("Unexpected response:", st, res)
  34. return
  35. }
  36. // Check a new tree was created
  37. st, _, res = sendTestRequest(queryURL+"?refresh=Hans1", "GET", nil)
  38. if st != "200 OK" || res != `
  39. {
  40. "Hans1": {
  41. "branches": [],
  42. "tree": []
  43. }
  44. }`[1:] {
  45. t.Error("Unexpected response:", st, res)
  46. return
  47. }
  48. // Add a new branch
  49. fooRPC := fmt.Sprintf("%v:%v", branchConfigs["footest"][config.RPCHost], branchConfigs["footest"][config.RPCPort])
  50. fooFP := footest.SSLFingerprint()
  51. st, _, res = sendTestRequest(queryURL+"Hans1/branch", "POST", []byte(fmt.Sprintf(`
  52. {
  53. "branch" : "footest",
  54. "rpc" : %#v,
  55. "fingerprint" : %#v
  56. }`, fooRPC, fooFP)))
  57. if st != "200 OK" || res != "" {
  58. t.Error("Unexpected response:", st, res)
  59. return
  60. }
  61. // Check the branch was added
  62. st, _, res = sendTestRequest(queryURL, "GET", nil)
  63. if st != "200 OK" || res != fmt.Sprintf(`
  64. {
  65. "Hans1": {
  66. "branches": [
  67. {
  68. "branch": "footest",
  69. "fingerprint": %#v,
  70. "rpc": %#v
  71. }
  72. ],
  73. "tree": []
  74. }
  75. }`[1:], fooFP, fooRPC) {
  76. t.Error("Unexpected response:", st, res)
  77. return
  78. }
  79. // Add a new mapping
  80. st, _, res = sendTestRequest(queryURL+"Hans1/mapping", "POST", []byte(`
  81. {
  82. "dir" : "/",
  83. "branch" : "footest",
  84. "writeable" : false
  85. }`))
  86. if st != "200 OK" || res != "" {
  87. t.Error("Unexpected response:", st, res)
  88. return
  89. }
  90. // Check the mapping was added
  91. st, _, res = sendTestRequest(queryURL, "GET", nil)
  92. if st != "200 OK" || res != fmt.Sprintf(`
  93. {
  94. "Hans1": {
  95. "branches": [
  96. {
  97. "branch": "footest",
  98. "fingerprint": %#v,
  99. "rpc": %#v
  100. }
  101. ],
  102. "tree": [
  103. {
  104. "branch": "footest",
  105. "path": "/",
  106. "writeable": false
  107. }
  108. ]
  109. }
  110. }`[1:], fooFP, fooRPC) {
  111. t.Error("Unexpected response:", st, res)
  112. return
  113. }
  114. // Test error cases
  115. st, _, res = sendTestRequest(queryURL, "POST", []byte(`""`))
  116. if st != "400 Bad Request" || res != "Body must contain the tree name as a non-empty JSON string" {
  117. t.Error("Unexpected response:", st, res)
  118. return
  119. }
  120. st, _, res = sendTestRequest(queryURL, "POST", []byte(`x`))
  121. if st != "400 Bad Request" || res != "Could not decode request body: invalid character 'x' looking for beginning of value" {
  122. t.Error("Unexpected response:", st, res)
  123. return
  124. }
  125. origConfig := api.TreeConfigTemplate
  126. api.TreeConfigTemplate = nil
  127. st, _, res = sendTestRequest(queryURL, "POST", []byte(`"xx"`))
  128. if st != "400 Bad Request" || res != "Could not create new tree: Missing TreeSecret key in tree config" {
  129. api.TreeConfigTemplate = origConfig
  130. t.Error("Unexpected response:", st, res)
  131. return
  132. }
  133. api.TreeConfigTemplate = origConfig
  134. // Test error cases
  135. origTrees := api.Trees
  136. api.Trees = func() (map[string]*rufs.Tree, error) {
  137. return nil, fmt.Errorf("Testerror")
  138. }
  139. st, _, res = sendTestRequest(queryURL, "GET", nil)
  140. if st != "400 Bad Request" || res != "Testerror" {
  141. api.Trees = origTrees
  142. t.Error("Unexpected response:", st, res)
  143. return
  144. }
  145. api.Trees = origTrees
  146. st, _, res = sendTestRequest(queryURL, "POST", []byte("\"Hans1\""))
  147. if st != "400 Bad Request" || res != "Could not add new tree: Tree Hans1 already exists" {
  148. t.Error("Unexpected response:", st, res)
  149. return
  150. }
  151. st, _, res = sendTestRequest(queryURL+"Hans2/mapping", "POST", nil)
  152. if st != "400 Bad Request" || res != "Unknown tree: Hans2" {
  153. t.Error("Unexpected response:", st, res)
  154. return
  155. }
  156. st, _, res = sendTestRequest(queryURL+"Hans1/mapping", "POST", []byte("aaa"))
  157. if st != "400 Bad Request" || res != "Could not decode request body: invalid character 'a' looking for beginning of value" {
  158. t.Error("Unexpected response:", st, res)
  159. return
  160. }
  161. st, _, res = sendTestRequest(queryURL+"Hans2/", "POST", []byte("aaa"))
  162. if st != "400 Bad Request" || res != "Need a tree name and a section (either branches or mapping)" {
  163. t.Error("Unexpected response:", st, res)
  164. return
  165. }
  166. st, _, res = sendTestRequest(queryURL+"Hans1/branch", "POST", []byte(fmt.Sprintf(`
  167. {
  168. "branch" : "footest",
  169. "rpc" : %#v,
  170. "fingerprint" : %#v
  171. }`, fooRPC, fooFP)))
  172. if st != "400 Bad Request" || res != "Could not add branch: Peer already registered: footest" {
  173. t.Error("Unexpected response:", st, res)
  174. return
  175. }
  176. st, _, res = sendTestRequest(queryURL+"Hans1/branch", "POST", []byte(fmt.Sprintf(`
  177. {
  178. "branch" : "footest",
  179. "rpc2" : %#v,
  180. "fingerprint" : %#v
  181. }`, fooRPC, fooFP)))
  182. if st != "400 Bad Request" || res != "Value for rpc is missing in posted data" {
  183. t.Error("Unexpected response:", st, res)
  184. return
  185. }
  186. st, _, res = sendTestRequest(queryURL+"Hans1/mapping", "POST", []byte(`
  187. {
  188. "dir" : "/",
  189. "branch" : "footest2",
  190. "writeable" : false
  191. }`))
  192. if st != "400 Bad Request" || res != "Could not add branch: Unknown target node" {
  193. t.Error("Unexpected response:", st, res)
  194. return
  195. }
  196. st, _, res = sendTestRequest(queryURL+"Hans1/mapping", "POST", []byte(`
  197. {
  198. "dir" : "/",
  199. "branch" : "footest2",
  200. "writeable" : "test"
  201. }`))
  202. if st != "400 Bad Request" || res != "Writeable value must be a boolean: strconv.ParseBool: parsing \"test\": invalid syntax" {
  203. t.Error("Unexpected response:", st, res)
  204. return
  205. }
  206. // Delete twice
  207. if trees, err := api.Trees(); len(trees) != 1 || err != nil {
  208. t.Error("Unexpected result:", trees, err)
  209. return
  210. }
  211. st, _, res = sendTestRequest(queryURL+"Hans1", "DELETE", nil)
  212. if st != "200 OK" || res != "" {
  213. t.Error("Unexpected response:", st, res)
  214. return
  215. }
  216. st, _, res = sendTestRequest(queryURL, "DELETE", nil)
  217. if st != "400 Bad Request" || res != "Need a tree name" {
  218. t.Error("Unexpected response:", st, res)
  219. return
  220. }
  221. st, _, res = sendTestRequest(queryURL+"Hans1/meyer", "DELETE", nil)
  222. if st != "400 Bad Request" || res != "Invalid resource specification: meyer" {
  223. t.Error("Unexpected response:", st, res)
  224. return
  225. }
  226. if trees, err := api.Trees(); len(trees) != 0 || err != nil {
  227. t.Error("Unexpected result:", trees, err)
  228. return
  229. }
  230. st, _, res = sendTestRequest(queryURL+"Hans1", "DELETE", nil)
  231. if st != "400 Bad Request" || res != "Could not remove tree: Tree Hans1 does not exist" {
  232. t.Error("Unexpected response:", st, res)
  233. return
  234. }
  235. }