Skip to content

Commit

Permalink
feat:add service unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
geebytes committed May 24, 2024
1 parent 55dc723 commit 2a1617a
Show file tree
Hide file tree
Showing 14 changed files with 622 additions and 84 deletions.
20 changes: 16 additions & 4 deletions internal/service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type AppService struct {
config *config.Config
}

func (app *AppService) Put(ctx context.Context, in *api.AppsRequest) (*api.AddAppResponse, error) {
func (app *AppService) Post(ctx context.Context, in *api.AppsRequest) (*api.AddAppResponse, error) {
owner := GetIdentity(ctx)

appInstance, err := app.biz.CreateApp(ctx, in, owner)
Expand All @@ -27,10 +27,11 @@ func (app *AppService) Put(ctx context.Context, in *api.AppsRequest) (*api.AddAp
}
return &api.AddAppResponse{Appid: appInstance.Appid, AccessKey: appInstance.AccessKey, Secret: appInstance.Secret}, nil
}

func (app *AppService) Get(ctx context.Context, in *api.GetAPPRequest) (*api.Apps, error) {
apps, err := app.biz.Get(ctx, in.Appid)
if err != nil {
app.log.Errorf(ctx,"GetApps failed: %v", err)
app.log.Errorf(ctx, "GetApps failed: %v", err)
return nil, err
}
return apps, nil
Expand All @@ -40,10 +41,11 @@ func (app *AppService) Desc() *grpc.ServiceDesc {
return &api.AppsService_ServiceDesc
}

func NewAppService(biz *biz.AppUsecase, log logger.Logger, config *config.Config) *AppService {
func NewAppService(biz *biz.AppUsecase, log logger.Logger, config *config.Config) api.AppsServiceServer {
return &AppService{biz: biz, log: log, config: config}
}
func (app *AppService) Patch(ctx context.Context, in *api.AppsRequest) (*api.Apps, error) {

func (app *AppService) Update(ctx context.Context, in *api.AppsRequest) (*api.Apps, error) {
owner := GetIdentity(ctx)
appInstance, err := app.biz.Patch(ctx, in, owner)
if err != nil {
Expand All @@ -52,10 +54,20 @@ func (app *AppService) Patch(ctx context.Context, in *api.AppsRequest) (*api.App
}
return appInstance, nil
}

func (app *AppService) Delete(ctx context.Context, in *api.DeleteAppRequest) (*api.DeleteAppResponse, error) {
err := app.biz.Del(ctx, in.Appid)
if err != nil {
return nil, err
}
return &api.DeleteAppResponse{}, nil
}

func (app *AppService) List(ctx context.Context, in *api.AppsListRequest) (*api.AppsListResponse, error) {
apps, err := app.biz.List(ctx, in)
if err != nil {
return nil, err
}
return &api.AppsListResponse{Apps: apps}, nil
// return nil, nil
}
91 changes: 90 additions & 1 deletion internal/service/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,48 @@ import (
"testing"
"time"

"github.com/agiledragon/gomonkey/v2"
"github.com/begonia-org/begonia"
"github.com/begonia-org/begonia/config"
"github.com/begonia-org/begonia/gateway"
"github.com/begonia-org/begonia/internal/biz"
"github.com/begonia-org/begonia/internal/service"
api "github.com/begonia-org/go-sdk/api/app/v1"
"github.com/begonia-org/go-sdk/client"
common "github.com/begonia-org/go-sdk/common/api/v1"
c "github.com/smartystreets/goconvey/convey"
)

var appid = ""
var name2 = ""

func addApp(t *testing.T) {
c.Convey(
"test add app",
t,
func() {
apiClient := client.NewAppAPI(apiAddr, accessKey, secret)
rsp, err := apiClient.PostAppConfig(context.Background(), &api.AppsRequest{Name: fmt.Sprintf("app-%s", time.Now().Format("20060102150405")), Description: "test"})
name := fmt.Sprintf("app-%s", time.Now().Format("20060102150405"))
rsp, err := apiClient.PostAppConfig(context.Background(), &api.AppsRequest{Name: name, Description: "test", Tags: []string{"test"}})
c.So(err, c.ShouldBeNil)
c.So(rsp.StatusCode, c.ShouldEqual, common.Code_OK)
c.So(rsp.Appid, c.ShouldNotBeEmpty)
appid = rsp.Appid

rsp2, err := apiClient.GetAPP(context.Background(), appid)
c.So(err, c.ShouldBeNil)
c.So(rsp2.StatusCode, c.ShouldEqual, common.Code_OK)
c.So(rsp2.Name, c.ShouldNotBeEmpty)
_, err = apiClient.PostAppConfig(context.Background(), &api.AppsRequest{Name: name, Description: "test"})
c.So(err, c.ShouldNotBeNil)
c.So(err.Error(), c.ShouldEqual, "duplicate app name")
// c.So(rsp3.StatusCode, c.ShouldEqual, common.Code_ERR)

name2 = fmt.Sprintf("app-service-2-%s", time.Now().Format("20060102150405"))
rsp, err = apiClient.PostAppConfig(context.Background(), &api.AppsRequest{Name: name2, Description: "test"})
c.So(err, c.ShouldBeNil)
c.So(rsp.StatusCode, c.ShouldEqual, common.Code_OK)

},
)
}
Expand All @@ -43,6 +65,29 @@ func getApp(t *testing.T) {
},
)
}
func testPatchApp(t *testing.T) {
c.Convey(
"test patch app",
t,
func() {
apiClient := client.NewAppAPI(apiAddr, accessKey, secret)
name := fmt.Sprintf("app-%s", time.Now().Format("20060102150405"))
rsp2, err := apiClient.UpdateAPP(context.Background(), appid, name, "test patch", nil)

Check failure on line 75 in internal/service/app_test.go

View workflow job for this annotation

GitHub Actions / test

apiClient.UpdateAPP undefined (type *"github.com/begonia-org/go-sdk/client".AppAPI has no field or method UpdateAPP)
c.So(err, c.ShouldBeNil)
c.So(rsp2.StatusCode, c.ShouldEqual, common.Code_OK)
rsp2, err = apiClient.GetAPP(context.Background(), appid)
c.So(err, c.ShouldBeNil)
c.So(rsp2.StatusCode, c.ShouldEqual, common.Code_OK)
c.So(rsp2.Name, c.ShouldEqual, name)

_, err = apiClient.UpdateAPP(context.Background(), appid, name2, "test patch", nil)

Check failure on line 83 in internal/service/app_test.go

View workflow job for this annotation

GitHub Actions / test

apiClient.UpdateAPP undefined (type *"github.com/begonia-org/go-sdk/client".AppAPI has no field or method UpdateAPP)
c.So(err, c.ShouldNotBeNil)
c.So(err.Error(), c.ShouldEqual, "duplicate app name")

},
)

}
func delApp(t *testing.T) {
c.Convey(
"test del app",
Expand All @@ -56,11 +101,55 @@ func delApp(t *testing.T) {

_, err = apiClient.GetAPP(context.Background(), appid)
c.So(err, c.ShouldNotBeNil)

_, err = apiClient.DeleteAPP(context.TODO(), appid)
c.So(err, c.ShouldNotBeNil)
c.So(err.Error(), c.ShouldEqual, "app not found")
// c.So(rsp3.StatusCode, c.ShouldEqual, common.Code_OK)
})
}

func listAPP(t *testing.T) {
c.Convey(
"test list app",
t,
func() {
apiClient := client.NewAppAPI(apiAddr, accessKey, secret)
rsp, err := apiClient.ListAPP(context.Background(), []string{"test", "test2"}, []api.APPStatus{api.APPStatus_APP_ENABLED}, 1, 10)

Check failure on line 118 in internal/service/app_test.go

View workflow job for this annotation

GitHub Actions / test

apiClient.ListAPP undefined (type *"github.com/begonia-org/go-sdk/client".AppAPI has no field or method ListAPP)
c.So(err, c.ShouldBeNil)
c.So(rsp.StatusCode, c.ShouldEqual, common.Code_OK)
c.So(rsp.Apps, c.ShouldNotBeEmpty)
c.So(rsp.Apps[0].Appid, c.ShouldNotBeEmpty)
},
)
}
func testListErr(t *testing.T) {
c.Convey(
"test list app",
t,
func() {
env := "dev"
if begonia.Env != "" {
env = begonia.Env
}
cnf := config.ReadConfig(env)
srv := service.NewAPPSvrForTest(cnf, gateway.Log)
patch := gomonkey.ApplyFuncReturn((*biz.AppUsecase).List, nil, fmt.Errorf("test list app error"))
defer patch.Reset()
_, err := srv.List(context.Background(), nil)
c.So(err, c.ShouldNotBeNil)
c.So(err.Error(), c.ShouldEqual, "test list app error")
patch.Reset()
},
)
}
func TestApp(t *testing.T) {
t.Run("add app", addApp)
t.Run("get app", getApp)
t.Run("list app", listAPP)
t.Run("list app err", testListErr)
t.Run("patch app", testPatchApp)
// appid = "442568851213783040"
t.Run("del app", delApp)

}
2 changes: 1 addition & 1 deletion internal/service/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type AuthzService struct {
authCrypto *crypto.UsersAuth
}

func NewAuthzService(biz *biz.AuthzUsecase, log logger.Logger, auth *crypto.UsersAuth, config *config.Config) *AuthzService {
func NewAuthzService(biz *biz.AuthzUsecase, log logger.Logger, auth *crypto.UsersAuth, config *config.Config) api.AuthServiceServer {
return &AuthzService{biz: biz, log: log, authCrypto: auth, config: config}
}

Expand Down
29 changes: 29 additions & 0 deletions internal/service/authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import (
"runtime"
"testing"

"github.com/begonia-org/begonia"
"github.com/begonia-org/begonia/config"
"github.com/begonia-org/begonia/gateway"
"github.com/begonia-org/begonia/internal/service"
sys "github.com/begonia-org/go-sdk/api/sys/v1"
v1 "github.com/begonia-org/go-sdk/api/user/v1"
"github.com/begonia-org/go-sdk/client"
common "github.com/begonia-org/go-sdk/common/api/v1"
c "github.com/smartystreets/goconvey/convey"
Expand Down Expand Up @@ -112,10 +117,34 @@ func testLogout(t *testing.T) {

c.So(err, c.ShouldBeNil)
c.So(apiRsp.Code, c.ShouldNotEqual, int32(common.Code_OK))

_, err = apiClient.Logout(context.Background(), xtoken)
c.So(err, c.ShouldNotBeNil)
t.Logf("logout error: %v", err)
// c.So(rsp.StatusCode, c.shoun, common.Code_OK)
},
)
}
func testAuthSeed(t *testing.T) {
c.Convey(
"test auth seed",
t,
func() {
env := "dev"
if begonia.Env != "" {
env = begonia.Env
}
config := config.ReadConfig(env)
srv := service.NewAuthzSvrForTest(config, gateway.Log)
_, err := srv.AuthSeed(context.Background(), &v1.AuthLogAPIRequest{})
c.So(err, c.ShouldNotBeNil)

},
)
}

func TestAuth(t *testing.T) {
t.Run("login", loginTest)
t.Run("logout", testLogout)
t.Run("auth seed", testAuthSeed)
}
6 changes: 3 additions & 3 deletions internal/service/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type EndpointsService struct {
api.UnimplementedEndpointServiceServer
}

func NewEndpointsService(biz *endpoint.EndpointUsecase, log logger.Logger, config *config.Config) *EndpointsService {
func NewEndpointsService(biz *endpoint.EndpointUsecase, log logger.Logger, config *config.Config) api.EndpointServiceServer {
return &EndpointsService{biz: biz, log: log, config: config}
}

Expand All @@ -33,7 +33,7 @@ func (e *EndpointsService) Update(ctx context.Context, in *api.EndpointSrvUpdate
tm, _ := time.Parse(time.RFC3339, timestamp)
return &api.UpdateEndpointResponse{UpdatedAt: timestamppb.New(tm)}, nil
}
func (e *EndpointsService) Put(ctx context.Context, in *api.EndpointSrvConfig) (*api.AddEndpointResponse, error) {
func (e *EndpointsService) Post(ctx context.Context, in *api.EndpointSrvConfig) (*api.AddEndpointResponse, error) {
id, err := e.biz.AddConfig(ctx, in)
if err != nil {
return nil, err
Expand Down Expand Up @@ -62,7 +62,7 @@ func (e *EndpointsService) Delete(ctx context.Context, in *api.DeleteEndpointReq
return &api.DeleteEndpointResponse{}, nil
}

func (e *EndpointsService) Details(ctx context.Context, in *api.DetailsEndpointRequest) (*api.DetailsEndpointResponse, error) {
func (e *EndpointsService) Get(ctx context.Context, in *api.DetailsEndpointRequest) (*api.DetailsEndpointResponse, error) {
endpoint, err := e.biz.Get(ctx, in.UniqueKey)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 2a1617a

Please sign in to comment.