Skip to content

Commit

Permalink
return error quicker, test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
parametalol committed Feb 9, 2024
1 parent 4c309d9 commit 433b9b9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
1 change: 0 additions & 1 deletion e2e/e2e_central_traits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ var _ = Describe("central traits", Ordered, func() {
WithTimeout(waitTimeout).
WithPolling(defaultPolling).
Should(Succeed())
defer adminAPI.DeleteDbCentralById(ctx, central.Id)

_, err = adminAPI.PutCentralTrait(ctx, central.Id, constants.CentralTraitPreserved)
Expect(err).Should(Succeed())
Expand Down
10 changes: 9 additions & 1 deletion internal/dinosaur/pkg/services/dinosaur.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ type CNameRecordStatus struct {
Status *string
}

var errPreservedCentral = func(id string) *errors.ServiceError {
return errors.BadRequest("central %q has %s trait", id, constants.CentralTraitPreserved)
}

// DinosaurService ...
//
//go:generate moq -out dinosaurservice_moq.go . DinosaurService
Expand Down Expand Up @@ -494,6 +498,10 @@ func (k *dinosaurService) RegisterDinosaurDeprovisionJob(ctx context.Context, id
}
metrics.IncreaseCentralTotalOperationsCountMetric(dinosaurConstants.CentralOperationDeprovision)

if arrays.Contains(dinosaurRequest.Traits, constants.CentralTraitPreserved) {
return errPreservedCentral(id)
}

deprovisionStatus := dinosaurConstants.CentralRequestStatusDeprovision

if executed, err := k.UpdateStatus(id, deprovisionStatus); executed {
Expand Down Expand Up @@ -580,7 +588,7 @@ func (k *dinosaurService) DeprovisionExpiredDinosaurs() *errors.ServiceError {
// but do not interrupt the deletion flow.
func (k *dinosaurService) Delete(centralRequest *dbapi.CentralRequest, force bool) *errors.ServiceError {
if !force && arrays.Contains(centralRequest.Traits, constants.CentralTraitPreserved) {
return errors.BadRequest("central %q has %s trait", centralRequest.ID, constants.CentralTraitPreserved)
return errPreservedCentral(centralRequest.ID)
}

dbConn := k.connectionFactory.New()
Expand Down
29 changes: 28 additions & 1 deletion internal/dinosaur/pkg/services/dinosaur_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package services

import (
"context"
"net/http"
"reflect"
"testing"

Expand Down Expand Up @@ -187,7 +188,33 @@ func Test_dinosaurService_Delete_Preserved(t *testing.T) {
centralRequest.Traits = append(centralRequest.Traits, constants.CentralTraitPreserved)
})
svcErr := k.Delete(preserved, false)
assert.Equal(t, svcErr.HTTPCode, 400)
assert.Equal(t, http.StatusBadRequest, svcErr.HTTPCode)
{
authHelper, err := auth.NewAuthHelper(JwtKeyFile, JwtCAFile, "")
if err != nil {
t.Fatalf("failed to create auth helper: %s", err.Error())
}
account, err := authHelper.NewAccount(testUser, "", "", "")
if err != nil {
t.Fatal("failed to build a new account")
}

jwt, err := authHelper.CreateJWTWithClaims(account, nil)
if err != nil {
t.Fatalf("failed to create jwt: %s", err.Error())
}
ctx := context.TODO()
authenticatedCtx := auth.SetTokenInContext(ctx, jwt)

mocket.Catcher.Reset().
NewMock().
WithQuery(`SELECT * FROM "central_requests" WHERE id = $1 AND owner = $2`).
WithArgs(testID, testUser).
WithReply(converters.ConvertDinosaurRequest(preserved))

svcErr = k.RegisterDinosaurDeprovisionJob(authenticatedCtx, preserved.ID)
assert.Equal(t, http.StatusBadRequest, svcErr.HTTPCode)
}
svcErr = k.Delete(preserved, true)
assert.Nil(t, svcErr)
}
Expand Down

0 comments on commit 433b9b9

Please sign in to comment.