|
| 1 | +// |
| 2 | +// Copyright 2026 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package service |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "testing" |
| 21 | + |
| 22 | + pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1" |
| 23 | + "github.com/chainloop-dev/chainloop/app/controlplane/internal/usercontext/entities" |
| 24 | + "github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz" |
| 25 | + "github.com/google/uuid" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | +) |
| 28 | + |
| 29 | +func TestAPITokenService_Create_OrgTokenWithoutProjectIsRejected(t *testing.T) { |
| 30 | + t.Parallel() |
| 31 | + |
| 32 | + svc := &APITokenService{service: newService()} |
| 33 | + |
| 34 | + ctx := context.Background() |
| 35 | + ctx = entities.WithCurrentOrg(ctx, &entities.Org{ID: uuid.NewString()}) |
| 36 | + ctx = entities.WithCurrentAPIToken(ctx, &entities.APIToken{ID: uuid.NewString(), ProjectID: nil}) |
| 37 | + |
| 38 | + req := &pb.APITokenServiceCreateRequest{Name: "test-token"} |
| 39 | + |
| 40 | + _, err := svc.Create(ctx, req) |
| 41 | + assert.Error(t, err) |
| 42 | + assert.Contains(t, err.Error(), "org-level API tokens must specify a project") |
| 43 | +} |
| 44 | + |
| 45 | +func TestAPITokenService_List_OrgTokenForcesProjectScope(t *testing.T) { |
| 46 | + t.Parallel() |
| 47 | + |
| 48 | + tests := []struct { |
| 49 | + name string |
| 50 | + token *entities.APIToken |
| 51 | + wantScope biz.APITokenScope |
| 52 | + }{ |
| 53 | + { |
| 54 | + name: "org-level token forces project scope", |
| 55 | + token: &entities.APIToken{ID: uuid.NewString(), ProjectID: nil}, |
| 56 | + wantScope: biz.APITokenScopeProject, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "project-scoped token does not override scope", |
| 60 | + token: &entities.APIToken{ID: uuid.NewString(), ProjectID: toUUIDPtr(uuid.New())}, |
| 61 | + wantScope: "", // mapTokenScope returns "" for SCOPE_UNSPECIFIED |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tc := range tests { |
| 66 | + t.Run(tc.name, func(t *testing.T) { |
| 67 | + ctx := context.Background() |
| 68 | + ctx = entities.WithCurrentAPIToken(ctx, tc.token) |
| 69 | + |
| 70 | + scope := mapTokenScope(pb.APITokenServiceListRequest_SCOPE_UNSPECIFIED) |
| 71 | + if token := entities.CurrentAPIToken(ctx); token != nil && token.ProjectID == nil { |
| 72 | + scope = biz.APITokenScopeProject |
| 73 | + } |
| 74 | + |
| 75 | + assert.Equal(t, tc.wantScope, scope) |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestAPITokenService_Revoke_OrgTokenCannotRevokeOrgTokens(t *testing.T) { |
| 81 | + t.Parallel() |
| 82 | + |
| 83 | + orgID := uuid.NewString() |
| 84 | + |
| 85 | + tests := []struct { |
| 86 | + name string |
| 87 | + callerToken *entities.APIToken |
| 88 | + targetToken *biz.APIToken |
| 89 | + wantForbidden bool |
| 90 | + }{ |
| 91 | + { |
| 92 | + name: "org-level token revoking org-level token is forbidden", |
| 93 | + callerToken: &entities.APIToken{ID: uuid.NewString(), ProjectID: nil}, |
| 94 | + targetToken: &biz.APIToken{ |
| 95 | + ID: uuid.New(), |
| 96 | + OrganizationID: uuid.MustParse(orgID), |
| 97 | + ProjectID: nil, |
| 98 | + }, |
| 99 | + wantForbidden: true, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "org-level token revoking project token is allowed", |
| 103 | + callerToken: &entities.APIToken{ID: uuid.NewString(), ProjectID: nil}, |
| 104 | + targetToken: &biz.APIToken{ |
| 105 | + ID: uuid.New(), |
| 106 | + OrganizationID: uuid.MustParse(orgID), |
| 107 | + ProjectID: toUUIDPtr(uuid.New()), |
| 108 | + }, |
| 109 | + wantForbidden: false, |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + for _, tc := range tests { |
| 114 | + t.Run(tc.name, func(t *testing.T) { |
| 115 | + ctx := context.Background() |
| 116 | + ctx = entities.WithCurrentAPIToken(ctx, tc.callerToken) |
| 117 | + |
| 118 | + forbidden := false |
| 119 | + if token := entities.CurrentAPIToken(ctx); token != nil && token.ProjectID == nil { |
| 120 | + if tc.targetToken.ProjectID == nil { |
| 121 | + forbidden = true |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + assert.Equal(t, tc.wantForbidden, forbidden) |
| 126 | + }) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func toUUIDPtr(id uuid.UUID) *uuid.UUID { |
| 131 | + return &id |
| 132 | +} |
0 commit comments