2023-01-02 15:11:32 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2023-01-02 15:11:32 +01:00
|
|
|
"encoding/json"
|
2023-02-03 21:47:20 +01:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
"github.com/gorilla/mux"
|
2023-02-28 15:01:24 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
|
|
|
"github.com/netbirdio/netbird/management/server/mock_server"
|
|
|
|
)
|
|
|
|
|
2024-09-22 22:44:10 +02:00
|
|
|
func initEventsTestData(account string, events ...*activity.Event) *EventsHandler {
|
2023-02-28 15:01:24 +01:00
|
|
|
return &EventsHandler{
|
2023-01-02 15:11:32 +01:00
|
|
|
accountManager: &mock_server.MockAccountManager{
|
2024-07-03 11:33:02 +02:00
|
|
|
GetEventsFunc: func(_ context.Context, accountID, userID string) ([]*activity.Event, error) {
|
2023-01-02 15:11:32 +01:00
|
|
|
if accountID == account {
|
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
return []*activity.Event{}, nil
|
|
|
|
},
|
2024-09-22 22:44:10 +02:00
|
|
|
GetAccountIDFromTokenFunc: func(_ context.Context, claims jwtclaims.AuthorizationClaims) (string, string, error) {
|
|
|
|
return claims.AccountId, claims.UserId, nil
|
2023-01-02 15:11:32 +01:00
|
|
|
},
|
2024-07-03 11:33:02 +02:00
|
|
|
GetUsersFromAccountFunc: func(_ context.Context, accountID, userID string) ([]*server.UserInfo, error) {
|
2023-09-19 18:08:40 +02:00
|
|
|
return make([]*server.UserInfo, 0), nil
|
|
|
|
},
|
2023-01-02 15:11:32 +01:00
|
|
|
},
|
2023-02-03 21:47:20 +01:00
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithFromRequestContext(func(r *http.Request) jwtclaims.AuthorizationClaims {
|
2023-01-02 15:11:32 +01:00
|
|
|
return jwtclaims.AuthorizationClaims{
|
|
|
|
UserId: "test_user",
|
|
|
|
Domain: "hotmail.com",
|
|
|
|
AccountId: "test_account",
|
|
|
|
}
|
2023-02-03 21:47:20 +01:00
|
|
|
}),
|
|
|
|
),
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateEvents(accountID, userID string) []*activity.Event {
|
|
|
|
ID := uint64(1)
|
|
|
|
events := make([]*activity.Event, 0)
|
|
|
|
events = append(events, &activity.Event{
|
2023-04-03 15:09:35 +02:00
|
|
|
Timestamp: time.Now().UTC(),
|
2023-01-02 15:11:32 +01:00
|
|
|
Activity: activity.PeerAddedByUser,
|
|
|
|
ID: ID,
|
|
|
|
InitiatorID: userID,
|
|
|
|
TargetID: "100.64.0.2",
|
|
|
|
AccountID: accountID,
|
|
|
|
Meta: map[string]any{"some": "meta"},
|
|
|
|
})
|
|
|
|
ID++
|
|
|
|
events = append(events, &activity.Event{
|
2023-04-03 15:09:35 +02:00
|
|
|
Timestamp: time.Now().UTC(),
|
2023-01-02 15:11:32 +01:00
|
|
|
Activity: activity.UserJoined,
|
|
|
|
ID: ID,
|
|
|
|
InitiatorID: userID,
|
|
|
|
TargetID: "",
|
|
|
|
AccountID: accountID,
|
|
|
|
Meta: map[string]any{"some": "meta"},
|
|
|
|
})
|
|
|
|
ID++
|
|
|
|
events = append(events, &activity.Event{
|
2023-04-03 15:09:35 +02:00
|
|
|
Timestamp: time.Now().UTC(),
|
2023-01-02 15:11:32 +01:00
|
|
|
Activity: activity.GroupCreated,
|
|
|
|
ID: ID,
|
|
|
|
InitiatorID: userID,
|
|
|
|
TargetID: "group-id",
|
|
|
|
AccountID: accountID,
|
|
|
|
Meta: map[string]any{"some": "meta"},
|
|
|
|
})
|
|
|
|
ID++
|
|
|
|
events = append(events, &activity.Event{
|
2023-04-03 15:09:35 +02:00
|
|
|
Timestamp: time.Now().UTC(),
|
2023-01-02 15:11:32 +01:00
|
|
|
Activity: activity.SetupKeyUpdated,
|
|
|
|
ID: ID,
|
|
|
|
InitiatorID: userID,
|
|
|
|
TargetID: "setup-key-id",
|
|
|
|
AccountID: accountID,
|
|
|
|
Meta: map[string]any{"some": "meta"},
|
|
|
|
})
|
|
|
|
ID++
|
|
|
|
events = append(events, &activity.Event{
|
2023-04-03 15:09:35 +02:00
|
|
|
Timestamp: time.Now().UTC(),
|
2023-01-02 15:11:32 +01:00
|
|
|
Activity: activity.SetupKeyUpdated,
|
|
|
|
ID: ID,
|
|
|
|
InitiatorID: userID,
|
|
|
|
TargetID: "setup-key-id",
|
|
|
|
AccountID: accountID,
|
|
|
|
Meta: map[string]any{"some": "meta"},
|
|
|
|
})
|
|
|
|
ID++
|
|
|
|
events = append(events, &activity.Event{
|
2023-04-03 15:09:35 +02:00
|
|
|
Timestamp: time.Now().UTC(),
|
2023-01-02 15:11:32 +01:00
|
|
|
Activity: activity.SetupKeyRevoked,
|
|
|
|
ID: ID,
|
|
|
|
InitiatorID: userID,
|
|
|
|
TargetID: "setup-key-id",
|
|
|
|
AccountID: accountID,
|
|
|
|
Meta: map[string]any{"some": "meta"},
|
|
|
|
})
|
|
|
|
ID++
|
|
|
|
events = append(events, &activity.Event{
|
2023-04-03 15:09:35 +02:00
|
|
|
Timestamp: time.Now().UTC(),
|
2023-01-02 15:11:32 +01:00
|
|
|
Activity: activity.SetupKeyOverused,
|
|
|
|
ID: ID,
|
|
|
|
InitiatorID: userID,
|
|
|
|
TargetID: "setup-key-id",
|
|
|
|
AccountID: accountID,
|
|
|
|
Meta: map[string]any{"some": "meta"},
|
|
|
|
})
|
|
|
|
ID++
|
|
|
|
events = append(events, &activity.Event{
|
2023-04-03 15:09:35 +02:00
|
|
|
Timestamp: time.Now().UTC(),
|
2023-01-02 15:11:32 +01:00
|
|
|
Activity: activity.SetupKeyCreated,
|
|
|
|
ID: ID,
|
|
|
|
InitiatorID: userID,
|
|
|
|
TargetID: "setup-key-id",
|
|
|
|
AccountID: accountID,
|
|
|
|
Meta: map[string]any{"some": "meta"},
|
|
|
|
})
|
|
|
|
ID++
|
|
|
|
events = append(events, &activity.Event{
|
2023-04-03 15:09:35 +02:00
|
|
|
Timestamp: time.Now().UTC(),
|
2023-01-02 15:11:32 +01:00
|
|
|
Activity: activity.RuleAdded,
|
|
|
|
ID: ID,
|
|
|
|
InitiatorID: userID,
|
|
|
|
TargetID: "some-id",
|
|
|
|
AccountID: accountID,
|
|
|
|
Meta: map[string]any{"some": "meta"},
|
|
|
|
})
|
|
|
|
ID++
|
|
|
|
events = append(events, &activity.Event{
|
2023-04-03 15:09:35 +02:00
|
|
|
Timestamp: time.Now().UTC(),
|
2023-01-02 15:11:32 +01:00
|
|
|
Activity: activity.RuleRemoved,
|
|
|
|
ID: ID,
|
|
|
|
InitiatorID: userID,
|
|
|
|
TargetID: "some-id",
|
|
|
|
AccountID: accountID,
|
|
|
|
Meta: map[string]any{"some": "meta"},
|
|
|
|
})
|
|
|
|
ID++
|
|
|
|
events = append(events, &activity.Event{
|
2023-04-03 15:09:35 +02:00
|
|
|
Timestamp: time.Now().UTC(),
|
2023-01-02 15:11:32 +01:00
|
|
|
Activity: activity.RuleUpdated,
|
|
|
|
ID: ID,
|
|
|
|
InitiatorID: userID,
|
|
|
|
TargetID: "some-id",
|
|
|
|
AccountID: accountID,
|
|
|
|
Meta: map[string]any{"some": "meta"},
|
|
|
|
})
|
|
|
|
ID++
|
|
|
|
events = append(events, &activity.Event{
|
2023-04-03 15:09:35 +02:00
|
|
|
Timestamp: time.Now().UTC(),
|
2023-01-02 15:11:32 +01:00
|
|
|
Activity: activity.PeerAddedWithSetupKey,
|
|
|
|
ID: ID,
|
|
|
|
InitiatorID: userID,
|
|
|
|
TargetID: "some-id",
|
|
|
|
AccountID: accountID,
|
|
|
|
Meta: map[string]any{"some": "meta"},
|
|
|
|
})
|
|
|
|
return events
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvents_GetEvents(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
expectedStatus int
|
|
|
|
expectedBody bool
|
|
|
|
requestType string
|
|
|
|
requestPath string
|
|
|
|
requestBody io.Reader
|
|
|
|
}{
|
|
|
|
{
|
2023-02-28 15:01:24 +01:00
|
|
|
name: "GetAllEvents OK",
|
2023-01-02 15:11:32 +01:00
|
|
|
expectedBody: true,
|
|
|
|
requestType: http.MethodGet,
|
|
|
|
requestPath: "/api/events/",
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
accountID := "test_account"
|
|
|
|
adminUser := server.NewAdminUser("test_user")
|
|
|
|
events := generateEvents(accountID, adminUser.Id)
|
2024-09-22 22:44:10 +02:00
|
|
|
handler := initEventsTestData(accountID, events...)
|
2023-01-02 15:11:32 +01:00
|
|
|
|
|
|
|
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-02-28 15:01:24 +01:00
|
|
|
router.HandleFunc("/api/events/", handler.GetAllEvents).Methods("GET")
|
2023-01-02 15:11:32 +01:00
|
|
|
router.ServeHTTP(recorder, req)
|
|
|
|
|
|
|
|
res := recorder.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
if status := recorder.Code; status != tc.expectedStatus {
|
|
|
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
|
|
|
status, tc.expectedStatus)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tc.expectedBody {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("I don't know what I expected; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var got []*api.Event
|
|
|
|
if err = json.Unmarshal(content, &got); err != nil {
|
|
|
|
t.Fatalf("Sent content is not in correct json format; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, got, len(events))
|
|
|
|
actual := map[string]*api.Event{}
|
|
|
|
for _, event := range got {
|
|
|
|
actual[event.Id] = event
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, expected := range events {
|
|
|
|
event, ok := actual[strconv.FormatUint(expected.ID, 10)]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, expected.InitiatorID, event.InitiatorId)
|
|
|
|
assert.Equal(t, expected.TargetID, event.TargetId)
|
|
|
|
assert.Equal(t, expected.Activity.Message(), event.Activity)
|
|
|
|
assert.Equal(t, expected.Activity.StringCode(), string(event.ActivityCode))
|
|
|
|
assert.Equal(t, expected.Meta["some"], event.Meta["some"])
|
|
|
|
assert.True(t, expected.Timestamp.Equal(event.Timestamp))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|