Skip to content

Commit

Permalink
fix ut check
Browse files Browse the repository at this point in the history
Signed-off-by: wang yan <[email protected]>
  • Loading branch information
wy65701436 committed Nov 15, 2024
1 parent b733cf8 commit a40e02a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/server/middleware/csrf/csrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func Middleware() func(handler http.Handler) http.Handler {
} else if len(key) != 32 {
log.Errorf("Invalid CSRF key length from the environment: %s. Please ensure the key length is 32 characters.", key)
protect = func(_ http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lib_http.SendError(w, errors.New("Invalid CSRF key length from the environment. Please ensure the key length is 32 characters."))
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
lib_http.SendError(w, errors.New("invalid CSRF key length from the environment. Please ensure the key length is 32 characters"))
})
}
return
Expand Down
42 changes: 25 additions & 17 deletions src/server/middleware/csrf/csrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import (
"net/http"
"net/http/httptest"
"os"
"sync"
"testing"

"github.com/stretchr/testify/assert"

"github.com/goharbor/harbor/src/common"
"github.com/goharbor/harbor/src/common/utils"
"github.com/goharbor/harbor/src/common/utils/test"
"github.com/goharbor/harbor/src/lib/config"
_ "github.com/goharbor/harbor/src/pkg/config/inmemory"
)

func resetMiddleware() {
once = sync.Once{}
}

func TestMain(m *testing.M) {
test.InitDatabaseFromEnv()
conf := map[string]interface{}{}
Expand All @@ -37,45 +41,29 @@ func TestMiddleware(t *testing.T) {
req *http.Request
statusCode int
returnToken bool
validKey bool
}{
{
req: httptest.NewRequest(http.MethodGet, "/", nil),
statusCode: http.StatusOK,
returnToken: true,
validKey: true,
},
{
req: httptest.NewRequest(http.MethodDelete, "/", nil),
statusCode: http.StatusForbidden,
returnToken: true,
validKey: true,
},
{
req: httptest.NewRequest(http.MethodGet, "/api/2.0/projects", nil), // should be skipped
statusCode: http.StatusOK,
returnToken: false,
validKey: true,
},
{
req: httptest.NewRequest(http.MethodDelete, "/v2/library/hello-world/manifests/latest", nil), // should be skipped
statusCode: http.StatusOK,
returnToken: false,
validKey: true,
},
{
req: httptest.NewRequest(http.MethodGet, "/", nil),
statusCode: http.StatusInternalServerError,
returnToken: false,
validKey: false,
},
}
for _, c := range cases {
if c.validKey {
os.Setenv(csrfKeyEnv, utils.GenerateRandomStringWithLen(32))
} else {
os.Setenv(csrfKeyEnv, utils.GenerateRandomStringWithLen(10))
}
srv := Middleware()(&handler{})
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, c.req)
Expand All @@ -84,6 +72,26 @@ func TestMiddleware(t *testing.T) {
}
}

func TestMiddlewareInvalidKey(t *testing.T) {
originalEnv := os.Getenv(csrfKeyEnv)
defer os.Setenv(csrfKeyEnv, originalEnv)

t.Run("invalid CSRF key", func(t *testing.T) {
os.Setenv(csrfKeyEnv, "invalidkey")
resetMiddleware()
middleware := Middleware()
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Error("handler should not be reached when CSRF key is invalid")
})

handler := middleware(testHandler)
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
assert.Equal(t, http.StatusInternalServerError, rec.Code)
})
}

func TestSecureCookie(t *testing.T) {
assert.True(t, secureCookie())
conf := map[string]interface{}{
Expand Down

0 comments on commit a40e02a

Please sign in to comment.