Skip to content

Commit

Permalink
feat: add a unit test for consumer and remove implicit init (apache#859)
Browse files Browse the repository at this point in the history
* feat: add a unit test for consumer and remove implicit init

* fix: add implict init function to compatible integration tests

* chore: add other cosumer unit test and refacotor some code

* fix: remove intergration instead of unit test

* fix: add EOL for file

* chore: use sub test to run table test

* chore: test desc

* chore: test desc

Co-authored-by: Wen Ming <moonbingbing@gmail.com>
  • Loading branch information
2 people authored and starsz committed Dec 14, 2020
1 parent c756b1c commit a87de5e
Show file tree
Hide file tree
Showing 7 changed files with 483 additions and 275 deletions.
4 changes: 4 additions & 0 deletions api/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ type Config struct {
Authentication Authentication
}

// TODO: it is just for integration tests, we should call "InitLog" explicitly when remove all handler's integration tests
func init() {
InitConf()
}
func InitConf() {
//go test
if workDir := os.Getenv("APISIX_API_WORKDIR"); workDir != "" {
WorkDir = workDir
Expand Down
34 changes: 17 additions & 17 deletions api/filter/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@
package filter

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

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"

"github.com/apisix/manager-api/log"
"github.com/apisix/manager-api/log"
)

func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
req := httptest.NewRequest(method, path, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
req := httptest.NewRequest(method, path, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}

func TestRequestLogHandler(t *testing.T) {
r := gin.New()
logger := log.GetLogger(log.AccessLog)
r.Use(RequestLogHandler(logger))
r.GET("/", func(c *gin.Context) {
})
r := gin.New()
logger := log.GetLogger(log.AccessLog)
r.Use(RequestLogHandler(logger))
r.GET("/", func(c *gin.Context) {
})

w := performRequest(r, "GET", "/")
assert.Equal(t, 200, w.Code)
w := performRequest(r, "GET", "/")
assert.Equal(t, 200, w.Code)
}
2 changes: 1 addition & 1 deletion api/internal/core/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Interface interface {
Get(key string) (interface{}, error)
List(input ListInput) (*ListOutput, error)
Create(ctx context.Context, obj interface{}) error
Update(ctx context.Context, obj interface{}, createOnFail bool) error
Update(ctx context.Context, obj interface{}, createIfNotExist bool) error
BatchDelete(ctx context.Context, keys []string) error
}

Expand Down
64 changes: 64 additions & 0 deletions api/internal/core/store/store_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package store

import (
"context"
"github.com/stretchr/testify/mock"
)

type MockInterface struct {
mock.Mock
}

func (m *MockInterface) Get(key string) (interface{}, error) {
ret := m.Mock.Called(key)
return ret.Get(0), ret.Error(1)
}

func (m *MockInterface) List(input ListInput) (*ListOutput, error) {
ret := m.Called(input)

var (
r0 *ListOutput
r1 error
)

if rf, ok := ret.Get(0).(func(ListInput) *ListOutput); ok {
r0 = rf(input)
} else {
r0 = ret.Get(0).(*ListOutput)
}
r1 = ret.Error(1)

return r0, r1
}

func (m *MockInterface) Create(ctx context.Context, obj interface{}) error {
ret := m.Mock.Called(ctx, obj)
return ret.Error(0)
}

func (m *MockInterface) Update(ctx context.Context, obj interface{}, createOnFail bool) error {
ret := m.Mock.Called(ctx, obj, createOnFail)
return ret.Error(0)
}

func (m *MockInterface) BatchDelete(ctx context.Context, keys []string) error {
ret := m.Mock.Called(ctx, keys)
return ret.Error(0)
}
52 changes: 15 additions & 37 deletions api/internal/handler/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,17 @@
package consumer

import (
"fmt"
"net/http"
"reflect"
"strings"

"github.com/gin-gonic/gin"
"github.com/shiningrush/droplet"
"github.com/shiningrush/droplet/data"
"github.com/shiningrush/droplet/wrapper"
wgin "github.com/shiningrush/droplet/wrapper/gin"

"github.com/apisix/manager-api/internal/core/entity"
"github.com/apisix/manager-api/internal/core/store"
"github.com/apisix/manager-api/internal/handler"
"github.com/apisix/manager-api/internal/utils"
)

type Handler struct {
Expand All @@ -56,7 +52,7 @@ func (h *Handler) ApplyRoute(r *gin.Engine) {
r.PUT("/apisix/admin/consumers", wgin.Wraps(h.Update,
wrapper.InputType(reflect.TypeOf(UpdateInput{}))))
r.DELETE("/apisix/admin/consumers/:usernames", wgin.Wraps(h.BatchDelete,
wrapper.InputType(reflect.TypeOf(BatchDelete{}))))
wrapper.InputType(reflect.TypeOf(BatchDeleteInput{}))))
}

type GetInput struct {
Expand Down Expand Up @@ -134,19 +130,9 @@ func (h *Handler) List(c droplet.Context) (interface{}, error) {

func (h *Handler) Create(c droplet.Context) (interface{}, error) {
input := c.Input().(*entity.Consumer)
if input.ID != nil && utils.InterfaceToString(input.ID) != input.Username {
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
fmt.Errorf("consumer's id and username must be a same value")
}
input.ID = input.Username

if _, ok := input.Plugins["jwt-auth"]; ok {
jwt := input.Plugins["jwt-auth"].(map[string]interface{})
jwt["exp"] = 86400

input.Plugins["jwt-auth"] = jwt
}

ensurePluginsDefValue(input.Plugins)
if err := h.consumerStore.Create(c.Context(), input); err != nil {
return handler.SpecCodeResponse(err), err
}
Expand All @@ -161,42 +147,34 @@ type UpdateInput struct {

func (h *Handler) Update(c droplet.Context) (interface{}, error) {
input := c.Input().(*UpdateInput)
if input.ID != nil && utils.InterfaceToString(input.ID) != input.Username {
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
fmt.Errorf("consumer's id and username must be a same value")
}
if input.Username != "" {
input.Consumer.Username = input.Username
}
input.Consumer.ID = input.Consumer.Username

if _, ok := input.Consumer.Plugins["jwt-auth"]; ok {
jwt := input.Consumer.Plugins["jwt-auth"].(map[string]interface{})
jwt["exp"] = 86400

input.Consumer.Plugins["jwt-auth"] = jwt
}
ensurePluginsDefValue(input.Plugins)

if err := h.consumerStore.Update(c.Context(), &input.Consumer, true); err != nil {
//if not exists, create
if err.Error() == fmt.Sprintf("key: %s is not found", input.Username) {
if err := h.consumerStore.Create(c.Context(), &input.Consumer); err != nil {
return handler.SpecCodeResponse(err), err
}
} else {
return handler.SpecCodeResponse(err), err
}
return handler.SpecCodeResponse(err), err
}

return nil, nil
}

type BatchDelete struct {
func ensurePluginsDefValue(plugins map[string]interface{}) {
if plugins["jwt-auth"] != nil {
jwtAuth, ok := plugins["jwt-auth"].(map[string]interface{})
if ok && jwtAuth["exp"] == nil {
jwtAuth["exp"] = 86400
}
}
}

type BatchDeleteInput struct {
UserNames string `auto_read:"usernames,path"`
}

func (h *Handler) BatchDelete(c droplet.Context) (interface{}, error) {
input := c.Input().(*BatchDelete)
input := c.Input().(*BatchDeleteInput)

if err := h.consumerStore.BatchDelete(c.Context(), strings.Split(input.UserNames, ",")); err != nil {
return handler.SpecCodeResponse(err), err
Expand Down
Loading

0 comments on commit a87de5e

Please sign in to comment.