2022-10-10 11:06:54 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/netip"
|
|
|
|
"testing"
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-02-03 21:47:20 +01:00
|
|
|
nbdns "github.com/netbirdio/netbird/dns"
|
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
|
|
|
|
2022-10-10 11:06:54 +02:00
|
|
|
"github.com/gorilla/mux"
|
2023-02-28 15:01:24 +01:00
|
|
|
|
2022-10-10 11:06:54 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
|
|
|
"github.com/netbirdio/netbird/management/server/mock_server"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
existingNSGroupID = "existingNSGroupID"
|
|
|
|
notFoundNSGroupID = "notFoundNSGroupID"
|
|
|
|
testNSGroupAccountID = "test_id"
|
|
|
|
)
|
|
|
|
|
|
|
|
var testingNSAccount = &server.Account{
|
|
|
|
Id: testNSGroupAccountID,
|
|
|
|
Domain: "hotmail.com",
|
2022-11-05 10:24:50 +01:00
|
|
|
Users: map[string]*server.User{
|
|
|
|
"test_user": server.NewAdminUser("test_user"),
|
|
|
|
},
|
2022-10-10 11:06:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var baseExistingNSGroup = &nbdns.NameServerGroup{
|
|
|
|
ID: existingNSGroupID,
|
|
|
|
Name: "super",
|
|
|
|
Description: "super",
|
2022-11-03 18:39:37 +01:00
|
|
|
Primary: true,
|
2022-10-10 11:06:54 +02:00
|
|
|
NameServers: []nbdns.NameServer{
|
|
|
|
{
|
|
|
|
IP: netip.MustParseAddr("1.1.1.1"),
|
|
|
|
NSType: nbdns.UDPNameServerType,
|
|
|
|
Port: nbdns.DefaultDNSPort,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IP: netip.MustParseAddr("1.1.2.2"),
|
|
|
|
NSType: nbdns.UDPNameServerType,
|
|
|
|
Port: nbdns.DefaultDNSPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Groups: []string{"testing"},
|
2023-08-22 17:56:39 +02:00
|
|
|
Domains: []string{"domain"},
|
2022-10-10 11:06:54 +02:00
|
|
|
Enabled: true,
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
func initNameserversTestData() *NameserversHandler {
|
|
|
|
return &NameserversHandler{
|
2022-10-10 11:06:54 +02:00
|
|
|
accountManager: &mock_server.MockAccountManager{
|
|
|
|
GetNameServerGroupFunc: func(accountID, nsGroupID string) (*nbdns.NameServerGroup, error) {
|
|
|
|
if nsGroupID == existingNSGroupID {
|
|
|
|
return baseExistingNSGroup.Copy(), nil
|
|
|
|
}
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.NotFound, "nameserver group with ID %s not found", nsGroupID)
|
2022-10-10 11:06:54 +02:00
|
|
|
},
|
2023-10-19 19:32:42 +02:00
|
|
|
CreateNameServerGroupFunc: func(accountID string, name, description string, nameServerList []nbdns.NameServer, groups []string, primary bool, domains []string, enabled bool, _ string, searchDomains bool) (*nbdns.NameServerGroup, error) {
|
2022-10-10 11:06:54 +02:00
|
|
|
return &nbdns.NameServerGroup{
|
2023-10-19 19:32:42 +02:00
|
|
|
ID: existingNSGroupID,
|
|
|
|
Name: name,
|
|
|
|
Description: description,
|
|
|
|
NameServers: nameServerList,
|
|
|
|
Groups: groups,
|
|
|
|
Enabled: enabled,
|
|
|
|
Primary: primary,
|
|
|
|
Domains: domains,
|
|
|
|
SearchDomainsEnabled: searchDomains,
|
2022-10-10 11:06:54 +02:00
|
|
|
}, nil
|
|
|
|
},
|
2023-01-25 16:29:59 +01:00
|
|
|
DeleteNameServerGroupFunc: func(accountID, nsGroupID, _ string) error {
|
2022-10-10 11:06:54 +02:00
|
|
|
return nil
|
|
|
|
},
|
2023-01-25 16:29:59 +01:00
|
|
|
SaveNameServerGroupFunc: func(accountID, _ string, nsGroupToSave *nbdns.NameServerGroup) error {
|
2022-10-10 11:06:54 +02:00
|
|
|
if nsGroupToSave.ID == existingNSGroupID {
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.NotFound, "nameserver group with ID %s was not found", nsGroupToSave.ID)
|
2022-10-10 11:06:54 +02:00
|
|
|
},
|
2022-11-11 20:36:45 +01:00
|
|
|
GetAccountFromTokenFunc: func(_ jwtclaims.AuthorizationClaims) (*server.Account, *server.User, error) {
|
2023-01-25 16:29:59 +01:00
|
|
|
return testingNSAccount, testingAccount.Users["test_user"], nil
|
2022-10-10 11:06:54 +02:00
|
|
|
},
|
|
|
|
},
|
2023-02-03 21:47:20 +01:00
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithFromRequestContext(func(r *http.Request) jwtclaims.AuthorizationClaims {
|
2022-10-10 11:06:54 +02:00
|
|
|
return jwtclaims.AuthorizationClaims{
|
|
|
|
UserId: "test_user",
|
|
|
|
Domain: "hotmail.com",
|
|
|
|
AccountId: testNSGroupAccountID,
|
|
|
|
}
|
2023-02-03 21:47:20 +01:00
|
|
|
}),
|
|
|
|
),
|
2022-10-10 11:06:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNameserversHandlers(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
expectedStatus int
|
|
|
|
expectedBody bool
|
|
|
|
expectedNSGroup *api.NameserverGroup
|
|
|
|
requestType string
|
|
|
|
requestPath string
|
|
|
|
requestBody io.Reader
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Get Existing Nameserver Group",
|
|
|
|
requestType: http.MethodGet,
|
|
|
|
requestPath: "/api/dns/nameservers/" + existingNSGroupID,
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedNSGroup: toNameserverGroupResponse(baseExistingNSGroup),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Get Not Existing Nameserver Group",
|
|
|
|
requestType: http.MethodGet,
|
|
|
|
requestPath: "/api/dns/nameservers/" + notFoundNSGroupID,
|
|
|
|
expectedStatus: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "POST OK",
|
|
|
|
requestType: http.MethodPost,
|
|
|
|
requestPath: "/api/dns/nameservers",
|
|
|
|
requestBody: bytes.NewBuffer(
|
2022-11-03 18:39:37 +01:00
|
|
|
[]byte("{\"name\":\"name\",\"Description\":\"Post\",\"nameservers\":[{\"ip\":\"1.1.1.1\",\"ns_type\":\"udp\",\"port\":53}],\"groups\":[\"group\"],\"enabled\":true,\"primary\":true}")),
|
2022-10-10 11:06:54 +02:00
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedNSGroup: &api.NameserverGroup{
|
|
|
|
Id: existingNSGroupID,
|
|
|
|
Name: "name",
|
|
|
|
Description: "Post",
|
|
|
|
Nameservers: []api.Nameserver{
|
|
|
|
{
|
|
|
|
Ip: "1.1.1.1",
|
|
|
|
NsType: "udp",
|
|
|
|
Port: 53,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Groups: []string{"group"},
|
|
|
|
Enabled: true,
|
2022-11-07 15:38:21 +01:00
|
|
|
Primary: true,
|
2022-10-10 11:06:54 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "POST Invalid Nameserver",
|
|
|
|
requestType: http.MethodPost,
|
|
|
|
requestPath: "/api/dns/nameservers",
|
|
|
|
requestBody: bytes.NewBuffer(
|
2022-11-03 18:39:37 +01:00
|
|
|
[]byte("{\"name\":\"name\",\"Description\":\"Post\",\"nameservers\":[{\"ip\":\"1000\",\"ns_type\":\"udp\",\"port\":53}],\"groups\":[\"group\"],\"enabled\":true,\"primary\":true}")),
|
2022-11-11 20:36:45 +01:00
|
|
|
expectedStatus: http.StatusUnprocessableEntity,
|
2022-10-10 11:06:54 +02:00
|
|
|
expectedBody: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "PUT OK",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/dns/nameservers/" + existingNSGroupID,
|
|
|
|
requestBody: bytes.NewBuffer(
|
2022-11-03 18:39:37 +01:00
|
|
|
[]byte("{\"name\":\"name\",\"Description\":\"Post\",\"nameservers\":[{\"ip\":\"1.1.1.1\",\"ns_type\":\"udp\",\"port\":53}],\"groups\":[\"group\"],\"enabled\":true,\"primary\":true}")),
|
2022-10-10 11:06:54 +02:00
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedNSGroup: &api.NameserverGroup{
|
|
|
|
Id: existingNSGroupID,
|
|
|
|
Name: "name",
|
|
|
|
Description: "Post",
|
|
|
|
Nameservers: []api.Nameserver{
|
|
|
|
{
|
|
|
|
Ip: "1.1.1.1",
|
|
|
|
NsType: "udp",
|
|
|
|
Port: 53,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Groups: []string{"group"},
|
|
|
|
Enabled: true,
|
2022-11-07 15:38:21 +01:00
|
|
|
Primary: true,
|
2022-10-10 11:06:54 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "PUT Not Existing Nameserver Group",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/dns/nameservers/" + notFoundNSGroupID,
|
|
|
|
requestBody: bytes.NewBuffer(
|
2022-11-03 18:39:37 +01:00
|
|
|
[]byte("{\"name\":\"name\",\"Description\":\"Post\",\"nameservers\":[{\"ip\":\"1.1.1.1\",\"ns_type\":\"udp\",\"port\":53}],\"groups\":[\"group\"],\"enabled\":true,\"primary\":true}")),
|
2022-10-10 11:06:54 +02:00
|
|
|
expectedStatus: http.StatusNotFound,
|
|
|
|
expectedBody: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "PUT Invalid Nameserver",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/dns/nameservers/" + notFoundNSGroupID,
|
|
|
|
requestBody: bytes.NewBuffer(
|
2022-11-03 18:39:37 +01:00
|
|
|
[]byte("{\"name\":\"name\",\"Description\":\"Post\",\"nameservers\":[{\"ip\":\"100\",\"ns_type\":\"udp\",\"port\":53}],\"groups\":[\"group\"],\"enabled\":true,\"primary\":true}")),
|
2022-11-11 20:36:45 +01:00
|
|
|
expectedStatus: http.StatusUnprocessableEntity,
|
2022-10-10 11:06:54 +02:00
|
|
|
expectedBody: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p := initNameserversTestData()
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody)
|
|
|
|
|
|
|
|
router := mux.NewRouter()
|
2023-05-03 00:15:25 +02:00
|
|
|
router.HandleFunc("/api/dns/nameservers/{nsgroupId}", p.GetNameserverGroup).Methods("GET")
|
2023-02-28 15:01:24 +01:00
|
|
|
router.HandleFunc("/api/dns/nameservers", p.CreateNameserverGroup).Methods("POST")
|
2023-05-03 00:15:25 +02:00
|
|
|
router.HandleFunc("/api/dns/nameservers/{nsgroupId}", p.DeleteNameserverGroup).Methods("DELETE")
|
|
|
|
router.HandleFunc("/api/dns/nameservers/{nsgroupId}", p.UpdateNameserverGroup).Methods("PUT")
|
2022-10-10 11:06:54 +02:00
|
|
|
router.ServeHTTP(recorder, req)
|
|
|
|
|
|
|
|
res := recorder.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
content, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("I don't know what I expected; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status := recorder.Code; status != tc.expectedStatus {
|
|
|
|
t.Errorf("handler returned wrong status code: got %v want %v, content: %s",
|
|
|
|
status, tc.expectedStatus, string(content))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tc.expectedBody {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
got := &api.NameserverGroup{}
|
|
|
|
if err = json.Unmarshal(content, &got); err != nil {
|
|
|
|
t.Fatalf("Sent content is not in correct json format; %v", err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, tc.expectedNSGroup, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|