From d8e1c2453b9494e69a62b80e8bd4b217bc186af4 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Tue, 8 Aug 2017 22:27:32 +0200 Subject: [PATCH] Removed Name from user, only use ID --- auth/authenticator.go | 5 ++--- auth/authenticator_test.go | 43 +++++++++++++------------------------- auth/session_test.go | 2 +- auth/user.go | 5 ++--- 4 files changed, 19 insertions(+), 36 deletions(-) diff --git a/auth/authenticator.go b/auth/authenticator.go index 8c164de..9535cac 100644 --- a/auth/authenticator.go +++ b/auth/authenticator.go @@ -3,8 +3,7 @@ package auth type Authenticator interface { ID() string Name() string - Exists(username string) bool Find(userid string) *User - Login(username, password string) (*User, error) - Register(username, password string, data map[string]string) (*User, error) + Login(userid, password string) (*User, error) + Register(userid, password string, data map[string]string) (*User, error) } diff --git a/auth/authenticator_test.go b/auth/authenticator_test.go index c98b9fb..4e7b4b0 100644 --- a/auth/authenticator_test.go +++ b/auth/authenticator_test.go @@ -4,8 +4,6 @@ import ( "errors" "strings" "testing" - - "git.aiterp.net/gisle/wrouter/generate" ) var ErrExists = errors.New("auth: user exists") @@ -24,17 +22,6 @@ func (ta *testAuther) ID() string { func (ta *testAuther) Name() string { 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 { for _, user := range ta.users { if user.ID == userid { @@ -45,9 +32,9 @@ func (ta *testAuther) Find(userid string) *User { 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 { - if user.Name == username && password == ta.passwords[user.ID] { + if user.ID == userid && password == ta.passwords[user.ID] { return user, nil } } @@ -55,8 +42,8 @@ func (ta *testAuther) Login(username, password string) (*User, error) { 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 } @@ -64,15 +51,13 @@ func (ta *testAuther) Register(username, password string, data 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.passwords[userid] = password return user, nil } -func TestList(t *testing.T) { +func TestAuthenticator(t *testing.T) { ta1 := testAuther{FullName: "Auth1"} ta2 := testAuther{FullName: "Auth2"} Register(&ta1) @@ -105,33 +90,33 @@ func TestList(t *testing.T) { t.Run("Register", func(t *testing.T) { 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() } - if !ta1.Exists("Test") { + if ta1.Find("Test") == nil { t.Log("Registered user does not exist") t.Fail() } user2, err := ta1.Register("Test", "CakesAndStuff", 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.Run("Login", func(t *testing.T) { 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() } user2, err := ta1.Login("Test", "WrongPassword") if err == nil || user2 != nil { - t.Logf("err = %v; name = \"%s\"", err, user.Name) + t.Logf("err = %v; user = %+v", err, user2) t.Fail() } }) diff --git a/auth/session_test.go b/auth/session_test.go index 2059ec8..f2089d0 100644 --- a/auth/session_test.go +++ b/auth/session_test.go @@ -5,7 +5,7 @@ import "testing" func TestSession(t *testing.T) { auther := testAuther{FullName: "Test"} - user := NewUser(&auther, "Tester", "Tester", "member", nil) + user := NewUser(&auther, "Tester", "member", nil) sessions := []*Session{OpenSession(user), OpenSession(user), OpenSession(user)} ids := []string{sessions[0].ID, sessions[1].ID, sessions[2].ID} diff --git a/auth/user.go b/auth/user.go index 933914a..3c032c4 100644 --- a/auth/user.go +++ b/auth/user.go @@ -2,7 +2,6 @@ package auth type User struct { ID string - Name string Level string Data map[string]string @@ -26,6 +25,6 @@ func (user *User) LoggedOut() bool { } // NewUser creates a new User object -func NewUser(method Authenticator, id, name, level string, data map[string]string) *User { - return &User{id, name, level, data, method, false} +func NewUser(method Authenticator, id, level string, data map[string]string) *User { + return &User{id, level, data, method, false} }