Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: bcmmbaga <[email protected]>
  • Loading branch information
bcmmbaga committed Jan 2, 2025
1 parent 7e3ff30 commit 64fa18f
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 52 deletions.
37 changes: 18 additions & 19 deletions management/server/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package server
import (
"context"
"crypto/sha256"
"database/sql"
b64 "encoding/base64"
"encoding/json"
"fmt"
Expand All @@ -17,6 +16,7 @@ import (
"time"

"github.com/golang-jwt/jwt"
"github.com/netbirdio/netbird/management/server/util"

resourceTypes "github.com/netbirdio/netbird/management/server/networks/resources/types"
routerTypes "github.com/netbirdio/netbird/management/server/networks/routers/types"
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestAccount_GetPeerNetworkMap(t *testing.T) {
LoginExpired: true,
},
UserID: userID,
LastLogin: time.Now().UTC().Add(-time.Hour * 24 * 30 * 30),
LastLogin: util.ToPtr(time.Now().UTC().Add(-time.Hour * 24 * 30 * 30)),
},
"peer-2": {
ID: peerID2,
Expand All @@ -202,7 +202,7 @@ func TestAccount_GetPeerNetworkMap(t *testing.T) {
LoginExpired: false,
},
UserID: userID,
LastLogin: time.Now().UTC(),
LastLogin: util.ToPtr(time.Now().UTC()),
LoginExpirationEnabled: true,
},
},
Expand All @@ -226,7 +226,7 @@ func TestAccount_GetPeerNetworkMap(t *testing.T) {
LoginExpired: true,
},
UserID: userID,
LastLogin: time.Now().UTC().Add(-time.Hour * 24 * 30 * 30),
LastLogin: util.ToPtr(time.Now().UTC().Add(-time.Hour * 24 * 30 * 30)),
LoginExpirationEnabled: true,
},
"peer-2": {
Expand All @@ -241,7 +241,7 @@ func TestAccount_GetPeerNetworkMap(t *testing.T) {
LoginExpired: true,
},
UserID: userID,
LastLogin: time.Now().UTC().Add(-time.Hour * 24 * 30 * 30),
LastLogin: util.ToPtr(time.Now().UTC().Add(-time.Hour * 24 * 30 * 30)),
LoginExpirationEnabled: true,
},
},
Expand Down Expand Up @@ -814,7 +814,6 @@ func TestDefaultAccountManager_MarkPATUsed(t *testing.T) {
"tokenId": {
ID: "tokenId",
HashedToken: encodedHashedToken,
LastUsed: time.Time{},
},
},
}
Expand Down Expand Up @@ -1097,7 +1096,7 @@ func genUsers(p string, n int) map[string]*types.User {
users[fmt.Sprintf("%s-%d", p, i)] = &types.User{
Id: fmt.Sprintf("%s-%d", p, i),
Role: types.UserRoleAdmin,
LastLogin: sql.NullTime{Time: now, Valid: !now.IsZero()},
LastLogin: util.ToPtr(now),
CreatedAt: now,
Issued: "api",
AutoGroups: []string{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"},
Expand Down Expand Up @@ -1749,10 +1748,10 @@ func TestAccount_Copy(t *testing.T) {
ID: "pat1",
Name: "First PAT",
HashedToken: "SoMeHaShEdToKeN",
ExpirationDate: time.Now().UTC().AddDate(0, 0, 7),
ExpirationDate: util.ToPtr(time.Now().UTC().AddDate(0, 0, 7)),
CreatedBy: "user1",
CreatedAt: time.Now().UTC(),
LastUsed: time.Now().UTC(),
LastUsed: util.ToPtr(time.Now().UTC()),
},
},
},
Expand Down Expand Up @@ -2108,7 +2107,7 @@ func TestAccount_GetExpiredPeers(t *testing.T) {
Connected: true,
LoginExpired: false,
},
LastLogin: time.Now().UTC().Add(-30 * time.Minute),
LastLogin: util.ToPtr(time.Now().UTC().Add(-30 * time.Minute)),
UserID: userID,
},
"peer-2": {
Expand All @@ -2119,7 +2118,7 @@ func TestAccount_GetExpiredPeers(t *testing.T) {
Connected: true,
LoginExpired: false,
},
LastLogin: time.Now().UTC().Add(-2 * time.Hour),
LastLogin: util.ToPtr(time.Now().UTC().Add(-2 * time.Hour)),
UserID: userID,
},

Expand All @@ -2131,7 +2130,7 @@ func TestAccount_GetExpiredPeers(t *testing.T) {
Connected: true,
LoginExpired: false,
},
LastLogin: time.Now().UTC().Add(-1 * time.Hour),
LastLogin: util.ToPtr(time.Now().UTC().Add(-1 * time.Hour)),
UserID: userID,
},
},
Expand Down Expand Up @@ -2193,7 +2192,7 @@ func TestAccount_GetInactivePeers(t *testing.T) {
Connected: false,
LoginExpired: false,
},
LastLogin: time.Now().UTC().Add(-30 * time.Minute),
LastLogin: util.ToPtr(time.Now().UTC().Add(-30 * time.Minute)),
UserID: userID,
},
"peer-2": {
Expand All @@ -2204,7 +2203,7 @@ func TestAccount_GetInactivePeers(t *testing.T) {
Connected: false,
LoginExpired: false,
},
LastLogin: time.Now().UTC().Add(-2 * time.Hour),
LastLogin: util.ToPtr(time.Now().UTC().Add(-2 * time.Hour)),
UserID: userID,
},
"peer-3": {
Expand All @@ -2215,7 +2214,7 @@ func TestAccount_GetInactivePeers(t *testing.T) {
Connected: true,
LoginExpired: false,
},
LastLogin: time.Now().UTC().Add(-1 * time.Hour),
LastLogin: util.ToPtr(time.Now().UTC().Add(-1 * time.Hour)),
UserID: userID,
},
},
Expand Down Expand Up @@ -2485,7 +2484,7 @@ func TestAccount_GetNextPeerExpiration(t *testing.T) {
LoginExpired: false,
},
LoginExpirationEnabled: true,
LastLogin: time.Now().UTC(),
LastLogin: util.ToPtr(time.Now().UTC()),
UserID: userID,
},
"peer-2": {
Expand Down Expand Up @@ -2645,7 +2644,7 @@ func TestAccount_GetNextInactivePeerExpiration(t *testing.T) {
LastSeen: time.Now().Add(-1 * time.Second),
},
InactivityExpirationEnabled: true,
LastLogin: time.Now().UTC(),
LastLogin: util.ToPtr(time.Now().UTC()),
UserID: userID,
},
"peer-2": {
Expand Down Expand Up @@ -2723,8 +2722,8 @@ func TestAccount_SetJWTGroups(t *testing.T) {
},
Settings: &types.Settings{GroupsPropagationEnabled: true, JWTGroupsEnabled: true, JWTGroupsClaimName: "groups"},
Users: map[string]*types.User{
"user1": {Id: "user1", AccountID: "accountID"},
"user2": {Id: "user2", AccountID: "accountID"},
"user1": {Id: "user1", AccountID: "accountID", CreatedAt: time.Now()},
"user2": {Id: "user2", AccountID: "accountID", CreatedAt: time.Now()},
},
}

Expand Down
1 change: 1 addition & 0 deletions management/server/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ func initTestGroupAccount(am *DefaultAccountManager) (*DefaultAccountManager, *t
setupKey := &types.SetupKey{
Id: "example setup key",
AutoGroups: []string{groupForSetupKeys.ID},
UpdatedAt: time.Now(),
}

user := &types.User{
Expand Down
17 changes: 7 additions & 10 deletions management/server/http/handlers/routes/routes_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/netip"
"testing"

"github.com/netbirdio/netbird/management/server/util"
"github.com/stretchr/testify/require"

"github.com/netbirdio/netbird/management/server/http/api"
Expand Down Expand Up @@ -239,7 +240,7 @@ func TestRoutesHandlers(t *testing.T) {
Id: existingRouteID,
Description: "Post",
NetworkId: "awesomeNet",
Network: toPtr("192.168.0.0/16"),
Network: util.ToPtr("192.168.0.0/16"),
Peer: &existingPeerID,
NetworkType: route.IPv4NetworkString,
Masquerade: false,
Expand All @@ -259,7 +260,7 @@ func TestRoutesHandlers(t *testing.T) {
Id: existingRouteID,
Description: "Post",
NetworkId: "domainNet",
Network: toPtr("invalid Prefix"),
Network: util.ToPtr("invalid Prefix"),
KeepRoute: true,
Domains: &[]string{existingDomain},
Peer: &existingPeerID,
Expand All @@ -281,7 +282,7 @@ func TestRoutesHandlers(t *testing.T) {
Id: existingRouteID,
Description: "Post",
NetworkId: "awesomeNet",
Network: toPtr("192.168.0.0/16"),
Network: util.ToPtr("192.168.0.0/16"),
Peer: &existingPeerID,
NetworkType: route.IPv4NetworkString,
Masquerade: false,
Expand Down Expand Up @@ -385,7 +386,7 @@ func TestRoutesHandlers(t *testing.T) {
Id: existingRouteID,
Description: "Post",
NetworkId: "awesomeNet",
Network: toPtr("192.168.0.0/16"),
Network: util.ToPtr("192.168.0.0/16"),
Peer: &existingPeerID,
NetworkType: route.IPv4NetworkString,
Masquerade: false,
Expand All @@ -404,7 +405,7 @@ func TestRoutesHandlers(t *testing.T) {
Id: existingRouteID,
Description: "Post",
NetworkId: "awesomeNet",
Network: toPtr("invalid Prefix"),
Network: util.ToPtr("invalid Prefix"),
Domains: &[]string{existingDomain},
Peer: &existingPeerID,
NetworkType: route.DomainNetworkString,
Expand All @@ -425,7 +426,7 @@ func TestRoutesHandlers(t *testing.T) {
Id: existingRouteID,
Description: "Post",
NetworkId: "awesomeNet",
Network: toPtr("192.168.0.0/16"),
Network: util.ToPtr("192.168.0.0/16"),
Peer: &emptyString,
PeerGroups: &[]string{existingGroupID},
NetworkType: route.IPv4NetworkString,
Expand Down Expand Up @@ -663,7 +664,3 @@ func toApiRoute(t *testing.T, r *route.Route) *api.Route {
require.NoError(t, err, "Failed to convert route")
return apiRoute
}

func toPtr[T any](v T) *T {
return &v
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func toResponseBody(key *types.SetupKey) *api.SetupKey {
Id: key.Id,
Key: key.KeySecret,
Name: key.Name,
Expires: key.ExpiresAt,
Expires: key.ExpirationTime(),
Type: string(key.Type),
Valid: key.IsValid(),
Revoked: key.Revoked,
Expand Down
13 changes: 7 additions & 6 deletions management/server/http/handlers/users/pat_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/gorilla/mux"
"github.com/netbirdio/netbird/management/server/util"
"github.com/stretchr/testify/assert"

"github.com/netbirdio/netbird/management/server/http/api"
Expand Down Expand Up @@ -42,19 +43,19 @@ var testAccount = &types.Account{
ID: existingTokenID,
Name: "My first token",
HashedToken: "someHash",
ExpirationDate: time.Now().UTC().AddDate(0, 0, 7),
ExpirationDate: util.ToPtr(time.Now().UTC().AddDate(0, 0, 7)),
CreatedBy: existingUserID,
CreatedAt: time.Now().UTC(),
LastUsed: time.Now().UTC(),
LastUsed: util.ToPtr(time.Now().UTC()),
},
"token2": {
ID: "token2",
Name: "My second token",
HashedToken: "someOtherHash",
ExpirationDate: time.Now().UTC().AddDate(0, 0, 7),
ExpirationDate: util.ToPtr(time.Now().UTC().AddDate(0, 0, 7)),
CreatedBy: existingUserID,
CreatedAt: time.Now().UTC(),
LastUsed: time.Now().UTC(),
LastUsed: util.ToPtr(time.Now().UTC()),
},
},
},
Expand Down Expand Up @@ -248,8 +249,8 @@ func toTokenResponse(serverToken types.PersonalAccessToken) api.PersonalAccessTo
Id: serverToken.ID,
Name: serverToken.Name,
CreatedAt: serverToken.CreatedAt,
LastUsed: &serverToken.LastUsed,
LastUsed: serverToken.LastUsed,
CreatedBy: serverToken.CreatedBy,
ExpirationDate: serverToken.ExpirationDate,
ExpirationDate: serverToken.ExpirationTime(),
}
}
5 changes: 3 additions & 2 deletions management/server/http/middleware/auth_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/golang-jwt/jwt"
"github.com/netbirdio/netbird/management/server/util"

"github.com/netbirdio/netbird/management/server/http/middleware/bypass"
"github.com/netbirdio/netbird/management/server/jwtclaims"
Expand Down Expand Up @@ -39,10 +40,10 @@ var testAccount = &types.Account{
ID: tokenID,
Name: "My first token",
HashedToken: "someHash",
ExpirationDate: time.Now().UTC().AddDate(0, 0, 7),
ExpirationDate: util.ToPtr(time.Now().UTC().AddDate(0, 0, 7)),
CreatedBy: userID,
CreatedAt: time.Now().UTC(),
LastUsed: time.Now().UTC(),
LastUsed: util.ToPtr(time.Now().UTC()),
},
},
},
Expand Down
7 changes: 4 additions & 3 deletions management/server/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"
"time"

"github.com/netbirdio/netbird/management/server/util"
"github.com/rs/xid"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -80,7 +81,7 @@ func TestPeer_LoginExpired(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
peer := &nbpeer.Peer{
LoginExpirationEnabled: c.expirationEnabled,
LastLogin: c.lastLogin,
LastLogin: util.ToPtr(c.lastLogin),
UserID: userID,
}

Expand Down Expand Up @@ -141,7 +142,7 @@ func TestPeer_SessionExpired(t *testing.T) {
}
peer := &nbpeer.Peer{
InactivityExpirationEnabled: c.expirationEnabled,
LastLogin: c.lastLogin,
LastLogin: util.ToPtr(c.lastLogin),
Status: peerStatus,
UserID: userID,
}
Expand Down Expand Up @@ -1209,7 +1210,7 @@ func Test_RegisterPeerByUser(t *testing.T) {
UserID: existingUserID,
Status: &nbpeer.PeerStatus{Connected: false, LastSeen: time.Now()},
SSHEnabled: false,
LastLogin: time.Now(),
LastLogin: util.ToPtr(time.Now()),
}

addedPeer, _, _, err := am.AddPeer(context.Background(), "", existingUserID, newPeer)
Expand Down
4 changes: 2 additions & 2 deletions management/server/setupkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestDefaultAccountManager_SaveSetupKey(t *testing.T) {
t.Fatal(err)
}

assertKey(t, newKey, keyName, revoked, "reusable", 0, key.CreatedAt, key.ExpiresAt,
assertKey(t, newKey, keyName, revoked, "reusable", 0, key.CreatedAt, key.ExpirationTime(),
key.Id, time.Now().UTC(), autoGroups, true)

// check the corresponding events that should have been generated
Expand Down Expand Up @@ -391,7 +391,7 @@ func TestSetupKey_Copy(t *testing.T) {
key, _ := types.GenerateSetupKey("key name", types.SetupKeyOneOff, time.Hour, []string{}, types.SetupKeyUnlimitedUsage, false)
keyCopy := key.Copy()

assertKey(t, keyCopy, key.Name, key.Revoked, string(key.Type), key.UsedTimes, key.CreatedAt, key.ExpiresAt, key.Id,
assertKey(t, keyCopy, key.Name, key.Revoked, string(key.Type), key.UsedTimes, key.CreatedAt, key.ExpirationTime(), key.Id,
key.UpdatedAt, key.AutoGroups, true)

}
Expand Down
Loading

0 comments on commit 64fa18f

Please sign in to comment.