Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a unit test for consumer and remove implicit init #859

Merged
merged 16 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,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
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 @@ -100,19 +96,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 @@ -127,42 +113,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