Skip to content

Commit

Permalink
Check domain ownership before delete it
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkRunWu committed Dec 14, 2023
1 parent d669613 commit e86c971
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/cron/verify_domain_ownership.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (v *VerifyDomainOwnership) Run(ctx context.Context, logger *zap.Logger) err
return err
}
} else {
if domain != nil {
if domain != nil && domain.AppID == domainVerification.AppID {
c.DeleteDomain(ctx, domain.ID, now)
}
err = c.LabelDomainVerificationAsInvalid(ctx, domainVerification.ID, now)
Expand Down
68 changes: 68 additions & 0 deletions internal/cron/verify_domain_ownership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,65 @@ func TestVerifyDomainOwnership(t *testing.T) {
}
})
})
t.Run("Should not delete domain owned by another app", func(t *testing.T) {
testutil.WithTestDB(func(database db.DB) {
data := setupDB(now, ctx, database)
err := db.WithTx(ctx, database, func(tx db.Tx) error {
return tx.CreateApp(ctx, models.NewApp(
now,
"test2",
data.userId,
))
})
assert.NoError(t, err)
err = db.WithTx(ctx, database, func(tx db.Tx) error {
return tx.CreateDomain(ctx, models.NewDomain(
now,
"test.com",
"test2",
"main",
))
})
assert.NoError(t, err)

db.WithTx(ctx, database, func(tx db.Tx) error {
return tx.CreateDomain(ctx, models.NewDomain(
now,
"test.com",
"test2",
"main",
))
})
domainVerification, err := database.GetDomainVerificationByName(ctx, "test.com", "test")
txtDomain, _ := domainVerification.GetTxtRecord()
r := mockdns.Resolver{
Zones: map[string]mockdns.Zone{
txtDomain + ".": {
TXT: []string{
"incorrect value",
},
},
},
}
job := cron.VerifyDomainOwnership{
DB: database,
Resolver: &r,
MaxConsumeActiveDomainCount: 1,
MaxConsumePendingDomainCount: 1,
RevalidatePeriod: time.Hour,
}
err = job.Run(ctx, logger)
assert.NoError(t, err)

domain, err := database.GetDomainByName(ctx, "test.com")
if assert.NoError(t, err) {
assert.Equal(t, domain.Domain, "test.com")
assert.Equal(t, domain.AppID, "test2")
}
})

})

t.Run("Should replace the conflict domain", func(t *testing.T) {
testutil.WithTestDB(func(database db.DB) {
data := setupDB(now, ctx, database)
Expand All @@ -239,6 +298,15 @@ func TestVerifyDomainOwnership(t *testing.T) {
))
})
assert.NoError(t, err)

db.WithTx(ctx, database, func(tx db.Tx) error {
return tx.CreateDomain(ctx, models.NewDomain(
now,
"test.com",
"test2",
"main",
))
})
domainVerification, err := database.GetDomainVerificationByName(ctx, "test.com", "test")
txtDomain, value := domainVerification.GetTxtRecord()
r := mockdns.Resolver{
Expand Down

0 comments on commit e86c971

Please sign in to comment.