Skip to content

Commit

Permalink
[Dembo] Security middleware authorization case metadata error
Browse files Browse the repository at this point in the history
  • Loading branch information
walbertus committed Aug 30, 2019
1 parent 4b557c7 commit 4b2b099
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
12 changes: 11 additions & 1 deletion internal/app/service/security/middleware/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package middleware
import (
"encoding/json"
"net/http"

"proctor/internal/app/service/execution/handler/parameter"
"proctor/internal/app/service/infra/logger"
"proctor/internal/app/service/metadata/repository"
"proctor/internal/app/service/security/service"
)

type authorizationMiddleware struct {
service service.SecurityService
service service.SecurityService
metadataRepository repository.MetadataRepository
}

func (middleware *authorizationMiddleware) MiddlewareFunc(next http.Handler) http.Handler {
Expand All @@ -19,6 +23,12 @@ func (middleware *authorizationMiddleware) MiddlewareFunc(next http.Handler) htt
w.WriteHeader(http.StatusBadRequest)
return
}
_, err := middleware.metadataRepository.GetByName(job.Name)
logger.LogErrors(err, "get metadata", job.Name)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}

next.ServeHTTP(w, r)
})
}
46 changes: 46 additions & 0 deletions internal/app/service/security/middleware/authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ import (
"net/http/httptest"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"

"proctor/internal/app/service/metadata/repository"
"proctor/internal/pkg/model/metadata"
)

type authorizationContext struct {
authorizationMiddleware authorizationMiddleware
requestHandler func(http.Handler) http.Handler
metadataRepository *repository.MockMetadataRepository
}

func (context *authorizationContext) setUp(t *testing.T) {
Expand All @@ -22,6 +27,8 @@ func (context *authorizationContext) setUp(t *testing.T) {
next.ServeHTTP(w, r)
})
}
context.metadataRepository = &repository.MockMetadataRepository{}
context.authorizationMiddleware.metadataRepository = context.metadataRepository
}

func (context *authorizationContext) tearDown() {
Expand All @@ -43,11 +50,22 @@ func TestAuthorizationMiddleware_MiddlewareFuncSuccess(t *testing.T) {
requestBody := map[string]string{}
requestBody["name"] = "a-job"
body, _ := json.Marshal(requestBody)
jobMetadata := &metadata.Metadata{
Name: "a-job",
Description: "jobMetadata of a job",
ImageName: "ubuntu-18.04",
AuthorizedGroups: []string{"system", "proctor_maintainer"},
Author: "systeam team",
Contributors: "proctor team",
Organization: "GoJek",
}

response := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(body))
requestHandler := ctx.instance().requestHandler
metadataRepository := ctx.metadataRepository

metadataRepository.On("GetByName", "a-job").Return(jobMetadata, nil)
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
Expand Down Expand Up @@ -78,3 +96,31 @@ func TestAuthorizationMiddleware_MiddlewareFuncWithoutName(t *testing.T) {
responseResult := response.Result()
assert.Equal(t, http.StatusBadRequest, responseResult.StatusCode)
}

func TestAuthorizationMiddleware_MiddlewareFuncMetadataError(t *testing.T) {
ctx := newAuthorizationContext()
ctx.setUp(t)
defer ctx.tearDown()

var jobMetadata *metadata.Metadata
requestBody := map[string]string{}
requestBody["name"] = "a-job"
body, _ := json.Marshal(requestBody)

response := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(body))
requestHandler := ctx.instance().requestHandler

metadataRepository := ctx.metadataRepository
err := errors.New("metadata not found")
metadataRepository.On("GetByName", "a-job").Return(jobMetadata, err)
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})

authorizationMiddleware := ctx.instance().authorizationMiddleware
requestHandler(authorizationMiddleware.MiddlewareFunc(testHandler)).ServeHTTP(response, request)

responseResult := response.Result()
assert.Equal(t, http.StatusInternalServerError, responseResult.StatusCode)
}

0 comments on commit 4b2b099

Please sign in to comment.