2022-07-29 20:37:09 +02:00
|
|
|
package http
|
2022-05-05 08:58:34 +02:00
|
|
|
|
|
|
|
import (
|
2023-04-22 12:57:51 +02:00
|
|
|
"bytes"
|
2022-05-05 08:58:34 +02:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2023-04-22 12:57:51 +02:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-02-28 15:01:24 +01:00
|
|
|
|
2022-05-05 08:58:34 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server"
|
2023-04-22 12:57:51 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
2022-05-05 08:58:34 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
|
|
|
"github.com/netbirdio/netbird/management/server/mock_server"
|
2023-04-22 12:57:51 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
2022-05-05 08:58:34 +02:00
|
|
|
)
|
|
|
|
|
2023-04-22 12:57:51 +02:00
|
|
|
const (
|
|
|
|
serviceUserID = "serviceUserID"
|
|
|
|
regularUserID = "regularUserID"
|
|
|
|
)
|
|
|
|
|
|
|
|
var usersTestAccount = &server.Account{
|
|
|
|
Id: existingAccountID,
|
|
|
|
Domain: domain,
|
|
|
|
Users: map[string]*server.User{
|
|
|
|
existingUserID: {
|
|
|
|
Id: existingUserID,
|
|
|
|
Role: "admin",
|
|
|
|
IsServiceUser: false,
|
|
|
|
},
|
|
|
|
regularUserID: {
|
|
|
|
Id: regularUserID,
|
|
|
|
Role: "user",
|
|
|
|
IsServiceUser: false,
|
|
|
|
},
|
|
|
|
serviceUserID: {
|
|
|
|
Id: serviceUserID,
|
|
|
|
Role: "user",
|
|
|
|
IsServiceUser: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func initUsersTestData() *UsersHandler {
|
2023-02-28 15:46:08 +01:00
|
|
|
return &UsersHandler{
|
2022-05-05 08:58:34 +02:00
|
|
|
accountManager: &mock_server.MockAccountManager{
|
2022-11-11 20:36:45 +01:00
|
|
|
GetAccountFromTokenFunc: func(claims jwtclaims.AuthorizationClaims) (*server.Account, *server.User, error) {
|
2023-04-22 12:57:51 +02:00
|
|
|
return usersTestAccount, usersTestAccount.Users[claims.UserId], nil
|
2022-05-05 08:58:34 +02:00
|
|
|
},
|
2022-11-05 10:24:50 +01:00
|
|
|
GetUsersFromAccountFunc: func(accountID, userID string) ([]*server.UserInfo, error) {
|
2022-05-05 08:58:34 +02:00
|
|
|
users := make([]*server.UserInfo, 0)
|
2023-04-22 12:57:51 +02:00
|
|
|
for _, v := range usersTestAccount.Users {
|
2022-05-05 08:58:34 +02:00
|
|
|
users = append(users, &server.UserInfo{
|
2023-04-22 12:57:51 +02:00
|
|
|
ID: v.Id,
|
|
|
|
Role: string(v.Role),
|
|
|
|
Name: "",
|
|
|
|
Email: "",
|
|
|
|
IsServiceUser: v.IsServiceUser,
|
2022-05-05 08:58:34 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
},
|
2023-04-22 12:57:51 +02:00
|
|
|
CreateUserFunc: func(accountID, userID string, key *server.UserInfo) (*server.UserInfo, error) {
|
|
|
|
if userID != existingUserID {
|
|
|
|
return nil, status.Errorf(status.NotFound, "user with ID %s does not exists", userID)
|
|
|
|
}
|
|
|
|
return key, nil
|
|
|
|
},
|
|
|
|
DeleteUserFunc: func(accountID string, executingUserID string, targetUserID string) error {
|
|
|
|
if targetUserID == notFoundUserID {
|
|
|
|
return status.Errorf(status.NotFound, "user with ID %s does not exists", targetUserID)
|
|
|
|
}
|
|
|
|
if !usersTestAccount.Users[targetUserID].IsServiceUser {
|
|
|
|
return status.Errorf(status.PermissionDenied, "user with ID %s is not a service user and can not be deleted", targetUserID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2022-05-05 08:58:34 +02:00
|
|
|
},
|
2023-02-03 21:47:20 +01:00
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithFromRequestContext(func(r *http.Request) jwtclaims.AuthorizationClaims {
|
2022-05-05 08:58:34 +02:00
|
|
|
return jwtclaims.AuthorizationClaims{
|
2023-04-22 12:57:51 +02:00
|
|
|
UserId: existingUserID,
|
|
|
|
Domain: domain,
|
|
|
|
AccountId: existingAccountID,
|
2022-05-05 08:58:34 +02:00
|
|
|
}
|
2023-02-03 21:47:20 +01:00
|
|
|
}),
|
|
|
|
),
|
2022-05-05 08:58:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUsers(t *testing.T) {
|
2023-04-22 12:57:51 +02:00
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
expectedStatus int
|
|
|
|
requestType string
|
|
|
|
requestPath string
|
|
|
|
expectedUserIDs []string
|
|
|
|
}{
|
|
|
|
{name: "GetAllUsers", requestType: http.MethodGet, requestPath: "/api/users", expectedStatus: http.StatusOK, expectedUserIDs: []string{existingUserID, regularUserID, serviceUserID}},
|
|
|
|
{name: "GetOnlyServiceUsers", requestType: http.MethodGet, requestPath: "/api/users?service_user=true", expectedStatus: http.StatusOK, expectedUserIDs: []string{serviceUserID}},
|
|
|
|
{name: "GetOnlyRegularUsers", requestType: http.MethodGet, requestPath: "/api/users?service_user=false", expectedStatus: http.StatusOK, expectedUserIDs: []string{existingUserID, regularUserID}},
|
|
|
|
}
|
|
|
|
|
|
|
|
userHandler := initUsersTestData()
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(tc.requestType, tc.requestPath, nil)
|
|
|
|
|
|
|
|
userHandler.GetAllUsers(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
|
|
|
|
}
|
|
|
|
|
|
|
|
respBody := []*server.UserInfo{}
|
|
|
|
err = json.Unmarshal(content, &respBody)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Sent content is not in correct json format; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, len(respBody), len(tc.expectedUserIDs))
|
|
|
|
for _, v := range respBody {
|
|
|
|
assert.Contains(t, tc.expectedUserIDs, v.ID)
|
|
|
|
assert.Equal(t, v.ID, usersTestAccount.Users[v.ID].Id)
|
|
|
|
assert.Equal(t, v.Role, string(usersTestAccount.Users[v.ID].Role))
|
|
|
|
assert.Equal(t, v.IsServiceUser, usersTestAccount.Users[v.ID].IsServiceUser)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateUser(t *testing.T) {
|
|
|
|
name := "name"
|
|
|
|
email := "email"
|
|
|
|
serviceUserToAdd := api.UserCreateRequest{
|
|
|
|
AutoGroups: []string{},
|
|
|
|
Email: nil,
|
|
|
|
IsServiceUser: true,
|
|
|
|
Name: &name,
|
|
|
|
Role: "admin",
|
|
|
|
}
|
|
|
|
serviceUserString, err := json.Marshal(serviceUserToAdd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
regularUserToAdd := api.UserCreateRequest{
|
|
|
|
AutoGroups: []string{},
|
|
|
|
Email: &email,
|
|
|
|
IsServiceUser: true,
|
|
|
|
Name: &name,
|
|
|
|
Role: "admin",
|
|
|
|
}
|
|
|
|
regularUserString, err := json.Marshal(regularUserToAdd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-05-05 08:58:34 +02:00
|
|
|
|
2023-02-03 21:47:20 +01:00
|
|
|
tt := []struct {
|
2022-05-05 08:58:34 +02:00
|
|
|
name string
|
|
|
|
expectedStatus int
|
|
|
|
requestType string
|
|
|
|
requestPath string
|
|
|
|
requestBody io.Reader
|
|
|
|
expectedResult []*server.User
|
|
|
|
}{
|
2023-04-22 12:57:51 +02:00
|
|
|
{name: "CreateServiceUser", requestType: http.MethodPost, requestPath: "/api/users", expectedStatus: http.StatusOK, requestBody: bytes.NewBuffer(serviceUserString)},
|
|
|
|
// right now creation is blocked in AC middleware, will be refactored in the future
|
|
|
|
{name: "CreateRegularUser", requestType: http.MethodPost, requestPath: "/api/users", expectedStatus: http.StatusOK, requestBody: bytes.NewBuffer(regularUserString)},
|
2022-05-05 08:58:34 +02:00
|
|
|
}
|
|
|
|
|
2023-04-22 12:57:51 +02:00
|
|
|
userHandler := initUsersTestData()
|
|
|
|
|
2022-05-05 08:58:34 +02:00
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-04-22 12:57:51 +02:00
|
|
|
req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody)
|
2022-05-05 08:58:34 +02:00
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
|
2023-04-22 12:57:51 +02:00
|
|
|
userHandler.CreateUser(rr, req)
|
2022-05-05 08:58:34 +02:00
|
|
|
|
|
|
|
res := rr.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
if status := rr.Code; status != tc.expectedStatus {
|
|
|
|
t.Fatalf("handler returned wrong status code: got %v want %v",
|
2023-04-22 12:57:51 +02:00
|
|
|
status, tc.expectedStatus)
|
2022-05-05 08:58:34 +02:00
|
|
|
}
|
2023-04-22 12:57:51 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-05-05 08:58:34 +02:00
|
|
|
|
2023-04-22 12:57:51 +02:00
|
|
|
func TestDeleteUser(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
expectedStatus int
|
|
|
|
expectedBody bool
|
|
|
|
requestType string
|
|
|
|
requestPath string
|
|
|
|
requestVars map[string]string
|
|
|
|
requestBody io.Reader
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Delete Regular User",
|
|
|
|
requestType: http.MethodDelete,
|
|
|
|
requestPath: "/api/users/" + regularUserID,
|
|
|
|
requestVars: map[string]string{"id": regularUserID},
|
|
|
|
expectedStatus: http.StatusForbidden,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Delete Service User",
|
|
|
|
requestType: http.MethodDelete,
|
|
|
|
requestPath: "/api/users/" + serviceUserID,
|
|
|
|
requestVars: map[string]string{"id": serviceUserID},
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Delete Not Existing User",
|
|
|
|
requestType: http.MethodDelete,
|
|
|
|
requestPath: "/api/users/" + notFoundUserID,
|
|
|
|
requestVars: map[string]string{"id": notFoundUserID},
|
|
|
|
expectedStatus: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
}
|
2022-05-05 08:58:34 +02:00
|
|
|
|
2023-04-22 12:57:51 +02:00
|
|
|
userHandler := initUsersTestData()
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest(tc.requestType, tc.requestPath, nil)
|
|
|
|
req = mux.SetURLVars(req, tc.requestVars)
|
|
|
|
rr := httptest.NewRecorder()
|
2022-05-05 08:58:34 +02:00
|
|
|
|
2023-04-22 12:57:51 +02:00
|
|
|
userHandler.DeleteUser(rr, req)
|
|
|
|
|
|
|
|
res := rr.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
if status := rr.Code; status != tc.expectedStatus {
|
|
|
|
t.Fatalf("handler returned wrong status code: got %v want %v",
|
|
|
|
status, tc.expectedStatus)
|
2022-05-05 08:58:34 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|