github.com/Axway/agent-sdk@v1.1.101/pkg/agent/credentialvalidator_test.go (about)

     1  package agent
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	agentcache "github.com/Axway/agent-sdk/pkg/agent/cache"
     8  	"github.com/Axway/agent-sdk/pkg/api"
     9  	v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
    10  	management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
    11  	"github.com/Axway/agent-sdk/pkg/apic/provisioning"
    12  	"github.com/Axway/agent-sdk/pkg/config"
    13  	"github.com/Axway/agent-sdk/pkg/jobs"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func setupCredCache(expireTime time.Time) {
    18  	cred := management.NewCredential("cred", "env")
    19  	cred.Policies.Expiry = &management.CredentialPoliciesExpiry{
    20  		Timestamp: v1.Time(expireTime),
    21  	}
    22  	ri, _ := cred.AsInstance()
    23  
    24  	agent.cacheManager = agentcache.NewAgentCacheManager(&config.CentralConfiguration{}, false)
    25  	agent.cacheManager.AddWatchResource(ri)
    26  }
    27  
    28  func TestRegisterCredentialChecker(t *testing.T) {
    29  	setupCredCache(time.Time{})
    30  	setupAPICClient([]api.MockResponse{})
    31  	cfg := createCentralCfg("apicentral.axway.com", envName)
    32  	agent.cfg = cfg
    33  	Initialize(cfg)
    34  
    35  	c := registerCredentialChecker()
    36  	assert.NotNil(t, c)
    37  
    38  	jobs.UnregisterJob(c.id)
    39  }
    40  
    41  func TestCredentialValidatorExecute(t *testing.T) {
    42  
    43  	tests := []struct {
    44  		name                string
    45  		expectedState       string
    46  		expectedStateReason string
    47  		expectedStatus      string
    48  		expireTime          time.Time
    49  	}{
    50  		{
    51  			name:                "should update expired credential",
    52  			expectedState:       v1.Inactive,
    53  			expectedStateReason: provisioning.CredExpDetail,
    54  			expectedStatus:      provisioning.Pending.String(),
    55  			expireTime:          time.Now().Add(-1 * time.Hour),
    56  		},
    57  		{
    58  			name:       "should not update credential that has not expired",
    59  			expireTime: time.Now().Add(1 * time.Hour),
    60  		},
    61  		{
    62  			name:       "should not update credential that does not expired",
    63  			expireTime: time.Time{},
    64  		},
    65  	}
    66  
    67  	for _, tc := range tests {
    68  		t.Run(tc.name, func(t *testing.T) {
    69  			client := &mockClient{
    70  				t:                   t,
    71  				expectedState:       tc.expectedState,
    72  				expectedStateReason: tc.expectedStateReason,
    73  				expectedStatus:      tc.expectedStatus,
    74  			}
    75  			setupCredCache(tc.expireTime)
    76  			c := newCredentialChecker(agent.cacheManager, client)
    77  			c.Execute()
    78  		})
    79  	}
    80  }
    81  
    82  type mockClient struct {
    83  	apicClient
    84  	t                   *testing.T
    85  	expectedState       string
    86  	expectedStateReason string
    87  	expectedStatus      string
    88  	updatedCred         bool
    89  }
    90  
    91  func (c *mockClient) UpdateResourceInstance(ri v1.Interface) (*v1.ResourceInstance, error) {
    92  	c.updatedCred = true
    93  	inst, _ := ri.AsInstance()
    94  	cred := &management.Credential{}
    95  	cred.FromInstance(inst)
    96  	assert.Equal(c.t, c.expectedState, cred.Spec.State.Name)
    97  	assert.Equal(c.t, c.expectedStateReason, cred.Spec.State.Reason)
    98  	assert.Equal(c.t, c.expectedStatus, cred.Status.Level)
    99  	return nil, nil
   100  }
   101  
   102  func (c *mockClient) CreateSubResource(rm v1.ResourceMeta, subs map[string]interface{}) error {
   103  	return nil
   104  }