group_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "testing"
  7. "github.com/gravitl/netmaker/models"
  8. "github.com/stretchr/testify/assert"
  9. "go.mongodb.org/mongo-driver/mongo"
  10. )
  11. func TestCreateNetwork(t *testing.T) {
  12. network := models.Network{}
  13. network.NetID = "skynet"
  14. network.AddressRange = "10.71.0.0/16"
  15. deleteNetworks(t)
  16. t.Run("CreateNetwork", func(t *testing.T) {
  17. response, err := api(t, network, http.MethodPost, baseURL+"/api/networks", "secretkey")
  18. assert.Nil(t, err, err)
  19. assert.Equal(t, http.StatusOK, response.StatusCode)
  20. })
  21. t.Run("InvalidToken", func(t *testing.T) {
  22. response, err := api(t, network, http.MethodPost, baseURL+"/api/networks", "badkey")
  23. assert.Nil(t, err, err)
  24. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  25. defer response.Body.Close()
  26. var message models.ErrorResponse
  27. err = json.NewDecoder(response.Body).Decode(&message)
  28. assert.Nil(t, err, err)
  29. assert.Equal(t, http.StatusUnauthorized, message.Code)
  30. assert.Equal(t, "W1R3: You are unauthorized to access this endpoint.", message.Message)
  31. })
  32. t.Run("BadName", func(t *testing.T) {
  33. //issue #42
  34. t.Skip()
  35. })
  36. t.Run("BadAddress", func(t *testing.T) {
  37. //issue #42
  38. t.Skip()
  39. })
  40. t.Run("DuplicateNetwork", func(t *testing.T) {
  41. //issue #42
  42. t.Skip()
  43. })
  44. }
  45. func TestGetNetworks(t *testing.T) {
  46. t.Run("ValidToken", func(t *testing.T) {
  47. response, err := api(t, "", http.MethodGet, baseURL+"/api/networks", "secretkey")
  48. assert.Nil(t, err, err)
  49. defer response.Body.Close()
  50. assert.Equal(t, http.StatusOK, response.StatusCode)
  51. err = json.NewDecoder(response.Body).Decode(&Networks)
  52. assert.Nil(t, err, err)
  53. })
  54. t.Run("InvalidToken", func(t *testing.T) {
  55. response, err := api(t, "", http.MethodGet, baseURL+"/api/networks", "badkey")
  56. assert.Nil(t, err, err)
  57. defer response.Body.Close()
  58. var message models.ErrorResponse
  59. err = json.NewDecoder(response.Body).Decode(&message)
  60. assert.Nil(t, err, err)
  61. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  62. assert.Equal(t, http.StatusUnauthorized, message.Code)
  63. assert.Equal(t, "W1R3: You are unauthorized to access this endpoint.", message.Message)
  64. })
  65. }
  66. func TestGetNetwork(t *testing.T) {
  67. t.Run("ValidToken", func(t *testing.T) {
  68. var network models.Network
  69. response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/skynet", "secretkey")
  70. assert.Nil(t, err, err)
  71. defer response.Body.Close()
  72. assert.Equal(t, http.StatusOK, response.StatusCode)
  73. err = json.NewDecoder(response.Body).Decode(&network)
  74. assert.Nil(t, err, err)
  75. // --- needs fixing ------ returns previous name
  76. //assert.Equal(t, "skynet", network.DisplayName)
  77. })
  78. t.Run("InvalidToken", func(t *testing.T) {
  79. response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/skynet", "badkey")
  80. assert.Nil(t, err, err)
  81. defer response.Body.Close()
  82. var message models.ErrorResponse
  83. err = json.NewDecoder(response.Body).Decode(&message)
  84. assert.Nil(t, err, err)
  85. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  86. assert.Equal(t, http.StatusUnauthorized, message.Code)
  87. assert.Equal(t, "W1R3: You are unauthorized to access this endpoint.", message.Message)
  88. })
  89. t.Run("InvalidNetwork", func(t *testing.T) {
  90. response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/badnetwork", "secretkey")
  91. assert.Nil(t, err, err)
  92. defer response.Body.Close()
  93. var message models.ErrorResponse
  94. err = json.NewDecoder(response.Body).Decode(&message)
  95. assert.Nil(t, err, err)
  96. assert.Equal(t, "W1R3: This network does not exist.", message.Message)
  97. assert.Equal(t, http.StatusNotFound, response.StatusCode)
  98. })
  99. }
  100. func TestGetnetworkNodeNumber(t *testing.T) {
  101. t.Skip()
  102. //not part of api anymore
  103. t.Run("ValidKey", func(t *testing.T) {
  104. response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/skynet/numnodes", "secretkey")
  105. assert.Nil(t, err, err)
  106. defer response.Body.Close()
  107. var message int
  108. err = json.NewDecoder(response.Body).Decode(&message)
  109. assert.Nil(t, err, err)
  110. //assert.Equal(t, "W1R3: This network does not exist.", message.Message)
  111. assert.Equal(t, http.StatusOK, response.StatusCode)
  112. })
  113. t.Run("InvalidKey", func(t *testing.T) {
  114. response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/skynet/numnodes", "badkey")
  115. assert.Nil(t, err, err)
  116. defer response.Body.Close()
  117. var message models.ErrorResponse
  118. err = json.NewDecoder(response.Body).Decode(&message)
  119. assert.Nil(t, err, err)
  120. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  121. assert.Equal(t, http.StatusUnauthorized, message.Code)
  122. assert.Equal(t, "W1R3: You are unauthorized to access this endpoint.", message.Message)
  123. })
  124. t.Run("Badnetwork", func(t *testing.T) {
  125. response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/badnetwork/numnodes", "secretkey")
  126. assert.Nil(t, err, err)
  127. defer response.Body.Close()
  128. var message models.ErrorResponse
  129. err = json.NewDecoder(response.Body).Decode(&message)
  130. assert.Nil(t, err, err)
  131. assert.Equal(t, "W1R3: This network does not exist.", message.Message)
  132. assert.Equal(t, http.StatusNotFound, response.StatusCode)
  133. })
  134. }
  135. func TestDeletenetwork(t *testing.T) {
  136. t.Run("InvalidKey", func(t *testing.T) {
  137. response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/skynet", "badkey")
  138. assert.Nil(t, err, err)
  139. defer response.Body.Close()
  140. var message models.ErrorResponse
  141. err = json.NewDecoder(response.Body).Decode(&message)
  142. assert.Nil(t, err, err)
  143. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  144. assert.Equal(t, http.StatusUnauthorized, message.Code)
  145. assert.Equal(t, "W1R3: You are unauthorized to access this endpoint.", message.Message)
  146. })
  147. t.Run("ValidKey", func(t *testing.T) {
  148. response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/skynet", "secretkey")
  149. assert.Nil(t, err, err)
  150. defer response.Body.Close()
  151. var message mongo.DeleteResult
  152. err = json.NewDecoder(response.Body).Decode(&message)
  153. assert.Nil(t, err, err)
  154. assert.Equal(t, http.StatusOK, response.StatusCode)
  155. assert.Equal(t, int64(1), message.DeletedCount)
  156. })
  157. t.Run("Badnetwork", func(t *testing.T) {
  158. response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/badnetwork", "secretkey")
  159. assert.Nil(t, err, err)
  160. defer response.Body.Close()
  161. var message models.ErrorResponse
  162. err = json.NewDecoder(response.Body).Decode(&message)
  163. assert.Nil(t, err, err)
  164. assert.Equal(t, "W1R3: This network does not exist.", message.Message)
  165. assert.Equal(t, http.StatusNotFound, response.StatusCode)
  166. })
  167. t.Run("NodesExist", func(t *testing.T) {
  168. t.Skip()
  169. })
  170. }
  171. func TestCreateAccessKey(t *testing.T) {
  172. if !networkExists(t) {
  173. createNetwork(t)
  174. }
  175. key := models.AccessKey{}
  176. key.Name = "skynet"
  177. key.Uses = 10
  178. t.Run("MultiUse", func(t *testing.T) {
  179. response, err := api(t, key, http.MethodPost, baseURL+"/api/networks/skynet/keys", "secretkey")
  180. assert.Nil(t, err, err)
  181. assert.Equal(t, http.StatusOK, response.StatusCode)
  182. defer response.Body.Close()
  183. message, err := ioutil.ReadAll(response.Body)
  184. assert.Nil(t, err, err)
  185. assert.NotNil(t, message, message)
  186. returnedkey := getKey(t, key.Name)
  187. assert.Equal(t, key.Name, returnedkey.Name)
  188. assert.Equal(t, key.Uses, returnedkey.Uses)
  189. })
  190. deleteKey(t, "skynet", "skynet")
  191. t.Run("ZeroUse", func(t *testing.T) {
  192. //t.Skip()
  193. key.Uses = 0
  194. response, err := api(t, key, http.MethodPost, baseURL+"/api/networks/skynet/keys", "secretkey")
  195. assert.Nil(t, err, err)
  196. assert.Equal(t, http.StatusOK, response.StatusCode)
  197. defer response.Body.Close()
  198. message, err := ioutil.ReadAll(response.Body)
  199. assert.Nil(t, err, err)
  200. assert.NotNil(t, message, message)
  201. returnedkey := getKey(t, key.Name)
  202. assert.Equal(t, key.Name, returnedkey.Name)
  203. assert.Equal(t, 1, returnedkey.Uses)
  204. })
  205. t.Run("DuplicateAccessKey", func(t *testing.T) {
  206. //this is allowed I think it should fail fail
  207. response, err := api(t, key, http.MethodPost, baseURL+"/api/networks/skynet/keys", "secretkey")
  208. assert.Nil(t, err, err)
  209. assert.Equal(t, http.StatusOK, response.StatusCode)
  210. deleteKey(t, key.Name, "skynet")
  211. })
  212. t.Run("InvalidToken", func(t *testing.T) {
  213. response, err := api(t, key, http.MethodPost, baseURL+"/api/networks/skynet/keys", "badkey")
  214. assert.Nil(t, err, err)
  215. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  216. defer response.Body.Close()
  217. var message models.ErrorResponse
  218. err = json.NewDecoder(response.Body).Decode(&message)
  219. assert.Nil(t, err, err)
  220. assert.Equal(t, http.StatusUnauthorized, message.Code)
  221. assert.Equal(t, "W1R3: You are unauthorized to access this endpoint.", message.Message)
  222. })
  223. t.Run("Badnetwork", func(t *testing.T) {
  224. response, err := api(t, key, http.MethodPost, baseURL+"/api/networks/badnetwork/keys", "secretkey")
  225. assert.Nil(t, err, err)
  226. defer response.Body.Close()
  227. var message models.ErrorResponse
  228. err = json.NewDecoder(response.Body).Decode(&message)
  229. assert.Nil(t, err, err)
  230. assert.Equal(t, "W1R3: This network does not exist.", message.Message)
  231. assert.Equal(t, http.StatusNotFound, response.StatusCode)
  232. })
  233. }
  234. func TestDeleteKey(t *testing.T) {
  235. t.Run("KeyValid", func(t *testing.T) {
  236. //fails -- deletecount not returned
  237. response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/skynet/keys/skynet", "secretkey")
  238. assert.Nil(t, err, err)
  239. defer response.Body.Close()
  240. //var message mongo.DeleteResult
  241. var messages []models.AccessKey
  242. err = json.NewDecoder(response.Body).Decode(&messages)
  243. assert.Nil(t, err, err)
  244. assert.Equal(t, http.StatusOK, response.StatusCode)
  245. for _, message := range messages {
  246. assert.Equal(t, "skynet", message.Name)
  247. }
  248. })
  249. t.Run("InValidKey", func(t *testing.T) {
  250. t.Skip()
  251. //responds ok, will nil record returned.. should be an error.
  252. response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/skynet/keys/badkey", "secretkey")
  253. assert.Nil(t, err, err)
  254. defer response.Body.Close()
  255. var message models.ErrorResponse
  256. err = json.NewDecoder(response.Body).Decode(&message)
  257. assert.Nil(t, err, err)
  258. assert.Equal(t, "W1R3: This key does not exist.", message.Message)
  259. assert.Equal(t, http.StatusNotFound, response.StatusCode)
  260. })
  261. t.Run("KeyInValidnetwork", func(t *testing.T) {
  262. response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/badnetwork/keys/skynet", "secretkey")
  263. assert.Nil(t, err, err)
  264. defer response.Body.Close()
  265. var message models.ErrorResponse
  266. err = json.NewDecoder(response.Body).Decode(&message)
  267. assert.Nil(t, err, err)
  268. assert.Equal(t, "W1R3: This network does not exist.", message.Message)
  269. assert.Equal(t, http.StatusNotFound, response.StatusCode)
  270. })
  271. t.Run("InvalidCredentials", func(t *testing.T) {
  272. response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/skynet/keys/skynet", "badkey")
  273. assert.Nil(t, err, err)
  274. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  275. defer response.Body.Close()
  276. var message models.ErrorResponse
  277. err = json.NewDecoder(response.Body).Decode(&message)
  278. assert.Nil(t, err, err)
  279. assert.Equal(t, http.StatusUnauthorized, message.Code)
  280. assert.Equal(t, "W1R3: You are unauthorized to access this endpoint.", message.Message)
  281. })
  282. }
  283. func TestGetKeys(t *testing.T) {
  284. createKey(t)
  285. t.Run("Valid", func(t *testing.T) {
  286. response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/skynet/keys", "secretkey")
  287. assert.Nil(t, err, err)
  288. assert.Equal(t, http.StatusOK, response.StatusCode)
  289. defer response.Body.Close()
  290. var keys []models.AccessKey
  291. err = json.NewDecoder(response.Body).Decode(&keys)
  292. assert.Nil(t, err, err)
  293. })
  294. //deletekeys
  295. t.Run("Invalidnetwork", func(t *testing.T) {
  296. response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/badnetwork/keys", "secretkey")
  297. assert.Nil(t, err, err)
  298. defer response.Body.Close()
  299. var message models.ErrorResponse
  300. err = json.NewDecoder(response.Body).Decode(&message)
  301. assert.Nil(t, err, err)
  302. assert.Equal(t, "W1R3: This network does not exist.", message.Message)
  303. assert.Equal(t, http.StatusNotFound, response.StatusCode)
  304. })
  305. t.Run("InvalidCredentials", func(t *testing.T) {
  306. response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/skynet/keys", "badkey")
  307. assert.Nil(t, err, err)
  308. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  309. defer response.Body.Close()
  310. var message models.ErrorResponse
  311. err = json.NewDecoder(response.Body).Decode(&message)
  312. assert.Nil(t, err, err)
  313. assert.Equal(t, http.StatusUnauthorized, message.Code)
  314. assert.Equal(t, "W1R3: You are unauthorized to access this endpoint.", message.Message)
  315. })
  316. }
  317. func TestUpdatenetwork(t *testing.T) {
  318. //ensure we are working with known networks
  319. deleteNetworks(t)
  320. createNetwork(t)
  321. var returnedNetwork models.Network
  322. t.Run("UpdateNetID", func(t *testing.T) {
  323. type Network struct {
  324. NetID string
  325. }
  326. var network Network
  327. network.NetID = "wirecat"
  328. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  329. assert.Nil(t, err, err)
  330. assert.Equal(t, http.StatusOK, response.StatusCode)
  331. defer response.Body.Close()
  332. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  333. assert.Nil(t, err, err)
  334. //returns previous value not the updated value
  335. // ----- needs fixing -----
  336. //assert.Equal(t, network.NetID, returnedNetwork.NetID)
  337. })
  338. t.Run("NetIDInvalidCredentials", func(t *testing.T) {
  339. type Network struct {
  340. NetID string
  341. }
  342. var network Network
  343. network.NetID = "wirecat"
  344. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "badkey")
  345. assert.Nil(t, err, err)
  346. var message models.ErrorResponse
  347. err = json.NewDecoder(response.Body).Decode(&message)
  348. assert.Nil(t, err, err)
  349. assert.Equal(t, http.StatusUnauthorized, message.Code)
  350. assert.Equal(t, "W1R3: You are unauthorized to access this endpoint.", message.Message)
  351. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  352. })
  353. t.Run("Invalidnetwork", func(t *testing.T) {
  354. type Network struct {
  355. NetID string
  356. }
  357. var network Network
  358. network.NetID = "wirecat"
  359. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/badnetwork", "secretkey")
  360. assert.Nil(t, err, err)
  361. defer response.Body.Close()
  362. var message models.ErrorResponse
  363. err = json.NewDecoder(response.Body).Decode(&message)
  364. assert.Nil(t, err, err)
  365. assert.Equal(t, http.StatusNotFound, message.Code)
  366. assert.Equal(t, "W1R3: This network does not exist.", message.Message)
  367. assert.Equal(t, http.StatusNotFound, response.StatusCode)
  368. })
  369. t.Run("UpdateNetIDTooLong", func(t *testing.T) {
  370. // ---- needs fixing -----
  371. // succeeds for some reason
  372. t.Skip()
  373. type Network struct {
  374. NetID string
  375. }
  376. var network Network
  377. network.NetID = "wirecat-skynet"
  378. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  379. assert.Nil(t, err, err)
  380. assert.Equal(t, http.StatusUnprocessableEntity, response.StatusCode)
  381. })
  382. t.Run("UpdateAddress", func(t *testing.T) {
  383. type Network struct {
  384. AddressRange string
  385. }
  386. var network Network
  387. network.AddressRange = "10.0.0.1/24"
  388. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  389. assert.Nil(t, err, err)
  390. assert.Equal(t, http.StatusOK, response.StatusCode)
  391. defer response.Body.Close()
  392. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  393. assert.Nil(t, err, err)
  394. assert.Equal(t, network.AddressRange, returnedNetwork.AddressRange)
  395. })
  396. t.Run("UpdateAddressInvalid", func(t *testing.T) {
  397. type Network struct {
  398. AddressRange string
  399. }
  400. var network Network
  401. network.AddressRange = "10.0.0.1/36"
  402. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  403. assert.Nil(t, err, err)
  404. assert.Equal(t, http.StatusInternalServerError, response.StatusCode)
  405. defer response.Body.Close()
  406. var message models.ErrorResponse
  407. err = json.NewDecoder(response.Body).Decode(&message)
  408. assert.Nil(t, err, err)
  409. assert.Equal(t, http.StatusInternalServerError, message.Code)
  410. assert.Contains(t, message.Message, "Invalid Range")
  411. })
  412. t.Run("UpdateDisplayName", func(t *testing.T) {
  413. type Network struct {
  414. DisplayName string
  415. }
  416. var network Network
  417. network.DisplayName = "wirecat"
  418. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  419. assert.Nil(t, err, err)
  420. assert.Equal(t, http.StatusOK, response.StatusCode)
  421. defer response.Body.Close()
  422. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  423. assert.Nil(t, err, err)
  424. assert.Equal(t, network.DisplayName, returnedNetwork.DisplayName)
  425. })
  426. t.Run("UpdateDisplayNameInvalidName", func(t *testing.T) {
  427. // -----needs fixing ----
  428. // fails silently
  429. t.Skip()
  430. type Network struct {
  431. DisplayName string
  432. }
  433. var network Network
  434. //create name that is longer than 100 chars
  435. name := ""
  436. for i := 0; i < 101; i++ {
  437. name = name + "a"
  438. }
  439. network.DisplayName = name
  440. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  441. assert.Nil(t, err, err)
  442. var message models.ErrorResponse
  443. err = json.NewDecoder(response.Body).Decode(&message)
  444. assert.Nil(t, err, err)
  445. assert.Equal(t, http.StatusUnprocessableEntity, message.Code)
  446. assert.Equal(t, "W1R3: Field validation for 'DisplayName' failed.", message.Message)
  447. assert.Equal(t, http.StatusUnprocessableEntity, response.StatusCode)
  448. })
  449. t.Run("UpdateInterface", func(t *testing.T) {
  450. type Network struct {
  451. DefaultInterface string
  452. }
  453. var network Network
  454. network.DefaultInterface = "netmaker"
  455. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  456. assert.Nil(t, err, err)
  457. assert.Equal(t, http.StatusOK, response.StatusCode)
  458. defer response.Body.Close()
  459. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  460. assert.Nil(t, err, err)
  461. assert.Equal(t, network.DefaultInterface, returnedNetwork.DefaultInterface)
  462. })
  463. t.Run("UpdateListenPort", func(t *testing.T) {
  464. type Network struct {
  465. DefaultListenPort int32
  466. }
  467. var network Network
  468. network.DefaultListenPort = 6000
  469. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  470. assert.Nil(t, err, err)
  471. assert.Equal(t, http.StatusOK, response.StatusCode)
  472. defer response.Body.Close()
  473. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  474. assert.Nil(t, err, err)
  475. assert.Equal(t, network.DefaultListenPort, returnedNetwork.DefaultListenPort)
  476. })
  477. t.Run("UpdateListenPortInvalidPort", func(t *testing.T) {
  478. // ---needs fixing -----
  479. // value is updated anyways
  480. t.Skip()
  481. type Network struct {
  482. DefaultListenPort int32
  483. }
  484. var network Network
  485. network.DefaultListenPort = 65540
  486. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  487. assert.Nil(t, err, err)
  488. var message models.ErrorResponse
  489. err = json.NewDecoder(response.Body).Decode(&message)
  490. assert.Nil(t, err, err)
  491. assert.Equal(t, http.StatusUnprocessableEntity, message.Code)
  492. assert.Equal(t, "W1R3: Field validation for 'DefaultListenPort' failed.", message.Message)
  493. assert.Equal(t, http.StatusUnprocessableEntity, response.StatusCode)
  494. })
  495. t.Run("UpdatePostUP", func(t *testing.T) {
  496. type Network struct {
  497. DefaultPostUp string
  498. }
  499. var network Network
  500. network.DefaultPostUp = "sudo wg add-conf wc-netmaker /etc/wireguard/peers/conf"
  501. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  502. assert.Nil(t, err, err)
  503. assert.Equal(t, http.StatusOK, response.StatusCode)
  504. defer response.Body.Close()
  505. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  506. assert.Nil(t, err, err)
  507. assert.Equal(t, network.DefaultPostUp, returnedNetwork.DefaultPostUp)
  508. })
  509. t.Run("UpdatePreUp", func(t *testing.T) {
  510. // -------needs fixing ------
  511. // mismatch in models.Network between struc name and json/bson name
  512. // does not get updated.
  513. t.Skip()
  514. type Network struct {
  515. DefaultPostDown string
  516. }
  517. var network Network
  518. network.DefaultPostDown = "test string"
  519. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  520. assert.Nil(t, err, err)
  521. assert.Equal(t, http.StatusOK, response.StatusCode)
  522. defer response.Body.Close()
  523. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  524. assert.Nil(t, err, err)
  525. assert.Equal(t, network.DefaultPostDown, returnedNetwork.DefaultPostDown)
  526. })
  527. t.Run("UpdateKeepAlive", func(t *testing.T) {
  528. type Network struct {
  529. DefaultKeepalive int32
  530. }
  531. var network Network
  532. network.DefaultKeepalive = 60
  533. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  534. assert.Nil(t, err, err)
  535. assert.Equal(t, http.StatusOK, response.StatusCode)
  536. defer response.Body.Close()
  537. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  538. assert.Nil(t, err, err)
  539. assert.Equal(t, network.DefaultKeepalive, returnedNetwork.DefaultKeepalive)
  540. })
  541. t.Run("UpdateKeepAliveTooBig", func(t *testing.T) {
  542. //fails silently
  543. // ----- needs fixing -----
  544. t.Skip()
  545. type Network struct {
  546. DefaultKeepAlive int32
  547. }
  548. var network Network
  549. network.DefaultKeepAlive = 1001
  550. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  551. assert.Nil(t, err, err)
  552. var message models.ErrorResponse
  553. err = json.NewDecoder(response.Body).Decode(&message)
  554. assert.Nil(t, err, err)
  555. assert.Equal(t, http.StatusUnprocessableEntity, message.Code)
  556. assert.Equal(t, "W1R3: Field validation for 'DefaultKeepAlive' failed.", message.Message)
  557. assert.Equal(t, http.StatusUnprocessableEntity, response.StatusCode)
  558. })
  559. t.Run("UpdateSaveConfig", func(t *testing.T) {
  560. //causes panic
  561. t.Skip()
  562. type Network struct {
  563. DefaultSaveConfig *bool
  564. }
  565. var network Network
  566. value := false
  567. network.DefaultSaveConfig = &value
  568. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  569. assert.Nil(t, err, err)
  570. assert.Equal(t, http.StatusOK, response.StatusCode)
  571. defer response.Body.Close()
  572. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  573. assert.Nil(t, err, err)
  574. assert.Equal(t, *network.DefaultSaveConfig, *returnedNetwork.DefaultSaveConfig)
  575. })
  576. t.Run("UpdateManualSignUP", func(t *testing.T) {
  577. t.Skip()
  578. type Network struct {
  579. AllowManualSignUp *bool
  580. }
  581. var network Network
  582. value := true
  583. network.AllowManualSignUp = &value
  584. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  585. assert.Nil(t, err, err)
  586. assert.Equal(t, http.StatusOK, response.StatusCode)
  587. defer response.Body.Close()
  588. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  589. assert.Nil(t, err, err)
  590. assert.Equal(t, *network.AllowManualSignUp, *returnedNetwork.AllowManualSignUp)
  591. })
  592. t.Run("DefaultCheckInterval", func(t *testing.T) {
  593. //value is not returned in struct ---
  594. t.Skip()
  595. type Network struct {
  596. DefaultCheckInInterval int32
  597. }
  598. var network Network
  599. network.DefaultCheckInInterval = 6000
  600. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  601. assert.Nil(t, err, err)
  602. assert.Equal(t, http.StatusOK, response.StatusCode)
  603. defer response.Body.Close()
  604. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  605. assert.Nil(t, err, err)
  606. assert.Equal(t, network.DefaultCheckInInterval, returnedNetwork.DefaultCheckInInterval)
  607. })
  608. t.Run("DefaultCheckIntervalTooBig", func(t *testing.T) {
  609. //value is not returned in struct ---
  610. t.Skip()
  611. type Network struct {
  612. DefaultCheckInInterval int32
  613. }
  614. var network Network
  615. network.DefaultCheckInInterval = 100001
  616. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  617. assert.Nil(t, err, err)
  618. var message models.ErrorResponse
  619. err = json.NewDecoder(response.Body).Decode(&message)
  620. assert.Nil(t, err, err)
  621. assert.Equal(t, http.StatusUnprocessableEntity, message.Code)
  622. assert.Equal(t, "W1R3: Field validation for 'DefaultCheckInInterval' failed.", message.Message)
  623. assert.Equal(t, http.StatusUnprocessableEntity, response.StatusCode)
  624. })
  625. t.Run("MultipleFields", func(t *testing.T) {
  626. type Network struct {
  627. DisplayName string
  628. DefaultListenPort int32
  629. }
  630. var network Network
  631. network.DefaultListenPort = 7777
  632. network.DisplayName = "multi"
  633. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  634. assert.Nil(t, err, err)
  635. assert.Equal(t, http.StatusOK, response.StatusCode)
  636. defer response.Body.Close()
  637. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  638. assert.Nil(t, err, err)
  639. assert.Equal(t, network.DisplayName, returnedNetwork.DisplayName)
  640. assert.Equal(t, network.DefaultListenPort, returnedNetwork.DefaultListenPort)
  641. })
  642. }