group_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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.Skip()
  245. t.Run("KeyValid", func(t *testing.T) {
  246. //fails -- deletecount not returned
  247. response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/skynet/keys/skynet", "secretkey")
  248. assert.Nil(t, err, err)
  249. defer response.Body.Close()
  250. //var message mongo.DeleteResult
  251. var messages []models.AccessKey
  252. err = json.NewDecoder(response.Body).Decode(&messages)
  253. assert.Nil(t, err, err)
  254. assert.Equal(t, http.StatusOK, response.StatusCode)
  255. for _, message := range messages {
  256. assert.Equal(t, "skynet", message.Name)
  257. }
  258. })
  259. t.Run("InValidKey", func(t *testing.T) {
  260. t.Skip()
  261. //responds ok, will nil record returned.. should be an error.
  262. response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/skynet/keys/badkey", "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 key does not exist.", message.Message)
  269. assert.Equal(t, http.StatusNotFound, response.StatusCode)
  270. })
  271. t.Run("KeyInValidnetwork", func(t *testing.T) {
  272. response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/badnetwork/keys/skynet", "secretkey")
  273. assert.Nil(t, err, err)
  274. defer response.Body.Close()
  275. var message models.ErrorResponse
  276. err = json.NewDecoder(response.Body).Decode(&message)
  277. assert.Nil(t, err, err)
  278. assert.Equal(t, "W1R3: This network does not exist.", message.Message)
  279. assert.Equal(t, http.StatusNotFound, response.StatusCode)
  280. })
  281. t.Run("InvalidCredentials", func(t *testing.T) {
  282. response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/skynet/keys/skynet", "badkey")
  283. assert.Nil(t, err, err)
  284. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  285. defer response.Body.Close()
  286. var message models.ErrorResponse
  287. err = json.NewDecoder(response.Body).Decode(&message)
  288. assert.Nil(t, err, err)
  289. assert.Equal(t, http.StatusUnauthorized, message.Code)
  290. assert.Equal(t, "W1R3: You are unauthorized to access this endpoint.", message.Message)
  291. })
  292. }
  293. func TestGetKeys(t *testing.T) {
  294. t.Skip()
  295. createKey(t)
  296. t.Run("Valid", func(t *testing.T) {
  297. response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/skynet/keys", "secretkey")
  298. assert.Nil(t, err, err)
  299. assert.Equal(t, http.StatusOK, response.StatusCode)
  300. defer response.Body.Close()
  301. var keys []models.AccessKey
  302. err = json.NewDecoder(response.Body).Decode(&keys)
  303. assert.Nil(t, err, err)
  304. })
  305. //deletekeys
  306. t.Run("Invalidnetwork", func(t *testing.T) {
  307. response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/badnetwork/keys", "secretkey")
  308. assert.Nil(t, err, err)
  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, "W1R3: This network does not exist.", message.Message)
  314. assert.Equal(t, http.StatusNotFound, response.StatusCode)
  315. })
  316. t.Run("InvalidCredentials", func(t *testing.T) {
  317. response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/skynet/keys", "badkey")
  318. assert.Nil(t, err, err)
  319. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  320. defer response.Body.Close()
  321. var message models.ErrorResponse
  322. err = json.NewDecoder(response.Body).Decode(&message)
  323. assert.Nil(t, err, err)
  324. assert.Equal(t, http.StatusUnauthorized, message.Code)
  325. assert.Equal(t, "W1R3: You are unauthorized to access this endpoint.", message.Message)
  326. })
  327. }
  328. func TestUpdatenetwork(t *testing.T) {
  329. t.Skip()
  330. //ensure we are working with known networks
  331. deleteNetworks(t)
  332. createNetwork(t)
  333. var returnedNetwork models.Network
  334. t.Run("UpdateNetID", func(t *testing.T) {
  335. type Network struct {
  336. NetID string
  337. }
  338. var network Network
  339. network.NetID = "wirecat"
  340. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  341. assert.Nil(t, err, err)
  342. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  343. defer response.Body.Close()
  344. var message models.ErrorResponse
  345. err = json.NewDecoder(response.Body).Decode(&message)
  346. assert.Nil(t, err, err)
  347. assert.Equal(t, http.StatusBadRequest, message.Code)
  348. assert.Equal(t, "NetID is not editable", message.Message)
  349. })
  350. t.Run("NetIDInvalidCredentials", func(t *testing.T) {
  351. type Network struct {
  352. NetID string
  353. }
  354. var network Network
  355. network.NetID = "wirecat"
  356. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "badkey")
  357. assert.Nil(t, err, err)
  358. var message models.ErrorResponse
  359. err = json.NewDecoder(response.Body).Decode(&message)
  360. assert.Nil(t, err, err)
  361. assert.Equal(t, http.StatusUnauthorized, message.Code)
  362. assert.Equal(t, "W1R3: You are unauthorized to access this endpoint.", message.Message)
  363. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  364. })
  365. t.Run("Invalidnetwork", func(t *testing.T) {
  366. type Network struct {
  367. NetID string
  368. }
  369. var network Network
  370. network.NetID = "wirecat"
  371. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/badnetwork", "secretkey")
  372. assert.Nil(t, err, err)
  373. defer response.Body.Close()
  374. var message models.ErrorResponse
  375. err = json.NewDecoder(response.Body).Decode(&message)
  376. assert.Nil(t, err, err)
  377. assert.Equal(t, http.StatusNotFound, message.Code)
  378. assert.Equal(t, "W1R3: This network does not exist.", message.Message)
  379. assert.Equal(t, http.StatusNotFound, response.StatusCode)
  380. })
  381. t.Run("UpdateNetIDTooLong", func(t *testing.T) {
  382. // ---- needs fixing -----
  383. // succeeds for some reason
  384. t.Skip()
  385. type Network struct {
  386. NetID string
  387. }
  388. var network Network
  389. network.NetID = "wirecat-skynet"
  390. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  391. assert.Nil(t, err, err)
  392. assert.Equal(t, http.StatusUnprocessableEntity, response.StatusCode)
  393. })
  394. t.Run("UpdateAddress", func(t *testing.T) {
  395. type Network struct {
  396. AddressRange string
  397. }
  398. var network Network
  399. network.AddressRange = "10.0.0.1/24"
  400. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  401. assert.Nil(t, err, err)
  402. assert.Equal(t, http.StatusOK, response.StatusCode)
  403. defer response.Body.Close()
  404. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  405. assert.Nil(t, err, err)
  406. assert.Equal(t, network.AddressRange, returnedNetwork.AddressRange)
  407. })
  408. t.Run("UpdateAddressInvalid", func(t *testing.T) {
  409. type Network struct {
  410. AddressRange string
  411. }
  412. var network Network
  413. network.AddressRange = "10.0.0.1/36"
  414. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  415. assert.Nil(t, err, err)
  416. assert.Equal(t, http.StatusInternalServerError, 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.StatusInternalServerError, message.Code)
  422. assert.Contains(t, message.Message, "Invalid Range")
  423. })
  424. t.Run("UpdateDisplayName", func(t *testing.T) {
  425. type Network struct {
  426. DisplayName string
  427. }
  428. var network Network
  429. network.DisplayName = "wirecat"
  430. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  431. assert.Nil(t, err, err)
  432. assert.Equal(t, http.StatusOK, response.StatusCode)
  433. defer response.Body.Close()
  434. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  435. assert.Nil(t, err, err)
  436. assert.Equal(t, network.DisplayName, returnedNetwork.DisplayName)
  437. })
  438. t.Run("UpdateDisplayNameInvalidName", func(t *testing.T) {
  439. // -----needs fixing ----
  440. // fails silently
  441. t.Skip()
  442. type Network struct {
  443. DisplayName string
  444. }
  445. var network Network
  446. //create name that is longer than 100 chars
  447. name := ""
  448. for i := 0; i < 101; i++ {
  449. name = name + "a"
  450. }
  451. network.DisplayName = name
  452. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  453. assert.Nil(t, err, err)
  454. var message models.ErrorResponse
  455. err = json.NewDecoder(response.Body).Decode(&message)
  456. assert.Nil(t, err, err)
  457. assert.Equal(t, http.StatusUnprocessableEntity, message.Code)
  458. assert.Equal(t, "W1R3: Field validation for 'DisplayName' failed.", message.Message)
  459. assert.Equal(t, http.StatusUnprocessableEntity, response.StatusCode)
  460. })
  461. t.Run("UpdateInterface", func(t *testing.T) {
  462. type Network struct {
  463. DefaultInterface string
  464. }
  465. var network Network
  466. network.DefaultInterface = "netmaker"
  467. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  468. assert.Nil(t, err, err)
  469. assert.Equal(t, http.StatusOK, response.StatusCode)
  470. defer response.Body.Close()
  471. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  472. assert.Nil(t, err, err)
  473. assert.Equal(t, network.DefaultInterface, returnedNetwork.DefaultInterface)
  474. })
  475. t.Run("UpdateListenPort", func(t *testing.T) {
  476. type Network struct {
  477. DefaultListenPort int32
  478. }
  479. var network Network
  480. network.DefaultListenPort = 6000
  481. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  482. assert.Nil(t, err, err)
  483. assert.Equal(t, http.StatusOK, response.StatusCode)
  484. defer response.Body.Close()
  485. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  486. assert.Nil(t, err, err)
  487. assert.Equal(t, network.DefaultListenPort, returnedNetwork.DefaultListenPort)
  488. })
  489. t.Run("UpdateListenPortInvalidPort", func(t *testing.T) {
  490. // ---needs fixing -----
  491. // value is updated anyways
  492. t.Skip()
  493. type Network struct {
  494. DefaultListenPort int32
  495. }
  496. var network Network
  497. network.DefaultListenPort = 65540
  498. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  499. assert.Nil(t, err, err)
  500. var message models.ErrorResponse
  501. err = json.NewDecoder(response.Body).Decode(&message)
  502. assert.Nil(t, err, err)
  503. assert.Equal(t, http.StatusUnprocessableEntity, message.Code)
  504. assert.Equal(t, "W1R3: Field validation for 'DefaultListenPort' failed.", message.Message)
  505. assert.Equal(t, http.StatusUnprocessableEntity, response.StatusCode)
  506. })
  507. t.Run("UpdatePostUP", func(t *testing.T) {
  508. type Network struct {
  509. DefaultPostUp string
  510. }
  511. var network Network
  512. network.DefaultPostUp = "sudo wg add-conf wc-netmaker /etc/wireguard/peers/conf"
  513. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  514. assert.Nil(t, err, err)
  515. assert.Equal(t, http.StatusOK, response.StatusCode)
  516. defer response.Body.Close()
  517. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  518. assert.Nil(t, err, err)
  519. assert.Equal(t, network.DefaultPostUp, returnedNetwork.DefaultPostUp)
  520. })
  521. t.Run("UpdatePreUp", func(t *testing.T) {
  522. // -------needs fixing ------
  523. // mismatch in models.Network between struc name and json/bson name
  524. // does not get updated.
  525. t.Skip()
  526. type Network struct {
  527. DefaultPostDown string
  528. }
  529. var network Network
  530. network.DefaultPostDown = "test string"
  531. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  532. assert.Nil(t, err, err)
  533. assert.Equal(t, http.StatusOK, response.StatusCode)
  534. defer response.Body.Close()
  535. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  536. assert.Nil(t, err, err)
  537. assert.Equal(t, network.DefaultPostDown, returnedNetwork.DefaultPostDown)
  538. })
  539. t.Run("UpdateKeepAlive", func(t *testing.T) {
  540. type Network struct {
  541. DefaultKeepalive int32
  542. }
  543. var network Network
  544. network.DefaultKeepalive = 60
  545. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  546. assert.Nil(t, err, err)
  547. assert.Equal(t, http.StatusOK, response.StatusCode)
  548. defer response.Body.Close()
  549. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  550. assert.Nil(t, err, err)
  551. assert.Equal(t, network.DefaultKeepalive, returnedNetwork.DefaultKeepalive)
  552. })
  553. t.Run("UpdateKeepAliveTooBig", func(t *testing.T) {
  554. //fails silently
  555. // ----- needs fixing -----
  556. t.Skip()
  557. type Network struct {
  558. DefaultKeepAlive int32
  559. }
  560. var network Network
  561. network.DefaultKeepAlive = 1001
  562. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  563. assert.Nil(t, err, err)
  564. var message models.ErrorResponse
  565. err = json.NewDecoder(response.Body).Decode(&message)
  566. assert.Nil(t, err, err)
  567. assert.Equal(t, http.StatusUnprocessableEntity, message.Code)
  568. assert.Equal(t, "W1R3: Field validation for 'DefaultKeepAlive' failed.", message.Message)
  569. assert.Equal(t, http.StatusUnprocessableEntity, response.StatusCode)
  570. })
  571. t.Run("UpdateSaveConfig", func(t *testing.T) {
  572. //causes panic
  573. t.Skip()
  574. type Network struct {
  575. DefaultSaveConfig *bool
  576. }
  577. var network Network
  578. value := false
  579. network.DefaultSaveConfig = &value
  580. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  581. assert.Nil(t, err, err)
  582. assert.Equal(t, http.StatusOK, response.StatusCode)
  583. defer response.Body.Close()
  584. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  585. assert.Nil(t, err, err)
  586. assert.Equal(t, *network.DefaultSaveConfig, *returnedNetwork.DefaultSaveConfig)
  587. })
  588. t.Run("UpdateManualSignUP", func(t *testing.T) {
  589. t.Skip()
  590. type Network struct {
  591. AllowManualSignUp *bool
  592. }
  593. var network Network
  594. value := true
  595. network.AllowManualSignUp = &value
  596. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  597. assert.Nil(t, err, err)
  598. assert.Equal(t, http.StatusOK, response.StatusCode)
  599. defer response.Body.Close()
  600. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  601. assert.Nil(t, err, err)
  602. //returns previous value not the updated value
  603. // ----- needs fixing -----
  604. //assert.Equal(t, network.NetID, returnedNetwork.NetID)
  605. })
  606. t.Run("DefaultCheckInterval", func(t *testing.T) {
  607. //value is not returned in struct ---
  608. t.Skip()
  609. type Network struct {
  610. DefaultCheckInInterval int32
  611. }
  612. var network Network
  613. network.DefaultCheckInInterval = 6000
  614. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  615. assert.Nil(t, err, err)
  616. assert.Equal(t, http.StatusOK, response.StatusCode)
  617. defer response.Body.Close()
  618. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  619. assert.Nil(t, err, err)
  620. assert.Equal(t, network.DefaultCheckInInterval, returnedNetwork.DefaultCheckInInterval)
  621. })
  622. t.Run("DefaultCheckIntervalTooBig", func(t *testing.T) {
  623. //value is not returned in struct ---
  624. t.Skip()
  625. type Network struct {
  626. DefaultCheckInInterval int32
  627. }
  628. var network Network
  629. network.DefaultCheckInInterval = 100001
  630. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  631. assert.Nil(t, err, err)
  632. var message models.ErrorResponse
  633. err = json.NewDecoder(response.Body).Decode(&message)
  634. assert.Nil(t, err, err)
  635. assert.Equal(t, http.StatusUnprocessableEntity, message.Code)
  636. assert.Equal(t, "W1R3: Field validation for 'DefaultCheckInInterval' failed.", message.Message)
  637. assert.Equal(t, http.StatusUnprocessableEntity, response.StatusCode)
  638. })
  639. t.Run("MultipleFields", func(t *testing.T) {
  640. type Network struct {
  641. DisplayName string
  642. DefaultListenPort int32
  643. }
  644. var network Network
  645. network.DefaultListenPort = 7777
  646. network.DisplayName = "multi"
  647. response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
  648. assert.Nil(t, err, err)
  649. assert.Equal(t, http.StatusOK, response.StatusCode)
  650. defer response.Body.Close()
  651. err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
  652. assert.Nil(t, err, err)
  653. assert.Equal(t, network.DisplayName, returnedNetwork.DisplayName)
  654. assert.Equal(t, network.DefaultListenPort, returnedNetwork.DefaultListenPort)
  655. })
  656. }