github.com/argoproj/argo-cd/v3@v3.2.1/applicationset/services/pull_request/github_test.go (about)

     1  package pull_request
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"testing"
     7  
     8  	"github.com/google/go-github/v69/github"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func toPtr(s string) *string {
    14  	return &s
    15  }
    16  
    17  func TestContainLabels(t *testing.T) {
    18  	cases := []struct {
    19  		Name       string
    20  		Labels     []string
    21  		PullLabels []*github.Label
    22  		Expect     bool
    23  	}{
    24  		{
    25  			Name:   "Match labels",
    26  			Labels: []string{"label1", "label2"},
    27  			PullLabels: []*github.Label{
    28  				{Name: toPtr("label1")},
    29  				{Name: toPtr("label2")},
    30  				{Name: toPtr("label3")},
    31  			},
    32  			Expect: true,
    33  		},
    34  		{
    35  			Name:   "Not match labels",
    36  			Labels: []string{"label1", "label4"},
    37  			PullLabels: []*github.Label{
    38  				{Name: toPtr("label1")},
    39  				{Name: toPtr("label2")},
    40  				{Name: toPtr("label3")},
    41  			},
    42  			Expect: false,
    43  		},
    44  		{
    45  			Name:   "No specify",
    46  			Labels: []string{},
    47  			PullLabels: []*github.Label{
    48  				{Name: toPtr("label1")},
    49  				{Name: toPtr("label2")},
    50  				{Name: toPtr("label3")},
    51  			},
    52  			Expect: true,
    53  		},
    54  	}
    55  
    56  	for _, c := range cases {
    57  		t.Run(c.Name, func(t *testing.T) {
    58  			got := containLabels(c.Labels, c.PullLabels)
    59  			require.Equal(t, got, c.Expect)
    60  		})
    61  	}
    62  }
    63  
    64  func TestGetGitHubPRLabelNames(t *testing.T) {
    65  	Tests := []struct {
    66  		Name           string
    67  		PullLabels     []*github.Label
    68  		ExpectedResult []string
    69  	}{
    70  		{
    71  			Name: "PR has labels",
    72  			PullLabels: []*github.Label{
    73  				{Name: toPtr("label1")},
    74  				{Name: toPtr("label2")},
    75  				{Name: toPtr("label3")},
    76  			},
    77  			ExpectedResult: []string{"label1", "label2", "label3"},
    78  		},
    79  		{
    80  			Name:           "PR does not have labels",
    81  			PullLabels:     []*github.Label{},
    82  			ExpectedResult: nil,
    83  		},
    84  	}
    85  	for _, test := range Tests {
    86  		t.Run(test.Name, func(t *testing.T) {
    87  			labels := getGithubPRLabelNames(test.PullLabels)
    88  			require.Equal(t, test.ExpectedResult, labels)
    89  		})
    90  	}
    91  }
    92  
    93  func TestGitHubListReturnsRepositoryNotFoundError(t *testing.T) {
    94  	mux := http.NewServeMux()
    95  	server := httptest.NewServer(mux)
    96  	defer server.Close()
    97  
    98  	path := "/repos/nonexistent/nonexistent/pulls"
    99  
   100  	mux.HandleFunc(path, func(w http.ResponseWriter, _ *http.Request) {
   101  		// Return 404 status to simulate repository not found
   102  		w.WriteHeader(http.StatusNotFound)
   103  		_, _ = w.Write([]byte(`{"message": "404 Project Not Found"}`))
   104  	})
   105  
   106  	svc, err := NewGithubService("", server.URL, "nonexistent", "nonexistent", []string{}, nil)
   107  	require.NoError(t, err)
   108  
   109  	prs, err := svc.List(t.Context())
   110  
   111  	// Should return empty pull requests list
   112  	assert.Empty(t, prs)
   113  
   114  	// Should return RepositoryNotFoundError
   115  	require.Error(t, err)
   116  	assert.True(t, IsRepositoryNotFoundError(err), "Expected RepositoryNotFoundError but got: %v", err)
   117  }