|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "net/http" |
5 | 6 | "os" |
6 | 7 | "reflect" |
| 8 | + "sort" |
7 | 9 | "strings" |
8 | 10 | "testing" |
9 | 11 | "time" |
@@ -115,18 +117,76 @@ func Test_processInput(t *testing.T) { |
115 | 117 |
|
116 | 118 | processInput(&input, &flags) |
117 | 119 |
|
118 | | - if len(input.Credentials) != 4 { |
119 | | - t.Fatal("expected credentials to be added") |
120 | | - } |
121 | | - // Ensure all credentials are either git_source or azure |
| 120 | + // Ensure all credentials are either git_source or nuget |
122 | 121 | for _, cred := range input.Credentials { |
123 | 122 | if cred["type"] != "git_source" && cred["type"] != "nuget_feed" { |
124 | 123 | t.Errorf("expected credentials to be either git_source or nuget_feed, got %s", cred["type"]) |
125 | 124 | } |
126 | 125 | } |
| 126 | + |
| 127 | + // Validate NuGet feeds |
| 128 | + actualNuGetFeedStrings := []string{} |
| 129 | + for _, cred := range input.Credentials { |
| 130 | + if cred["type"] == "nuget_feed" { |
| 131 | + actualNuGetFeedStrings = append(actualNuGetFeedStrings, fmt.Sprintf("%s|%s", cred["host"], cred["password"])) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + expectedNuGetFeeds := []string{ |
| 136 | + "org.pkgs.visualstudio.com|$LOCAL_AZURE_ACCESS_TOKEN", |
| 137 | + "pkgs.dev.azure.com|$LOCAL_AZURE_ACCESS_TOKEN", |
| 138 | + } |
| 139 | + |
| 140 | + assertStringArraysEqual(t, expectedNuGetFeeds, actualNuGetFeedStrings) |
| 141 | + |
| 142 | + // Validate credentials |
| 143 | + actualCredentialStrings := []string{} |
| 144 | + for _, cred := range input.Credentials { |
| 145 | + if cred["type"] == "git_source" { |
| 146 | + actualCredentialStrings = append(actualCredentialStrings, fmt.Sprintf("%s|%s|%s", cred["host"], cred["username"], cred["password"])) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + expectedGitCredentials := []string{ |
| 151 | + "dev.azure.com|org|$LOCAL_AZURE_ACCESS_TOKEN", |
| 152 | + "dev.azure.com|x-access-token|$LOCAL_AZURE_ACCESS_TOKEN", |
| 153 | + "org.visualstudio.com|x-access-token|$LOCAL_AZURE_ACCESS_TOKEN", |
| 154 | + } |
| 155 | + |
| 156 | + assertStringArraysEqual(t, expectedGitCredentials, actualCredentialStrings) |
| 157 | + |
| 158 | + // Validate credentials metadata |
| 159 | + credentialsMetadataHosts := map[string]string{} |
| 160 | + for _, cred := range input.Job.CredentialsMetadata { |
| 161 | + if cred["type"] == "git_source" { |
| 162 | + // dedup hosts with a map |
| 163 | + credentialsMetadataHosts[fmt.Sprintf("%s", cred["host"])] = "" |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + actualCredentialsMetadataHosts := []string{} |
| 168 | + for host := range credentialsMetadataHosts { |
| 169 | + actualCredentialsMetadataHosts = append(actualCredentialsMetadataHosts, host) |
| 170 | + } |
| 171 | + |
| 172 | + expectedGitCredentalsMetadataHosts := []string{ |
| 173 | + "dev.azure.com", |
| 174 | + "org.visualstudio.com", |
| 175 | + } |
| 176 | + |
| 177 | + assertStringArraysEqual(t, expectedGitCredentalsMetadataHosts, actualCredentialsMetadataHosts) |
127 | 178 | }) |
128 | 179 | } |
129 | 180 |
|
| 181 | +func assertStringArraysEqual(t *testing.T, expected, actual []string) { |
| 182 | + sort.Strings(expected) |
| 183 | + sort.Strings(actual) |
| 184 | + |
| 185 | + if !reflect.DeepEqual(expected, actual) { |
| 186 | + t.Errorf("expected strings to be\n\t%v\n got\n\t%v", expected, actual) |
| 187 | + } |
| 188 | +} |
| 189 | + |
130 | 190 | func Test_extractInput(t *testing.T) { |
131 | 191 | t.Run("test arguments", func(t *testing.T) { |
132 | 192 | cmd := NewUpdateCommand() |
|
0 commit comments