mirror of
https://github.com/redis/go-redis.git
synced 2025-07-19 11:43:14 +03:00
test: refactor TestBasicCredentials using table-driven tests (#3406)
* test: refactor TestBasicCredentials using table-driven tests * Included additional edge cases: - Empty passwords - Special characters - Long strings - Unicode characters
This commit is contained in:
@ -2,6 +2,7 @@ package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@ -179,36 +180,90 @@ func TestStreamingCredentialsProvider(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBasicCredentials(t *testing.T) {
|
||||
t.Run("basic auth", func(t *testing.T) {
|
||||
creds := NewBasicCredentials("user1", "pass1")
|
||||
username, password := creds.BasicAuth()
|
||||
if username != "user1" {
|
||||
t.Fatalf("expected username 'user1', got '%s'", username)
|
||||
tests := []struct {
|
||||
name string
|
||||
username string
|
||||
password string
|
||||
expectedUser string
|
||||
expectedPass string
|
||||
expectedRaw string
|
||||
}{
|
||||
{
|
||||
name: "basic auth",
|
||||
username: "user1",
|
||||
password: "pass1",
|
||||
expectedUser: "user1",
|
||||
expectedPass: "pass1",
|
||||
expectedRaw: "user1:pass1",
|
||||
},
|
||||
{
|
||||
name: "empty username",
|
||||
username: "",
|
||||
password: "pass1",
|
||||
expectedUser: "",
|
||||
expectedPass: "pass1",
|
||||
expectedRaw: ":pass1",
|
||||
},
|
||||
{
|
||||
name: "empty password",
|
||||
username: "user1",
|
||||
password: "",
|
||||
expectedUser: "user1",
|
||||
expectedPass: "",
|
||||
expectedRaw: "user1:",
|
||||
},
|
||||
{
|
||||
name: "both username and password empty",
|
||||
username: "",
|
||||
password: "",
|
||||
expectedUser: "",
|
||||
expectedPass: "",
|
||||
expectedRaw: ":",
|
||||
},
|
||||
{
|
||||
name: "special characters",
|
||||
username: "user:1",
|
||||
password: "pa:ss@!#",
|
||||
expectedUser: "user:1",
|
||||
expectedPass: "pa:ss@!#",
|
||||
expectedRaw: "user:1:pa:ss@!#",
|
||||
},
|
||||
{
|
||||
name: "unicode characters",
|
||||
username: "ユーザー",
|
||||
password: "密碼123",
|
||||
expectedUser: "ユーザー",
|
||||
expectedPass: "密碼123",
|
||||
expectedRaw: "ユーザー:密碼123",
|
||||
},
|
||||
{
|
||||
name: "long credentials",
|
||||
username: strings.Repeat("u", 1000),
|
||||
password: strings.Repeat("p", 1000),
|
||||
expectedUser: strings.Repeat("u", 1000),
|
||||
expectedPass: strings.Repeat("p", 1000),
|
||||
expectedRaw: strings.Repeat("u", 1000) + ":" + strings.Repeat("p", 1000),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
creds := NewBasicCredentials(tt.username, tt.password)
|
||||
|
||||
user, pass := creds.BasicAuth()
|
||||
if user != tt.expectedUser {
|
||||
t.Errorf("BasicAuth() username = %q; want %q", user, tt.expectedUser)
|
||||
}
|
||||
if pass != tt.expectedPass {
|
||||
t.Errorf("BasicAuth() password = %q; want %q", pass, tt.expectedPass)
|
||||
}
|
||||
if password != "pass1" {
|
||||
t.Fatalf("expected password 'pass1', got '%s'", password)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("raw credentials", func(t *testing.T) {
|
||||
creds := NewBasicCredentials("user1", "pass1")
|
||||
raw := creds.RawCredentials()
|
||||
expected := "user1:pass1"
|
||||
if raw != expected {
|
||||
t.Fatalf("expected raw credentials '%s', got '%s'", expected, raw)
|
||||
if raw != tt.expectedRaw {
|
||||
t.Errorf("RawCredentials() = %q; want %q", raw, tt.expectedRaw)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("empty username", func(t *testing.T) {
|
||||
creds := NewBasicCredentials("", "pass1")
|
||||
username, password := creds.BasicAuth()
|
||||
if username != "" {
|
||||
t.Fatalf("expected empty username, got '%s'", username)
|
||||
}
|
||||
if password != "pass1" {
|
||||
t.Fatalf("expected password 'pass1', got '%s'", password)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestReAuthCredentialsListener(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user