github.com/argoproj/argo-cd/v2@v2.10.9/applicationset/services/pull_request/azure_devops_test.go (about) 1 package pull_request 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/microsoft/azure-devops-go-api/azuredevops/core" 8 git "github.com/microsoft/azure-devops-go-api/azuredevops/git" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/mock" 11 12 azureMock "github.com/argoproj/argo-cd/v2/applicationset/services/scm_provider/azure_devops/git/mocks" 13 ) 14 15 func createBoolPtr(x bool) *bool { 16 return &x 17 } 18 19 func createStringPtr(x string) *string { 20 return &x 21 } 22 23 func createIntPtr(x int) *int { 24 return &x 25 } 26 27 func createLabelsPtr(x []core.WebApiTagDefinition) *[]core.WebApiTagDefinition { 28 return &x 29 } 30 31 type AzureClientFactoryMock struct { 32 mock *mock.Mock 33 } 34 35 func (m *AzureClientFactoryMock) GetClient(ctx context.Context) (git.Client, error) { 36 args := m.mock.Called(ctx) 37 38 var client git.Client 39 c := args.Get(0) 40 if c != nil { 41 client = c.(git.Client) 42 } 43 44 var err error 45 if len(args) > 1 { 46 if e, ok := args.Get(1).(error); ok { 47 err = e 48 } 49 } 50 51 return client, err 52 } 53 54 func TestListPullRequest(t *testing.T) { 55 teamProject := "myorg_project" 56 repoName := "myorg_project_repo" 57 pr_id := 123 58 pr_head_sha := "cd4973d9d14a08ffe6b641a89a68891d6aac8056" 59 ctx := context.Background() 60 61 pullRequestMock := []git.GitPullRequest{ 62 { 63 PullRequestId: createIntPtr(pr_id), 64 SourceRefName: createStringPtr("refs/heads/feature-branch"), 65 LastMergeSourceCommit: &git.GitCommitRef{ 66 CommitId: createStringPtr(pr_head_sha), 67 }, 68 Labels: &[]core.WebApiTagDefinition{}, 69 Repository: &git.GitRepository{ 70 Name: createStringPtr(repoName), 71 }, 72 }, 73 } 74 75 args := git.GetPullRequestsByProjectArgs{ 76 Project: &teamProject, 77 SearchCriteria: &git.GitPullRequestSearchCriteria{}, 78 } 79 80 gitClientMock := azureMock.Client{} 81 clientFactoryMock := &AzureClientFactoryMock{mock: &mock.Mock{}} 82 clientFactoryMock.mock.On("GetClient", mock.Anything).Return(&gitClientMock, nil) 83 gitClientMock.On("GetPullRequestsByProject", ctx, args).Return(&pullRequestMock, nil) 84 85 provider := AzureDevOpsService{ 86 clientFactory: clientFactoryMock, 87 project: teamProject, 88 repo: repoName, 89 labels: nil, 90 } 91 92 list, err := provider.List(ctx) 93 assert.NoError(t, err) 94 assert.Equal(t, 1, len(list)) 95 assert.Equal(t, "feature-branch", list[0].Branch) 96 assert.Equal(t, pr_head_sha, list[0].HeadSHA) 97 assert.Equal(t, pr_id, list[0].Number) 98 } 99 100 func TestConvertLabes(t *testing.T) { 101 testCases := []struct { 102 name string 103 gotLabels *[]core.WebApiTagDefinition 104 expectedLabels []string 105 }{ 106 { 107 name: "empty labels", 108 gotLabels: createLabelsPtr([]core.WebApiTagDefinition{}), 109 expectedLabels: []string{}, 110 }, 111 { 112 name: "nil labels", 113 gotLabels: createLabelsPtr(nil), 114 expectedLabels: []string{}, 115 }, 116 { 117 name: "one label", 118 gotLabels: createLabelsPtr([]core.WebApiTagDefinition{ 119 {Name: createStringPtr("label1"), Active: createBoolPtr(true)}, 120 }), 121 expectedLabels: []string{"label1"}, 122 }, 123 { 124 name: "two label", 125 gotLabels: createLabelsPtr([]core.WebApiTagDefinition{ 126 {Name: createStringPtr("label1"), Active: createBoolPtr(true)}, 127 {Name: createStringPtr("label2"), Active: createBoolPtr(true)}, 128 }), 129 expectedLabels: []string{"label1", "label2"}, 130 }, 131 } 132 133 for _, tc := range testCases { 134 t.Run(tc.name, func(t *testing.T) { 135 got := convertLabels(tc.gotLabels) 136 assert.Equal(t, tc.expectedLabels, got) 137 }) 138 } 139 } 140 141 func TestContainAzureDevOpsLabels(t *testing.T) { 142 testCases := []struct { 143 name string 144 expectedLabels []string 145 gotLabels []string 146 expectedResult bool 147 }{ 148 { 149 name: "empty labels", 150 expectedLabels: []string{}, 151 gotLabels: []string{}, 152 expectedResult: true, 153 }, 154 { 155 name: "no matching labels", 156 expectedLabels: []string{"label1", "label2"}, 157 gotLabels: []string{"label3", "label4"}, 158 expectedResult: false, 159 }, 160 { 161 name: "some matching labels", 162 expectedLabels: []string{"label1", "label2"}, 163 gotLabels: []string{"label1", "label3"}, 164 expectedResult: false, 165 }, 166 { 167 name: "all matching labels", 168 expectedLabels: []string{"label1", "label2"}, 169 gotLabels: []string{"label1", "label2"}, 170 expectedResult: true, 171 }, 172 } 173 174 for _, tc := range testCases { 175 t.Run(tc.name, func(t *testing.T) { 176 got := containAzureDevOpsLabels(tc.expectedLabels, tc.gotLabels) 177 assert.Equal(t, tc.expectedResult, got) 178 }) 179 } 180 } 181 182 func TestBuildURL(t *testing.T) { 183 testCases := []struct { 184 name string 185 url string 186 organization string 187 expected string 188 }{ 189 { 190 name: "Provided default URL and organization", 191 url: "https://dev.azure.com/", 192 organization: "myorganization", 193 expected: "https://dev.azure.com/myorganization", 194 }, 195 { 196 name: "Provided default URL and organization without trailing slash", 197 url: "https://dev.azure.com", 198 organization: "myorganization", 199 expected: "https://dev.azure.com/myorganization", 200 }, 201 { 202 name: "Provided no URL and organization", 203 url: "", 204 organization: "myorganization", 205 expected: "https://dev.azure.com/myorganization", 206 }, 207 { 208 name: "Provided custom URL and organization", 209 url: "https://azuredevops.example.com/", 210 organization: "myorganization", 211 expected: "https://azuredevops.example.com/myorganization", 212 }, 213 } 214 215 for _, tc := range testCases { 216 t.Run(tc.name, func(t *testing.T) { 217 result := buildURL(tc.url, tc.organization) 218 assert.Equal(t, result, tc.expected) 219 }) 220 } 221 }