@ -488,41 +488,86 @@ func TestIntegrationServiceAccountsStoreImpl_SearchOrgServiceAccounts(t *testing
t . Skip ( "skipping test in short mode" )
}
db , store := setupTestDatabase ( t )
initUsers := [ ] tests . TestUser {
{ Name : "satest-1" , Role : string ( org . RoleViewer ) , Login : "sa-1-satest-1" , IsServiceAccount : true } ,
{ Name : "extsvc- test-1" , Role : string ( org . RoleNone ) , Login : "sa-1-extsvc- test-1" , IsServiceAccount : true } ,
{ Name : "usertest-2" , Role : string ( org . RoleEditor ) , Login : "usertest-2" , IsServiceAccount : false } ,
{ Name : "sa test-3" , Role : string ( org . RoleEditor ) , Login : "sa-1-sa test-3" , IsServiceAccount : true } ,
{ Name : "sa test-4" , Role : string ( org . RoleAdmin ) , Login : "sa-1-sa test-4" , IsServiceAccount : true } ,
{ Name : "extsvc- test-3" , Role : string ( org . RoleNone ) , Login : "sa-1-extsvc- test-3" , IsServiceAccount : true } ,
{ Name : "extsvc- test-4" , Role : string ( org . RoleNone ) , Login : "sa-1-extsvc- test-4" , IsServiceAccount : true } ,
{ Name : "extsvc-test-5" , Role : string ( org . RoleNone ) , Login : "sa-1-extsvc-test-5" , IsServiceAccount : true } ,
{ Name : "extsvc- test-6" , Role : string ( org . RoleNone ) , Login : "sa-1-extsvc- test-6" , IsServiceAccount : true } ,
{ Name : "extsvc- test-7" , Role : string ( org . RoleNone ) , Login : "sa-1-extsvc- test-7" , IsServiceAccount : true } ,
{ Name : "extsvc- test-8" , Role : string ( org . RoleNone ) , Login : "sa-1-extsvc- test-8" , IsServiceAccount : true } ,
{ Name : "sa test-6" , Role : string ( org . RoleViewer ) , Login : "sa-1-sa test-6" , IsServiceAccount : true } ,
{ Name : "sa test-7" , Role : string ( org . RoleEditor ) , Login : "sa-1-sa test-7" , IsServiceAccount : true } ,
{ Name : "sa test-8" , Role : string ( org . RoleAdmin ) , Login : "sa-1-sa test-8" , IsServiceAccount : true } ,
}
db , store := setupTestDatabase ( t )
orgID := tests . SetupUsersServiceAccounts ( t , db , store . cfg , initUsers )
users , orgID := tests . SetupUsersServiceAccounts ( t , db , store . cfg , initUsers )
apiKeys := [ ] tests . TestApiKey {
{ Name : "sa-01-apikey-01" , OrgId : orgID , Key : "key01" , IsExpired : false , ServiceAccountID : & users [ 0 ] . ID } ,
{ Name : "sa-01-apikey-02" , OrgId : orgID , Key : "key02" , IsExpired : false , ServiceAccountID : & users [ 0 ] . ID } ,
{ Name : "sa-01-apikey-03" , OrgId : orgID , Key : "key03" , IsExpired : false , ServiceAccountID : & users [ 0 ] . ID } ,
{ Name : "sa-02-apikey-01" , OrgId : orgID , Key : "key04" , IsExpired : false , ServiceAccountID : & users [ 2 ] . ID } ,
{ Name : "sa-02-apikey-02" , OrgId : orgID , Key : "key05" , IsExpired : false , ServiceAccountID : & users [ 2 ] . ID } ,
{ Name : "sa-03-apikey-01" , OrgId : orgID , Key : "key06" , IsExpired : false , ServiceAccountID : & users [ 3 ] . ID } ,
}
tests . SetupApiKeys ( t , db , store . cfg , apiKeys )
userWithPerm := & user . SignedInUser {
OrgID : orgID ,
Permissions : map [ int64 ] map [ string ] [ ] string { orgID : { serviceaccounts . ActionRead : { serviceaccounts . ScopeAll } } } ,
}
expectedServiceAccount := func ( i int , tokens int64 ) * serviceaccounts . ServiceAccountDTO {
return & serviceaccounts . ServiceAccountDTO {
Id : users [ i ] . ID , UID : users [ i ] . UID , Name : users [ i ] . Name , Login : users [ i ] . Login , OrgId : orgID , Role : "None" , Tokens : tokens ,
}
}
tt := [ ] struct {
desc string
query * serviceaccounts . SearchOrgServiceAccountsQuery
expectedTotal int64 // Value of the result.TotalCount
expectedCount int // Length of the result.ServiceAccounts slice
expectedErr error
desc string
query * serviceaccounts . SearchOrgServiceAccountsQuery
expectedTotal int64 // Value of the result.TotalCount
expectedServiceAccounts [ ] * serviceaccounts . ServiceAccountDTO
expectedErr error
} {
{
desc : "should list all service accounts" ,
desc : "should list all service accounts with tokens count " ,
query : & serviceaccounts . SearchOrgServiceAccountsQuery {
OrgID : orgID ,
SignedInUser : userWithPerm ,
Filter : serviceaccounts . FilterIncludeAll ,
CountTokens : true ,
} ,
expectedTotal : 7 ,
expectedCount : 7 ,
expectedServiceAccounts : [ ] * serviceaccounts . ServiceAccountDTO {
expectedServiceAccount ( 0 , 3 ) ,
expectedServiceAccount ( 2 , 2 ) ,
expectedServiceAccount ( 3 , 1 ) ,
expectedServiceAccount ( 4 , 0 ) ,
expectedServiceAccount ( 5 , 0 ) ,
expectedServiceAccount ( 6 , 0 ) ,
expectedServiceAccount ( 7 , 0 ) ,
} ,
} ,
{
desc : "should list all service accounts with no tokens count" ,
query : & serviceaccounts . SearchOrgServiceAccountsQuery {
OrgID : orgID ,
SignedInUser : userWithPerm ,
Filter : serviceaccounts . FilterIncludeAll ,
} ,
expectedTotal : 7 ,
expectedServiceAccounts : [ ] * serviceaccounts . ServiceAccountDTO {
expectedServiceAccount ( 0 , 0 ) ,
expectedServiceAccount ( 2 , 0 ) ,
expectedServiceAccount ( 3 , 0 ) ,
expectedServiceAccount ( 4 , 0 ) ,
expectedServiceAccount ( 5 , 0 ) ,
expectedServiceAccount ( 6 , 0 ) ,
expectedServiceAccount ( 7 , 0 ) ,
} ,
} ,
{
desc : "should list no service accounts without permissions" ,
@ -534,8 +579,8 @@ func TestIntegrationServiceAccountsStoreImpl_SearchOrgServiceAccounts(t *testing
} ,
Filter : serviceaccounts . FilterIncludeAll ,
} ,
expectedTotal : 0 ,
expectedCount : 0 ,
expectedTotal : 0 ,
expectedServiceAccounts : [ ] * serviceaccounts . ServiceAccountDTO { } ,
} ,
{
desc : "should list one service accounts with restricted permissions" ,
@ -551,7 +596,10 @@ func TestIntegrationServiceAccountsStoreImpl_SearchOrgServiceAccounts(t *testing
Filter : serviceaccounts . FilterIncludeAll ,
} ,
expectedTotal : 2 ,
expectedCount : 2 ,
expectedServiceAccounts : [ ] * serviceaccounts . ServiceAccountDTO {
expectedServiceAccount ( 0 , 0 ) ,
expectedServiceAccount ( 6 , 0 ) ,
} ,
} ,
{
desc : "should list only external service accounts" ,
@ -559,9 +607,15 @@ func TestIntegrationServiceAccountsStoreImpl_SearchOrgServiceAccounts(t *testing
OrgID : orgID ,
SignedInUser : userWithPerm ,
Filter : serviceaccounts . FilterOnlyExternal ,
CountTokens : true ,
} ,
expectedTotal : 4 ,
expectedCount : 4 ,
expectedServiceAccounts : [ ] * serviceaccounts . ServiceAccountDTO {
expectedServiceAccount ( 0 , 3 ) ,
expectedServiceAccount ( 2 , 2 ) ,
expectedServiceAccount ( 3 , 1 ) ,
expectedServiceAccount ( 4 , 0 ) ,
} ,
} ,
{
desc : "should return service accounts with sa-1-satest login" ,
@ -570,9 +624,14 @@ func TestIntegrationServiceAccountsStoreImpl_SearchOrgServiceAccounts(t *testing
Query : "sa-1-satest" ,
SignedInUser : userWithPerm ,
Filter : serviceaccounts . FilterIncludeAll ,
CountTokens : true ,
} ,
expectedTotal : 3 ,
expectedCount : 3 ,
expectedServiceAccounts : [ ] * serviceaccounts . ServiceAccountDTO {
expectedServiceAccount ( 5 , 0 ) ,
expectedServiceAccount ( 6 , 0 ) ,
expectedServiceAccount ( 7 , 0 ) ,
} ,
} ,
{
desc : "should only count service accounts" ,
@ -582,8 +641,8 @@ func TestIntegrationServiceAccountsStoreImpl_SearchOrgServiceAccounts(t *testing
Filter : serviceaccounts . FilterIncludeAll ,
CountOnly : true ,
} ,
expectedTotal : 7 ,
expectedCount : 0 ,
expectedTotal : 7 ,
expectedServiceAccounts : [ ] * serviceaccounts . ServiceAccountDTO { } ,
} ,
{
desc : "should paginate result" ,
@ -595,7 +654,9 @@ func TestIntegrationServiceAccountsStoreImpl_SearchOrgServiceAccounts(t *testing
Filter : serviceaccounts . FilterIncludeAll ,
} ,
expectedTotal : 7 ,
expectedCount : 1 ,
expectedServiceAccounts : [ ] * serviceaccounts . ServiceAccountDTO {
expectedServiceAccount ( 7 , 0 ) ,
} ,
} ,
}
for _ , tc := range tt {
@ -609,7 +670,10 @@ func TestIntegrationServiceAccountsStoreImpl_SearchOrgServiceAccounts(t *testing
}
require . Equal ( t , tc . expectedTotal , got . TotalCount )
require . Len ( t , got . ServiceAccounts , tc . expectedCount )
require . Len ( t , got . ServiceAccounts , len ( tc . expectedServiceAccounts ) )
for i , sa := range got . ServiceAccounts {
require . EqualValues ( t , tc . expectedServiceAccounts [ i ] , sa )
}
} )
}
}
@ -628,7 +692,7 @@ func TestIntegrationServiceAccountsStoreImpl_EnableServiceAccounts(t *testing.T)
}
db , store := setupTestDatabase ( t )
orgID := tests . SetupUsersServiceAccounts ( t , db , store . cfg , initUsers )
_ , orgID := tests . SetupUsersServiceAccounts ( t , db , store . cfg , initUsers )
fetchStates := func ( ) map [ int64 ] bool {
sa1 , err := store . RetrieveServiceAccount ( ctx , & serviceaccounts . GetServiceAccountQuery { OrgID : orgID , ID : 1 } )