70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/gochat/gochat/internal/config"
|
|
)
|
|
|
|
func TestRefreshTokenStore_ValidateForClient_Expired_Cov5(t *testing.T) {
|
|
store := NewRefreshTokenStore(nil, &config.JWTConfig{RefreshExpiryHours: 1})
|
|
ctx := context.Background()
|
|
// Store a token
|
|
err := store.StoreForClient(ctx, 1, "client1", "token123")
|
|
require.NoError(t, err)
|
|
|
|
// Validate with correct token
|
|
valid, err := store.ValidateForClient(ctx, 1, "client1", "token123")
|
|
assert.NoError(t, err)
|
|
assert.True(t, valid)
|
|
|
|
// Validate with wrong token
|
|
valid, err = store.ValidateForClient(ctx, 1, "client1", "wrong")
|
|
assert.NoError(t, err)
|
|
assert.False(t, valid)
|
|
|
|
// Validate with non-existent user
|
|
valid, err = store.ValidateForClient(ctx, 999, "client1", "token123")
|
|
assert.NoError(t, err)
|
|
assert.False(t, valid)
|
|
}
|
|
|
|
func TestRefreshTokenStore_HasClient_Cov5(t *testing.T) {
|
|
store := NewRefreshTokenStore(nil, &config.JWTConfig{RefreshExpiryHours: 3600})
|
|
ctx := context.Background()
|
|
|
|
// Store a token
|
|
err := store.StoreForClient(ctx, 1, "client1", "token123")
|
|
require.NoError(t, err)
|
|
|
|
// HasClient should return true
|
|
has, err := store.HasClient(ctx, 1, "client1")
|
|
assert.NoError(t, err)
|
|
assert.True(t, has)
|
|
|
|
// Non-existent client
|
|
has, err = store.HasClient(ctx, 1, "nonexistent")
|
|
assert.NoError(t, err)
|
|
assert.False(t, has)
|
|
}
|
|
|
|
func TestRefreshTokenStore_RevokeClient_Cov5(t *testing.T) {
|
|
store := NewRefreshTokenStore(nil, &config.JWTConfig{RefreshExpiryHours: 3600})
|
|
ctx := context.Background()
|
|
|
|
err := store.StoreForClient(ctx, 1, "client1", "token123")
|
|
require.NoError(t, err)
|
|
|
|
// Revoke
|
|
err = store.RevokeClient(ctx, 1, "client1")
|
|
assert.NoError(t, err)
|
|
|
|
// Should no longer have client
|
|
has, _ := store.HasClient(ctx, 1, "client1")
|
|
assert.False(t, has)
|
|
}
|