netmaker/test/network_test.go.bak

621 lines
23 KiB
Go
Raw Normal View History

2021-04-01 23:01:06 +08:00
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"testing"
2021-04-25 20:18:43 +08:00
"time"
2021-04-01 23:01:06 +08:00
"github.com/gravitl/netmaker/models"
"github.com/stretchr/testify/assert"
)
2021-04-14 05:14:26 +08:00
func TestCreateNetwork(t *testing.T) {
2021-04-18 03:12:03 +08:00
2021-04-14 05:14:26 +08:00
network := models.Network{}
network.NetID = "skynet"
network.AddressRange = "10.71.0.0/16"
2021-04-25 20:18:43 +08:00
deleteNetworks(t)
2021-04-01 23:01:06 +08:00
t.Run("InvalidToken", func(t *testing.T) {
response, err := api(t, network, http.MethodPost, baseURL+"/api/networks", "badkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
assert.Equal(t, http.StatusUnauthorized, message.Code)
2021-07-02 12:16:29 +08:00
assert.Contains(t, message.Message, "rror verifying user toke")
2021-04-01 23:01:06 +08:00
})
2021-04-16 03:26:15 +08:00
t.Run("CreateNetwork", func(t *testing.T) {
response, err := api(t, network, http.MethodPost, baseURL+"/api/networks", "secretkey")
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
})
t.Run("DuplicateNetwork", func(t *testing.T) {
response, err := api(t, network, http.MethodPost, baseURL+"/api/networks", "secretkey")
assert.Nil(t, err, err)
2021-04-17 02:10:05 +08:00
assert.Equal(t, http.StatusBadRequest, response.StatusCode)
2021-04-01 23:01:06 +08:00
})
2021-04-16 03:26:15 +08:00
t.Run("BadName", func(t *testing.T) {
network.NetID = "thisnameistoolong"
response, err := api(t, network, http.MethodPost, baseURL+"/api/networks", "secretkey")
assert.Nil(t, err, err)
2021-04-17 02:10:05 +08:00
assert.Equal(t, http.StatusBadRequest, response.StatusCode)
2021-04-01 23:01:06 +08:00
})
2021-04-16 03:26:15 +08:00
t.Run("BadAddress", func(t *testing.T) {
network.NetID = "wirecat"
network.AddressRange = "10.300.20.56/36"
response, err := api(t, network, http.MethodPost, baseURL+"/api/networks", "secretkey")
assert.Nil(t, err, err)
2021-04-17 02:10:05 +08:00
assert.Equal(t, http.StatusBadRequest, response.StatusCode)
2021-04-01 23:01:06 +08:00
})
2021-04-16 03:26:15 +08:00
2021-04-01 23:01:06 +08:00
}
2021-04-14 05:14:26 +08:00
func TestGetNetworks(t *testing.T) {
2021-04-18 03:12:03 +08:00
2021-04-01 23:01:06 +08:00
t.Run("ValidToken", func(t *testing.T) {
response, err := api(t, "", http.MethodGet, baseURL+"/api/networks", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
defer response.Body.Close()
assert.Equal(t, http.StatusOK, response.StatusCode)
2021-04-14 05:14:26 +08:00
err = json.NewDecoder(response.Body).Decode(&Networks)
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
})
t.Run("InvalidToken", func(t *testing.T) {
response, err := api(t, "", http.MethodGet, baseURL+"/api/networks", "badkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
assert.Equal(t, http.StatusUnauthorized, message.Code)
2021-07-02 12:16:29 +08:00
assert.Contains(t, message.Message, "rror verifying user toke")
2021-04-01 23:01:06 +08:00
})
}
2021-04-14 05:14:26 +08:00
func TestGetNetwork(t *testing.T) {
2021-04-18 03:12:03 +08:00
2021-04-01 23:01:06 +08:00
t.Run("ValidToken", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
var network models.Network
response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
defer response.Body.Close()
assert.Equal(t, http.StatusOK, response.StatusCode)
err = json.NewDecoder(response.Body).Decode(&network)
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
// --- needs fixing ------ returns previous name
//assert.Equal(t, "skynet", network.DisplayName)
2021-04-01 23:01:06 +08:00
})
t.Run("InvalidToken", func(t *testing.T) {
response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/skynet", "badkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
assert.Equal(t, http.StatusUnauthorized, message.Code)
2021-07-02 12:16:29 +08:00
assert.Contains(t, message.Message, "rror verifying user toke")
2021-04-01 23:01:06 +08:00
})
2021-04-14 05:14:26 +08:00
t.Run("InvalidNetwork", func(t *testing.T) {
response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/badnetwork", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
2021-05-28 22:04:07 +08:00
assert.Contains(t, message.Message, "his network does not exist")
2021-05-28 04:48:47 +08:00
assert.Equal(t, http.StatusNotFound, response.StatusCode)
2021-04-01 23:01:06 +08:00
})
}
2021-04-23 04:52:44 +08:00
func TestDeleteNetwork(t *testing.T) {
2021-04-18 03:12:03 +08:00
2021-04-01 23:01:06 +08:00
t.Run("InvalidKey", func(t *testing.T) {
setup(t)
response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/skynet", "badkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
assert.Equal(t, http.StatusUnauthorized, message.Code)
2021-07-02 12:16:29 +08:00
assert.Contains(t, message.Message, "rror verifying user toke")
2021-04-01 23:01:06 +08:00
})
t.Run("Badnetwork", func(t *testing.T) {
response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/badnetwork", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
2021-05-28 22:04:07 +08:00
assert.Contains(t, message.Message, "his network does not exist")
2021-05-28 04:48:47 +08:00
assert.Equal(t, http.StatusNotFound, response.StatusCode)
2021-04-01 23:01:06 +08:00
})
t.Run("NodesExist", func(t *testing.T) {
setup(t)
response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/skynet", "secretkey")
assert.Nil(t, err, err)
2021-05-28 04:48:47 +08:00
assert.Equal(t, http.StatusForbidden, response.StatusCode)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
assert.Contains(t, message.Message, "Node check failed")
2021-05-28 22:04:07 +08:00
assert.Equal(t, http.StatusForbidden, message.Code)
})
t.Run("ValidKey", func(t *testing.T) {
type Message struct {
DeletedCount int64
}
setup(t)
deleteAllNodes(t)
response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/skynet", "secretkey")
assert.Nil(t, err, err)
defer response.Body.Close()
var message Message
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Equal(t, int64(1), message.DeletedCount)
2021-04-01 23:01:06 +08:00
})
}
2021-04-18 12:31:20 +08:00
func TestCreateKey(t *testing.T) {
//ensure we are working with known networks
deleteNetworks(t)
createNetwork(t)
2021-04-01 23:01:06 +08:00
key := models.AccessKey{}
key.Name = "skynet"
key.Uses = 10
t.Run("MultiUse", func(t *testing.T) {
response, err := api(t, key, http.MethodPost, baseURL+"/api/networks/skynet/keys", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
defer response.Body.Close()
message, err := ioutil.ReadAll(response.Body)
assert.Nil(t, err, err)
assert.NotNil(t, message, message)
returnedkey := getKey(t, key.Name)
assert.Equal(t, key.Name, returnedkey.Name)
assert.Equal(t, key.Uses, returnedkey.Uses)
})
deleteKey(t, "skynet", "skynet")
t.Run("ZeroUse", func(t *testing.T) {
2021-04-18 03:12:03 +08:00
//
2021-04-01 23:01:06 +08:00
key.Uses = 0
response, err := api(t, key, http.MethodPost, baseURL+"/api/networks/skynet/keys", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
defer response.Body.Close()
message, err := ioutil.ReadAll(response.Body)
assert.Nil(t, err, err)
assert.NotNil(t, message, message)
returnedkey := getKey(t, key.Name)
assert.Equal(t, key.Name, returnedkey.Name)
assert.Equal(t, 1, returnedkey.Uses)
})
t.Run("DuplicateAccessKey", func(t *testing.T) {
response, err := api(t, key, http.MethodPost, baseURL+"/api/networks/skynet/keys", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
2021-05-01 19:57:49 +08:00
assert.Equal(t, http.StatusBadRequest, response.StatusCode)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err)
assert.Equal(t, http.StatusBadRequest, message.Code)
assert.Equal(t, "Duplicate AccessKey Name", message.Message)
2021-04-01 23:01:06 +08:00
})
t.Run("InvalidToken", func(t *testing.T) {
response, err := api(t, key, http.MethodPost, baseURL+"/api/networks/skynet/keys", "badkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
assert.Equal(t, http.StatusUnauthorized, message.Code)
2021-07-02 12:16:29 +08:00
assert.Contains(t, message.Message, "rror verifying user toke")
2021-04-01 23:01:06 +08:00
})
t.Run("Badnetwork", func(t *testing.T) {
response, err := api(t, key, http.MethodPost, baseURL+"/api/networks/badnetwork/keys", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
2021-05-28 22:04:07 +08:00
assert.Contains(t, message.Message, "his network does not exist")
2021-05-28 04:48:47 +08:00
assert.Equal(t, http.StatusNotFound, response.StatusCode)
2021-04-01 23:01:06 +08:00
})
}
func TestDeleteKey(t *testing.T) {
2021-04-18 12:31:20 +08:00
//ensure we are working with known networks
deleteNetworks(t)
createNetwork(t)
2021-04-18 04:52:53 +08:00
//ensure key exists
createKey(t)
2021-04-01 23:01:06 +08:00
t.Run("KeyValid", func(t *testing.T) {
response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/skynet/keys/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
})
t.Run("InValidKey", func(t *testing.T) {
response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/skynet/keys/badkey", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
2021-04-18 04:52:53 +08:00
assert.Equal(t, http.StatusBadRequest, message.Code)
assert.Equal(t, "key badkey does not exist", message.Message)
assert.Equal(t, http.StatusBadRequest, response.StatusCode)
2021-04-01 23:01:06 +08:00
})
t.Run("KeyInValidnetwork", func(t *testing.T) {
response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/badnetwork/keys/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
2021-05-28 22:04:07 +08:00
assert.Contains(t, message.Message, "his network does not exist")
2021-05-28 04:48:47 +08:00
assert.Equal(t, http.StatusNotFound, response.StatusCode)
2021-04-01 23:01:06 +08:00
})
t.Run("InvalidCredentials", func(t *testing.T) {
response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/skynet/keys/skynet", "badkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
assert.Equal(t, http.StatusUnauthorized, message.Code)
2021-07-02 12:16:29 +08:00
assert.Contains(t, message.Message, "rror verifying user toke")
2021-04-01 23:01:06 +08:00
})
}
func TestGetKeys(t *testing.T) {
2021-04-18 12:31:20 +08:00
//ensure we are working with known networks
deleteNetworks(t)
createNetwork(t)
2021-04-01 23:01:06 +08:00
createKey(t)
t.Run("Valid", func(t *testing.T) {
response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/skynet/keys", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
defer response.Body.Close()
var keys []models.AccessKey
err = json.NewDecoder(response.Body).Decode(&keys)
assert.Nil(t, err, err)
})
t.Run("Invalidnetwork", func(t *testing.T) {
response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/badnetwork/keys", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
2021-05-28 05:21:28 +08:00
assert.Contains(t, message.Message, "his network does not exist")
2021-05-28 04:48:47 +08:00
assert.Equal(t, http.StatusNotFound, response.StatusCode)
2021-04-01 23:01:06 +08:00
})
t.Run("InvalidCredentials", func(t *testing.T) {
response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/skynet/keys", "badkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
assert.Equal(t, http.StatusUnauthorized, message.Code)
2021-07-02 12:16:29 +08:00
assert.Contains(t, message.Message, "rror verifying user toke")
2021-04-01 23:01:06 +08:00
})
}
2021-04-18 03:12:03 +08:00
func TestUpdateNetwork(t *testing.T) {
//ensure we are working with known networks
deleteNetworks(t)
createNetwork(t)
2021-04-14 05:14:26 +08:00
var returnedNetwork models.Network
t.Run("UpdateNetID", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
type Network struct {
NetID string
2021-04-01 23:01:06 +08:00
}
2021-04-14 05:14:26 +08:00
var network Network
network.NetID = "wirecat"
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
2021-04-16 02:48:10 +08:00
assert.Equal(t, http.StatusBadRequest, response.StatusCode)
2021-04-01 23:01:06 +08:00
defer response.Body.Close()
2021-04-16 02:48:10 +08:00
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
2021-04-16 02:48:10 +08:00
assert.Equal(t, http.StatusBadRequest, message.Code)
2021-04-16 03:26:15 +08:00
assert.Equal(t, "NetID is not editable", message.Message)
2021-04-01 23:01:06 +08:00
})
t.Run("Invalidnetwork", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
type Network struct {
NetID string
2021-04-01 23:01:06 +08:00
}
2021-04-14 05:14:26 +08:00
var network Network
network.NetID = "wirecat"
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/badnetwork", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
2021-05-28 22:04:07 +08:00
assert.Equal(t, http.StatusNotFound, message.Code)
assert.Contains(t, message.Message, "his network does not exist")
2021-05-28 04:48:47 +08:00
assert.Equal(t, http.StatusNotFound, response.StatusCode)
2021-04-01 23:01:06 +08:00
})
t.Run("UpdateAddress", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
type Network struct {
2021-04-01 23:01:06 +08:00
AddressRange string
}
2021-04-14 05:14:26 +08:00
var network Network
network.AddressRange = "10.0.0.1/24"
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
defer response.Body.Close()
2021-04-14 05:14:26 +08:00
err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
2021-04-14 05:14:26 +08:00
assert.Equal(t, network.AddressRange, returnedNetwork.AddressRange)
2021-04-01 23:01:06 +08:00
})
t.Run("UpdateAddressInvalid", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
type Network struct {
2021-04-01 23:01:06 +08:00
AddressRange string
}
2021-04-14 05:14:26 +08:00
var network Network
network.AddressRange = "10.0.0.1/36"
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
2021-04-18 03:12:03 +08:00
assert.Equal(t, http.StatusBadRequest, response.StatusCode)
defer response.Body.Close()
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
2021-04-18 03:12:03 +08:00
assert.Equal(t, http.StatusBadRequest, message.Code)
assert.Contains(t, message.Message, "validation for 'AddressRange' failed")
2021-04-01 23:01:06 +08:00
})
t.Run("UpdateDisplayName", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
type Network struct {
2021-04-01 23:01:06 +08:00
DisplayName string
}
2021-04-14 05:14:26 +08:00
var network Network
network.DisplayName = "wirecat"
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
defer response.Body.Close()
2021-04-14 05:14:26 +08:00
err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
2021-04-14 05:14:26 +08:00
assert.Equal(t, network.DisplayName, returnedNetwork.DisplayName)
2021-04-01 23:01:06 +08:00
})
t.Run("UpdateDisplayNameInvalidName", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
type Network struct {
2021-04-01 23:01:06 +08:00
DisplayName string
}
2021-04-14 05:14:26 +08:00
var network Network
2021-04-01 23:01:06 +08:00
//create name that is longer than 100 chars
name := ""
for i := 0; i < 101; i++ {
name = name + "a"
}
network.DisplayName = name
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
2021-04-18 04:30:07 +08:00
assert.Equal(t, http.StatusBadRequest, response.StatusCode)
2021-04-01 23:01:06 +08:00
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
2021-04-18 04:30:07 +08:00
assert.Equal(t, http.StatusBadRequest, message.Code)
assert.Contains(t, message.Message, "Field validation for 'DisplayName' failed")
2021-04-01 23:01:06 +08:00
})
t.Run("UpdateInterface", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
type Network struct {
2021-04-01 23:01:06 +08:00
DefaultInterface string
}
2021-04-14 05:14:26 +08:00
var network Network
network.DefaultInterface = "netmaker"
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
defer response.Body.Close()
2021-04-14 05:14:26 +08:00
err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
2021-04-14 05:14:26 +08:00
assert.Equal(t, network.DefaultInterface, returnedNetwork.DefaultInterface)
2021-04-01 23:01:06 +08:00
})
t.Run("UpdateListenPort", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
type Network struct {
2021-04-01 23:01:06 +08:00
DefaultListenPort int32
}
2021-04-14 05:14:26 +08:00
var network Network
network.DefaultListenPort = 6000
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
defer response.Body.Close()
2021-04-14 05:14:26 +08:00
err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
2021-04-14 05:14:26 +08:00
assert.Equal(t, network.DefaultListenPort, returnedNetwork.DefaultListenPort)
2021-04-01 23:01:06 +08:00
})
t.Run("UpdateListenPortInvalidPort", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
type Network struct {
2021-04-01 23:01:06 +08:00
DefaultListenPort int32
}
2021-04-14 05:14:26 +08:00
var network Network
network.DefaultListenPort = 65540
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
2021-04-18 04:30:07 +08:00
assert.Equal(t, http.StatusBadRequest, message.Code)
assert.Contains(t, message.Message, "Field validation for 'DefaultListenPort' failed")
assert.Equal(t, http.StatusBadRequest, response.StatusCode)
2021-04-01 23:01:06 +08:00
})
t.Run("UpdatePostUP", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
type Network struct {
2021-04-01 23:01:06 +08:00
}
2021-04-14 05:14:26 +08:00
var network Network
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
defer response.Body.Close()
2021-04-14 05:14:26 +08:00
err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
})
2021-04-19 23:02:59 +08:00
t.Run("UpdatePostDown", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
type Network struct {
2021-04-20 01:41:27 +08:00
DefaultPostDown string
2021-04-01 23:01:06 +08:00
}
2021-04-14 05:14:26 +08:00
var network Network
2021-04-20 01:41:27 +08:00
network.DefaultPostDown = "test string"
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
defer response.Body.Close()
2021-04-14 05:14:26 +08:00
err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
2021-04-20 01:41:27 +08:00
assert.Equal(t, network.DefaultPostDown, returnedNetwork.DefaultPostDown)
2021-04-01 23:01:06 +08:00
})
t.Run("UpdateKeepAlive", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
type Network struct {
2021-04-01 23:01:06 +08:00
DefaultKeepalive int32
}
2021-04-14 05:14:26 +08:00
var network Network
network.DefaultKeepalive = 60
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
defer response.Body.Close()
2021-04-14 05:14:26 +08:00
err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
2021-04-14 05:14:26 +08:00
assert.Equal(t, network.DefaultKeepalive, returnedNetwork.DefaultKeepalive)
2021-04-01 23:01:06 +08:00
})
t.Run("UpdateKeepAliveTooBig", func(t *testing.T) {
2021-04-18 04:30:07 +08:00
//does not fails ----- value gets updated.
// ----- needs fixing -----
2021-05-01 19:57:49 +08:00
//t.Skip()
2021-04-14 05:14:26 +08:00
type Network struct {
2021-04-01 23:01:06 +08:00
DefaultKeepAlive int32
}
2021-04-14 05:14:26 +08:00
var network Network
2021-04-25 20:18:43 +08:00
network.DefaultKeepAlive = 2000
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
2021-04-18 04:30:07 +08:00
assert.Equal(t, http.StatusBadRequest, message.Code)
2021-05-01 19:57:49 +08:00
assert.Contains(t, message.Message, "Field validation for 'DefaultKeepalive' failed on the 'max' tag")
2021-04-18 04:30:07 +08:00
assert.Equal(t, http.StatusBadRequest, response.StatusCode)
2021-04-01 23:01:06 +08:00
})
t.Run("UpdateSaveConfig", func(t *testing.T) {
2021-04-25 20:18:43 +08:00
//t.Skip()
//not updatable, ensure attempt to change does not result in change
2021-04-14 05:14:26 +08:00
type Network struct {
2021-04-01 23:01:06 +08:00
DefaultSaveConfig *bool
}
2021-04-14 05:14:26 +08:00
var network Network
2021-04-25 20:18:43 +08:00
var value bool
oldnet := getNetwork(t, "skynet")
if *oldnet.DefaultSaveConfig == true {
value = false
} else {
value = true
}
network.DefaultSaveConfig = &value
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
2021-04-25 20:18:43 +08:00
newnet := getNetwork(t, "skynet")
assert.Equal(t, oldnet.DefaultSaveConfig, newnet.DefaultSaveConfig)
2021-04-01 23:01:06 +08:00
})
t.Run("UpdateManualSignUP", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
type Network struct {
2021-04-01 23:01:06 +08:00
AllowManualSignUp *bool
}
2021-04-14 05:14:26 +08:00
var network Network
2021-04-01 23:01:06 +08:00
value := true
network.AllowManualSignUp = &value
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
defer response.Body.Close()
2021-04-14 05:14:26 +08:00
err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
2021-04-18 04:30:07 +08:00
assert.Equal(t, network.AllowManualSignUp, returnedNetwork.AllowManualSignUp)
2021-04-01 23:01:06 +08:00
})
t.Run("DefaultCheckInterval", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
type Network struct {
2021-04-19 23:56:57 +08:00
CheckInInterval int32
2021-04-01 23:01:06 +08:00
}
2021-04-14 05:14:26 +08:00
var network Network
2021-04-19 23:56:57 +08:00
network.CheckInInterval = 60
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
defer response.Body.Close()
2021-04-14 05:14:26 +08:00
err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
2021-04-19 23:56:57 +08:00
assert.Equal(t, network.CheckInInterval, returnedNetwork.DefaultCheckInInterval)
2021-04-01 23:01:06 +08:00
})
t.Run("DefaultCheckIntervalTooBig", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
type Network struct {
2021-04-19 23:56:57 +08:00
CheckInInterval int32
2021-04-01 23:01:06 +08:00
}
2021-04-14 05:14:26 +08:00
var network Network
2021-04-19 23:56:57 +08:00
network.CheckInInterval = 100001
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
var message models.ErrorResponse
err = json.NewDecoder(response.Body).Decode(&message)
assert.Nil(t, err, err)
2021-04-19 23:56:57 +08:00
assert.Equal(t, http.StatusBadRequest, message.Code)
assert.Contains(t, message.Message, "Field validation for 'DefaultCheckInInterval' failed")
assert.Equal(t, http.StatusBadRequest, response.StatusCode)
2021-04-01 23:01:06 +08:00
})
t.Run("MultipleFields", func(t *testing.T) {
2021-04-14 05:14:26 +08:00
type Network struct {
2021-04-01 23:01:06 +08:00
DisplayName string
DefaultListenPort int32
}
2021-04-14 05:14:26 +08:00
var network Network
network.DefaultListenPort = 7777
network.DisplayName = "multi"
response, err := api(t, network, http.MethodPut, baseURL+"/api/networks/skynet", "secretkey")
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, response.StatusCode)
defer response.Body.Close()
2021-04-14 05:14:26 +08:00
err = json.NewDecoder(response.Body).Decode(&returnedNetwork)
2021-04-01 23:01:06 +08:00
assert.Nil(t, err, err)
2021-04-14 05:14:26 +08:00
assert.Equal(t, network.DisplayName, returnedNetwork.DisplayName)
assert.Equal(t, network.DefaultListenPort, returnedNetwork.DefaultListenPort)
2021-04-01 23:01:06 +08:00
})
}
2021-04-25 20:18:43 +08:00
func TestKeyUpdate(t *testing.T) {
//get current network settings
oldnet := getNetwork(t, "skynet")
//update key
time.Sleep(time.Second * 1)
reply, err := api(t, "", http.MethodPost, baseURL+"/api/networks/skynet/keyupdate", "secretkey")
assert.Nil(t, err, err)
assert.Equal(t, http.StatusOK, reply.StatusCode)
newnet := getNetwork(t, "skynet")
assert.Greater(t, newnet.KeyUpdateTimeStamp, oldnet.KeyUpdateTimeStamp)
}