github.com/argoproj/argo-cd/v2@v2.10.9/applicationset/services/pull_request/bitbucket_server_test.go (about)

     1  package pull_request
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func defaultHandler(t *testing.T) func(http.ResponseWriter, *http.Request) {
    15  	return func(w http.ResponseWriter, r *http.Request) {
    16  		w.Header().Set("Content-Type", "application/json")
    17  		var err error
    18  		switch r.RequestURI {
    19  		case "/rest/api/1.0/projects/PROJECT/repos/REPO/pull-requests?limit=100":
    20  			_, err = io.WriteString(w, `{
    21  					"size": 1,
    22  					"limit": 100,
    23  					"isLastPage": true,
    24  					"values": [
    25  						{
    26  							"id": 101,
    27  							"toRef": {
    28  								"latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a",
    29  								"displayId": "master",
    30  								"id": "refs/heads/master"
    31  							},
    32  							"fromRef": {
    33  								"id": "refs/heads/feature-ABC-123",
    34  								"displayId": "feature-ABC-123",
    35  								"latestCommit": "cb3cf2e4d1517c83e720d2585b9402dbef71f992"
    36  							}
    37  						}
    38  					],
    39  					"start": 0
    40  				}`)
    41  		default:
    42  			t.Fail()
    43  		}
    44  		if err != nil {
    45  			t.Fail()
    46  		}
    47  	}
    48  }
    49  
    50  func TestListPullRequestNoAuth(t *testing.T) {
    51  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    52  		assert.Empty(t, r.Header.Get("Authorization"))
    53  		defaultHandler(t)(w, r)
    54  	}))
    55  	defer ts.Close()
    56  	svc, err := NewBitbucketServiceNoAuth(context.Background(), ts.URL, "PROJECT", "REPO")
    57  	assert.NoError(t, err)
    58  	pullRequests, err := ListPullRequests(context.Background(), svc, []v1alpha1.PullRequestGeneratorFilter{})
    59  	assert.NoError(t, err)
    60  	assert.Equal(t, 1, len(pullRequests))
    61  	assert.Equal(t, 101, pullRequests[0].Number)
    62  	assert.Equal(t, "feature-ABC-123", pullRequests[0].Branch)
    63  	assert.Equal(t, "master", pullRequests[0].TargetBranch)
    64  	assert.Equal(t, "cb3cf2e4d1517c83e720d2585b9402dbef71f992", pullRequests[0].HeadSHA)
    65  }
    66  
    67  func TestListPullRequestPagination(t *testing.T) {
    68  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    69  		w.Header().Set("Content-Type", "application/json")
    70  		var err error
    71  		switch r.RequestURI {
    72  		case "/rest/api/1.0/projects/PROJECT/repos/REPO/pull-requests?limit=100":
    73  			_, err = io.WriteString(w, `{
    74  					"size": 2,
    75  					"limit": 2,
    76  					"isLastPage": false,
    77  					"values": [
    78  						{
    79  							"id": 101,
    80  							"toRef": {
    81  								"latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a",
    82  								"displayId": "master",
    83  								"id": "refs/heads/master"
    84  							},
    85  							"fromRef": {
    86  								"id": "refs/heads/feature-101",
    87  								"displayId": "feature-101",
    88  								"latestCommit": "ab3cf2e4d1517c83e720d2585b9402dbef71f992"
    89  							}
    90  						},
    91  						{
    92  							"id": 102,
    93  							"toRef": {
    94  								"latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a",
    95  								"displayId": "branch",
    96  								"id": "refs/heads/branch"
    97  							},
    98  							"fromRef": {
    99  								"id": "refs/heads/feature-102",
   100  								"displayId": "feature-102",
   101  								"latestCommit": "bb3cf2e4d1517c83e720d2585b9402dbef71f992"
   102  							}
   103  						}
   104  					],
   105  					"nextPageStart": 200
   106  				}`)
   107  		case "/rest/api/1.0/projects/PROJECT/repos/REPO/pull-requests?limit=100&start=200":
   108  			_, err = io.WriteString(w, `{
   109  				"size": 1,
   110  				"limit": 2,
   111  				"isLastPage": true,
   112  				"values": [
   113  					{
   114  						"id": 200,
   115  						"toRef": {
   116  							"latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a",
   117  							"displayId": "master",
   118  							"id": "refs/heads/master"
   119  						},
   120  						"fromRef": {
   121  							"id": "refs/heads/feature-200",
   122  							"displayId": "feature-200",
   123  							"latestCommit": "cb3cf2e4d1517c83e720d2585b9402dbef71f992"
   124  						}
   125  					}
   126  				],
   127  				"start": 200
   128  			}`)
   129  		default:
   130  			t.Fail()
   131  		}
   132  		if err != nil {
   133  			t.Fail()
   134  		}
   135  	}))
   136  	defer ts.Close()
   137  	svc, err := NewBitbucketServiceNoAuth(context.Background(), ts.URL, "PROJECT", "REPO")
   138  	assert.NoError(t, err)
   139  	pullRequests, err := ListPullRequests(context.Background(), svc, []v1alpha1.PullRequestGeneratorFilter{})
   140  	assert.NoError(t, err)
   141  	assert.Equal(t, 3, len(pullRequests))
   142  	assert.Equal(t, PullRequest{
   143  		Number:       101,
   144  		Branch:       "feature-101",
   145  		TargetBranch: "master",
   146  		HeadSHA:      "ab3cf2e4d1517c83e720d2585b9402dbef71f992",
   147  		Labels:       []string{},
   148  	}, *pullRequests[0])
   149  	assert.Equal(t, PullRequest{
   150  		Number:       102,
   151  		Branch:       "feature-102",
   152  		TargetBranch: "branch",
   153  		HeadSHA:      "bb3cf2e4d1517c83e720d2585b9402dbef71f992",
   154  		Labels:       []string{},
   155  	}, *pullRequests[1])
   156  	assert.Equal(t, PullRequest{
   157  		Number:       200,
   158  		Branch:       "feature-200",
   159  		TargetBranch: "master",
   160  		HeadSHA:      "cb3cf2e4d1517c83e720d2585b9402dbef71f992",
   161  		Labels:       []string{},
   162  	}, *pullRequests[2])
   163  }
   164  
   165  func TestListPullRequestBasicAuth(t *testing.T) {
   166  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   167  		// base64(user:password)
   168  		assert.Equal(t, "Basic dXNlcjpwYXNzd29yZA==", r.Header.Get("Authorization"))
   169  		assert.Equal(t, "no-check", r.Header.Get("X-Atlassian-Token"))
   170  		defaultHandler(t)(w, r)
   171  	}))
   172  	defer ts.Close()
   173  	svc, err := NewBitbucketServiceBasicAuth(context.Background(), "user", "password", ts.URL, "PROJECT", "REPO")
   174  	assert.NoError(t, err)
   175  	pullRequests, err := ListPullRequests(context.Background(), svc, []v1alpha1.PullRequestGeneratorFilter{})
   176  	assert.NoError(t, err)
   177  	assert.Equal(t, 1, len(pullRequests))
   178  	assert.Equal(t, 101, pullRequests[0].Number)
   179  	assert.Equal(t, "feature-ABC-123", pullRequests[0].Branch)
   180  	assert.Equal(t, "cb3cf2e4d1517c83e720d2585b9402dbef71f992", pullRequests[0].HeadSHA)
   181  }
   182  
   183  func TestListResponseError(t *testing.T) {
   184  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   185  		w.WriteHeader(http.StatusInternalServerError)
   186  	}))
   187  	defer ts.Close()
   188  	svc, _ := NewBitbucketServiceNoAuth(context.Background(), ts.URL, "PROJECT", "REPO")
   189  	_, err := ListPullRequests(context.Background(), svc, []v1alpha1.PullRequestGeneratorFilter{})
   190  	assert.Error(t, err)
   191  }
   192  
   193  func TestListResponseMalformed(t *testing.T) {
   194  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   195  		w.Header().Set("Content-Type", "application/json")
   196  		switch r.RequestURI {
   197  		case "/rest/api/1.0/projects/PROJECT/repos/REPO/pull-requests?limit=100":
   198  			_, err := io.WriteString(w, `{
   199  					"size": 1,
   200  					"limit": 100,
   201  					"isLastPage": true,
   202  					"values": { "id": 101 },
   203  					"start": 0
   204  				}`)
   205  			if err != nil {
   206  				t.Fail()
   207  			}
   208  		default:
   209  			t.Fail()
   210  		}
   211  	}))
   212  	defer ts.Close()
   213  	svc, _ := NewBitbucketServiceNoAuth(context.Background(), ts.URL, "PROJECT", "REPO")
   214  	_, err := ListPullRequests(context.Background(), svc, []v1alpha1.PullRequestGeneratorFilter{})
   215  	assert.Error(t, err)
   216  }
   217  
   218  func TestListResponseEmpty(t *testing.T) {
   219  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   220  		w.Header().Set("Content-Type", "application/json")
   221  		switch r.RequestURI {
   222  		case "/rest/api/1.0/projects/PROJECT/repos/REPO/pull-requests?limit=100":
   223  			_, err := io.WriteString(w, `{
   224  					"size": 0,
   225  					"limit": 100,
   226  					"isLastPage": true,
   227  					"values": [],
   228  					"start": 0
   229  				}`)
   230  			if err != nil {
   231  				t.Fail()
   232  			}
   233  		default:
   234  			t.Fail()
   235  		}
   236  	}))
   237  	defer ts.Close()
   238  	svc, err := NewBitbucketServiceNoAuth(context.Background(), ts.URL, "PROJECT", "REPO")
   239  	assert.NoError(t, err)
   240  	pullRequests, err := ListPullRequests(context.Background(), svc, []v1alpha1.PullRequestGeneratorFilter{})
   241  	assert.NoError(t, err)
   242  	assert.Empty(t, pullRequests)
   243  }
   244  
   245  func TestListPullRequestBranchMatch(t *testing.T) {
   246  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   247  		w.Header().Set("Content-Type", "application/json")
   248  		var err error
   249  		switch r.RequestURI {
   250  		case "/rest/api/1.0/projects/PROJECT/repos/REPO/pull-requests?limit=100":
   251  			_, err = io.WriteString(w, `{
   252  					"size": 2,
   253  					"limit": 2,
   254  					"isLastPage": false,
   255  					"values": [
   256  						{
   257  							"id": 101,
   258  							"toRef": {
   259  								"latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a",
   260  								"displayId": "master",
   261  								"id": "refs/heads/master"
   262  							},
   263  							"fromRef": {
   264  								"id": "refs/heads/feature-101",
   265  								"displayId": "feature-101",
   266  								"latestCommit": "ab3cf2e4d1517c83e720d2585b9402dbef71f992"
   267  							}
   268  						},
   269  						{
   270  							"id": 102,
   271  							"toRef": {
   272  								"latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a",
   273  								"displayId": "branch",
   274  								"id": "refs/heads/branch"
   275  							},
   276  							"fromRef": {
   277  								"id": "refs/heads/feature-102",
   278  								"displayId": "feature-102",
   279  								"latestCommit": "bb3cf2e4d1517c83e720d2585b9402dbef71f992"
   280  							}
   281  						}
   282  					],
   283  					"nextPageStart": 200
   284  				}`)
   285  		case "/rest/api/1.0/projects/PROJECT/repos/REPO/pull-requests?limit=100&start=200":
   286  			_, err = io.WriteString(w, `{
   287  				"size": 1,
   288  				"limit": 2,
   289  				"isLastPage": true,
   290  				"values": [
   291  					{
   292  						"id": 200,
   293  						"toRef": {
   294  							"latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a",
   295  							"displayId": "master",
   296  							"id": "refs/heads/master"
   297  						},
   298  						"fromRef": {
   299  							"id": "refs/heads/feature-200",
   300  							"displayId": "feature-200",
   301  							"latestCommit": "cb3cf2e4d1517c83e720d2585b9402dbef71f992"
   302  						}
   303  					}
   304  				],
   305  				"start": 200
   306  			}`)
   307  		default:
   308  			t.Fail()
   309  		}
   310  		if err != nil {
   311  			t.Fail()
   312  		}
   313  	}))
   314  	defer ts.Close()
   315  	regexp := `feature-1[\d]{2}`
   316  	svc, err := NewBitbucketServiceNoAuth(context.Background(), ts.URL, "PROJECT", "REPO")
   317  	assert.NoError(t, err)
   318  	pullRequests, err := ListPullRequests(context.Background(), svc, []v1alpha1.PullRequestGeneratorFilter{
   319  		{
   320  			BranchMatch: &regexp,
   321  		},
   322  	})
   323  	assert.NoError(t, err)
   324  	assert.Equal(t, 2, len(pullRequests))
   325  	assert.Equal(t, PullRequest{
   326  		Number:       101,
   327  		Branch:       "feature-101",
   328  		TargetBranch: "master",
   329  		HeadSHA:      "ab3cf2e4d1517c83e720d2585b9402dbef71f992",
   330  		Labels:       []string{},
   331  	}, *pullRequests[0])
   332  	assert.Equal(t, PullRequest{
   333  		Number:       102,
   334  		Branch:       "feature-102",
   335  		TargetBranch: "branch",
   336  		HeadSHA:      "bb3cf2e4d1517c83e720d2585b9402dbef71f992",
   337  		Labels:       []string{},
   338  	}, *pullRequests[1])
   339  
   340  	regexp = `.*2$`
   341  	svc, err = NewBitbucketServiceNoAuth(context.Background(), ts.URL, "PROJECT", "REPO")
   342  	assert.NoError(t, err)
   343  	pullRequests, err = ListPullRequests(context.Background(), svc, []v1alpha1.PullRequestGeneratorFilter{
   344  		{
   345  			BranchMatch: &regexp,
   346  		},
   347  	})
   348  	assert.NoError(t, err)
   349  	assert.Equal(t, 1, len(pullRequests))
   350  	assert.Equal(t, PullRequest{
   351  		Number:       102,
   352  		Branch:       "feature-102",
   353  		TargetBranch: "branch",
   354  		HeadSHA:      "bb3cf2e4d1517c83e720d2585b9402dbef71f992",
   355  		Labels:       []string{},
   356  	}, *pullRequests[0])
   357  
   358  	regexp = `[\d{2}`
   359  	svc, err = NewBitbucketServiceNoAuth(context.Background(), ts.URL, "PROJECT", "REPO")
   360  	assert.NoError(t, err)
   361  	_, err = ListPullRequests(context.Background(), svc, []v1alpha1.PullRequestGeneratorFilter{
   362  		{
   363  			BranchMatch: &regexp,
   364  		},
   365  	})
   366  	assert.Error(t, err)
   367  }