Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Oct 1, 2021
1 parent ddcdb0b commit 24abef3
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 16 deletions.
10 changes: 8 additions & 2 deletions core/controller/api_auth_data.go
Expand Up @@ -54,7 +54,10 @@ func CreateKeyBasedAuthData(c echo.Context, gc *GlobalContext) error {
"id": method.ID,
}).Info(`Auth method not found`)

return c.NoContent(http.StatusNotFound)
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": "Auth method not found",
"error": fmt.Sprintf("code=%d, message=BadRequest", http.StatusBadRequest),
})
}

if !util.InArray(method.Type, []string{migration.KeyAuthentication, migration.AnyAuthentication}) {
Expand Down Expand Up @@ -167,7 +170,10 @@ func UpdateKeyBasedAuthData(c echo.Context, gc *GlobalContext) error {
"id": id,
}).Info(`Auth method not found`)

return c.NoContent(http.StatusNotFound)
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": "Auth method not found",
"error": fmt.Sprintf("code=%d, message=BadRequest", http.StatusBadRequest),
})
}

if !util.InArray(method.Type, []string{migration.KeyAuthentication, migration.AnyAuthentication}) {
Expand Down
28 changes: 23 additions & 5 deletions core/controller/api_auth_data_test.go
Expand Up @@ -6,16 +6,17 @@ package controller

import (
"fmt"
_ "net/http"
_ "net/http/httptest"
_ "strings"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/spacewalkio/helmet/core/model"
"github.com/spacewalkio/helmet/core/module"
"github.com/spacewalkio/helmet/pkg"

"github.com/franela/goblin"
_ "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4"
)

// TestUnitApiAuthDataEndpoints test cases
Expand All @@ -33,9 +34,26 @@ func TestUnitApiAuthDataEndpoints(t *testing.T) {

defer database.Close()

g.Describe("#CreateKeyBasedAuthData", func() {
database.CreateAuthMethod(&model.AuthMethod{
Name: "customers_public",
Description: "Public API",
Type: "any_authentication",
Endpoints: "orders_service",
})

g.Describe("#CreateKeyBasedAuthData.Failure", func() {
g.It("It should satisfy all provided test cases", func() {
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/apigw/api/v1/auth/key", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetPath("/apigw/api/v1/auth/key")

err := CreateKeyBasedAuthData(c, &GlobalContext{Database: database})

g.Assert(err).Equal(nil)
g.Assert(rec.Code).Equal(http.StatusBadRequest)
g.Assert(strings.Contains(rec.Body.String(), "message=BadReques")).Equal(true)
})
})

Expand Down
10 changes: 8 additions & 2 deletions core/controller/basic_auth_data.go
Expand Up @@ -58,7 +58,10 @@ func CreateBasicAuthData(c echo.Context, gc *GlobalContext) error {
"id": method.ID,
}).Info(`Auth method not found`)

return c.NoContent(http.StatusNotFound)
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": "Auth method not found",
"error": fmt.Sprintf("code=%d, message=BadRequest", http.StatusBadRequest),
})
}

if !util.InArray(method.Type, []string{migration.BasicAuthentication, migration.AnyAuthentication}) {
Expand Down Expand Up @@ -175,7 +178,10 @@ func UpdateBasicAuthData(c echo.Context, gc *GlobalContext) error {
"id": id,
}).Info(`Auth method not found`)

return c.NoContent(http.StatusNotFound)
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": "Auth method not found",
"error": fmt.Sprintf("code=%d, message=BadRequest", http.StatusBadRequest),
})
}

if !util.InArray(method.Type, []string{migration.BasicAuthentication, migration.AnyAuthentication}) {
Expand Down
28 changes: 23 additions & 5 deletions core/controller/basic_auth_data_test.go
Expand Up @@ -6,16 +6,17 @@ package controller

import (
"fmt"
_ "net/http"
_ "net/http/httptest"
_ "strings"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/spacewalkio/helmet/core/model"
"github.com/spacewalkio/helmet/core/module"
"github.com/spacewalkio/helmet/pkg"

"github.com/franela/goblin"
_ "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4"
)

// TestUnitBasicAuthDataEndpoints test cases
Expand All @@ -33,9 +34,26 @@ func TestUnitBasicAuthDataEndpoints(t *testing.T) {

defer database.Close()

g.Describe("#CreateBasicAuthData", func() {
database.CreateAuthMethod(&model.AuthMethod{
Name: "customers_public",
Description: "Public API",
Type: "any_authentication",
Endpoints: "orders_service",
})

g.Describe("#CreateBasicAuthData.Failure", func() {
g.It("It should satisfy all provided test cases", func() {
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/apigw/api/v1/auth/basic", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetPath("/apigw/api/v1/auth/basic")

err := CreateBasicAuthData(c, &GlobalContext{Database: database})

g.Assert(err).Equal(nil)
g.Assert(rec.Code).Equal(http.StatusBadRequest)
g.Assert(strings.Contains(rec.Body.String(), "message=BadReques")).Equal(true)
})
})

Expand Down
10 changes: 8 additions & 2 deletions core/controller/oauth_data.go
Expand Up @@ -58,7 +58,10 @@ func CreateOAuthData(c echo.Context, gc *GlobalContext) error {
"id": method.ID,
}).Info(`Auth method not found`)

return c.NoContent(http.StatusNotFound)
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": "Auth method not found",
"error": fmt.Sprintf("code=%d, message=BadRequest", http.StatusBadRequest),
})
}

if !util.InArray(method.Type, []string{migration.OAuthAuthentication, migration.AnyAuthentication}) {
Expand Down Expand Up @@ -175,7 +178,10 @@ func UpdateOAuthData(c echo.Context, gc *GlobalContext) error {
"id": id,
}).Info(`Auth method not found`)

return c.NoContent(http.StatusNotFound)
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": "Auth method not found",
"error": fmt.Sprintf("code=%d, message=BadRequest", http.StatusBadRequest),
})
}

if !util.InArray(method.Type, []string{migration.OAuthAuthentication, migration.AnyAuthentication}) {
Expand Down
54 changes: 54 additions & 0 deletions core/controller/oauth_data_test.go
Expand Up @@ -3,3 +3,57 @@
// license that can be found in the LICENSE file.

package controller

import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/spacewalkio/helmet/core/model"
"github.com/spacewalkio/helmet/core/module"
"github.com/spacewalkio/helmet/pkg"

"github.com/franela/goblin"
"github.com/labstack/echo/v4"
)

// TestUnitOAuthDataEndpoints test cases
func TestUnitOAuthDataEndpoints(t *testing.T) {
g := goblin.Goblin(t)

pkg.LoadConfigs(fmt.Sprintf("%s/config.dist.yml", pkg.GetBaseDir("cache")))

database := &module.Database{}

// Reset DB
database.AutoConnect()
database.Rollback()
database.Migrate()

defer database.Close()

database.CreateAuthMethod(&model.AuthMethod{
Name: "customers_public",
Description: "Public API",
Type: "any_authentication",
Endpoints: "orders_service",
})

g.Describe("#CreateOAuthData.Failure", func() {
g.It("It should satisfy all provided test cases", func() {
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/apigw/api/v1/auth/oauth", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetPath("/apigw/api/v1/auth/oauth")

err := CreateOAuthData(c, &GlobalContext{Database: database})

g.Assert(err).Equal(nil)
g.Assert(rec.Code).Equal(http.StatusBadRequest)
g.Assert(strings.Contains(rec.Body.String(), "message=BadReques")).Equal(true)
})
})
}

0 comments on commit 24abef3

Please sign in to comment.