Skip to content

Commit eda0159

Browse files
committed
Let HasEmail use the new FindIDByFieldValue function, ref #1
1 parent 6000b22 commit eda0159

File tree

2 files changed

+71
-16
lines changed

2 files changed

+71
-16
lines changed

userstate.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -309,26 +309,21 @@ func (state *UserState) HasUser2(username string) (bool, error) {
309309
return val, nil
310310
}
311311

312-
// HasEmail finds the user that has a given e-mail address.
313-
// Returns the username and nil if found or a blank string and ErrNotFound if not.
312+
// HasEmail finds the username that has a given email address.
313+
// Returns the username and nil if found, or a blank string and ErrNotFound if not.
314314
func (state *UserState) HasEmail(email string) (string, error) {
315315
if email == "" {
316316
return "", ErrNotFound
317317
}
318-
usernames, err := state.AllUsernames()
318+
// Use the new FindIDByFieldValue method to find the username by email
319+
username, err := state.users.FindIDByFieldValue("email", email)
319320
if err != nil {
320-
return "", err
321-
}
322-
for _, username := range usernames {
323-
userEmail, err := state.Email(username)
324-
if err != nil {
325-
return "", err
326-
}
327-
if userEmail == email {
328-
return username, nil
321+
if err == simpleredis.ErrNotFound {
322+
return "", ErrNotFound
329323
}
324+
return "", err
330325
}
331-
return "", ErrNotFound
326+
return username, nil
332327
}
333328

334329
// BooleanField returns the boolean value for a given username and field name.

userstate_test.go

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package permissions
22

33
import (
4-
"github.com/xyproto/pinterface"
54
"testing"
65
"time"
6+
7+
"github.com/xyproto/pinterface"
78
)
89

910
func TestPerm(t *testing.T) {
@@ -57,7 +58,6 @@ func TestPasswordBasic(t *testing.T) {
5758
if userstate.PasswordAlgo() != "sha256" {
5859
t.Error("Error, setting password algorithm failed")
5960
}
60-
6161
}
6262

6363
func TestPasswordBasic2(t *testing.T) {
@@ -79,7 +79,6 @@ func TestPasswordBasic2(t *testing.T) {
7979
if userstate.PasswordAlgo() != "sha256" {
8080
t.Error("Error, setting password algorithm failed")
8181
}
82-
8382
}
8483

8584
// Check if the functionality for backwards compatible hashing works
@@ -318,3 +317,64 @@ func TestTiming(t *testing.T) {
318317
}
319318
//fmt.Println(baseMin, base_max, elapsed1, elapsed2, elapsed3, elapsed4)
320319
}
320+
321+
func TestHasEmail(t *testing.T) {
322+
const (
323+
username1 = "alice"
324+
password1 = "password1"
325+
email1 = "[email protected]"
326+
327+
username2 = "bob"
328+
password2 = "password2"
329+
email2 = "[email protected]"
330+
331+
username3 = "charlie"
332+
password3 = "password3"
333+
email3 = "[email protected]"
334+
335+
nonExistentEmail = "[email protected]"
336+
)
337+
338+
userstate := NewUserStateSimple()
339+
defer userstate.Close()
340+
341+
// Add test users
342+
userstate.AddUser(username1, password1, email1)
343+
defer userstate.RemoveUser(username1)
344+
345+
userstate.AddUser(username2, password2, email2)
346+
defer userstate.RemoveUser(username2)
347+
348+
userstate.AddUser(username3, password3, email3)
349+
defer userstate.RemoveUser(username3)
350+
351+
// Test HasEmail with an existing email
352+
username, err := userstate.HasEmail(email2)
353+
if err != nil {
354+
t.Errorf("Expected to find email '%s', but got error: %v", email2, err)
355+
} else if username != username2 {
356+
t.Errorf("Expected username '%s', but got '%s'", username2, username)
357+
} else {
358+
t.Logf("Successfully found username '%s' for email '%s'", username, email2)
359+
}
360+
361+
// Test HasEmail with a non-existent email
362+
username, err = userstate.HasEmail(nonExistentEmail)
363+
if err != ErrNotFound {
364+
t.Errorf("Expected ErrNotFound for email '%s', but got error: %v", nonExistentEmail, err)
365+
} else if username != "" {
366+
t.Errorf("Expected empty username for non-existent email, but got '%s'", username)
367+
} else {
368+
t.Logf("Correctly did not find username for non-existent email '%s'", nonExistentEmail)
369+
}
370+
371+
// Test HasEmail with an empty email
372+
username, err = userstate.HasEmail("")
373+
if err != ErrNotFound {
374+
t.Errorf("Expected ErrNotFound for empty email, but got error: %v", err)
375+
} else if username != "" {
376+
t.Errorf("Expected empty username for empty email, but got '%s'", username)
377+
} else {
378+
t.Log("Correctly handled empty email input")
379+
}
380+
}

0 commit comments

Comments
 (0)