lib/auth: follow-up after b6a6a659f4

This commit is contained in:
Aliaksandr Valialkin 2022-08-07 23:14:39 +03:00
parent a266e3e136
commit a17030090b
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1

View File

@ -6,54 +6,72 @@ import (
) )
func TestNewTokenSuccess(t *testing.T) { func TestNewTokenSuccess(t *testing.T) {
f := func(name string, token string, want string) { f := func(token string, want string) {
t.Helper() t.Helper()
t.Run(name, func(t *testing.T) {
newToken, err := NewToken(token) newToken, err := NewToken(token)
if err != nil { if err != nil {
t.Fatalf("expecting nil error") t.Fatalf("unexpected error: %s", err)
} }
got := fmt.Sprintf("%d:%d", newToken.AccountID, newToken.ProjectID) got := fmt.Sprintf("%d:%d", newToken.AccountID, newToken.ProjectID)
if got != want { if got != want {
t.Errorf("NewToken() got = %v, want %v", newToken, want) t.Fatalf("unexpected NewToken() result;got\n%s\nwant\n%s", got, want)
} }
})
} }
f("token with accountID and projectID", "1:2", "1:2") // token with accountID only
f("max uint32 accountID", "4294967295:1", "4294967295:1") f("1", "1:0")
f("max uint32 projectID", "1:4294967295", "1:4294967295") // token with accountID and projecTID
f("max uint32 accountID and projectID", "4294967295:4294967295", "4294967295:4294967295") f("1:2", "1:2")
// max uint32 accountID
f("4294967295:1", "4294967295:1")
// max uint32 projectID
f("1:4294967295", "1:4294967295")
// max uint32 accountID and projectID
f("4294967295:4294967295", "4294967295:4294967295")
} }
func TestNewTokenFailure(t *testing.T) { func TestNewTokenFailure(t *testing.T) {
f := func(name string, token string) { f := func(token string) {
t.Helper() t.Helper()
t.Run(name, func(t *testing.T) {
newToken, err := NewToken(token) newToken, err := NewToken(token)
if err == nil { if err == nil {
t.Fatalf("expecting non-nil error") t.Fatalf("expecting non-nil error")
} }
if newToken != nil { if newToken != nil {
t.Fatalf("expecting nil token") t.Fatalf("expecting nil token; got\n%#v", newToken)
} }
})
} }
f("empty token", "") // empty token
f("empty accountID and projectID", ":") f("")
f("accountID and projectID not int values", "a:b") // empty accountID and projectID"
f("missed projectID", "1:") f(":")
f("missed accountID", ":2") // accountID and projectID not int values
f("large int value for accountID", "9223372036854775808:1") f("a:b")
f("large int value for projectID", "2:9223372036854775808") // missed projectID
f("both large int values incorrect", "9223372036854775809:9223372036854775808") f("1:")
f("large uint32 values incorrect", "4294967297:4294967295") // missed accountID
f("negative accountID", "-100:100") f(":2")
f("negative projectID", "100:-100") // large int value for accountID
f("negative accountID and projectID", "-100:-100") f("9223372036854775808:1")
f("accountID is string", "abcd:2") // large value for projectID
f("projectID is string", "2:abcd") f("2:9223372036854775808")
f("empty many parts in the token", "::") // both large values incorrect
f("many string parts in the token", "a:b:c") f("9223372036854775809:9223372036854775808")
f("many int parts in the token", "1:2:3") // large uint32 values incorrect
f("4294967297:4294967295")
//negative accountID
f("-100:100")
// negative projectID
f("100:-100")
// negative accountID and projectID
f("-100:-100")
// accountID is string
f("abcd:2")
// projectID is string
f("2:abcd")
// empty many parts in the token
f("::")
// many string parts in the token
f("a:b:c")
// many int parts in the token"
f("1:2:3")
} }