MSP-5372 Implement /application/get_application_by_token
This commit is contained in:
1 parent
42873c2f75
commit
41dd618412
7 files changed
+150
No files matched your search
@@ -11,8 +11,10 @@ import (
|
||||
"google.golang.org/grpc/codes"
|
||||
)
|
||||
|
||||
// nolint:interfacebloat
|
||||
type ApplicationService interface {
|
||||
GetById(ctx context.Context, appId int) (*domain.ApplicationWithTokens, error)
|
||||
GetByToken(ctx context.Context, token string) (*domain.GetApplicationByTokenResponse, error)
|
||||
GetByIdList(ctx context.Context, idList []int) ([]*domain.ApplicationWithTokens, error)
|
||||
GetByServiceId(ctx context.Context, id int) ([]*domain.ApplicationWithTokens, error)
|
||||
SystemTree(ctx context.Context, systemId int) ([]*domain.DomainWithService, error)
|
||||
@@ -64,6 +66,36 @@ func (c Application) GetById(ctx context.Context, req domain.Identity) (*domain.
|
||||
}
|
||||
}
|
||||
|
||||
// GetByToken godoc
|
||||
//
|
||||
// @Tags application
|
||||
// @Summary Получить приложение по токену
|
||||
// @Description Возвращает идентификатор приложения по токену
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body domain.GetApplicationByTokenRequest true "Идентификатор приложения"
|
||||
// @Success 200 {object} domain.GetApplicationByTokenResponse
|
||||
// @Failure 400 {object} apierrors.Error
|
||||
// @Failure 404 {object} apierrors.Error
|
||||
// @Failure 500 {object} apierrors.Error
|
||||
// @Router /application/get_application_by_token [POST]
|
||||
func (c Application) GetByToken(ctx context.Context, req domain.GetApplicationByTokenRequest) (*domain.GetApplicationByTokenResponse, error) {
|
||||
result, err := c.service.GetByToken(ctx, req.Token)
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrApplicationNotFound):
|
||||
return nil, apierrors.New(
|
||||
codes.NotFound,
|
||||
domain.ErrCodeApplicationNotFound,
|
||||
fmt.Sprintf("application with token '%s' not found", req.Token),
|
||||
err,
|
||||
)
|
||||
case err != nil:
|
||||
return nil, err
|
||||
default:
|
||||
return result, nil
|
||||
}
|
||||
}
|
||||
|
||||
// GetByIdList godoc
|
||||
//
|
||||
// @Tags application
|
||||
|
||||
@@ -230,6 +230,20 @@ definitions:
|
||||
$ref: '#/definitions/domain.ServiceWithApps'
|
||||
type: array
|
||||
type: object
|
||||
domain.GetApplicationByTokenRequest:
|
||||
properties:
|
||||
token:
|
||||
type: string
|
||||
required:
|
||||
- token
|
||||
type: object
|
||||
domain.GetApplicationByTokenResponse:
|
||||
properties:
|
||||
applicationGroupId:
|
||||
type: integer
|
||||
applicationId:
|
||||
type: integer
|
||||
type: object
|
||||
domain.IdListRequest:
|
||||
properties:
|
||||
idList:
|
||||
@@ -629,6 +643,40 @@ paths:
|
||||
summary: Получить приложение по идентификатору
|
||||
tags:
|
||||
- application
|
||||
/application/get_application_by_token:
|
||||
post:
|
||||
consumes:
|
||||
- application/json
|
||||
description: Возвращает идентификатор приложения по токену
|
||||
parameters:
|
||||
- description: Идентификатор приложения
|
||||
in: body
|
||||
name: body
|
||||
required: true
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GetApplicationByTokenRequest'
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GetApplicationByTokenResponse'
|
||||
"400":
|
||||
description: Bad Request
|
||||
schema:
|
||||
$ref: '#/definitions/apierrors.Error'
|
||||
"404":
|
||||
description: Not Found
|
||||
schema:
|
||||
$ref: '#/definitions/apierrors.Error'
|
||||
"500":
|
||||
description: Internal Server Error
|
||||
schema:
|
||||
$ref: '#/definitions/apierrors.Error'
|
||||
summary: Получить приложение по токену
|
||||
tags:
|
||||
- application
|
||||
/application/get_applications:
|
||||
post:
|
||||
consumes:
|
||||
|
||||
@@ -54,3 +54,12 @@ type UpdateApplicationRequest struct {
|
||||
Name string `validate:"required"`
|
||||
Description string
|
||||
}
|
||||
|
||||
type GetApplicationByTokenRequest struct {
|
||||
Token string `validate:"required"`
|
||||
}
|
||||
|
||||
type GetApplicationByTokenResponse struct {
|
||||
ApplicationId int
|
||||
ApplicationGroupId int
|
||||
}
|
||||
@@ -154,6 +154,11 @@ func applicationCluster(c Controllers) []cluster.EndpointDescriptor {
|
||||
Inner: true,
|
||||
Handler: c.Application.GetById,
|
||||
},
|
||||
{
|
||||
Path: "system/application/get_application_by_token",
|
||||
Inner: true,
|
||||
Handler: c.Application.GetByToken,
|
||||
},
|
||||
{
|
||||
Path: "system/application/delete_applications",
|
||||
Inner: true,
|
||||
|
||||
@@ -55,6 +55,25 @@ func (s Application) GetById(ctx context.Context, appId int) (*domain.Applicatio
|
||||
return arr[0], nil
|
||||
}
|
||||
|
||||
func (s Application) GetByToken(ctx context.Context, tokenStr string) (*domain.GetApplicationByTokenResponse, error) {
|
||||
token, err := s.tokenRepo.GetTokenById(ctx, tokenStr)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get token by id")
|
||||
}
|
||||
if token == nil {
|
||||
return nil, domain.ErrApplicationNotFound
|
||||
}
|
||||
|
||||
app, err := s.appRepo.GetApplicationById(ctx, token.AppId)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get app by id")
|
||||
}
|
||||
return &domain.GetApplicationByTokenResponse{
|
||||
ApplicationId: app.Id,
|
||||
ApplicationGroupId: app.ApplicationGroupId,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s Application) GetByIdList(ctx context.Context, idList []int) ([]*domain.ApplicationWithTokens, error) {
|
||||
res, err := s.appRepo.GetApplicationByIdList(ctx, idList)
|
||||
if err != nil {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
type TokenRepo interface {
|
||||
GetTokenByAppIdList(ctx context.Context, appIdList []int) ([]entity.Token, error)
|
||||
GetTokenById(ctx context.Context, token string) (*entity.Token, error)
|
||||
}
|
||||
|
||||
type DomainRepo interface {
|
||||
|
||||
@@ -31,6 +31,7 @@ type ApplicationSuite struct {
|
||||
domainRepo repository.Domain
|
||||
appRepo repository.Application
|
||||
appGroupRepo repository.AppGroup
|
||||
tokenRepo repository.Token
|
||||
api *client.Client
|
||||
}
|
||||
|
||||
@@ -45,6 +46,7 @@ func (s *ApplicationSuite) SetupTest() {
|
||||
s.domainRepo = repository.NewDomain(s.testDb)
|
||||
s.appRepo = repository.NewApplication(s.testDb)
|
||||
s.appGroupRepo = repository.NewAppGroup(s.testDb)
|
||||
s.tokenRepo = repository.NewToken(s.testDb)
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) TestGetAllApplications() {
|
||||
@@ -243,6 +245,40 @@ func (s *ApplicationSuite) TestUpdate_AppNotFound() {
|
||||
s.Require().Equal(domain.ErrCodeApplicationNotFound, apiError.ErrorCode)
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) TestGetByToken() {
|
||||
toInsert := fake.It[[]domain.Application](fake.MaxSliceSize(2), fake.MinSliceSize(2))
|
||||
insertedApps := s.insertApps(toInsert)
|
||||
token := fake.It[string]()
|
||||
expectedApp := domain.GetApplicationByTokenResponse{
|
||||
ApplicationId: insertedApps[0].Id,
|
||||
ApplicationGroupId: insertedApps[0].ServiceId,
|
||||
}
|
||||
_, err := s.tokenRepo.SaveToken(s.T().Context(), token, expectedApp.ApplicationId, fake.It[int]())
|
||||
s.Require().NoError(err)
|
||||
|
||||
result := domain.GetApplicationByTokenResponse{}
|
||||
err = s.api.Invoke("system/application/get_application_by_token").
|
||||
JsonRequestBody(domain.GetApplicationByTokenRequest{Token: token}).
|
||||
JsonResponseBody(&result).
|
||||
Do(s.T().Context())
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(expectedApp, result)
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) TestGetByToken_NotFound() {
|
||||
toInsert := fake.It[[]domain.Application](fake.MaxSliceSize(2), fake.MinSliceSize(2))
|
||||
s.insertApps(toInsert)
|
||||
|
||||
result := domain.GetApplicationByTokenResponse{}
|
||||
err := s.api.Invoke("system/application/get_application_by_token").
|
||||
JsonRequestBody(domain.GetApplicationByTokenRequest{Token: fake.It[string]()}).
|
||||
JsonResponseBody(&result).
|
||||
Do(s.T().Context())
|
||||
apiError := apierrors.FromError(err)
|
||||
s.Require().NotNil(apiError)
|
||||
s.Require().Equal(domain.ErrCodeApplicationNotFound, apiError.ErrorCode)
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) createAppGroup() *entity.AppGroup {
|
||||
createdDomain, err := s.domainRepo.CreateDomain(
|
||||
s.T().Context(),
|
||||
|
||||
Reference in new issue
Block a user