MSP-5262 Implement /access_list/delete_list
This commit is contained in:
1 parent
d6f3462e87
commit
b5a7ce5404
13 files changed
+344
-12
No files matched your search
@@ -2,8 +2,11 @@ package assembly
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/txix-open/isp-kit/rc"
|
||||
|
||||
"isp-system-service/conf"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/app"
|
||||
"github.com/txix-open/isp-kit/bootstrap"
|
||||
@@ -12,7 +15,6 @@ import (
|
||||
"github.com/txix-open/isp-kit/dbx"
|
||||
"github.com/txix-open/isp-kit/grpc"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
"isp-system-service/conf"
|
||||
)
|
||||
|
||||
type Assembly struct {
|
||||
|
||||
@@ -15,6 +15,7 @@ type AccessListService interface {
|
||||
GetById(ctx context.Context, appId int) ([]domain.MethodInfo, error)
|
||||
SetOne(ctx context.Context, request domain.AccessListSetOneRequest) (*domain.AccessListSetOneResponse, error)
|
||||
SetList(ctx context.Context, req domain.AccessListSetListRequest) ([]domain.MethodInfo, error)
|
||||
DeleteList(ctx context.Context, req domain.AccessListDeleteListRequest) error
|
||||
}
|
||||
|
||||
type AccessList struct {
|
||||
@@ -113,3 +114,30 @@ func (c AccessList) SetList(ctx context.Context, req domain.AccessListSetListReq
|
||||
return result, nil
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteList godoc
|
||||
//
|
||||
// @Tags accessList
|
||||
// @Summary Удалить список доступных методов для приложения
|
||||
// @Description Удаляет заданный список методов для приложения
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body domain.AccessListDeleteListRequest false "тело запроса"
|
||||
// @Success 200 {object} any
|
||||
// @Failure 404 {object} apierrors.Error
|
||||
// @Failure 500 {object} apierrors.Error
|
||||
// @Router /access_list/delete_list [POST]
|
||||
func (c AccessList) DeleteList(ctx context.Context, req domain.AccessListDeleteListRequest) error {
|
||||
err := c.service.DeleteList(ctx, req)
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrApplicationNotFound):
|
||||
return apierrors.New(
|
||||
codes.NotFound,
|
||||
domain.ErrCodeApplicationNotFound,
|
||||
fmt.Sprintf("application with id %d not found", req.AppId),
|
||||
err,
|
||||
)
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,9 @@ package controller
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"isp-system-service/domain"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type SecureService interface {
|
||||
|
||||
@@ -10,6 +10,19 @@ definitions:
|
||||
errorMessage:
|
||||
type: string
|
||||
type: object
|
||||
domain.AccessListDeleteListRequest:
|
||||
properties:
|
||||
appId:
|
||||
type: integer
|
||||
methods:
|
||||
items:
|
||||
type: string
|
||||
minItems: 1
|
||||
type: array
|
||||
required:
|
||||
- appId
|
||||
- methods
|
||||
type: object
|
||||
domain.AccessListSetListRequest:
|
||||
properties:
|
||||
appId:
|
||||
@@ -389,6 +402,34 @@ info:
|
||||
title: isp-system-service
|
||||
version: 1.0.0
|
||||
paths:
|
||||
/access_list/delete_list:
|
||||
post:
|
||||
consumes:
|
||||
- application/json
|
||||
description: Удаляет заданный список методов для приложения
|
||||
parameters:
|
||||
- description: тело запроса
|
||||
in: body
|
||||
name: body
|
||||
schema:
|
||||
$ref: '#/definitions/domain.AccessListDeleteListRequest'
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema: {}
|
||||
"404":
|
||||
description: Not Found
|
||||
schema:
|
||||
$ref: '#/definitions/apierrors.Error'
|
||||
"500":
|
||||
description: Internal Server Error
|
||||
schema:
|
||||
$ref: '#/definitions/apierrors.Error'
|
||||
summary: Удалить список доступных методов для приложения
|
||||
tags:
|
||||
- accessList
|
||||
/access_list/get_by_id:
|
||||
post:
|
||||
consumes:
|
||||
|
||||
@@ -20,3 +20,8 @@ type MethodInfo struct {
|
||||
Method string
|
||||
Value bool
|
||||
}
|
||||
|
||||
type AccessListDeleteListRequest struct {
|
||||
AppId int `validate:"required"`
|
||||
Methods []string `validate:"required,min=1"`
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/txix-open/isp-kit/bootstrap"
|
||||
"github.com/txix-open/isp-kit/shutdown"
|
||||
"isp-system-service/assembly"
|
||||
"isp-system-service/conf"
|
||||
"isp-system-service/routes"
|
||||
|
||||
"github.com/txix-open/isp-kit/bootstrap"
|
||||
"github.com/txix-open/isp-kit/shutdown"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@@ -4,12 +4,13 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/db"
|
||||
"github.com/txix-open/isp-kit/db/query"
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
)
|
||||
|
||||
type AccessList struct {
|
||||
|
||||
@@ -4,12 +4,13 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/db"
|
||||
"github.com/txix-open/isp-kit/db/query"
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
)
|
||||
|
||||
type Domain struct {
|
||||
|
||||
+3
-2
@@ -4,12 +4,13 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/db"
|
||||
"github.com/txix-open/isp-kit/db/query"
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
)
|
||||
|
||||
type Token struct {
|
||||
|
||||
+7
-1
@@ -1,10 +1,11 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"isp-system-service/controller"
|
||||
|
||||
"github.com/txix-open/isp-kit/cluster"
|
||||
"github.com/txix-open/isp-kit/grpc"
|
||||
"github.com/txix-open/isp-kit/grpc/endpoint"
|
||||
"isp-system-service/controller"
|
||||
)
|
||||
|
||||
type Controllers struct {
|
||||
@@ -73,6 +74,11 @@ func accessListCluster(c Controllers) []cluster.EndpointDescriptor {
|
||||
Inner: true,
|
||||
Handler: c.AccessList.SetList,
|
||||
},
|
||||
{
|
||||
Path: "system/access_list/delete_list",
|
||||
Inner: true,
|
||||
Handler: c.AccessList.DeleteList,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
type AccessListRepo interface {
|
||||
GetAccessListByAppId(ctx context.Context, appId int) ([]entity.AccessList, error)
|
||||
DeleteAccessList(ctx context.Context, appId int, methods []string) error
|
||||
}
|
||||
|
||||
type AccessListSetOneTx interface {
|
||||
@@ -155,3 +156,15 @@ func (s AccessList) SetList(ctx context.Context, req domain.AccessListSetListReq
|
||||
|
||||
return methodInfos, nil
|
||||
}
|
||||
|
||||
func (s AccessList) DeleteList(ctx context.Context, req domain.AccessListDeleteListRequest) error {
|
||||
_, err := s.appRepo.GetApplicationById(ctx, req.AppId)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get application by id")
|
||||
}
|
||||
err = s.accessListRepo.DeleteAccessList(ctx, req.AppId, req.Methods)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "delete access_list")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+2
-1
@@ -3,9 +3,10 @@ package service
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type Domain struct {
|
||||
|
||||
@@ -0,0 +1,231 @@
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/txix-open/isp-kit/dbx"
|
||||
|
||||
"isp-system-service/assembly"
|
||||
"isp-system-service/conf"
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
"isp-system-service/repository"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
"github.com/txix-open/isp-kit/grpc/client"
|
||||
"github.com/txix-open/isp-kit/test"
|
||||
"github.com/txix-open/isp-kit/test/dbt"
|
||||
"github.com/txix-open/isp-kit/test/fake"
|
||||
"github.com/txix-open/isp-kit/test/grpct"
|
||||
)
|
||||
|
||||
func TestAccessListSuite(t *testing.T) {
|
||||
t.Parallel()
|
||||
suite.Run(t, &AccessListSuite{})
|
||||
}
|
||||
|
||||
type AccessListSuite struct {
|
||||
suite.Suite
|
||||
test *test.Test
|
||||
testDb *dbt.TestDb
|
||||
api *client.Client
|
||||
accessListRepo repository.AccessList
|
||||
appId int
|
||||
}
|
||||
|
||||
func (s *AccessListSuite) SetupTest() {
|
||||
s.test, _ = test.New(s.T())
|
||||
|
||||
s.testDb = dbt.New(s.test, dbx.WithMigrationRunner("../migrations", s.test.Logger()))
|
||||
|
||||
locator := assembly.NewLocator(s.testDb, s.test.Logger())
|
||||
config := locator.Config(conf.Remote{})
|
||||
_, s.api = grpct.TestServer(s.test, config.Handler)
|
||||
s.accessListRepo = repository.NewAccessList(s.testDb)
|
||||
|
||||
createdDomain, err := repository.NewDomain(s.testDb).CreateDomain(
|
||||
s.T().Context(),
|
||||
fake.It[string](),
|
||||
fake.It[string](),
|
||||
1,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
|
||||
appGroup, err := repository.NewAppGroup(s.testDb).CreateAppGroup(
|
||||
s.T().Context(),
|
||||
fake.It[string](),
|
||||
fake.It[string](),
|
||||
createdDomain.Id,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
|
||||
s.appId = fake.It[int]()
|
||||
|
||||
_, err = repository.NewApplication(s.testDb).CreateApplication(
|
||||
s.T().Context(),
|
||||
s.appId,
|
||||
fake.It[string](),
|
||||
fake.It[string](),
|
||||
appGroup.Id,
|
||||
domain.ApplicationSystemType,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
func (s *AccessListSuite) TestGetById_HappyPath() {
|
||||
expectedAccessList := []entity.AccessList{
|
||||
{
|
||||
AppId: s.appId,
|
||||
Method: fake.It[string](),
|
||||
Value: false,
|
||||
},
|
||||
{
|
||||
AppId: s.appId,
|
||||
Method: fake.It[string](),
|
||||
Value: true,
|
||||
},
|
||||
{
|
||||
AppId: s.appId,
|
||||
Method: fake.It[string](),
|
||||
Value: true,
|
||||
},
|
||||
}
|
||||
|
||||
err := s.accessListRepo.InsertArrayAccessList(s.T().Context(), expectedAccessList)
|
||||
s.Require().NoError(err)
|
||||
|
||||
req := domain.Identity{
|
||||
Id: s.appId,
|
||||
}
|
||||
var accessList []domain.MethodInfo
|
||||
err = s.api.Invoke("system/access_list/get_by_id").
|
||||
JsonRequestBody(&req).
|
||||
JsonResponseBody(&accessList).
|
||||
Do(s.T().Context())
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(accessList, len(expectedAccessList))
|
||||
|
||||
actualAccessList := s.convertAccessList(accessList)
|
||||
s.Require().ElementsMatch(expectedAccessList, actualAccessList)
|
||||
}
|
||||
|
||||
func (s *AccessListSuite) TestSetOne_HappyPath() {
|
||||
expectedAccessList := []entity.AccessList{
|
||||
{
|
||||
AppId: s.appId,
|
||||
Method: fake.It[string](),
|
||||
Value: false,
|
||||
},
|
||||
{
|
||||
AppId: s.appId,
|
||||
Method: fake.It[string](),
|
||||
Value: true,
|
||||
},
|
||||
{
|
||||
AppId: s.appId,
|
||||
Method: fake.It[string](),
|
||||
Value: true,
|
||||
},
|
||||
}
|
||||
|
||||
err := s.accessListRepo.InsertArrayAccessList(s.T().Context(), expectedAccessList)
|
||||
s.Require().NoError(err)
|
||||
|
||||
req := domain.AccessListSetOneRequest{
|
||||
AppId: s.appId,
|
||||
Method: expectedAccessList[0].Method,
|
||||
Value: true,
|
||||
}
|
||||
err = s.api.Invoke("system/access_list/set_one").
|
||||
JsonRequestBody(&req).
|
||||
Do(s.T().Context())
|
||||
s.Require().NoError(err)
|
||||
|
||||
actualAccessList, err := s.accessListRepo.GetAccessListByAppId(s.T().Context(), s.appId)
|
||||
s.Require().NoError(err)
|
||||
|
||||
for i := range expectedAccessList {
|
||||
expectedAccessList[i].Value = true
|
||||
}
|
||||
s.Require().ElementsMatch(expectedAccessList, actualAccessList)
|
||||
}
|
||||
|
||||
func (s *AccessListSuite) TestSetList_HappyPath() {
|
||||
req := domain.AccessListSetListRequest{
|
||||
AppId: s.appId,
|
||||
Methods: []domain.MethodInfo{
|
||||
{
|
||||
Method: fake.It[string](),
|
||||
Value: true,
|
||||
},
|
||||
{
|
||||
Method: fake.It[string](),
|
||||
Value: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := s.api.Invoke("system/access_list/set_list").
|
||||
JsonRequestBody(&req).
|
||||
Do(s.T().Context())
|
||||
s.Require().NoError(err)
|
||||
|
||||
actualAccessList, err := s.accessListRepo.GetAccessListByAppId(s.T().Context(), s.appId)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(actualAccessList, len(req.Methods))
|
||||
|
||||
expectedAccessList := s.convertAccessList(req.Methods)
|
||||
s.Require().ElementsMatch(expectedAccessList, actualAccessList)
|
||||
}
|
||||
|
||||
func (s *AccessListSuite) TestDeleteList_HappyPath() {
|
||||
accessList := []entity.AccessList{
|
||||
{
|
||||
AppId: s.appId,
|
||||
Method: fake.It[string](),
|
||||
Value: false,
|
||||
},
|
||||
{
|
||||
AppId: s.appId,
|
||||
Method: fake.It[string](),
|
||||
Value: true,
|
||||
},
|
||||
{
|
||||
AppId: s.appId,
|
||||
Method: fake.It[string](),
|
||||
Value: true,
|
||||
},
|
||||
}
|
||||
|
||||
err := s.accessListRepo.InsertArrayAccessList(s.T().Context(), accessList)
|
||||
s.Require().NoError(err)
|
||||
|
||||
req := domain.AccessListDeleteListRequest{
|
||||
AppId: s.appId,
|
||||
Methods: []string{
|
||||
accessList[0].Method,
|
||||
accessList[1].Method,
|
||||
},
|
||||
}
|
||||
err = s.api.Invoke("system/access_list/delete_list").
|
||||
JsonRequestBody(&req).
|
||||
Do(s.T().Context())
|
||||
s.Require().NoError(err)
|
||||
|
||||
actualAccessList, err := s.accessListRepo.GetAccessListByAppId(s.T().Context(), s.appId)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(actualAccessList, 1)
|
||||
s.Require().Equal(accessList[2], actualAccessList[0])
|
||||
}
|
||||
|
||||
func (s *AccessListSuite) convertAccessList(methods []domain.MethodInfo) []entity.AccessList {
|
||||
converted := make([]entity.AccessList, 0, len(methods))
|
||||
for _, method := range methods {
|
||||
converted = append(converted, entity.AccessList{
|
||||
AppId: s.appId,
|
||||
Method: method.Method,
|
||||
Value: method.Value,
|
||||
})
|
||||
}
|
||||
return converted
|
||||
}
|
||||
Reference in new issue
Block a user