Skip to content

Commit

Permalink
Cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abador committed Oct 26, 2021
1 parent 053da6b commit 0c72018
Show file tree
Hide file tree
Showing 25 changed files with 38 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"recovery_address": null,
"expires_at": "2000-01-01T00:00:00Z",
"issued_at": "2000-01-01T00:00:00Z",
"identity_id": "b8293f1c-010f-45d9-b809-f3fc5365ba80"
"identity_id": "a251ebc2-880c-4f76-a8f3-38e6940eab0e"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
UPDATE identity_recovery_tokens SET identity_id = 'a251ebc2-880c-4f76-a8f3-38e6940eab0e' WHERE id = '5529d454-2946-404e-b681-d950f8657fd0';
UPDATE identity_recovery_tokens SET identity_id = 'b8293f1c-010f-45d9-b809-f3fc5365ba80' WHERE id = '77ca3f5c-cd39-488b-9f1d-cc7166d14bdc';
UPDATE identity_recovery_tokens SET identity_id = 'a251ebc2-880c-4f76-a8f3-38e6940eab0e' WHERE id = '77ca3f5c-cd39-488b-9f1d-cc7166d14bdc';
UPDATE identity_recovery_tokens SET identity_id = 'a251ebc2-880c-4f76-a8f3-38e6940eab0e' WHERE id = '1b667e6d-8fda-4194-a765-08185185d7e4';

Original file line number Diff line number Diff line change
@@ -1 +1 @@
ALTER TABLE `identity_recovery_tokens` ADD COLUMN `identity_id` char(36) NOT NULL;
ALTER TABLE `identity_recovery_tokens` ADD COLUMN `identity_id` char(36);
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ALTER TABLE "identity_recovery_tokens" ADD COLUMN "identity_id" UUID NOT NULL;
ALTER TABLE "identity_recovery_tokens" ADD COLUMN "identity_id" UUID;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE identity_recovery_tokens SET identity_id=(SELECT identity_id FROM identity_recovery_addresses WHERE id=identity_recovery_address_id) WHERE identity_id = '';
UPDATE identity_recovery_tokens SET identity_id=(SELECT identity_id FROM identity_recovery_addresses WHERE id=identity_recovery_address_id) WHERE identity_id = '' OR identity_id IS NULL;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE identity_recovery_tokens SET identity_id=(SELECT identity_id FROM identity_recovery_addresses WHERE id=identity_recovery_address_id) WHERE identity_id = '00000000-0000-0000-0000-000000000000';
UPDATE identity_recovery_tokens SET identity_id=(SELECT identity_id FROM identity_recovery_addresses WHERE id=identity_recovery_address_id) WHERE identity_id = '00000000-0000-0000-0000-000000000000' OR identity_id IS NULL;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DELETE FROM identity_recovery_tokens WHERE identity_recovery_address_id IS NULL AND identity_id = '';
ALTER TABLE `identity_recovery_tokens` MODIFY `identity_id` char(36) NOT NULL;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DELETE FROM identity_recovery_tokens WHERE identity_recovery_address_id IS NULL AND identity_id = '00000000-0000-0000-0000-000000000000';
ALTER TABLE "identity_recovery_tokens" ALTER COLUMN "identity_id" TYPE UUID, ALTER COLUMN "identity_id" SET NOT NULL;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ALTER TABLE "identity_recovery_tokens" ADD COLUMN "identity_id" UUID NOT NULL;
ALTER TABLE "identity_recovery_tokens" ADD COLUMN "identity_id" UUID;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE identity_recovery_tokens SET identity_id=(SELECT identity_id FROM identity_recovery_addresses WHERE id=identity_recovery_address_id) WHERE identity_id = '00000000-0000-0000-0000-000000000000';
UPDATE identity_recovery_tokens SET identity_id=(SELECT identity_id FROM identity_recovery_addresses WHERE id=identity_recovery_address_id) WHERE identity_id = '00000000-0000-0000-0000-000000000000' OR identity_id IS NULL;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DELETE FROM identity_recovery_tokens WHERE identity_recovery_address_id IS NULL AND identity_id = '00000000-0000-0000-0000-000000000000';
ALTER TABLE "identity_recovery_tokens" RENAME COLUMN "identity_id" TO "_identity_id_tmp";
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ALTER TABLE "identity_recovery_tokens" ADD CONSTRAINT "identity_recovery_tokens_identity_id_fk_idx" FOREIGN KEY ("identity_id") REFERENCES "identities" ("id") ON UPDATE RESTRICT ON DELETE CASCADE;
ALTER TABLE "identity_recovery_tokens" ADD COLUMN "identity_id" UUID;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "identity_recovery_tokens" SET "identity_id" = "_identity_id_tmp";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" ALTER COLUMN "identity_id" SET NOT NULL;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE identity_recovery_tokens SET identity_id=(SELECT identity_id FROM identity_recovery_addresses WHERE id=identity_recovery_address_id) WHERE identity_id = '';
UPDATE identity_recovery_tokens SET identity_id=(SELECT identity_id FROM identity_recovery_addresses WHERE id=identity_recovery_address_id) WHERE identity_id = '' OR identity_id IS NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" DROP COLUMN "_identity_id_tmp";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" ADD CONSTRAINT "identity_recovery_tokens_identity_id_fk_idx" FOREIGN KEY ("identity_id") REFERENCES "identities" ("id") ON UPDATE RESTRICT ON DELETE CASCADE;
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ change_column("identity_recovery_tokens", "identity_recovery_address_id", "uuid"
{{ if .IsSQLite }}
sql("ALTER TABLE identity_recovery_tokens ADD COLUMN identity_id CHAR(36) NULL REFERENCES identities(id) ON DELETE CASCADE ON UPDATE RESTRICT")
{{ else }}
add_column("identity_recovery_tokens", "identity_id", "uuid", {"size": 36})
add_column("identity_recovery_tokens", "identity_id", "uuid", {"size": 36,"null": true})
{{ end }}
{{ if or .IsPostgreSQL .IsCockroach }}
sql("UPDATE identity_recovery_tokens SET identity_id=(SELECT identity_id FROM identity_recovery_addresses WHERE id=identity_recovery_address_id) WHERE identity_id = '00000000-0000-0000-0000-000000000000'")
{{ else }}
sql("UPDATE identity_recovery_tokens SET identity_id=(SELECT identity_id FROM identity_recovery_addresses WHERE id=identity_recovery_address_id) WHERE identity_id = ''")
{{ end }}

{{ if not .IsSQLite }}
change_column("identity_recovery_tokens", "identity_id", "uuid", {"size": 36})
add_foreign_key("identity_recovery_tokens", "identity_id", {"identities": ["id"]}, {
"name": "identity_recovery_tokens_identity_id_fk_idx",
"on_delete": "CASCADE",
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/link/strategy_recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (s *Strategy) recoveryUseToken(w http.ResponseWriter, r *http.Request, body
return s.retryRecoveryFlowWithError(w, r, flow.TypeBrowser, err)
}

recovered, err := s.d.IdentityPool().GetIdentity(r.Context(), token.IdentityID.UUID)
recovered, err := s.d.IdentityPool().GetIdentity(r.Context(), token.IdentityID)
if err != nil {
return s.HandleRecoveryError(w, r, f, nil, err)
}
Expand Down
9 changes: 4 additions & 5 deletions selfservice/strategy/link/test/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ func TestPersister(ctx context.Context, conf *config.Config, p interface {
require.NoError(t, p.CreateIdentity(ctx, &i))

return &link.RecoveryToken{Token: x.NewUUID().String(), FlowID: uuid.NullUUID{UUID: req.ID, Valid: true},
RecoveryAddress: &i.RecoveryAddresses[0],
RecoveryAddressID: i.RecoveryAddresses[0].ID,
ExpiresAt: time.Now(),
IssuedAt: time.Now(),
IdentityID: uuid.NullUUID{UUID: i.ID, Valid: true},
RecoveryAddress: &i.RecoveryAddresses[0],
ExpiresAt: time.Now(),
IssuedAt: time.Now(),
IdentityID: i.ID,
}
}

Expand Down
35 changes: 14 additions & 21 deletions selfservice/strategy/link/token_recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ type RecoveryToken struct {
// UpdatedAt is a helper struct field for gobuffalo.pop.
UpdatedAt time.Time `json:"-" faker:"-" db:"updated_at"`
// RecoveryAddressID is a helper struct field for gobuffalo.pop.
RecoveryAddressID uuid.UUID `json:"-" faker:"-" db:"identity_recovery_address_id"`
RecoveryAddressID *uuid.UUID `json:"-" faker:"-" db:"identity_recovery_address_id"`
// FlowID is a helper struct field for gobuffalo.pop.
FlowID uuid.NullUUID `json:"-" faker:"-" db:"selfservice_recovery_flow_id"`
NID uuid.UUID `json:"-" faker:"-" db:"nid"`
IdentityID uuid.NullUUID `json:"identity_id" faker:"-" db:"identity_id"`
IdentityID uuid.UUID `json:"identity_id" faker:"-" db:"identity_id"`
}

func (RecoveryToken) TableName(ctx context.Context) string {
Expand All @@ -66,32 +66,25 @@ func NewSelfServiceRecoveryToken(address *identity.RecoveryAddress, f *recovery.
recoveryAddressID = address.ID
}
return &RecoveryToken{
ID: x.NewUUID(),
Token: randx.MustString(32, randx.AlphaNum),
RecoveryAddress: address,
ExpiresAt: now.Add(expiresIn),
IssuedAt: now,
IdentityID: uuid.NullUUID{
UUID: identityID,
Valid: uuid.Nil != identityID,
},
ID: x.NewUUID(),
Token: randx.MustString(32, randx.AlphaNum),
RecoveryAddress: address,
ExpiresAt: now.Add(expiresIn),
IssuedAt: now,
IdentityID: identityID,
FlowID: uuid.NullUUID{UUID: f.ID, Valid: true},
RecoveryAddressID: recoveryAddressID,
RecoveryAddressID: &recoveryAddressID,
}
}

func NewRecoveryToken(identityID uuid.UUID, expiresIn time.Duration) *RecoveryToken {
now := time.Now().UTC()
return &RecoveryToken{
ID: x.NewUUID(),
Token: randx.MustString(32, randx.AlphaNum),
ExpiresAt: now.Add(expiresIn),
IssuedAt: now,
IdentityID: uuid.NullUUID{
UUID: identityID,
Valid: uuid.Nil != identityID,
},
RecoveryAddressID: uuid.UUID{},
ID: x.NewUUID(),
Token: randx.MustString(32, randx.AlphaNum),
ExpiresAt: now.Add(expiresIn),
IssuedAt: now,
IdentityID: identityID,
}
}

Expand Down

0 comments on commit 0c72018

Please sign in to comment.