github.com/argoproj/argo-cd/v3@v3.2.1/util/github_app/repos_test.go (about)

     1  package github_app
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/mock"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/argoproj/argo-cd/v3/applicationset/services/github_app_auth"
    12  	"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
    13  )
    14  
    15  type ArgocdRepositoryMock struct {
    16  	mock *mock.Mock
    17  }
    18  
    19  func (a ArgocdRepositoryMock) GetRepoCredsBySecretName(ctx context.Context, secretName string) (*v1alpha1.RepoCreds, error) {
    20  	args := a.mock.Called(ctx, secretName)
    21  
    22  	return args.Get(0).(*v1alpha1.RepoCreds), args.Error(1)
    23  }
    24  
    25  func Test_repoAsCredentials_GetAuth(t *testing.T) {
    26  	tests := []struct {
    27  		name    string
    28  		repo    v1alpha1.RepoCreds
    29  		want    *github_app_auth.Authentication
    30  		wantErr bool
    31  	}{
    32  		{name: "missing", wantErr: true},
    33  		{name: "found", repo: v1alpha1.RepoCreds{
    34  			GithubAppId:             123,
    35  			GithubAppInstallationId: 456,
    36  			GithubAppPrivateKey:     "private key",
    37  		}, want: &github_app_auth.Authentication{
    38  			Id:                123,
    39  			InstallationId:    456,
    40  			EnterpriseBaseURL: "",
    41  			PrivateKey:        "private key",
    42  		}, wantErr: false},
    43  	}
    44  	for _, tt := range tests {
    45  		t.Run(tt.name, func(t *testing.T) {
    46  			m := mock.Mock{}
    47  			m.On("GetRepoCredsBySecretName", mock.Anything, mock.Anything).Return(&tt.repo, nil)
    48  			creds := NewAuthCredentials(ArgocdRepositoryMock{mock: &m})
    49  
    50  			auth, err := creds.GetAuthSecret(t.Context(), "https://github.com/foo")
    51  			if tt.wantErr {
    52  				assert.Error(t, err)
    53  				return
    54  			}
    55  			require.NoError(t, err)
    56  			assert.Equal(t, tt.want, auth)
    57  		})
    58  	}
    59  }