@ -70,17 +70,23 @@ func TestExtSvcAccountsService_ManageExtSvcAccount(t *testing.T) {
name string
init func ( env * TestEnv )
cmd sa . ManageExtSvcAccountCmd
checks func ( t * testing . T , env * TestEnv )
want int64
wantErr bool
} {
{
name : "should remove service account when disabled " ,
name : "should disable service account " ,
init : func ( env * TestEnv ) {
// A previous service account was attached to this slug
env . SaSvc . On ( "RetrieveServiceAccountIdByName" , mock . Anything , mock . Anything , mock . Anything ) . Return ( extSvcAccID , nil )
env . SaSvc . On ( "DeleteServiceAccount" , mock . Anything , mock . Anything , mock . Anything ) . Return ( nil )
env . AcStore . On ( "DeleteExternalServiceRole" , mock . Anything , mock . Anything ) . Return ( nil )
env . SaSvc . On ( "RetrieveServiceAccountIdByName" , mock . Anything , extSvcOrgID , sa . ExtSvcPrefix + extSvcSlug ) . Return ( extSvcAccID , nil )
env . SaSvc . On ( "EnableServiceAccount" , mock . Anything , extSvcOrgID , extSvcAccID , false ) . Return ( nil )
env . AcStore . On ( "SaveExternalServiceRole" ,
mock . Anything ,
mock . MatchedBy ( func ( cmd ac . SaveExternalServiceRoleCommand ) bool {
return cmd . ServiceAccountID == extSvcAccID && cmd . ExternalServiceID == extSvcSlug &&
cmd . OrgID == int64 ( ac . GlobalOrgID ) && len ( cmd . Permissions ) == 1 &&
cmd . Permissions [ 0 ] == extSvcPerms [ 0 ]
} ) ) .
Return ( nil )
} ,
cmd : sa . ManageExtSvcAccountCmd {
ExtSvcSlug : extSvcSlug ,
@ -88,26 +94,16 @@ func TestExtSvcAccountsService_ManageExtSvcAccount(t *testing.T) {
OrgID : extSvcOrgID ,
Permissions : extSvcPerms ,
} ,
checks : func ( t * testing . T , env * TestEnv ) {
env . SaSvc . AssertCalled ( t , "RetrieveServiceAccountIdByName" , mock . Anything ,
mock . MatchedBy ( func ( orgID int64 ) bool { return orgID == extSvcOrgID } ) ,
mock . MatchedBy ( func ( slug string ) bool { return slug == sa . ExtSvcPrefix + extSvcSlug } ) )
env . SaSvc . AssertCalled ( t , "DeleteServiceAccount" , mock . Anything ,
mock . MatchedBy ( func ( orgID int64 ) bool { return orgID == extSvcOrgID } ) ,
mock . MatchedBy ( func ( saID int64 ) bool { return saID == extSvcAccID } ) )
env . AcStore . AssertCalled ( t , "DeleteExternalServiceRole" , mock . Anything ,
mock . MatchedBy ( func ( slug string ) bool { return slug == extSvcSlug } ) )
} ,
want : 0 ,
want : extSvcAccID ,
wantErr : false ,
} ,
{
name : "should remove service account when no permission" ,
init : func ( env * TestEnv ) {
// A previous service account was attached to this slug
env . SaSvc . On ( "RetrieveServiceAccountIdByName" , mock . Anything , mock . Anything , mock . Anythin g) . Return ( extSvcAccID , nil )
env . SaSvc . On ( "DeleteServiceAccount" , mock . Anything , mock . Anything , mock . Anything ) . Return ( nil )
env . AcStore . On ( "DeleteExternalServiceRole" , mock . Anything , mock . Anythin g) . Return ( nil )
env . SaSvc . On ( "RetrieveServiceAccountIdByName" , mock . Anything , extSvcOrgID , sa . ExtSvcPrefix + extSvcSlug ) . Return ( extSvcAccID , nil )
env . SaSvc . On ( "DeleteServiceAccount" , mock . Anything , extSvcOrgID , extSvcAccID ) . Return ( nil )
env . AcStore . On ( "DeleteExternalServiceRole" , mock . Anything , extSvcSlug ) . Return ( nil )
} ,
cmd : sa . ManageExtSvcAccountCmd {
ExtSvcSlug : extSvcSlug ,
@ -115,16 +111,6 @@ func TestExtSvcAccountsService_ManageExtSvcAccount(t *testing.T) {
OrgID : extSvcOrgID ,
Permissions : [ ] ac . Permission { } ,
} ,
checks : func ( t * testing . T , env * TestEnv ) {
env . SaSvc . AssertCalled ( t , "RetrieveServiceAccountIdByName" , mock . Anything ,
mock . MatchedBy ( func ( orgID int64 ) bool { return orgID == extSvcOrgID } ) ,
mock . MatchedBy ( func ( slug string ) bool { return slug == sa . ExtSvcPrefix + extSvcSlug } ) )
env . SaSvc . AssertCalled ( t , "DeleteServiceAccount" , mock . Anything ,
mock . MatchedBy ( func ( orgID int64 ) bool { return orgID == extSvcOrgID } ) ,
mock . MatchedBy ( func ( saID int64 ) bool { return saID == extSvcAccID } ) )
env . AcStore . AssertCalled ( t , "DeleteExternalServiceRole" , mock . Anything ,
mock . MatchedBy ( func ( slug string ) bool { return slug == extSvcSlug } ) )
} ,
want : 0 ,
wantErr : false ,
} ,
@ -132,11 +118,24 @@ func TestExtSvcAccountsService_ManageExtSvcAccount(t *testing.T) {
name : "should create new service account" ,
init : func ( env * TestEnv ) {
// No previous service account was attached to this slug
env . SaSvc . On ( "RetrieveServiceAccountIdByName" , mock . Anything , mock . Anything , mock . Anythin g) .
env . SaSvc . On ( "RetrieveServiceAccountIdByName" , mock . Anything , extSvcOrgID , sa . ExtSvcPrefix + extSvcSlu g) .
Return ( int64 ( 0 ) , sa . ErrServiceAccountNotFound . Errorf ( "mock" ) )
env . SaSvc . On ( "CreateServiceAccount" , mock . Anything , mock . Anything , mock . Anything ) .
env . SaSvc . On ( "CreateServiceAccount" ,
mock . Anything ,
extSvcOrgID ,
mock . MatchedBy ( func ( cmd * sa . CreateServiceAccountForm ) bool {
return cmd . Name == sa . ExtSvcPrefix + extSvcSlug && * cmd . Role == roletype . RoleNone
} ) ) .
Return ( extSvcAccount , nil )
env . AcStore . On ( "SaveExternalServiceRole" , mock . Anything , mock . Anything ) . Return ( nil )
env . SaSvc . On ( "EnableServiceAccount" , mock . Anything , extSvcOrgID , extSvcAccount . Id , true ) . Return ( nil )
env . AcStore . On ( "SaveExternalServiceRole" ,
mock . Anything ,
mock . MatchedBy ( func ( cmd ac . SaveExternalServiceRoleCommand ) bool {
return cmd . ServiceAccountID == extSvcAccount . Id && cmd . ExternalServiceID == extSvcSlug &&
cmd . OrgID == int64 ( ac . GlobalOrgID ) && len ( cmd . Permissions ) == 1 &&
cmd . Permissions [ 0 ] == extSvcPerms [ 0 ]
} ) ) .
Return ( nil )
} ,
cmd : sa . ManageExtSvcAccountCmd {
ExtSvcSlug : extSvcSlug ,
@ -144,23 +143,6 @@ func TestExtSvcAccountsService_ManageExtSvcAccount(t *testing.T) {
OrgID : extSvcOrgID ,
Permissions : extSvcPerms ,
} ,
checks : func ( t * testing . T , env * TestEnv ) {
env . SaSvc . AssertCalled ( t , "RetrieveServiceAccountIdByName" , mock . Anything ,
mock . MatchedBy ( func ( orgID int64 ) bool { return orgID == extSvcOrgID } ) ,
mock . MatchedBy ( func ( slug string ) bool { return slug == sa . ExtSvcPrefix + extSvcSlug } ) )
env . SaSvc . AssertCalled ( t , "CreateServiceAccount" , mock . Anything ,
mock . MatchedBy ( func ( orgID int64 ) bool { return orgID == extSvcOrgID } ) ,
mock . MatchedBy ( func ( cmd * sa . CreateServiceAccountForm ) bool {
return cmd . Name == sa . ExtSvcPrefix + extSvcSlug && * cmd . Role == roletype . RoleNone
} ) ,
)
env . AcStore . AssertCalled ( t , "SaveExternalServiceRole" , mock . Anything ,
mock . MatchedBy ( func ( cmd ac . SaveExternalServiceRoleCommand ) bool {
return cmd . ServiceAccountID == extSvcAccount . Id && cmd . ExternalServiceID == extSvcSlug &&
cmd . OrgID == int64 ( ac . GlobalOrgID ) && len ( cmd . Permissions ) == 1 &&
cmd . Permissions [ 0 ] == extSvcPerms [ 0 ]
} ) )
} ,
want : extSvcAccID ,
wantErr : false ,
} ,
@ -168,9 +150,17 @@ func TestExtSvcAccountsService_ManageExtSvcAccount(t *testing.T) {
name : "should update service account" ,
init : func ( env * TestEnv ) {
// A previous service account was attached to this slug
env . SaSvc . On ( "RetrieveServiceAccountIdByName" , mock . Anything , mock . Anything , mock . Anythin g) .
env . SaSvc . On ( "RetrieveServiceAccountIdByName" , mock . Anything , extSvcOrgID , sa . ExtSvcPrefix + extSvcSlu g) .
Return ( int64 ( 11 ) , nil )
env . AcStore . On ( "SaveExternalServiceRole" , mock . Anything , mock . Anything ) . Return ( nil )
env . SaSvc . On ( "EnableServiceAccount" , mock . Anything , extSvcOrgID , int64 ( 11 ) , true ) . Return ( nil )
env . AcStore . On ( "SaveExternalServiceRole" ,
mock . Anything ,
mock . MatchedBy ( func ( cmd ac . SaveExternalServiceRoleCommand ) bool {
return cmd . ServiceAccountID == int64 ( 11 ) && cmd . ExternalServiceID == extSvcSlug &&
cmd . OrgID == int64 ( ac . GlobalOrgID ) && len ( cmd . Permissions ) == 1 &&
cmd . Permissions [ 0 ] == extSvcPerms [ 0 ]
} ) ) .
Return ( nil )
} ,
cmd : sa . ManageExtSvcAccountCmd {
ExtSvcSlug : extSvcSlug ,
@ -178,17 +168,6 @@ func TestExtSvcAccountsService_ManageExtSvcAccount(t *testing.T) {
OrgID : extSvcOrgID ,
Permissions : extSvcPerms ,
} ,
checks : func ( t * testing . T , env * TestEnv ) {
env . SaSvc . AssertCalled ( t , "RetrieveServiceAccountIdByName" , mock . Anything ,
mock . MatchedBy ( func ( orgID int64 ) bool { return orgID == extSvcOrgID } ) ,
mock . MatchedBy ( func ( slug string ) bool { return slug == sa . ExtSvcPrefix + extSvcSlug } ) )
env . AcStore . AssertCalled ( t , "SaveExternalServiceRole" , mock . Anything ,
mock . MatchedBy ( func ( cmd ac . SaveExternalServiceRoleCommand ) bool {
return cmd . ServiceAccountID == int64 ( 11 ) && cmd . ExternalServiceID == extSvcSlug &&
cmd . OrgID == int64 ( ac . GlobalOrgID ) && len ( cmd . Permissions ) == 1 &&
cmd . Permissions [ 0 ] == extSvcPerms [ 0 ]
} ) )
} ,
want : 11 ,
wantErr : false ,
} ,
@ -210,10 +189,6 @@ func TestExtSvcAccountsService_ManageExtSvcAccount(t *testing.T) {
}
require . NoError ( t , err )
if tt . checks != nil {
tt . checks ( t , env )
}
require . Equal ( t , tt . want , got )
} )
}
@ -242,12 +217,20 @@ func TestExtSvcAccountsService_SaveExternalService(t *testing.T) {
wantErr bool
} {
{
name : "should remove service account when disabled " ,
name : "should disable service account " ,
init : func ( env * TestEnv ) {
// A previous service account was attached to this slug
env . SaSvc . On ( "RetrieveServiceAccountIdByName" , mock . Anything , mock . Anything , mock . Anything ) . Return ( extSvcAccID , nil )
env . SaSvc . On ( "DeleteServiceAccount" , mock . Anything , mock . Anything , mock . Anything ) . Return ( nil )
env . AcStore . On ( "DeleteExternalServiceRole" , mock . Anything , mock . Anything ) . Return ( nil )
env . SaSvc . On ( "RetrieveServiceAccountIdByName" , mock . Anything , tmpOrgID , sa . ExtSvcPrefix + extSvcSlug ) .
Return ( extSvcAccID , nil )
env . SaSvc . On ( "EnableServiceAccount" , mock . Anything , tmpOrgID , extSvcAccID , false ) . Return ( nil )
env . AcStore . On ( "SaveExternalServiceRole" ,
mock . Anything ,
mock . MatchedBy ( func ( cmd ac . SaveExternalServiceRoleCommand ) bool {
return cmd . ServiceAccountID == extSvcAccID && cmd . ExternalServiceID == extSvcSlug &&
cmd . OrgID == int64 ( ac . GlobalOrgID ) && len ( cmd . Permissions ) == 1 &&
cmd . Permissions [ 0 ] == extSvcPerms [ 0 ]
} ) ) .
Return ( nil )
// A token was previously stored in the secret store
_ = env . SkvStore . Set ( context . Background ( ) , tmpOrgID , extSvcSlug , kvStoreType , "ExtSvcSecretToken" )
} ,
@ -259,27 +242,26 @@ func TestExtSvcAccountsService_SaveExternalService(t *testing.T) {
} ,
} ,
checks : func ( t * testing . T , env * TestEnv ) {
env . SaSvc . AssertCalled ( t , "RetrieveServiceAccountIdByName" , mock . Anything ,
mock . MatchedBy ( func ( orgID int64 ) bool { return orgID == tmpOrgID } ) ,
mock . MatchedBy ( func ( slug string ) bool { return slug == sa . ExtSvcPrefix + extSvcSlug } ) )
env . SaSvc . AssertCalled ( t , "DeleteServiceAccount" , mock . Anything ,
mock . MatchedBy ( func ( orgID int64 ) bool { return orgID == tmpOrgID } ) ,
mock . MatchedBy ( func ( saID int64 ) bool { return saID == extSvcAccID } ) )
env . AcStore . AssertCalled ( t , "DeleteExternalServiceRole" , mock . Anything ,
mock . MatchedBy ( func ( slug string ) bool { return slug == extSvcSlug } ) )
_ , ok , _ := env . SkvStore . Get ( context . Background ( ) , tmpOrgID , extSvcSlug , kvStoreType )
require . False ( t , ok , "secret should have been removed from store" )
require . True ( t , ok , "secret should have been kept in store" )
} ,
want : & extsvcauth . ExternalService {
Name : extSvcSlug ,
ID : extSvcSlug ,
Secret : "not empty" ,
} ,
want : nil ,
wantErr : false ,
} ,
{
name : "should remove service account when no permission" ,
init : func ( env * TestEnv ) {
// A previous service account was attached to this slug
env . SaSvc . On ( "RetrieveServiceAccountIdByName" , mock . Anything , mock . Anything , mock . Anything ) . Return ( extSvcAccID , nil )
env . SaSvc . On ( "DeleteServiceAccount" , mock . Anything , mock . Anything , mock . Anything ) . Return ( nil )
env . AcStore . On ( "DeleteExternalServiceRole" , mock . Anything , mock . Anything ) . Return ( nil )
env . SaSvc . On ( "RetrieveServiceAccountIdByName" , mock . Anything , tmpOrgID , sa . ExtSvcPrefix + extSvcSlug ) .
Return ( extSvcAccID , nil )
env . SaSvc . On ( "DeleteServiceAccount" , mock . Anything , tmpOrgID , extSvcAccID ) . Return ( nil )
env . AcStore . On ( "DeleteExternalServiceRole" , mock . Anything , extSvcSlug ) . Return ( nil )
// A token was previously stored in the secret store
_ = env . SkvStore . Set ( context . Background ( ) , tmpOrgID , extSvcSlug , kvStoreType , "ExtSvcSecretToken" )
} ,
cmd : extsvcauth . ExternalServiceRegistration {
Name : extSvcSlug ,
@ -289,14 +271,8 @@ func TestExtSvcAccountsService_SaveExternalService(t *testing.T) {
} ,
} ,
checks : func ( t * testing . T , env * TestEnv ) {
env . SaSvc . AssertCalled ( t , "RetrieveServiceAccountIdByName" , mock . Anything ,
mock . MatchedBy ( func ( orgID int64 ) bool { return orgID == tmpOrgID } ) ,
mock . MatchedBy ( func ( slug string ) bool { return slug == sa . ExtSvcPrefix + extSvcSlug } ) )
env . SaSvc . AssertCalled ( t , "DeleteServiceAccount" , mock . Anything ,
mock . MatchedBy ( func ( orgID int64 ) bool { return orgID == tmpOrgID } ) ,
mock . MatchedBy ( func ( saID int64 ) bool { return saID == extSvcAccID } ) )
env . AcStore . AssertCalled ( t , "DeleteExternalServiceRole" , mock . Anything ,
mock . MatchedBy ( func ( slug string ) bool { return slug == extSvcSlug } ) )
_ , ok , _ := env . SkvStore . Get ( context . Background ( ) , tmpOrgID , extSvcSlug , kvStoreType )
require . False ( t , ok , "secret should have been removed from store" )
} ,
want : nil ,
wantErr : false ,
@ -305,13 +281,26 @@ func TestExtSvcAccountsService_SaveExternalService(t *testing.T) {
name : "should create new service account" ,
init : func ( env * TestEnv ) {
// No previous service account was attached to this slug
env . SaSvc . On ( "RetrieveServiceAccountIdByName" , mock . Anything , mock . Anything , mock . Anythin g) .
env . SaSvc . On ( "RetrieveServiceAccountIdByName" , mock . Anything , tmpOrgID , sa . ExtSvcPrefix + extSvcSlu g) .
Return ( int64 ( 0 ) , sa . ErrServiceAccountNotFound . Errorf ( "mock" ) )
env . SaSvc . On ( "CreateServiceAccount" , mock . Anything , mock . Anything , mock . Anything ) .
env . SaSvc . On ( "CreateServiceAccount" ,
mock . Anything ,
tmpOrgID ,
mock . MatchedBy ( func ( cmd * sa . CreateServiceAccountForm ) bool {
return cmd . Name == sa . ExtSvcPrefix + extSvcSlug && * cmd . Role == roletype . RoleNone
} ) ) .
Return ( extSvcAccount , nil )
env . SaSvc . On ( "EnableServiceAccount" , mock . Anything , extsvcauth . TmpOrgID , extSvcAccID , true ) . Return ( nil )
// Api Key was added without problem
env . SaSvc . On ( "AddServiceAccountToken" , mock . Anything , mock . Anything , mock . Anything ) . Return ( & apikey . APIKey { } , nil )
env . AcStore . On ( "SaveExternalServiceRole" , mock . Anything , mock . Anything ) . Return ( nil )
env . AcStore . On ( "SaveExternalServiceRole" ,
mock . Anything ,
mock . MatchedBy ( func ( cmd ac . SaveExternalServiceRoleCommand ) bool {
return cmd . ServiceAccountID == extSvcAccount . Id && cmd . ExternalServiceID == extSvcSlug &&
cmd . OrgID == int64 ( ac . GlobalOrgID ) && len ( cmd . Permissions ) == 1 &&
cmd . Permissions [ 0 ] == extSvcPerms [ 0 ]
} ) ) .
Return ( nil )
} ,
cmd : extsvcauth . ExternalServiceRegistration {
Name : extSvcSlug ,
@ -320,23 +309,6 @@ func TestExtSvcAccountsService_SaveExternalService(t *testing.T) {
Permissions : extSvcPerms ,
} ,
} ,
checks : func ( t * testing . T , env * TestEnv ) {
env . SaSvc . AssertCalled ( t , "RetrieveServiceAccountIdByName" , mock . Anything ,
mock . MatchedBy ( func ( orgID int64 ) bool { return orgID == tmpOrgID } ) ,
mock . MatchedBy ( func ( slug string ) bool { return slug == sa . ExtSvcPrefix + extSvcSlug } ) )
env . SaSvc . AssertCalled ( t , "CreateServiceAccount" , mock . Anything ,
mock . MatchedBy ( func ( orgID int64 ) bool { return orgID == tmpOrgID } ) ,
mock . MatchedBy ( func ( cmd * sa . CreateServiceAccountForm ) bool {
return cmd . Name == sa . ExtSvcPrefix + extSvcSlug && * cmd . Role == roletype . RoleNone
} ) ,
)
env . AcStore . AssertCalled ( t , "SaveExternalServiceRole" , mock . Anything ,
mock . MatchedBy ( func ( cmd ac . SaveExternalServiceRoleCommand ) bool {
return cmd . ServiceAccountID == extSvcAccount . Id && cmd . ExternalServiceID == extSvcSlug &&
cmd . OrgID == int64 ( ac . GlobalOrgID ) && len ( cmd . Permissions ) == 1 &&
cmd . Permissions [ 0 ] == extSvcPerms [ 0 ]
} ) )
} ,
want : & extsvcauth . ExternalService {
Name : extSvcSlug ,
ID : extSvcSlug ,
@ -348,9 +320,17 @@ func TestExtSvcAccountsService_SaveExternalService(t *testing.T) {
name : "should update service account" ,
init : func ( env * TestEnv ) {
// A previous service account was attached to this slug
env . SaSvc . On ( "RetrieveServiceAccountIdByName" , mock . Anything , mock . Anything , mock . Anythin g) .
env . SaSvc . On ( "RetrieveServiceAccountIdByName" , mock . Anything , tmpOrgID , sa . ExtSvcPrefix + extSvcSlu g) .
Return ( int64 ( 11 ) , nil )
env . AcStore . On ( "SaveExternalServiceRole" , mock . Anything , mock . Anything ) . Return ( nil )
env . AcStore . On ( "SaveExternalServiceRole" ,
mock . Anything ,
mock . MatchedBy ( func ( cmd ac . SaveExternalServiceRoleCommand ) bool {
return cmd . ServiceAccountID == int64 ( 11 ) && cmd . ExternalServiceID == extSvcSlug &&
cmd . OrgID == int64 ( ac . GlobalOrgID ) && len ( cmd . Permissions ) == 1 &&
cmd . Permissions [ 0 ] == extSvcPerms [ 0 ]
} ) ) .
Return ( nil )
env . SaSvc . On ( "EnableServiceAccount" , mock . Anything , extsvcauth . TmpOrgID , int64 ( 11 ) , true ) . Return ( nil )
// This time we don't add a token but rely on the secret store
_ = env . SkvStore . Set ( context . Background ( ) , tmpOrgID , extSvcSlug , kvStoreType , "ExtSvcSecretToken" )
} ,
@ -361,17 +341,6 @@ func TestExtSvcAccountsService_SaveExternalService(t *testing.T) {
Permissions : extSvcPerms ,
} ,
} ,
checks : func ( t * testing . T , env * TestEnv ) {
env . SaSvc . AssertCalled ( t , "RetrieveServiceAccountIdByName" , mock . Anything ,
mock . MatchedBy ( func ( orgID int64 ) bool { return orgID == tmpOrgID } ) ,
mock . MatchedBy ( func ( slug string ) bool { return slug == sa . ExtSvcPrefix + extSvcSlug } ) )
env . AcStore . AssertCalled ( t , "SaveExternalServiceRole" , mock . Anything ,
mock . MatchedBy ( func ( cmd ac . SaveExternalServiceRoleCommand ) bool {
return cmd . ServiceAccountID == int64 ( 11 ) && cmd . ExternalServiceID == extSvcSlug &&
cmd . OrgID == int64 ( ac . GlobalOrgID ) && len ( cmd . Permissions ) == 1 &&
cmd . Permissions [ 0 ] == extSvcPerms [ 0 ]
} ) )
} ,
want : & extsvcauth . ExternalService {
Name : extSvcSlug ,
ID : extSvcSlug ,