2023-01-17 17:34:40 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2023-01-17 17:34:40 +01:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-02-03 21:47:20 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
|
|
|
|
2023-01-17 17:34:40 +01:00
|
|
|
"github.com/gorilla/mux"
|
2023-02-28 15:01:24 +01:00
|
|
|
|
2023-01-17 17:34:40 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
|
|
|
"github.com/netbirdio/netbird/management/server/mock_server"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
testDNSSettingsAccountID = "test_id"
|
|
|
|
testDNSSettingsExistingGroup = "test_group"
|
|
|
|
testDNSSettingsUserID = "test_user"
|
|
|
|
)
|
|
|
|
|
2023-10-11 23:00:56 +02:00
|
|
|
var baseExistingDNSSettings = server.DNSSettings{
|
2023-01-17 17:34:40 +01:00
|
|
|
DisabledManagementGroups: []string{testDNSSettingsExistingGroup},
|
|
|
|
}
|
|
|
|
|
|
|
|
var testingDNSSettingsAccount = &server.Account{
|
|
|
|
Id: testDNSSettingsAccountID,
|
|
|
|
Domain: "hotmail.com",
|
|
|
|
Users: map[string]*server.User{
|
|
|
|
testDNSSettingsUserID: server.NewAdminUser("test_user"),
|
|
|
|
},
|
|
|
|
DNSSettings: baseExistingDNSSettings,
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
func initDNSSettingsTestData() *DNSSettingsHandler {
|
|
|
|
return &DNSSettingsHandler{
|
2023-01-17 17:34:40 +01:00
|
|
|
accountManager: &mock_server.MockAccountManager{
|
2024-07-03 11:33:02 +02:00
|
|
|
GetDNSSettingsFunc: func(ctx context.Context, accountID string, userID string) (*server.DNSSettings, error) {
|
2023-10-11 23:00:56 +02:00
|
|
|
return &testingDNSSettingsAccount.DNSSettings, nil
|
2023-01-17 17:34:40 +01:00
|
|
|
},
|
2024-07-03 11:33:02 +02:00
|
|
|
SaveDNSSettingsFunc: func(ctx context.Context, accountID string, userID string, dnsSettingsToSave *server.DNSSettings) error {
|
2023-01-17 17:34:40 +01:00
|
|
|
if dnsSettingsToSave != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return status.Errorf(status.InvalidArgument, "the dns settings provided are nil")
|
|
|
|
},
|
2024-09-27 16:10:50 +02:00
|
|
|
GetAccountIDFromTokenFunc: func(ctx context.Context, _ jwtclaims.AuthorizationClaims) (string, string, error) {
|
|
|
|
return testingDNSSettingsAccount.Id, testingDNSSettingsAccount.Users[testDNSSettingsUserID].Id, nil
|
2023-01-17 17:34:40 +01:00
|
|
|
},
|
|
|
|
},
|
2023-02-03 21:47:20 +01:00
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithFromRequestContext(func(r *http.Request) jwtclaims.AuthorizationClaims {
|
2023-01-17 17:34:40 +01:00
|
|
|
return jwtclaims.AuthorizationClaims{
|
|
|
|
UserId: "test_user",
|
|
|
|
Domain: "hotmail.com",
|
|
|
|
AccountId: testDNSSettingsAccountID,
|
|
|
|
}
|
2023-02-03 21:47:20 +01:00
|
|
|
}),
|
|
|
|
),
|
2023-01-17 17:34:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSSettingsHandlers(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
expectedStatus int
|
|
|
|
expectedBody bool
|
|
|
|
expectedDNSSettings *api.DNSSettings
|
|
|
|
requestType string
|
|
|
|
requestPath string
|
|
|
|
requestBody io.Reader
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Get DNS Settings",
|
|
|
|
requestType: http.MethodGet,
|
|
|
|
requestPath: "/api/dns/settings",
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedDNSSettings: &api.DNSSettings{
|
|
|
|
DisabledManagementGroups: baseExistingDNSSettings.DisabledManagementGroups,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Update DNS Settings",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/dns/settings",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte("{\"disabled_management_groups\":[\"group1\",\"group2\"]}")),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedDNSSettings: &api.DNSSettings{
|
|
|
|
DisabledManagementGroups: []string{"group1", "group2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Update DNS Settings Empty Body",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/dns/settings",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte("{}")),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedDNSSettings: &api.DNSSettings{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p := initDNSSettingsTestData()
|
|
|
|
|
|
|
|
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()
|
|
|
|
router.HandleFunc("/api/dns/settings", p.GetDNSSettings).Methods("GET")
|
|
|
|
router.HandleFunc("/api/dns/settings", p.UpdateDNSSettings).Methods("PUT")
|
|
|
|
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.DNSSettings{}
|
|
|
|
if err = json.Unmarshal(content, &got); err != nil {
|
|
|
|
t.Fatalf("Sent content is not in correct json format; %v", err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, tc.expectedDNSSettings, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|