Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db: email comparison should be case insensitive #339

Merged
merged 7 commits into from
Mar 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions db/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ func DropMigrationsTable(dbMap *gorp.DbMap) error {
func migrationSource(dbMap *gorp.DbMap) (src migrate.MigrationSource, dialect string, err error) {
switch dbMap.Dialect.(type) {
case gorp.PostgresDialect:
src = &migrate.AssetMigrationSource{
Dir: migrationDir,
Asset: migrations.Asset,
AssetDir: migrations.AssetDir,
}
return src, "postgres", nil
return migrations.PostgresMigrations, "postgres", nil
case gorp.SqliteDialect:
src = &migrate.MemoryMigrationSource{
Migrations: []*migrate.Migration{
Expand Down
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 {
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)
}
}
}
22 changes: 22 additions & 0 deletions db/migrations/0011_case_insensitive_emails.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- +migrate Up

-- This migration is a fix for a bug that allowed duplicate emails if they used different cases (see #338).
-- When migrating, dex will not take the liberty of deleting rows for duplicate cases. Instead it will
-- raise an exception and call for an admin to remove duplicates manually.

CREATE OR REPLACE FUNCTION raise_exp() RETURNS VOID AS $$
BEGIN
RAISE EXCEPTION 'Found duplicate emails when using case insensitive comparision, cannot perform migration.';
END;
$$ LANGUAGE plpgsql;

SELECT LOWER(email),
COUNT(email),
CASE
WHEN COUNT(email) > 1 THEN raise_exp()
ELSE NULL
END
FROM authd_user
GROUP BY LOWER(email);

UPDATE authd_user SET email = LOWER(email);
Loading