Skip to content

Commit

Permalink
*: add functional tests for case insensitive emails
Browse files Browse the repository at this point in the history
  • Loading branch information
ericchiang committed Mar 1, 2016
1 parent 9bc68ed commit 208afd3
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 5 deletions.
86 changes: 86 additions & 0 deletions db/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
@ericchiang

ericchiang Mar 1, 2016

Author Owner

This also fixes dexidp#265 btw

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))
}
Expand Down Expand Up @@ -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)
}
}
}
27 changes: 22 additions & 5 deletions functional/repo/user_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]",
Expand Down Expand Up @@ -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]",
Expand All @@ -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)
}
}
Expand Down

0 comments on commit 208afd3

Please sign in to comment.