11package permissions
22
33import (
4- "github.com/xyproto/pinterface"
54 "testing"
65 "time"
6+
7+ "github.com/xyproto/pinterface"
78)
89
910func 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
6363func 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+ 326+
327+ username2 = "bob"
328+ password2 = "password2"
329+ 330+
331+ username3 = "charlie"
332+ password3 = "password3"
333+ 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