forked from dexidp/dex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*: add functional tests for case insensitive emails
- Loading branch information
1 parent
9bc68ed
commit 208afd3
Showing
2 changed files
with
108 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,17 +3,22 @@ package db | |
import ( | ||
"fmt" | ||
"os" | ||
"sort" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/go-gorp/gorp" | ||
"github.com/kylelemons/godebug/pretty" | ||
) | ||
|
||
func initDB(dsn string) *gorp.DbMap { | ||
c, err := NewConnection(Config{DSN: dsn}) | ||
if err != nil { | ||
panic(fmt.Sprintf("error making db connection: %q", err)) | ||
} | ||
if _, err := c.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s;", migrationTable)); err != nil { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
panic(fmt.Sprintf("failed to drop migration table: %v", err)) | ||
} | ||
if err = c.DropTablesIfExists(); err != nil { | ||
panic(fmt.Sprintf("Unable to drop database tables: %v", err)) | ||
} | ||
|
@@ -119,3 +124,84 @@ func TestMigrateClientMetadata(t *testing.T) { | |
} | ||
} | ||
} | ||
|
||
func TestMigrationNumber11(t *testing.T) { | ||
dsn := os.Getenv("DEX_TEST_DSN") | ||
if dsn == "" { | ||
t.Skip("Test will not run without DEX_TEST_DSN environment variable.") | ||
return | ||
} | ||
|
||
tests := []struct { | ||
sqlStmt string | ||
wantEmails []string | ||
wantError bool | ||
}{ | ||
{ | ||
sqlStmt: `INSERT INTO authd_user | ||
(id, email, email_verified, display_name, admin, created_at) | ||
VALUES | ||
(1, '[email protected]', TRUE, 'foo', FALSE, extract(epoch from now())), | ||
(2, '[email protected]', TRUE, 'foo', FALSE, extract(epoch from now())) | ||
;`, | ||
wantEmails: []string{"[email protected]", "[email protected]"}, | ||
wantError: false, | ||
}, | ||
{ | ||
sqlStmt: `INSERT INTO authd_user | ||
(id, email, email_verified, display_name, admin, created_at) | ||
VALUES | ||
(1, '[email protected]', TRUE, 'foo', FALSE, extract(epoch from now())), | ||
(2, '[email protected]', TRUE, 'foo', FALSE, extract(epoch from now())), | ||
(3, '[email protected]', TRUE, 'foo', FALSE, extract(epoch from now())) | ||
;`, | ||
wantError: true, | ||
}, | ||
} | ||
migrateN := func(dbMap *gorp.DbMap, n int) error { | ||
nPerformed, err := MigrateMaxMigrations(dbMap, n) | ||
if err == nil && n != nPerformed { | ||
err = fmt.Errorf("expected to perform %d migrations, performed %d", n, nPerformed) | ||
} | ||
return err | ||
} | ||
|
||
for i, tt := range tests { | ||
err := func() error { | ||
dbMap := initDB(dsn) | ||
|
||
nMigrations := 10 | ||
if err := migrateN(dbMap, nMigrations); err != nil { | ||
return fmt.Errorf("failed to perform initial migration: %v", err) | ||
} | ||
if _, err := dbMap.Exec(tt.sqlStmt); err != nil { | ||
return fmt.Errorf("failed to insert users: %v", err) | ||
} | ||
if err := migrateN(dbMap, 1); err != nil { | ||
if tt.wantError { | ||
return nil | ||
} | ||
return fmt.Errorf("failed to perform migration: %v", err) | ||
} | ||
|
||
if tt.wantError { | ||
return fmt.Errorf("expected an error when migrating") | ||
} | ||
|
||
var gotEmails []string | ||
if _, err := dbMap.Select(&gotEmails, `SELECT email FROM authd_user;`); err != nil { | ||
return fmt.Errorf("could not get user emails: %v", err) | ||
} | ||
|
||
sort.Strings(tt.wantEmails) | ||
sort.Strings(gotEmails) | ||
if diff := pretty.Compare(tt.wantEmails, gotEmails); diff != "" { | ||
return fmt.Errorf("wantEmails != gotEmails: %s", diff) | ||
} | ||
return nil | ||
}() | ||
if err != nil { | ||
t.Errorf("case %d: %v", i, err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,15 @@ func TestNewUser(t *testing.T) { | |
}, | ||
err: user.ErrorDuplicateEmail, | ||
}, | ||
{ | ||
user: user.User{ | ||
ID: "ID-same", | ||
Email: "[email protected]", | ||
DisplayName: "A lower case version of the original email", | ||
CreatedAt: now, | ||
}, | ||
err: user.ErrorDuplicateEmail, | ||
}, | ||
{ | ||
user: user.User{ | ||
Email: "[email protected]", | ||
|
@@ -421,12 +430,19 @@ func findRemoteIdentity(rids []user.RemoteIdentity, rid user.RemoteIdentity) int | |
|
||
func TestGetByEmail(t *testing.T) { | ||
tests := []struct { | ||
email string | ||
wantErr error | ||
email string | ||
wantEmail string | ||
wantErr error | ||
}{ | ||
{ | ||
email: "[email protected]", | ||
wantErr: nil, | ||
email: "[email protected]", | ||
wantEmail: "[email protected]", | ||
wantErr: nil, | ||
}, | ||
{ | ||
email: "[email protected]", // Emails should be case insensitive. | ||
wantEmail: "[email protected]", | ||
wantErr: nil, | ||
}, | ||
{ | ||
email: "[email protected]", | ||
|
@@ -446,9 +462,10 @@ func TestGetByEmail(t *testing.T) { | |
|
||
if gotErr != nil { | ||
t.Errorf("case %d: want nil err:% q", i, gotErr) | ||
continue | ||
} | ||
|
||
if tt.email != gotUser.Email { | ||
if tt.wantEmail != gotUser.Email { | ||
t.Errorf("case %d: want=%q, got=%q", i, tt.email, gotUser.Email) | ||
} | ||
} | ||
|
This also fixes dexidp#265 btw