Skip to content

Commit

Permalink
Let HasEmail use the new FindIDByFieldValue function, ref #1
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Dec 1, 2024
1 parent 6000b22 commit eda0159
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 16 deletions.
21 changes: 8 additions & 13 deletions userstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,26 +309,21 @@ func (state *UserState) HasUser2(username string) (bool, error) {
return val, nil
}

// HasEmail finds the user that has a given e-mail address.
// Returns the username and nil if found or a blank string and ErrNotFound if not.
// HasEmail finds the username that has a given email address.
// Returns the username and nil if found, or a blank string and ErrNotFound if not.
func (state *UserState) HasEmail(email string) (string, error) {
if email == "" {
return "", ErrNotFound
}
usernames, err := state.AllUsernames()
// Use the new FindIDByFieldValue method to find the username by email
username, err := state.users.FindIDByFieldValue("email", email)
if err != nil {
return "", err
}
for _, username := range usernames {
userEmail, err := state.Email(username)
if err != nil {
return "", err
}
if userEmail == email {
return username, nil
if err == simpleredis.ErrNotFound {
return "", ErrNotFound
}
return "", err
}
return "", ErrNotFound
return username, nil
}

// BooleanField returns the boolean value for a given username and field name.
Expand Down
66 changes: 63 additions & 3 deletions userstate_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package permissions

import (
"github.com/xyproto/pinterface"
"testing"
"time"

"github.com/xyproto/pinterface"
)

func TestPerm(t *testing.T) {
Expand Down Expand Up @@ -57,7 +58,6 @@ func TestPasswordBasic(t *testing.T) {
if userstate.PasswordAlgo() != "sha256" {
t.Error("Error, setting password algorithm failed")
}

}

func TestPasswordBasic2(t *testing.T) {
Expand All @@ -79,7 +79,6 @@ func TestPasswordBasic2(t *testing.T) {
if userstate.PasswordAlgo() != "sha256" {
t.Error("Error, setting password algorithm failed")
}

}

// Check if the functionality for backwards compatible hashing works
Expand Down Expand Up @@ -318,3 +317,64 @@ func TestTiming(t *testing.T) {
}
//fmt.Println(baseMin, base_max, elapsed1, elapsed2, elapsed3, elapsed4)
}

func TestHasEmail(t *testing.T) {
const (
username1 = "alice"
password1 = "password1"
email1 = "[email protected]"

username2 = "bob"
password2 = "password2"
email2 = "[email protected]"

username3 = "charlie"
password3 = "password3"
email3 = "[email protected]"

nonExistentEmail = "[email protected]"
)

userstate := NewUserStateSimple()
defer userstate.Close()

// Add test users
userstate.AddUser(username1, password1, email1)
defer userstate.RemoveUser(username1)

userstate.AddUser(username2, password2, email2)
defer userstate.RemoveUser(username2)

userstate.AddUser(username3, password3, email3)
defer userstate.RemoveUser(username3)

// Test HasEmail with an existing email
username, err := userstate.HasEmail(email2)
if err != nil {
t.Errorf("Expected to find email '%s', but got error: %v", email2, err)
} else if username != username2 {
t.Errorf("Expected username '%s', but got '%s'", username2, username)
} else {
t.Logf("Successfully found username '%s' for email '%s'", username, email2)
}

// Test HasEmail with a non-existent email
username, err = userstate.HasEmail(nonExistentEmail)
if err != ErrNotFound {
t.Errorf("Expected ErrNotFound for email '%s', but got error: %v", nonExistentEmail, err)
} else if username != "" {
t.Errorf("Expected empty username for non-existent email, but got '%s'", username)
} else {
t.Logf("Correctly did not find username for non-existent email '%s'", nonExistentEmail)
}

// Test HasEmail with an empty email
username, err = userstate.HasEmail("")
if err != ErrNotFound {
t.Errorf("Expected ErrNotFound for empty email, but got error: %v", err)
} else if username != "" {
t.Errorf("Expected empty username for empty email, but got '%s'", username)
} else {
t.Log("Correctly handled empty email input")
}
}

0 comments on commit eda0159

Please sign in to comment.