Skip to content

Commit

Permalink
fix: fix testcases
Browse files Browse the repository at this point in the history
Signed-off-by: namkyu1999 <[email protected]>
  • Loading branch information
namkyu1999 committed Jul 12, 2023
1 parent a7b7b3a commit a15fc97
Showing 1 changed file with 67 additions and 24 deletions.
91 changes: 67 additions & 24 deletions litmus-portal/graphql-server/pkg/authorization/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/rest_handlers"
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/utils"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
)

// GetTestGinContext returns a gin context for testing
Expand All @@ -29,39 +31,77 @@ func GetTestGinContext(w *httptest.ResponseRecorder) *gin.Context {
// TestMiddleware tests the middleware function
func TestMiddleware(t *testing.T) {
//given
w := httptest.NewRecorder()
ctx := GetTestGinContext(w)
ctx.Request.AddCookie(&http.Cookie{
Name: authorization.CookieName,
Value: "test",
})
handlerMock := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))

ts := httptest.NewServer(handlerMock)
defer ts.Close()

// when
authorization.Middleware(handlerMock)(ctx)

// then
assert.Equal(t, http.StatusOK, w.Code)
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
testcases := []struct {
name string
isRevokedToken bool
statusCode int
}{
{
name: "unauthorized: revoked token",
isRevokedToken: true,
statusCode: http.StatusUnauthorized,
},
{
name: "success",
isRevokedToken: false,
statusCode: http.StatusOK,
},
}
for _, tc := range testcases {
mt.Run(tc.name, func(mt *mtest.T) {
// given
w := httptest.NewRecorder()
ctx := GetTestGinContext(w)
ctx.Request.AddCookie(&http.Cookie{
Name: authorization.CookieName,
Value: "test",
})
handlerMock := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
ts := httptest.NewServer(handlerMock)
defer ts.Close()
if tc.isRevokedToken {
mt.AddMockResponses(mtest.CreateCursorResponse(1, "auth.revoked-token", mtest.FirstBatch, bson.D{{"token", "test"}}))
}
// when
authorization.Middleware(handlerMock, mt.Client)(ctx)
// then
assert.Equal(mt, tc.statusCode, w.Code)
})
}
}

func TestRestMiddlewareWithRole(t *testing.T) {
//given
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
var (
w *httptest.ResponseRecorder
ctx *gin.Context
username = uuid.NewString()
)
testcases := []struct {
name string
given func() string
statusCode int
roles []string
name string
given func() string
statusCode int
roles []string
isRevokedToken bool
}{
{
name: "unauthorized: revoked token",
given: func() string {
w = httptest.NewRecorder()
ctx = GetTestGinContext(w)
claims := jwt.MapClaims{}
claims["username"] = username
token := jwt.NewWithClaims(jwt.SigningMethodHS512, claims)
tokenString, _ := token.SignedString([]byte(utils.Config.JwtSecret))
return tokenString
},
statusCode: http.StatusUnauthorized,
isRevokedToken: true,
},
{
name: "success",
given: func() string {
Expand Down Expand Up @@ -101,15 +141,18 @@ func TestRestMiddlewareWithRole(t *testing.T) {
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
mt.Run(tc.name, func(mt *mtest.T) {
// given
tokenString := tc.given()
ctx.Request.AddCookie(&http.Cookie{
Name: authorization.CookieName,
Value: tokenString,
})
if tc.isRevokedToken {
mt.AddMockResponses(mtest.CreateCursorResponse(1, "auth.revoked-token", mtest.FirstBatch, bson.D{{"token", tokenString}}))
}
// when
authorization.RestMiddlewareWithRole(rest_handlers.PlaygroundHandler(), tc.roles)(ctx)
authorization.RestMiddlewareWithRole(rest_handlers.PlaygroundHandler(), mt.Client, tc.roles)(ctx)
// then
assert.Equal(t, tc.statusCode, w.Code)
})
Expand Down

0 comments on commit a15fc97

Please sign in to comment.