mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-29 07:49:14 +01:00
[management] reduce calls to update last usage for users and pats (#8290)
This commit is contained in:
@@ -779,3 +779,66 @@ func TestAuthMiddleware_Handler_Child(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthMiddleware_MarkPATUsedThrottled(t *testing.T) {
|
||||
tt := []struct {
|
||||
name string
|
||||
lastUsed *time.Time
|
||||
expectMark bool
|
||||
}{
|
||||
{name: "never used", lastUsed: nil, expectMark: true},
|
||||
{name: "used just now", lastUsed: util.ToPtr(time.Now().UTC()), expectMark: false},
|
||||
{name: "used before the update interval", lastUsed: util.ToPtr(time.Now().UTC().Add(-2 * types.PATLastUsedUpdateInterval)), expectMark: true},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
pat := &types.PersonalAccessToken{
|
||||
ID: tokenID,
|
||||
ExpirationDate: util.ToPtr(time.Now().UTC().AddDate(0, 0, 7)),
|
||||
LastUsed: tc.lastUsed,
|
||||
}
|
||||
|
||||
marked := 0
|
||||
mockAuth := &auth.MockManager{
|
||||
GetPATInfoFunc: func(_ context.Context, _ string) (*types.User, *types.PersonalAccessToken, string, string, error) {
|
||||
return testAccount.Users[userID], pat, testAccount.Domain, testAccount.DomainCategory, nil
|
||||
},
|
||||
MarkPATUsedFunc: func(_ context.Context, _ string) error {
|
||||
marked++
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
disabledLimiter := ratelimit.NewAPIRateLimiter(nil)
|
||||
disabledLimiter.SetEnabled(false)
|
||||
authMiddleware := NewAuthMiddleware(
|
||||
mockAuth,
|
||||
func(ctx context.Context, userAuth nbauth.UserAuth) (string, string, error) {
|
||||
return userAuth.AccountId, userAuth.UserId, nil
|
||||
},
|
||||
func(ctx context.Context, userAuth nbauth.UserAuth) error {
|
||||
return nil
|
||||
},
|
||||
func(ctx context.Context, userAuth nbauth.UserAuth) (*types.User, error) {
|
||||
return &types.User{}, nil
|
||||
},
|
||||
disabledLimiter,
|
||||
nil,
|
||||
)
|
||||
|
||||
handler := authMiddleware.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
||||
req := httptest.NewRequest("GET", "http://testing/test", nil)
|
||||
req.Header.Set("Authorization", "Token "+PAT)
|
||||
rec := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rec, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
if tc.expectMark {
|
||||
assert.Equal(t, 1, marked, "MarkPATUsed must be called")
|
||||
} else {
|
||||
assert.Equal(t, 0, marked, "MarkPATUsed must be skipped")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user