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 (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -179,36 +180,90 @@ func TestStreamingCredentialsProvider(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestBasicCredentials(t *testing.T) {
|
func TestBasicCredentials(t *testing.T) {
|
||||||
t.Run("basic auth", func(t *testing.T) {
|
tests := []struct {
|
||||||
creds := NewBasicCredentials("user1", "pass1")
|
name string
|
||||||
username, password := creds.BasicAuth()
|
username string
|
||||||
if username != "user1" {
|
password string
|
||||||
t.Fatalf("expected username 'user1', got '%s'", username)
|
expectedUser string
|
||||||
}
|
expectedPass string
|
||||||
if password != "pass1" {
|
expectedRaw string
|
||||||
t.Fatalf("expected password 'pass1', got '%s'", password)
|
}{
|
||||||
}
|
{
|
||||||
})
|
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),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
t.Run("raw credentials", func(t *testing.T) {
|
for _, tt := range tests {
|
||||||
creds := NewBasicCredentials("user1", "pass1")
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
raw := creds.RawCredentials()
|
creds := NewBasicCredentials(tt.username, tt.password)
|
||||||
expected := "user1:pass1"
|
|
||||||
if raw != expected {
|
|
||||||
t.Fatalf("expected raw credentials '%s', got '%s'", expected, raw)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("empty username", func(t *testing.T) {
|
user, pass := creds.BasicAuth()
|
||||||
creds := NewBasicCredentials("", "pass1")
|
if user != tt.expectedUser {
|
||||||
username, password := creds.BasicAuth()
|
t.Errorf("BasicAuth() username = %q; want %q", user, tt.expectedUser)
|
||||||
if username != "" {
|
}
|
||||||
t.Fatalf("expected empty username, got '%s'", username)
|
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)
|
|
||||||
}
|
raw := creds.RawCredentials()
|
||||||
})
|
if raw != tt.expectedRaw {
|
||||||
|
t.Errorf("RawCredentials() = %q; want %q", raw, tt.expectedRaw)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReAuthCredentialsListener(t *testing.T) {
|
func TestReAuthCredentialsListener(t *testing.T) {
|
||||||
|
Reference in New Issue
Block a user