Skip to content

Commit

Permalink
Fallback handling for existing domain
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkRunWu committed Dec 14, 2023
1 parent 3072735 commit 4e72cba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
6 changes: 6 additions & 0 deletions internal/handler/controller/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func (c *Controller) handleDomainVerification(w http.ResponseWriter, r *http.Req
if !ok {
respond(w, func() (any, error) { return nil, models.ErrUndefinedDomain })
}

domain, _ := c.DB.GetDomainByName(r.Context(), domainName)
if domain != nil {
c.handleDomainCreate(w, r)
return
}
respond(w, withTx(r.Context(), c.DB, func(tx db.Tx) (any, error) {
var domainVerification *models.DomainVerification
domain, _ := tx.GetDomainByName(r.Context(), domainName)
Expand Down
19 changes: 15 additions & 4 deletions internal/handler/controller/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,16 +364,26 @@ func TestDomainVerification(t *testing.T) {
}
})
})
testutil.WithTestController(func(c *testutil.TestController) {
token := setupDomainVerification(c)
db.WithTx(c.Context, c.DB, func(tx db.Tx) error {
return tx.CreateDomain(c.Context, models.NewDomain(time.Now(), "test.com", "test", "main"))
})

t.Run("Should raise used domain error", func(t *testing.T) {
req := httptest.NewRequest("POST", "http://localtest.me/api/v1/apps/test2/domains/test.com", nil)
req.Header.Add("Authorization", "bearer "+token)
w := httptest.NewRecorder()
c.ServeHTTP(w, req)
domain, err := testutil.DecodeJSONResponse[*api.APIDomain](w.Result())
if assert.NoError(t, err) {
assert.Nil(t, domain.Domain)
assert.Equal(t, "test.com", domain.DomainVerification.Domain)
_, err := testutil.DecodeJSONResponse[*api.APIDomain](w.Result())
if assert.Error(t, err) {
assert.Equal(t, 409, err.(api.ServerError).Code)
assert.Equal(t, models.ErrDomainUsedName.Error(), err.(api.ServerError).Message)
}
})

t.Run("Should replace app of the used domain", func(t *testing.T) {
req := httptest.NewRequest("POST", "http://localtest.me/api/v1/apps/test2/domains/test.com?replaceApp=test", nil)
req.Header.Add("Authorization", "bearer "+token)
w := httptest.NewRecorder()
c.ServeHTTP(w, req)
Expand All @@ -382,6 +392,7 @@ func TestDomainVerification(t *testing.T) {
assert.Nil(t, domain.DomainVerification)
assert.NotNil(t, domain.Domain)
assert.Equal(t, "test.com", domain.Domain.Domain)
assert.Equal(t, "test2", domain.Domain.AppID)
}
})
})
Expand Down

0 comments on commit 4e72cba

Please sign in to comment.