|
1 | 1 | package config_helpers
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/base64" |
4 | 5 | "io/ioutil"
|
| 6 | + gourl "net/url" |
5 | 7 | "os"
|
6 | 8 | "path/filepath"
|
7 | 9 | "strings"
|
@@ -104,81 +106,61 @@ func TestConfigDir_IbmCloudConfigHomeSet_Exists(t *testing.T) {
|
104 | 106 | assert.Equal(userHome, ConfigDir())
|
105 | 107 | }
|
106 | 108 |
|
107 |
| -// func TestMigrateFromOldConfig(t *testing.T) { |
108 |
| -// assert := assert.New(t) |
109 |
| - |
110 |
| -// err := prepareBluemixHome() |
111 |
| -// assert.NoError(err) |
112 |
| -// defer clearBluemixHome() |
113 |
| - |
114 |
| -// err = os.MkdirAll(oldConfigDir(), 0700) |
115 |
| -// assert.NoError(err) |
116 |
| -// oldConfigPath := filepath.Join(oldConfigDir(), "config.json") |
117 |
| -// err = ioutil.WriteFile(oldConfigPath, []byte("old"), 0600) |
118 |
| -// assert.NoError(err) |
119 |
| - |
120 |
| -// err = MigrateFromOldConfig() |
121 |
| -// assert.NoError(err) |
122 |
| - |
123 |
| -// newConfigPath := filepath.Join(newConfigDir(), "config.json") |
124 |
| -// assert.True(file_helpers.FileExists(newConfigPath)) |
125 |
| -// content, err := ioutil.ReadFile(newConfigPath) |
126 |
| -// assert.NoError(err) |
127 |
| -// assert.Equal([]byte("old"), content, "should copy old config file") |
128 |
| - |
129 |
| -// assert.False(file_helpers.FileExists(oldConfigDir()), "old config dir should be deleted") |
130 |
| -// } |
131 |
| - |
132 |
| -// func TestMigrateFromOldConfig_NewConfigExist(t *testing.T) { |
133 |
| -// assert := assert.New(t) |
134 |
| - |
135 |
| -// err := prepareBluemixHome() |
136 |
| -// assert.NoError(err) |
137 |
| -// defer clearBluemixHome() |
138 |
| - |
139 |
| -// err = os.MkdirAll(oldConfigDir(), 0700) |
140 |
| -// assert.NoError(err) |
141 |
| -// oldConfigPath := filepath.Join(oldConfigDir(), "config.json") |
142 |
| -// err = ioutil.WriteFile(oldConfigPath, []byte("old"), 0600) |
143 |
| -// assert.NoError(err) |
144 |
| - |
145 |
| -// err = os.MkdirAll(newConfigDir(), 0700) |
146 |
| -// assert.NoError(err) |
147 |
| -// newConfigPath := filepath.Join(newConfigDir(), "config.json") |
148 |
| -// err = ioutil.WriteFile(newConfigPath, []byte("new"), 0600) |
149 |
| -// assert.NoError(err) |
150 |
| - |
151 |
| -// err = MigrateFromOldConfig() |
152 |
| -// assert.NoError(err) |
153 |
| - |
154 |
| -// content, err := ioutil.ReadFile(newConfigPath) |
155 |
| -// assert.NoError(err) |
156 |
| -// assert.Equal([]byte("new"), content, "should not copy old config file") |
157 |
| -// } |
158 |
| - |
159 |
| -// func TestMigrateFromOldConfig_OldConfigNotExist(t *testing.T) { |
160 |
| -// assert := assert.New(t) |
161 |
| - |
162 |
| -// err := prepareBluemixHome() |
163 |
| -// assert.NoError(err) |
164 |
| -// defer clearBluemixHome() |
165 |
| - |
166 |
| -// err = MigrateFromOldConfig() |
167 |
| -// assert.NoError(err) |
168 |
| -// } |
169 |
| - |
170 |
| -// func prepareBluemixHome() error { |
171 |
| -// temp, err := ioutil.TempDir("", "IBMCloudSDKConfigTest") |
172 |
| -// if err != nil { |
173 |
| -// return err |
174 |
| -// } |
175 |
| -// os.Setenv("BLUEMIX_HOME", temp) |
176 |
| -// return nil |
177 |
| -// } |
178 |
| - |
179 |
| -// func clearBluemixHome() { |
180 |
| -// if homeDir := os.Getenv("BLUEMIX_HOME"); homeDir != "" { |
181 |
| -// os.RemoveAll(homeDir) |
182 |
| -// os.Unsetenv("BLUEMIX_HOME") |
183 |
| -// } |
184 |
| -// } |
| 109 | +func TestIsValidPaginationNextURL(t *testing.T) { |
| 110 | + assert := assert.New(t) |
| 111 | + |
| 112 | + testCases := []struct { |
| 113 | + name string |
| 114 | + nextURL string |
| 115 | + encodedQueryParam string |
| 116 | + expectedQueries gourl.Values |
| 117 | + isValid bool |
| 118 | + }{ |
| 119 | + { |
| 120 | + name: "return true for matching expected queries in pagination url", |
| 121 | + nextURL: "/api/example?cursor=" + base64.RawURLEncoding.EncodeToString([]byte("limit=100&active=true")), |
| 122 | + encodedQueryParam: "cursor", |
| 123 | + expectedQueries: gourl.Values{ |
| 124 | + "limit": []string{"100"}, |
| 125 | + "active": []string{"true"}, |
| 126 | + }, |
| 127 | + isValid: true, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "return true for matching expected queries with extraneous queries in pagination url", |
| 131 | + nextURL: "/api/example?cursor=" + base64.RawURLEncoding.EncodeToString([]byte("limit=100&active=true&extra=foo")), |
| 132 | + encodedQueryParam: "cursor", |
| 133 | + expectedQueries: gourl.Values{ |
| 134 | + "limit": []string{"100"}, |
| 135 | + "active": []string{"true"}, |
| 136 | + }, |
| 137 | + isValid: true, |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "return false for different limit in pagination url", |
| 141 | + nextURL: "/api/example?cursor=" + base64.RawURLEncoding.EncodeToString([]byte("limit=200")), |
| 142 | + encodedQueryParam: "cursor", |
| 143 | + expectedQueries: gourl.Values{ |
| 144 | + "limit": []string{"100"}, |
| 145 | + }, |
| 146 | + isValid: false, |
| 147 | + }, |
| 148 | + { |
| 149 | + name: "return false for different query among multiple parameters in the pagination url", |
| 150 | + nextURL: "/api/example?cursor=" + base64.RawURLEncoding.EncodeToString([]byte("limit=100&active=true")), |
| 151 | + encodedQueryParam: "cursor", |
| 152 | + expectedQueries: gourl.Values{ |
| 153 | + "limit": []string{"100"}, |
| 154 | + "active": []string{"false"}, |
| 155 | + }, |
| 156 | + isValid: false, |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + for _, tc := range testCases { |
| 161 | + t.Run(tc.name, func(_ *testing.T) { |
| 162 | + isValid := IsValidPaginationNextURL(tc.nextURL, tc.encodedQueryParam, tc.expectedQueries) |
| 163 | + assert.Equal(tc.isValid, isValid) |
| 164 | + }) |
| 165 | + } |
| 166 | +} |
0 commit comments