Service accounts: UI migration results (#68789)

* ui migration WIP

* merge

* migration tests for api

* revert chagnes to align with main

* revert chagnes to align with main

* revert chagnes to align with main

* remove unused code and comments

* revert gen files

* retry logic inplace

* fix a any

* fixed types

* migraiton results now show only result if no failures

* review comments

* wording to make it more actionable

* add migraiton summary text onyl for failed apikeys

* fixed wording and added a close button to the modal

* made the button close the modal

* moved state into component

* fix based on review, naming and removed unused code

* service account migration state optional

* making migration result undefined

* showing total and migrated numbers for a successful migration

* fix payload const to take the payload
This commit is contained in:
Eric Leijonmarck
2023-06-08 10:09:30 +02:00
committed by GitHub
parent 862b04c1b2
commit 081f59feba
14 changed files with 291 additions and 77 deletions
@@ -2,6 +2,7 @@ package api
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
@@ -235,6 +236,66 @@ func TestServiceAccountsAPI_UpdateServiceAccount(t *testing.T) {
}
}
func TestServiceAccountsAPI_MigrateApiKeysToServiceAccounts(t *testing.T) {
type TestCase struct {
desc string
orgId int64
basicRole org.RoleType
permissions []accesscontrol.Permission
expectedMigrationResult *serviceaccounts.MigrationResult
expectedCode int
}
tests := []TestCase{
{
desc: "should be able to migrate API keys to service accounts with correct permissions",
orgId: 1,
basicRole: org.RoleAdmin,
permissions: []accesscontrol.Permission{
{Action: serviceaccounts.ActionCreate, Scope: serviceaccounts.ScopeAll},
},
expectedMigrationResult: &serviceaccounts.MigrationResult{
Total: 5,
Migrated: 4,
Failed: 1,
FailedDetails: []string{"API key name: failedKey - Error: migration error"},
},
expectedCode: http.StatusOK,
},
{
desc: "should not be able to migrate API keys to service accounts with wrong permissions",
orgId: 2,
basicRole: org.RoleAdmin,
permissions: []accesscontrol.Permission{
{Action: serviceaccounts.ActionCreate, Scope: serviceaccounts.ScopeAll},
},
expectedCode: http.StatusForbidden,
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
server := setupTests(t, func(a *ServiceAccountsAPI) {
a.service = &fakeServiceAccountService{ExpectedMigrationResult: tt.expectedMigrationResult}
})
req := server.NewRequest(http.MethodPost, "/api/serviceaccounts/migrate", nil)
webtest.RequestWithSignedInUser(req, &user.SignedInUser{OrgRole: tt.basicRole, OrgID: tt.orgId, Permissions: map[int64]map[string][]string{1: accesscontrol.GroupScopesByAction(tt.permissions)}})
res, err := server.SendJSON(req)
require.NoError(t, err)
assert.Equal(t, tt.expectedCode, res.StatusCode)
if tt.expectedCode == http.StatusOK {
var result serviceaccounts.MigrationResult
err := json.NewDecoder(res.Body).Decode(&result)
require.NoError(t, err)
assert.Equal(t, tt.expectedMigrationResult, &result)
}
require.NoError(t, res.Body.Close())
})
}
}
func setupTests(t *testing.T, opts ...func(a *ServiceAccountsAPI)) *webtest.Server {
t.Helper()
cfg := setting.NewCfg()
@@ -264,6 +325,7 @@ type fakeServiceAccountService struct {
ExpectedServiceAccountTokens []apikey.APIKey
ExpectedServiceAccount *serviceaccounts.ServiceAccountDTO
ExpectedServiceAccountProfile *serviceaccounts.ServiceAccountProfileDTO
ExpectedMigrationResult *serviceaccounts.MigrationResult
}
func (f *fakeServiceAccountService) CreateServiceAccount(ctx context.Context, orgID int64, saForm *serviceaccounts.CreateServiceAccountForm) (*serviceaccounts.ServiceAccountDTO, error) {
@@ -293,3 +355,8 @@ func (f *fakeServiceAccountService) AddServiceAccountToken(ctx context.Context,
func (f *fakeServiceAccountService) DeleteServiceAccountToken(ctx context.Context, orgID, id, tokenID int64) error {
return f.ExpectedErr
}
func (f *fakeServiceAccountService) MigrateApiKeysToServiceAccounts(ctx context.Context, orgID int64) (*serviceaccounts.MigrationResult, error) {
fmt.Printf("fake migration result: %v", f.ExpectedMigrationResult)
return f.ExpectedMigrationResult, f.ExpectedErr
}