Skip to content

Commit fec853b

Browse files
authored
feat: implement CheckCurrentUserPATTitle RPC (#1469)
1 parent 6f1b83f commit fec853b

14 files changed

Lines changed: 2614 additions & 1778 deletions

File tree

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ TAG := $(shell git rev-list --tags --max-count=1)
44
VERSION := $(shell git describe --tags ${TAG})
55
.PHONY: build check fmt lint test test-race vet test-cover-html help install proto admin-app compose-up-dev
66
.DEFAULT_GOAL := build
7-
PROTON_COMMIT := "0e66eea2643eb54aba01be2c2b7298e96c82d749"
7+
PROTON_COMMIT := "3acffc58e07cdfdc080d041f4a1bdb5e8545bbd5"
88

99
admin-app:
1010
@echo " > generating admin build"

‎core/userpat/mocks/repository.go‎

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎core/userpat/service.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ func (s *Service) GetByID(ctx context.Context, id string) (patmodels.PAT, error)
9595
return s.repo.GetByID(ctx, id)
9696
}
9797

98+
// IsTitleAvailable checks if a PAT title is available for the given user and org.
99+
func (s *Service) IsTitleAvailable(ctx context.Context, userID, orgID, title string) (bool, error) {
100+
if !s.config.Enabled {
101+
return false, paterrors.ErrDisabled
102+
}
103+
return s.repo.IsTitleAvailable(ctx, userID, orgID, title)
104+
}
105+
98106
// Get retrieves a PAT by ID, verifying it belongs to the given user.
99107
// Returns ErrDisabled if PATs are not enabled, ErrNotFound if the PAT
100108
// does not exist or belongs to a different user.

‎core/userpat/service_test.go‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,3 +2369,91 @@ func TestService_Regenerate(t *testing.T) {
23692369
})
23702370
}
23712371
}
2372+
2373+
func TestService_IsTitleAvailable(t *testing.T) {
2374+
tests := []struct {
2375+
name string
2376+
setup func() *userpat.Service
2377+
userID string
2378+
orgID string
2379+
title string
2380+
wantAvailable bool
2381+
wantErr bool
2382+
wantErrIs error
2383+
}{
2384+
{
2385+
name: "should return ErrDisabled when PAT feature is disabled",
2386+
userID: "user-1",
2387+
orgID: "org-1",
2388+
title: "my-token",
2389+
setup: func() *userpat.Service {
2390+
return userpat.NewService(log.NewNoop(), nil, userpat.Config{
2391+
Enabled: false,
2392+
}, nil, nil, nil, nil)
2393+
},
2394+
wantErr: true,
2395+
wantErrIs: paterrors.ErrDisabled,
2396+
},
2397+
{
2398+
name: "should return true when title is available",
2399+
userID: "user-1",
2400+
orgID: "org-1",
2401+
title: "new-token",
2402+
setup: func() *userpat.Service {
2403+
repo := mocks.NewRepository(t)
2404+
repo.EXPECT().IsTitleAvailable(mock.Anything, "user-1", "org-1", "new-token").
2405+
Return(true, nil)
2406+
return userpat.NewService(log.NewNoop(), repo, defaultConfig, nil, nil, nil, nil)
2407+
},
2408+
wantAvailable: true,
2409+
},
2410+
{
2411+
name: "should return false when title is taken",
2412+
userID: "user-1",
2413+
orgID: "org-1",
2414+
title: "existing-token",
2415+
setup: func() *userpat.Service {
2416+
repo := mocks.NewRepository(t)
2417+
repo.EXPECT().IsTitleAvailable(mock.Anything, "user-1", "org-1", "existing-token").
2418+
Return(false, nil)
2419+
return userpat.NewService(log.NewNoop(), repo, defaultConfig, nil, nil, nil, nil)
2420+
},
2421+
wantAvailable: false,
2422+
},
2423+
{
2424+
name: "should return error when repo fails",
2425+
userID: "user-1",
2426+
orgID: "org-1",
2427+
title: "my-token",
2428+
setup: func() *userpat.Service {
2429+
repo := mocks.NewRepository(t)
2430+
repo.EXPECT().IsTitleAvailable(mock.Anything, "user-1", "org-1", "my-token").
2431+
Return(false, errors.New("db error"))
2432+
return userpat.NewService(log.NewNoop(), repo, defaultConfig, nil, nil, nil, nil)
2433+
},
2434+
wantErr: true,
2435+
},
2436+
}
2437+
2438+
for _, tt := range tests {
2439+
t.Run(tt.name, func(t *testing.T) {
2440+
svc := tt.setup()
2441+
available, err := svc.IsTitleAvailable(context.Background(), tt.userID, tt.orgID, tt.title)
2442+
if tt.wantErr {
2443+
if err == nil {
2444+
t.Fatal("IsTitleAvailable() expected error, got nil")
2445+
}
2446+
if tt.wantErrIs != nil && !errors.Is(err, tt.wantErrIs) {
2447+
t.Errorf("IsTitleAvailable() error = %v, want %v", err, tt.wantErrIs)
2448+
}
2449+
return
2450+
}
2451+
if err != nil {
2452+
t.Fatalf("IsTitleAvailable() unexpected error: %v", err)
2453+
}
2454+
if available != tt.wantAvailable {
2455+
t.Errorf("IsTitleAvailable() = %v, want %v", available, tt.wantAvailable)
2456+
}
2457+
})
2458+
}
2459+
}

‎core/userpat/userpat.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Repository interface {
1414
GetByID(ctx context.Context, id string) (models.PAT, error)
1515
List(ctx context.Context, userID, orgID string, query *rql.Query) (models.PATList, error)
1616
GetBySecretHash(ctx context.Context, secretHash string) (models.PAT, error)
17+
IsTitleAvailable(ctx context.Context, userID, orgID, title string) (bool, error)
1718
UpdateLastUsedAt(ctx context.Context, id string, at time.Time) error
1819
Update(ctx context.Context, pat models.PAT) (models.PAT, error)
1920
Regenerate(ctx context.Context, id, secretHash string, expiresAt time.Time) (models.PAT, error)

‎internal/api/v1beta1connect/interfaces.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,5 +407,6 @@ type UserPATService interface {
407407
Delete(ctx context.Context, userID, id string) error
408408
Update(ctx context.Context, toUpdate models.PAT) (models.PAT, error)
409409
Regenerate(ctx context.Context, userID, id string, newExpiresAt time.Time) (models.PAT, string, error)
410+
IsTitleAvailable(ctx context.Context, userID, orgID, title string) (bool, error)
410411
ListAllowedRoles(ctx context.Context, scopes []string) ([]role.Role, error)
411412
}

‎internal/api/v1beta1connect/mocks/user_pat_service.go‎

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/api/v1beta1connect/user_pat.go‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,40 @@ func (h *ConnectHandler) RegenerateCurrentUserPAT(ctx context.Context, request *
270270
}), nil
271271
}
272272

273+
func (h *ConnectHandler) CheckCurrentUserPATTitle(ctx context.Context, request *connect.Request[frontierv1beta1.CheckCurrentUserPATTitleRequest]) (*connect.Response[frontierv1beta1.CheckCurrentUserPATTitleResponse], error) {
274+
errorLogger := NewErrorLogger()
275+
276+
principal, err := h.GetLoggedInPrincipal(ctx)
277+
if err != nil {
278+
return nil, err
279+
}
280+
if principal.User == nil {
281+
return nil, connect.NewError(connect.CodePermissionDenied, ErrUnauthenticated)
282+
}
283+
284+
if err := request.Msg.Validate(); err != nil {
285+
return nil, connect.NewError(connect.CodeInvalidArgument, err)
286+
}
287+
288+
available, err := h.userPATService.IsTitleAvailable(ctx, principal.User.ID, request.Msg.GetOrgId(), request.Msg.GetTitle())
289+
if err != nil {
290+
errorLogger.LogServiceError(ctx, request, "CheckCurrentUserPATTitle", err,
291+
zap.String("user_id", principal.User.ID),
292+
zap.String("org_id", request.Msg.GetOrgId()))
293+
294+
switch {
295+
case errors.Is(err, paterrors.ErrDisabled):
296+
return nil, connect.NewError(connect.CodeFailedPrecondition, err)
297+
default:
298+
return nil, connect.NewError(connect.CodeInternal, ErrInternalServerError)
299+
}
300+
}
301+
302+
return connect.NewResponse(&frontierv1beta1.CheckCurrentUserPATTitleResponse{
303+
Available: available,
304+
}), nil
305+
}
306+
273307
func transformPATToPB(pat models.PAT, patValue string) *frontierv1beta1.PAT {
274308
pbPAT := &frontierv1beta1.PAT{
275309
Id: pat.ID,

0 commit comments

Comments
 (0)