2023-01-02 15:11:32 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2023-01-02 15:11:32 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
2023-04-03 15:09:35 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
2023-01-02 15:11:32 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func generateAndStoreEvents(t *testing.T, manager *DefaultAccountManager, typ activity.Activity, initiatorID, targetID,
|
|
|
|
accountID string, count int) {
|
2023-11-10 16:33:13 +01:00
|
|
|
t.Helper()
|
2023-01-02 15:11:32 +01:00
|
|
|
for i := 0; i < count; i++ {
|
2024-07-03 11:33:02 +02:00
|
|
|
_, err := manager.eventStore.Save(context.Background(), &activity.Event{
|
2023-04-03 15:09:35 +02:00
|
|
|
Timestamp: time.Now().UTC(),
|
2023-01-02 15:11:32 +01:00
|
|
|
Activity: typ,
|
|
|
|
InitiatorID: initiatorID,
|
|
|
|
TargetID: targetID,
|
|
|
|
AccountID: accountID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultAccountManager_GetEvents(t *testing.T) {
|
|
|
|
manager, err := createManager(t)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
accountID := "accountID"
|
|
|
|
|
|
|
|
t.Run("get empty events list", func(t *testing.T) {
|
2024-07-03 11:33:02 +02:00
|
|
|
events, err := manager.GetEvents(context.Background(), accountID, userID)
|
2023-01-02 15:11:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Len(t, events, 0)
|
2024-07-03 11:33:02 +02:00
|
|
|
_ = manager.eventStore.Close(context.Background()) //nolint
|
2023-01-02 15:11:32 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("get events", func(t *testing.T) {
|
|
|
|
generateAndStoreEvents(t, manager, activity.PeerAddedByUser, userID, "peer", accountID, 10)
|
2024-07-03 11:33:02 +02:00
|
|
|
events, err := manager.GetEvents(context.Background(), accountID, userID)
|
2023-01-02 15:11:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, events, 10)
|
2024-07-03 11:33:02 +02:00
|
|
|
_ = manager.eventStore.Close(context.Background()) //nolint
|
2023-01-02 15:11:32 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("get events without duplicates", func(t *testing.T) {
|
|
|
|
generateAndStoreEvents(t, manager, activity.UserJoined, userID, "", accountID, 10)
|
2024-07-03 11:33:02 +02:00
|
|
|
events, err := manager.GetEvents(context.Background(), accountID, userID)
|
2023-01-02 15:11:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Len(t, events, 1)
|
2024-07-03 11:33:02 +02:00
|
|
|
_ = manager.eventStore.Close(context.Background()) //nolint
|
2023-01-02 15:11:32 +01:00
|
|
|
})
|
|
|
|
}
|