node_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "testing"
  6. "time"
  7. "github.com/gravitl/netmaker/models"
  8. "github.com/stretchr/testify/assert"
  9. "golang.org/x/crypto/bcrypt"
  10. )
  11. func TestRainyDay(t *testing.T) {
  12. t.Run("badkey", func(t *testing.T) {
  13. response, err := api(t, "", http.MethodGet, baseURL+"/api/nodes", "badkey")
  14. assert.Nil(t, err, err)
  15. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  16. })
  17. t.Run("badURL", func(t *testing.T) {
  18. response, err := api(t, "", http.MethodGet, baseURL+"/api/nodes/adm/skynet/01:02:03:04:05:07", "secretkey")
  19. assert.Nil(t, err, err)
  20. assert.Equal(t, http.StatusNotFound, response.StatusCode)
  21. })
  22. t.Run("NonExistentNetwork", func(t *testing.T) {
  23. response, err := api(t, "", http.MethodGet, baseURL+"/api/nodes/badnet", "secretkey")
  24. assert.Nil(t, err, err)
  25. assert.Equal(t, http.StatusNotFound, response.StatusCode)
  26. })
  27. }
  28. func TestGetAllNodes(t *testing.T) {
  29. setup(t)
  30. t.Run("NodesExist", func(t *testing.T) {
  31. response, err := api(t, "", http.MethodGet, baseURL+"/api/nodes", "secretkey")
  32. assert.Nil(t, err, err)
  33. assert.Equal(t, http.StatusOK, response.StatusCode)
  34. var nodes []models.ReturnNode
  35. defer response.Body.Close()
  36. err = json.NewDecoder(response.Body).Decode(&nodes)
  37. assert.Nil(t, err, err)
  38. assert.NotEmpty(t, nodes)
  39. })
  40. t.Run("NodeDoesNotExist", func(t *testing.T) {
  41. deleteNode(t)
  42. response, err := api(t, "", http.MethodGet, baseURL+"/api/nodes", "secretkey")
  43. assert.Nil(t, err, err)
  44. assert.Equal(t, http.StatusOK, response.StatusCode)
  45. var nodes []models.ReturnNode
  46. defer response.Body.Close()
  47. err = json.NewDecoder(response.Body).Decode(&nodes)
  48. assert.Nil(t, err, err)
  49. assert.Empty(t, nodes)
  50. })
  51. }
  52. func TestGetNetworkNodes(t *testing.T) {
  53. setup(t)
  54. t.Run("NodeExists", func(t *testing.T) {
  55. response, err := api(t, "", http.MethodGet, baseURL+"/api/nodes/skynet", "secretkey")
  56. assert.Nil(t, err, err)
  57. assert.Equal(t, http.StatusOK, response.StatusCode)
  58. var nodes []models.ReturnNode
  59. defer response.Body.Close()
  60. err = json.NewDecoder(response.Body).Decode(&nodes)
  61. assert.Nil(t, err, err)
  62. assert.NotEmpty(t, nodes)
  63. })
  64. t.Run("NodeDoesNotExit", func(t *testing.T) {
  65. deleteNode(t)
  66. response, err := api(t, "", http.MethodGet, baseURL+"/api/nodes/skynet", "secretkey")
  67. assert.Nil(t, err, err)
  68. assert.Equal(t, http.StatusOK, response.StatusCode)
  69. var nodes []models.ReturnNode
  70. defer response.Body.Close()
  71. err = json.NewDecoder(response.Body).Decode(&nodes)
  72. assert.Nil(t, err, err)
  73. assert.Empty(t, nodes)
  74. })
  75. }
  76. func TestGetNode(t *testing.T) {
  77. setup(t)
  78. t.Run("NodeExists", func(t *testing.T) {
  79. response, err := api(t, "", http.MethodGet, baseURL+"/api/nodes/skynet/01:02:03:04:05:06", "secretkey")
  80. assert.Nil(t, err, err)
  81. assert.Equal(t, http.StatusOK, response.StatusCode)
  82. var node models.Node
  83. defer response.Body.Close()
  84. err = json.NewDecoder(response.Body).Decode(&node)
  85. assert.Nil(t, err, err)
  86. assert.Equal(t, "01:02:03:04:05:06", node.MacAddress)
  87. })
  88. t.Run("NodeDoesNotExist", func(t *testing.T) {
  89. deleteNode(t)
  90. response, err := api(t, "", http.MethodGet, baseURL+"/api/nodes/skynet/01:02:03:04:05:06", "secretkey")
  91. assert.Nil(t, err, err)
  92. assert.Equal(t, http.StatusInternalServerError, response.StatusCode)
  93. })
  94. }
  95. func TestUpdateNode(t *testing.T) {
  96. var data models.Node
  97. setup(t)
  98. t.Run("UpdateMulti", func(t *testing.T) {
  99. data.Address = "10.1.0.2"
  100. data.MacAddress = "01:02:03:04:05:05"
  101. data.Name = "NewName"
  102. data.PublicKey = "DM5qhLAE20PG9BbfBCgfr+Ac9D2NDOwCtY1rbYDLf34="
  103. data.Password = "newpass"
  104. data.LocalAddress = "192.168.0.2"
  105. data.Endpoint = "10.100.100.5"
  106. response, err := api(t, data, http.MethodPut, baseURL+"/api/nodes/skynet/01:02:03:04:05:06", "secretkey")
  107. assert.Nil(t, err, err)
  108. assert.Equal(t, http.StatusOK, response.StatusCode)
  109. defer response.Body.Close()
  110. var node models.Node
  111. err = json.NewDecoder(response.Body).Decode(&node)
  112. assert.Nil(t, err, err)
  113. assert.Equal(t, data.Name, node.Name)
  114. assert.Equal(t, data.PublicKey, node.PublicKey)
  115. err = bcrypt.CompareHashAndPassword([]byte(node.Password), []byte(data.Password))
  116. assert.Nil(t, err, err)
  117. assert.Equal(t, data.LocalAddress, node.LocalAddress)
  118. assert.Equal(t, data.Endpoint, node.Endpoint)
  119. })
  120. t.Run("InvalidAddress", func(t *testing.T) {
  121. data.Address = "10.300.2.0"
  122. response, err := api(t, data, http.MethodPut, baseURL+"/api/nodes/skynet/01:02:03:04:05:05", "secretkey")
  123. assert.Nil(t, err, err)
  124. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  125. var message models.ErrorResponse
  126. defer response.Body.Close()
  127. err = json.NewDecoder(response.Body).Decode(&message)
  128. assert.Nil(t, err, err)
  129. assert.Equal(t, http.StatusBadRequest, message.Code)
  130. assert.Contains(t, message.Message, "Field validation for 'Address' failed")
  131. })
  132. t.Run("InvalidMacAddress", func(t *testing.T) {
  133. data.MacAddress = "10:11:12:13:14:15:16"
  134. response, err := api(t, data, http.MethodPut, baseURL+"/api/nodes/skynet/01:02:03:04:05:05", "secretkey")
  135. assert.Nil(t, err, err)
  136. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  137. var message models.ErrorResponse
  138. defer response.Body.Close()
  139. err = json.NewDecoder(response.Body).Decode(&message)
  140. assert.Nil(t, err, err)
  141. assert.Equal(t, http.StatusBadRequest, message.Code)
  142. assert.Contains(t, message.Message, "Field validation for 'MacAddress' failed")
  143. })
  144. t.Run("InvalidEndpoint", func(t *testing.T) {
  145. data.Endpoint = "10.10.10.300"
  146. response, err := api(t, data, http.MethodPut, baseURL+"/api/nodes/skynet/01:02:03:04:05:05", "secretkey")
  147. assert.Nil(t, err, err)
  148. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  149. var message models.ErrorResponse
  150. defer response.Body.Close()
  151. err = json.NewDecoder(response.Body).Decode(&message)
  152. assert.Nil(t, err, err)
  153. assert.Equal(t, http.StatusBadRequest, message.Code)
  154. assert.Contains(t, message.Message, "Field validation for 'Endpoint' failed")
  155. })
  156. t.Run("InvalidLocalAddress", func(t *testing.T) {
  157. data.LocalAddress = "10.10.10.300"
  158. response, err := api(t, data, http.MethodPut, baseURL+"/api/nodes/skynet/01:02:03:04:05:05", "secretkey")
  159. assert.Nil(t, err, err)
  160. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  161. var message models.ErrorResponse
  162. defer response.Body.Close()
  163. err = json.NewDecoder(response.Body).Decode(&message)
  164. assert.Nil(t, err, err)
  165. assert.Equal(t, http.StatusBadRequest, message.Code)
  166. assert.Contains(t, message.Message, "Field validation for 'LocalAddress' failed")
  167. })
  168. t.Run("InvalidName", func(t *testing.T) {
  169. var data struct {
  170. Name string
  171. }
  172. data.Name = "New*Name"
  173. response, err := api(t, data, http.MethodPut, baseURL+"/api/nodes/skynet/01:02:03:04:05:05", "secretkey")
  174. assert.Nil(t, err, err)
  175. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  176. var message models.ErrorResponse
  177. defer response.Body.Close()
  178. err = json.NewDecoder(response.Body).Decode(&message)
  179. assert.Nil(t, err, err)
  180. assert.Equal(t, http.StatusBadRequest, message.Code)
  181. assert.Contains(t, message.Message, "Field validation for 'Name' failed")
  182. })
  183. t.Run("InvalidPublicKey", func(t *testing.T) {
  184. data.PublicKey = "xxx"
  185. response, err := api(t, data, http.MethodPut, baseURL+"/api/nodes/skynet/01:02:03:04:05:05", "secretkey")
  186. assert.Nil(t, err, err)
  187. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  188. var message models.ErrorResponse
  189. defer response.Body.Close()
  190. err = json.NewDecoder(response.Body).Decode(&message)
  191. assert.Nil(t, err, err)
  192. assert.Equal(t, http.StatusBadRequest, message.Code)
  193. assert.Contains(t, message.Message, "Field validation for 'PublicKey' failed")
  194. })
  195. t.Run("InvalidPassword", func(t *testing.T) {
  196. data.Password = "1234"
  197. response, err := api(t, data, http.MethodPut, baseURL+"/api/nodes/skynet/01:02:03:04:05:05", "secretkey")
  198. assert.Nil(t, err, err)
  199. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  200. var message models.ErrorResponse
  201. defer response.Body.Close()
  202. err = json.NewDecoder(response.Body).Decode(&message)
  203. assert.Nil(t, err, err)
  204. assert.Equal(t, http.StatusBadRequest, message.Code)
  205. assert.Contains(t, message.Message, "Field validation for 'Password' failed")
  206. })
  207. }
  208. func TestDeleteNode(t *testing.T) {
  209. setup(t)
  210. t.Run("ExistingNode", func(t *testing.T) {
  211. response, err := api(t, "", http.MethodDelete, baseURL+"/api/nodes/skynet/01:02:03:04:05:06", "secretkey")
  212. assert.Nil(t, err, err)
  213. assert.Equal(t, http.StatusOK, response.StatusCode)
  214. defer response.Body.Close()
  215. var message models.SuccessResponse
  216. err = json.NewDecoder(response.Body).Decode(&message)
  217. assert.Nil(t, err, err)
  218. assert.Equal(t, "01:02:03:04:05:06 deleted.", message.Message)
  219. assert.Equal(t, http.StatusOK, message.Code)
  220. })
  221. t.Run("NonExistantNode", func(t *testing.T) {
  222. response, err := api(t, "", http.MethodDelete, baseURL+"/api/nodes/skynet/01:02:03:04:05:06", "secretkey")
  223. assert.Nil(t, err, err)
  224. assert.Equal(t, http.StatusInternalServerError, response.StatusCode)
  225. defer response.Body.Close()
  226. var message models.ErrorResponse
  227. err = json.NewDecoder(response.Body).Decode(&message)
  228. assert.Nil(t, err, err)
  229. assert.Equal(t, http.StatusInternalServerError, message.Code)
  230. assert.Equal(t, "Could not delete node 01:02:03:04:05:06", message.Message)
  231. })
  232. }
  233. func TestCheckIn(t *testing.T) {
  234. setup(t)
  235. oldNode := getNode(t)
  236. time.Sleep(1 * time.Second)
  237. t.Run("Valid", func(t *testing.T) {
  238. response, err := api(t, "", http.MethodPost, baseURL+"/api/nodes/skynet/01:02:03:04:05:06/checkin", "secretkey")
  239. assert.Nil(t, err, err)
  240. assert.Equal(t, http.StatusOK, response.StatusCode)
  241. var node models.Node
  242. defer response.Body.Close()
  243. err = json.NewDecoder(response.Body).Decode(&node)
  244. assert.Nil(t, err, err)
  245. assert.Greater(t, node.LastCheckIn, oldNode.LastCheckIn)
  246. })
  247. t.Run("NodeDoesNotExist", func(t *testing.T) {
  248. deleteNode(t)
  249. response, err := api(t, "", http.MethodPost, baseURL+"/api/nodes/skynet/01:02:03:04:05:06/checkin", "secretkey")
  250. assert.Nil(t, err, err)
  251. assert.Equal(t, http.StatusInternalServerError, response.StatusCode)
  252. var message models.ErrorResponse
  253. defer response.Body.Close()
  254. err = json.NewDecoder(response.Body).Decode(&message)
  255. assert.Nil(t, err, err)
  256. assert.Equal(t, http.StatusInternalServerError, message.Code)
  257. assert.Equal(t, "mongo: no documents in result", message.Message)
  258. })
  259. }
  260. func TestCreateGateway(t *testing.T) {
  261. setup(t)
  262. //assert.False(t, node.IsGateway)
  263. var gateway models.GatewayRequest
  264. t.Run("Valid", func(t *testing.T) {
  265. gateway.RangeString = "0.0.0.0/0"
  266. gateway.Interface = "eth0"
  267. response, err := api(t, gateway, http.MethodPost, baseURL+"/api/nodes/skynet/01:02:03:04:05:06/creategateway", "secretkey")
  268. assert.Nil(t, err, err)
  269. assert.Equal(t, http.StatusOK, response.StatusCode)
  270. defer response.Body.Close()
  271. var message models.Node
  272. err = json.NewDecoder(response.Body).Decode(&message)
  273. assert.Nil(t, err, err)
  274. assert.True(t, message.IsGateway)
  275. })
  276. t.Run("BadRange", func(t *testing.T) {
  277. gateway.RangeString = "0.0.0.0/36"
  278. gateway.Interface = "eth0"
  279. response, err := api(t, gateway, http.MethodPost, baseURL+"/api/nodes/skynet/01:02:03:04:05:06/creategateway", "secretkey")
  280. assert.Nil(t, err, err)
  281. assert.Equal(t, http.StatusInternalServerError, response.StatusCode)
  282. defer response.Body.Close()
  283. var message models.ErrorResponse
  284. err = json.NewDecoder(response.Body).Decode(&message)
  285. assert.Nil(t, err, err)
  286. assert.Equal(t, http.StatusInternalServerError, message.Code)
  287. assert.Equal(t, "IP Range Not Valid", message.Message)
  288. })
  289. t.Run("BadInterface", func(t *testing.T) {
  290. gateway.RangeString = "0.0.0.0/0"
  291. gateway.Interface = ""
  292. response, err := api(t, gateway, http.MethodPost, baseURL+"/api/nodes/skynet/01:02:03:04:05:06/creategateway", "secretkey")
  293. assert.Nil(t, err, err)
  294. assert.Equal(t, http.StatusInternalServerError, response.StatusCode)
  295. defer response.Body.Close()
  296. var message models.ErrorResponse
  297. err = json.NewDecoder(response.Body).Decode(&message)
  298. assert.Nil(t, err, err)
  299. assert.Equal(t, http.StatusInternalServerError, message.Code)
  300. assert.Equal(t, "Interface cannot be empty", message.Message)
  301. })
  302. }
  303. func TestDeleteGateway(t *testing.T) {
  304. setup(t)
  305. response, err := api(t, "", http.MethodDelete, baseURL+"/api/nodes/skynet/01:02:03:04:05:06/deletegateway", "secretkey")
  306. assert.Nil(t, err, err)
  307. assert.Equal(t, http.StatusOK, response.StatusCode)
  308. defer response.Body.Close()
  309. var message models.Node
  310. err = json.NewDecoder(response.Body).Decode(&message)
  311. assert.Nil(t, err, err)
  312. assert.False(t, message.IsGateway)
  313. }
  314. func TestUncordonNode(t *testing.T) {
  315. setup(t)
  316. response, err := api(t, "", http.MethodPost, baseURL+"/api/nodes/skynet/01:02:03:04:05:06/approve", "secretkey")
  317. assert.Nil(t, err, err)
  318. assert.Equal(t, http.StatusOK, response.StatusCode)
  319. defer response.Body.Close()
  320. var message string
  321. err = json.NewDecoder(response.Body).Decode(&message)
  322. assert.Nil(t, err, err)
  323. assert.Equal(t, "SUCCESS", message)
  324. }
  325. func TestCreateNode(t *testing.T) {
  326. setup(t)
  327. key := createAccessKey(t)
  328. t.Run("NodeExists", func(t *testing.T) {
  329. var node models.Node
  330. node.AccessKey = key.Value
  331. node.MacAddress = "01:02:03:04:05:06"
  332. node.Name = "myNode"
  333. node.PublicKey = "DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34="
  334. node.Password = "tobedetermined"
  335. node.LocalAddress = "192.168.0.1"
  336. node.Endpoint = "10.100.100.4"
  337. response, err := api(t, node, http.MethodPost, "http://localhost:8081:/api/nodes/skynet", "secretkey")
  338. assert.Nil(t, err, err)
  339. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  340. defer response.Body.Close()
  341. var message models.ErrorResponse
  342. err = json.NewDecoder(response.Body).Decode(&message)
  343. assert.Nil(t, err, err)
  344. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  345. assert.Contains(t, message.Message, "Field validation for 'MacAddress' failed on the 'macaddress_unique' tag")
  346. })
  347. t.Run("BadKey", func(t *testing.T) {
  348. deleteNode(t)
  349. var node models.Node
  350. node.AccessKey = "badkey"
  351. node.MacAddress = "01:02:03:04:05:06"
  352. node.Name = "myNode"
  353. node.PublicKey = "DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34="
  354. node.Password = "tobedetermined"
  355. node.LocalAddress = "192.168.0.1"
  356. node.Endpoint = "10.100.100.4"
  357. response, err := api(t, node, http.MethodPost, "http://localhost:8081:/api/nodes/skynet", "secretkey")
  358. assert.Nil(t, err, err)
  359. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  360. defer response.Body.Close()
  361. var message models.ErrorResponse
  362. err = json.NewDecoder(response.Body).Decode(&message)
  363. assert.Nil(t, err, err)
  364. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  365. assert.Contains(t, message.Message, "W1R3: Key invalid, or none provided")
  366. })
  367. t.Run("BadMac", func(t *testing.T) {
  368. var node models.Node
  369. node.AccessKey = key.Value
  370. node.MacAddress = "badmac"
  371. node.Name = "myNode"
  372. node.PublicKey = "DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34="
  373. node.Password = "tobedetermined"
  374. node.LocalAddress = "192.168.0.1"
  375. node.Endpoint = "10.100.100.4"
  376. response, err := api(t, node, http.MethodPost, "http://localhost:8081:/api/nodes/skynet", "secretkey")
  377. assert.Nil(t, err, err)
  378. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  379. defer response.Body.Close()
  380. var message models.ErrorResponse
  381. err = json.NewDecoder(response.Body).Decode(&message)
  382. assert.Nil(t, err, err)
  383. assert.Equal(t, http.StatusBadRequest, message.Code)
  384. assert.Contains(t, message.Message, "Field validation for 'MacAddress' failed on the 'macaddress_valid' tag")
  385. })
  386. t.Run("BadPublicKey", func(t *testing.T) {
  387. var node models.Node
  388. node.AccessKey = key.Value
  389. node.MacAddress = "01:02:03:04:05:06"
  390. node.Name = "myNode"
  391. node.PublicKey = "xxx"
  392. node.Password = "tobedetermined"
  393. node.LocalAddress = "192.168.0.1"
  394. node.Endpoint = "10.100.100.4"
  395. response, err := api(t, node, http.MethodPost, "http://localhost:8081:/api/nodes/skynet", "secretkey")
  396. assert.Nil(t, err, err)
  397. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  398. defer response.Body.Close()
  399. var message models.ErrorResponse
  400. err = json.NewDecoder(response.Body).Decode(&message)
  401. assert.Nil(t, err, err)
  402. assert.Equal(t, http.StatusBadRequest, message.Code)
  403. assert.Contains(t, message.Message, "Field validation for 'PublicKey' failed")
  404. })
  405. t.Run("BadPass", func(t *testing.T) {
  406. var node models.Node
  407. node.AccessKey = key.Value
  408. node.MacAddress = "01:02:03:04:05:06"
  409. node.Name = "myNode"
  410. node.PublicKey = "DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34="
  411. node.Password = ""
  412. node.LocalAddress = "192.168.0.1"
  413. node.Endpoint = "10.100.100.4"
  414. response, err := api(t, node, http.MethodPost, "http://localhost:8081:/api/nodes/skynet", "secretkey")
  415. assert.Nil(t, err, err)
  416. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  417. defer response.Body.Close()
  418. var message models.ErrorResponse
  419. err = json.NewDecoder(response.Body).Decode(&message)
  420. assert.Nil(t, err, err)
  421. assert.Equal(t, http.StatusBadRequest, message.Code)
  422. assert.Contains(t, message.Message, "Error:Field validation for 'Password' failed")
  423. })
  424. t.Run("BadLocalAddress", func(t *testing.T) {
  425. var node models.Node
  426. node.AccessKey = key.Value
  427. node.MacAddress = "01:02:03:04:05:06"
  428. node.Name = "myNode"
  429. node.PublicKey = "DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34="
  430. node.Password = "tobedetermined"
  431. node.LocalAddress = "192.168.300.1"
  432. node.Endpoint = "10.100.100.4"
  433. response, err := api(t, node, http.MethodPost, "http://localhost:8081:/api/nodes/skynet", "secretkey")
  434. assert.Nil(t, err, err)
  435. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  436. defer response.Body.Close()
  437. var message models.ErrorResponse
  438. err = json.NewDecoder(response.Body).Decode(&message)
  439. assert.Nil(t, err, err)
  440. assert.Equal(t, http.StatusBadRequest, message.Code)
  441. assert.Contains(t, message.Message, "Field validation for 'LocalAddress' failed")
  442. })
  443. t.Run("BadEndpoint", func(t *testing.T) {
  444. var node models.Node
  445. node.AccessKey = key.Value
  446. node.MacAddress = "01:02:03:04:05:06"
  447. node.Name = "myNode"
  448. node.PublicKey = "DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34="
  449. node.Password = "tobedetermined"
  450. node.LocalAddress = "192.168.0.1"
  451. node.Endpoint = "10.100.300.4"
  452. response, err := api(t, node, http.MethodPost, "http://localhost:8081:/api/nodes/skynet", "secretkey")
  453. assert.Nil(t, err, err)
  454. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  455. defer response.Body.Close()
  456. var message models.ErrorResponse
  457. err = json.NewDecoder(response.Body).Decode(&message)
  458. assert.Nil(t, err, err)
  459. assert.Equal(t, http.StatusBadRequest, message.Code)
  460. assert.Contains(t, message.Message, "Field validation for 'Endpoint' failed")
  461. })
  462. t.Run("NetworkDoesNotExist", func(t *testing.T) {
  463. deleteNetworks(t)
  464. var node models.Node
  465. node.AccessKey = key.Value
  466. node.MacAddress = "01:02:03:04:05:06"
  467. node.Name = "myNode"
  468. node.PublicKey = "DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34="
  469. node.Password = "tobedetermined"
  470. node.LocalAddress = "192.168.0.1"
  471. node.Endpoint = "10.100.100.4"
  472. response, err := api(t, node, http.MethodPost, "http://localhost:8081:/api/nodes/skynet", "secretkey")
  473. assert.Nil(t, err, err)
  474. assert.Equal(t, http.StatusNotFound, response.StatusCode)
  475. defer response.Body.Close()
  476. var message models.ErrorResponse
  477. err = json.NewDecoder(response.Body).Decode(&message)
  478. assert.Nil(t, err, err)
  479. assert.Equal(t, http.StatusNotFound, message.Code)
  480. assert.Contains(t, "W1R3: Network does not exist! ", message.Message)
  481. })
  482. t.Run("Valid", func(t *testing.T) {
  483. deleteNetworks(t)
  484. createNetwork(t)
  485. key := createAccessKey(t)
  486. var node models.Node
  487. node.AccessKey = key.Value
  488. node.Address = "10.200.200.1"
  489. node.MacAddress = "01:02:03:04:05:06"
  490. node.Name = "myNode"
  491. node.PublicKey = "DM5qhLAE20PG9BbfBCger+Ac9D2NDOwCtY1rbYDLf34="
  492. node.Password = "tobedetermined"
  493. node.LocalAddress = "192.168.0.1"
  494. node.Endpoint = "10.100.100.4"
  495. response, err := api(t, node, http.MethodPost, "http://localhost:8081:/api/nodes/skynet", "secretkey")
  496. assert.Nil(t, err, err)
  497. assert.Equal(t, http.StatusOK, response.StatusCode)
  498. defer response.Body.Close()
  499. var message models.Node
  500. err = json.NewDecoder(response.Body).Decode(&message)
  501. assert.Nil(t, err, err)
  502. assert.Equal(t, node.Name, message.Name)
  503. })
  504. }
  505. func TestGetLastModified(t *testing.T) {
  506. deleteNetworks(t)
  507. createNetwork(t)
  508. t.Run("Valid", func(t *testing.T) {
  509. response, err := api(t, "", http.MethodGet, baseURL+"/api/nodes/adm/skynet/lastmodified", "secretkey")
  510. assert.Nil(t, err, err)
  511. assert.Equal(t, http.StatusOK, response.StatusCode)
  512. })
  513. deleteNetworks(t)
  514. t.Run("NoNetwork", func(t *testing.T) {
  515. response, err := api(t, "", http.MethodGet, baseURL+"/api/nodes/adm/skynet/lastmodified", "secretkey")
  516. assert.Nil(t, err, err)
  517. assert.Equal(t, http.StatusNotFound, response.StatusCode)
  518. })
  519. }
  520. func TestNodeAuthenticate(t *testing.T) {
  521. setup(t)
  522. t.Run("Valid", func(t *testing.T) {
  523. var authRequest models.AuthParams
  524. authRequest.MacAddress = "01:02:03:04:05:06"
  525. authRequest.Password = "tobedetermined"
  526. response, err := api(t, authRequest, http.MethodPost, "http://localhost:8081:/api/nodes/adm/skynet/authenticate", "")
  527. assert.Nil(t, err, err)
  528. assert.Equal(t, http.StatusOK, response.StatusCode)
  529. defer response.Body.Close()
  530. var message models.SuccessResponse
  531. err = json.NewDecoder(response.Body).Decode(&message)
  532. assert.Nil(t, err, err)
  533. assert.Equal(t, http.StatusOK, message.Code)
  534. assert.Equal(t, "W1R3: Device 01:02:03:04:05:06 Authorized", message.Message)
  535. })
  536. t.Run("MacEmpty", func(t *testing.T) {
  537. var authRequest models.AuthParams
  538. authRequest.MacAddress = ""
  539. authRequest.Password = "tobedetermined"
  540. response, err := api(t, authRequest, http.MethodPost, "http://localhost:8081:/api/nodes/adm/skynet/authenticate", "")
  541. assert.Nil(t, err, err)
  542. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  543. defer response.Body.Close()
  544. var message models.SuccessResponse
  545. err = json.NewDecoder(response.Body).Decode(&message)
  546. assert.Nil(t, err, err)
  547. assert.Equal(t, http.StatusBadRequest, message.Code)
  548. assert.Equal(t, "W1R3: MacAddress can't be empty", message.Message)
  549. })
  550. t.Run("EmptyPass", func(t *testing.T) {
  551. var authRequest models.AuthParams
  552. authRequest.MacAddress = "01:02:03:04:05:06"
  553. authRequest.Password = ""
  554. response, err := api(t, authRequest, http.MethodPost, "http://localhost:8081:/api/nodes/adm/skynet/authenticate", "")
  555. assert.Nil(t, err, err)
  556. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  557. defer response.Body.Close()
  558. var message models.SuccessResponse
  559. err = json.NewDecoder(response.Body).Decode(&message)
  560. assert.Nil(t, err, err)
  561. assert.Equal(t, http.StatusBadRequest, message.Code)
  562. assert.Equal(t, "W1R3: Password can't be empty", message.Message)
  563. })
  564. t.Run("BadPass", func(t *testing.T) {
  565. var authRequest models.AuthParams
  566. authRequest.MacAddress = "01:02:03:04:05:06"
  567. authRequest.Password = "badpass"
  568. response, err := api(t, authRequest, http.MethodPost, "http://localhost:8081:/api/nodes/adm/skynet/authenticate", "")
  569. assert.Nil(t, err, err)
  570. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  571. defer response.Body.Close()
  572. var message models.SuccessResponse
  573. err = json.NewDecoder(response.Body).Decode(&message)
  574. assert.Nil(t, err, err)
  575. assert.Equal(t, http.StatusBadRequest, message.Code)
  576. assert.Equal(t, "crypto/bcrypt: hashedPassword is not the hash of the given password", message.Message)
  577. })
  578. t.Run("BadMac", func(t *testing.T) {
  579. var authRequest models.AuthParams
  580. authRequest.MacAddress = "01:02:03:04:05:07"
  581. authRequest.Password = "tobedetermined"
  582. response, err := api(t, authRequest, http.MethodPost, "http://localhost:8081:/api/nodes/adm/skynet/authenticate", "")
  583. assert.Nil(t, err, err)
  584. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  585. defer response.Body.Close()
  586. var message models.SuccessResponse
  587. err = json.NewDecoder(response.Body).Decode(&message)
  588. assert.Nil(t, err, err)
  589. assert.Equal(t, http.StatusBadRequest, message.Code)
  590. assert.Equal(t, "mongo: no documents in result", message.Message)
  591. })
  592. }