Skip to content

Commit

Permalink
chore: remove unnecessary functions
Browse files Browse the repository at this point in the history
Signed-off-by: Shahram Kalantari <[email protected]>
  • Loading branch information
shahramk64 committed Oct 3, 2024
1 parent 51d30ec commit 2c3e52a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 143 deletions.
15 changes: 0 additions & 15 deletions pkg/common/oras/authprovider/azure/azureidentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,6 @@ type MIAuthProvider struct {
getManagedIdentityToken func(ctx context.Context, clientID string) (azcore.AccessToken, error)
}

// NewAzureWIAuthProvider is defined to enable mocking of some of the function in unit tests
func NewAzureMIAuthProvider() *MIAuthProvider {
return &MIAuthProvider{
authClientFactory: func(serverURL string, options *azcontainerregistry.AuthenticationClientOptions) (AuthClient, error) {
client, err := azcontainerregistry.NewAuthenticationClient(serverURL, options)
if err != nil {
return nil, err
}
return &AuthenticationClientWrapper{client: client}, nil
},
getRegistryHost: provider.GetRegistryHostName,
getManagedIdentityToken: getManagedIdentityToken,
}
}

type azureManagedIdentityAuthProviderConf struct {
Name string `json:"name"`
ClientID string `json:"clientID"`
Expand Down
66 changes: 11 additions & 55 deletions pkg/common/oras/authprovider/azure/azureidentity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,61 +103,6 @@ func TestAzureMSIValidation_EnvironmentVariables_ExpectedResults(t *testing.T) {
}
}

func TestNewAzureMIAuthProvider_AuthenticationClientError(t *testing.T) {
// Create a new mock client factory
mockFactory := new(MockAuthClientFactory)

// Setup mock to return an error
mockFactory.On("NewAuthenticationClient", mock.Anything, mock.Anything).
Return(nil, errors.New("failed to create authentication client"))

// Create a new WIAuthProvider instance
provider := NewAzureMIAuthProvider()
provider.authClientFactory = mockFactory.NewAuthenticationClient

// Call authClientFactory to test error handling
_, err := provider.authClientFactory("https://myregistry.azurecr.io", nil)

// Assert that an error is returned
assert.Error(t, err)
assert.Equal(t, "failed to create authentication client", err.Error())

// Verify that the mock was called
mockFactory.AssertCalled(t, "NewAuthenticationClient", "https://myregistry.azurecr.io", mock.Anything)
}

func TestNewAzureMIAuthProvider_Success(t *testing.T) {
// Create a new mock client factory
mockFactory := new(MockAuthClientFactory)

// Create a mock auth client to return from the factory
mockAuthClient := new(MockAuthClient)

// Setup mock to return a successful auth client
mockFactory.On("NewAuthenticationClient", mock.Anything, mock.Anything).
Return(mockAuthClient, nil)

// Create a new WIAuthProvider instance
provider := NewAzureMIAuthProvider()

// Replace authClientFactory with the mock factory
provider.authClientFactory = mockFactory.NewAuthenticationClient

// Call authClientFactory to test successful return
client, err := provider.authClientFactory("https://myregistry.azurecr.io", nil)

// Assert that the client is returned without an error
assert.NoError(t, err)
assert.NotNil(t, client)

// Assert that the returned client is of the expected type
_, ok := client.(*MockAuthClient)
assert.True(t, ok, "expected client to be of type *MockAuthClient")

// Verify that the mock was called
mockFactory.AssertCalled(t, "NewAuthenticationClient", "https://myregistry.azurecr.io", mock.Anything)
}

func TestMIProvide_Success(t *testing.T) {
const registryHost = "myregistry.azurecr.io"
mockClient := new(MockAuthClient)
Expand Down Expand Up @@ -243,3 +188,14 @@ func TestMIProvide_RefreshAAD(t *testing.T) {
assert.NoError(t, err)
mockGetManagedIdentityToken.AssertCalled(t, "GetManagedIdentityToken", mock.Anything, "mockClientID") // Assert that getManagedIdentityToken was called
}

func TestMIProvide_Failure_InvalidHostName(t *testing.T) {
provider := &MIAuthProvider{
getRegistryHost: func(_ string) (string, error) {
return "", errors.New("invalid hostname")
},
}

_, err := provider.Provide(context.Background(), "artifact")
assert.Error(t, err)
}
17 changes: 0 additions & 17 deletions pkg/common/oras/authprovider/azure/azureworkloadidentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
re "github.com/ratify-project/ratify/errors"
"github.com/ratify-project/ratify/internal/logger"
provider "github.com/ratify-project/ratify/pkg/common/oras/authprovider"
"github.com/ratify-project/ratify/pkg/metrics"
"github.com/ratify-project/ratify/pkg/utils/azureauth"

"github.com/AzureAD/microsoft-authentication-library-for-go/apps/confidential"
Expand Down Expand Up @@ -54,22 +53,6 @@ type AuthClient interface {
ExchangeAADAccessTokenForACRRefreshToken(ctx context.Context, grantType, service string, options *azcontainerregistry.AuthenticationClientExchangeAADAccessTokenForACRRefreshTokenOptions) (azcontainerregistry.AuthenticationClientExchangeAADAccessTokenForACRRefreshTokenResponse, error)
}

// NewAzureWIAuthProvider is defined to enable mocking of some of the function in unit tests
func NewAzureWIAuthProvider() *WIAuthProvider {
return &WIAuthProvider{
authClientFactory: func(serverURL string, options *azcontainerregistry.AuthenticationClientOptions) (AuthClient, error) {
client, err := azcontainerregistry.NewAuthenticationClient(serverURL, options)
if err != nil {
return nil, err
}
return &AuthenticationClientWrapper{client: client}, nil
},
getRegistryHost: provider.GetRegistryHostName,
getAADAccessToken: azureauth.GetAADAccessToken,
reportMetrics: metrics.ReportACRExchangeDuration,
}
}

type azureWIAuthProviderConf struct {
Name string `json:"name"`
ClientID string `json:"clientID,omitempty"`
Expand Down
57 changes: 1 addition & 56 deletions pkg/common/oras/authprovider/azure/azureworkloadidentity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,61 +165,6 @@ func TestAzureWIValidation_EnvironmentVariables_ExpectedResults(t *testing.T) {
}
}

func TestNewAzureWIAuthProvider_AuthenticationClientError(t *testing.T) {
// Create a new mock client factory
mockFactory := new(MockAuthClientFactory)

// Setup mock to return an error
mockFactory.On("NewAuthenticationClient", mock.Anything, mock.Anything).
Return(nil, errors.New("failed to create authentication client"))

// Create a new WIAuthProvider instance
provider := NewAzureWIAuthProvider()
provider.authClientFactory = mockFactory.NewAuthenticationClient

// Call authClientFactory to test error handling
_, err := provider.authClientFactory("https://myregistry.azurecr.io", nil)

// Assert that an error is returned
assert.Error(t, err)
assert.Equal(t, "failed to create authentication client", err.Error())

// Verify that the mock was called
mockFactory.AssertCalled(t, "NewAuthenticationClient", "https://myregistry.azurecr.io", mock.Anything)
}

func TestNewAzureWIAuthProvider_Success(t *testing.T) {
// Create a new mock client factory
mockFactory := new(MockAuthClientFactory)

// Create a mock auth client to return from the factory
mockAuthClient := new(MockAuthClient)

// Setup mock to return a successful auth client
mockFactory.On("NewAuthenticationClient", mock.Anything, mock.Anything).
Return(mockAuthClient, nil)

// Create a new WIAuthProvider instance
provider := NewAzureWIAuthProvider()

// Replace authClientFactory with the mock factory
provider.authClientFactory = mockFactory.NewAuthenticationClient

// Call authClientFactory to test successful return
client, err := provider.authClientFactory("https://myregistry.azurecr.io", nil)

// Assert that the client is returned without an error
assert.NoError(t, err)
assert.NotNil(t, client)

// Assert that the returned client is of the expected type
_, ok := client.(*MockAuthClient)
assert.True(t, ok, "expected client to be of type *MockAuthClient")

// Verify that the mock was called
mockFactory.AssertCalled(t, "NewAuthenticationClient", "https://myregistry.azurecr.io", mock.Anything)
}

func TestWIProvide_Success(t *testing.T) {
mockClient := new(MockAuthClient)
expectedRefreshToken := "mocked_refresh_token"
Expand Down Expand Up @@ -300,7 +245,7 @@ func TestWIProvide_RefreshAAD(t *testing.T) {
mockAzureAuth.AssertCalled(t, "GetAADAccessToken", mock.Anything, mock.Anything, mock.Anything, mock.Anything)
}

func TestProvide_Failure_InvalidHostName(t *testing.T) {
func TestWIProvide_Failure_InvalidHostName(t *testing.T) {
provider := &WIAuthProvider{
getRegistryHost: func(_ string) (string, error) {
return "", errors.New("invalid hostname")
Expand Down

0 comments on commit 2c3e52a

Please sign in to comment.