|
@ -4,8 +4,6 @@ import ( |
|
|
"errors" |
|
|
"errors" |
|
|
"strings" |
|
|
"strings" |
|
|
"testing" |
|
|
"testing" |
|
|
|
|
|
|
|
|
"git.aiterp.net/gisle/wrouter/generate" |
|
|
|
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
var ErrExists = errors.New("auth: user exists") |
|
|
var ErrExists = errors.New("auth: user exists") |
|
@ -24,17 +22,6 @@ func (ta *testAuther) ID() string { |
|
|
func (ta *testAuther) Name() string { |
|
|
func (ta *testAuther) Name() string { |
|
|
return ta.FullName |
|
|
return ta.FullName |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (ta *testAuther) Exists(username string) bool { |
|
|
|
|
|
for _, user := range ta.users { |
|
|
|
|
|
if user.Name == username { |
|
|
|
|
|
return true |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return false |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (ta *testAuther) Find(userid string) *User { |
|
|
func (ta *testAuther) Find(userid string) *User { |
|
|
for _, user := range ta.users { |
|
|
for _, user := range ta.users { |
|
|
if user.ID == userid { |
|
|
if user.ID == userid { |
|
@ -45,9 +32,9 @@ func (ta *testAuther) Find(userid string) *User { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (ta *testAuther) Login(username, password string) (*User, error) { |
|
|
|
|
|
|
|
|
func (ta *testAuther) Login(userid, password string) (*User, error) { |
|
|
for _, user := range ta.users { |
|
|
for _, user := range ta.users { |
|
|
if user.Name == username && password == ta.passwords[user.ID] { |
|
|
|
|
|
|
|
|
if user.ID == userid && password == ta.passwords[user.ID] { |
|
|
return user, nil |
|
|
return user, nil |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -55,8 +42,8 @@ func (ta *testAuther) Login(username, password string) (*User, error) { |
|
|
return nil, ErrLogin |
|
|
return nil, ErrLogin |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (ta *testAuther) Register(username, password string, data map[string]string) (*User, error) { |
|
|
|
|
|
if ta.Exists(username) { |
|
|
|
|
|
|
|
|
func (ta *testAuther) Register(userid, password string, data map[string]string) (*User, error) { |
|
|
|
|
|
if ta.Find(userid) != nil { |
|
|
return nil, ErrExists |
|
|
return nil, ErrExists |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -64,15 +51,13 @@ func (ta *testAuther) Register(username, password string, data map[string]string |
|
|
ta.passwords = make(map[string]string) |
|
|
ta.passwords = make(map[string]string) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
id := generate.ID() |
|
|
|
|
|
ta.passwords[id] = password |
|
|
|
|
|
|
|
|
|
|
|
user := NewUser(ta, id, username, "member", data) |
|
|
|
|
|
|
|
|
user := NewUser(ta, userid, "member", data) |
|
|
ta.users = append(ta.users, user) |
|
|
ta.users = append(ta.users, user) |
|
|
|
|
|
ta.passwords[userid] = password |
|
|
return user, nil |
|
|
return user, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func TestList(t *testing.T) { |
|
|
|
|
|
|
|
|
func TestAuthenticator(t *testing.T) { |
|
|
ta1 := testAuther{FullName: "Auth1"} |
|
|
ta1 := testAuther{FullName: "Auth1"} |
|
|
ta2 := testAuther{FullName: "Auth2"} |
|
|
ta2 := testAuther{FullName: "Auth2"} |
|
|
Register(&ta1) |
|
|
Register(&ta1) |
|
@ -105,33 +90,33 @@ func TestList(t *testing.T) { |
|
|
|
|
|
|
|
|
t.Run("Register", func(t *testing.T) { |
|
|
t.Run("Register", func(t *testing.T) { |
|
|
user, err := ta1.Register("Test", "CakesAndStuff", nil) |
|
|
user, err := ta1.Register("Test", "CakesAndStuff", nil) |
|
|
if err != nil || user.Name != "Test" { |
|
|
|
|
|
t.Logf("err = %v; name = \"%s\"", err, user.Name) |
|
|
|
|
|
|
|
|
if err != nil || user.ID != "Test" { |
|
|
|
|
|
t.Logf("err = %v; name = \"%s\"", err, user.ID) |
|
|
t.Fail() |
|
|
t.Fail() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if !ta1.Exists("Test") { |
|
|
|
|
|
|
|
|
if ta1.Find("Test") == nil { |
|
|
t.Log("Registered user does not exist") |
|
|
t.Log("Registered user does not exist") |
|
|
t.Fail() |
|
|
t.Fail() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
user2, err := ta1.Register("Test", "CakesAndStuff", nil) |
|
|
user2, err := ta1.Register("Test", "CakesAndStuff", nil) |
|
|
if err == nil || user2 != nil { |
|
|
if err == nil || user2 != nil { |
|
|
t.Logf("err = %s; name = \"%s\"", err, user2.Name) |
|
|
|
|
|
|
|
|
t.Logf("err = %s; name = \"%s\"", err, user2.ID) |
|
|
t.Fail() |
|
|
t.Fail() |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
t.Run("Login", func(t *testing.T) { |
|
|
t.Run("Login", func(t *testing.T) { |
|
|
user, err := ta1.Login("Test", "CakesAndStuff") |
|
|
user, err := ta1.Login("Test", "CakesAndStuff") |
|
|
if err != nil || user.Name != "Test" { |
|
|
|
|
|
t.Logf("err = %v; name = \"%s\"", err, user.Name) |
|
|
|
|
|
|
|
|
if err != nil || user.ID != "Test" { |
|
|
|
|
|
t.Logf("err = %v; user = %+v", err, user) |
|
|
t.Fail() |
|
|
t.Fail() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
user2, err := ta1.Login("Test", "WrongPassword") |
|
|
user2, err := ta1.Login("Test", "WrongPassword") |
|
|
if err == nil || user2 != nil { |
|
|
if err == nil || user2 != nil { |
|
|
t.Logf("err = %v; name = \"%s\"", err, user.Name) |
|
|
|
|
|
|
|
|
t.Logf("err = %v; user = %+v", err, user2) |
|
|
t.Fail() |
|
|
t.Fail() |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|