group_test.go 25 KB

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