|
4 | 4 | "encoding/json" |
5 | 5 | "fmt" |
6 | 6 | "io" |
| 7 | + "math/rand" |
7 | 8 | "net/http" |
8 | 9 | "os" |
9 | 10 | "sort" |
@@ -83,13 +84,15 @@ var ( |
83 | 84 | "gitlab_repos": "repository_name", |
84 | 85 | "gerrit_repos": "gerrit_url", |
85 | 86 | } |
86 | | - UserActiveSignatureAPIPath = [2]string{"/v2/user/%s/active-signature", "/v4/user/%s/active-signature"} |
| 87 | + UserActiveSignatureAPIPath = [2]string{"/v2/user/%s/active-signature", "/v4/user/%s/active-signature"} |
| 88 | + // Optional field: true means the key may be missing in both APIs and still be valid |
87 | 89 | UserActiveSignatureAPIKeyMapping = map[string]interface{}{ |
88 | | - "project_id": nil, |
89 | | - "pull_request_id": nil, |
90 | | - "repository_id": nil, |
91 | | - "return_url": nil, |
92 | | - "user_id": nil, |
| 90 | + "project_id": nil, |
| 91 | + "pull_request_id": nil, |
| 92 | + "repository_id": nil, |
| 93 | + "return_url": nil, |
| 94 | + "user_id": nil, |
| 95 | + "merge_request_id": true, |
93 | 96 | } |
94 | 97 | ) |
95 | 98 |
|
@@ -140,6 +143,7 @@ func init() { |
140 | 143 | PR_ID = iPar |
141 | 144 | } |
142 | 145 | } |
| 146 | + rand.Seed(time.Now().UnixNano()) |
143 | 147 | } |
144 | 148 |
|
145 | 149 | func tryParseTime(val interface{}) (time.Time, bool) { |
@@ -210,12 +214,19 @@ func sortByKey(arr []interface{}, key string) { |
210 | 214 |
|
211 | 215 | func compareNestedFields(t *testing.T, pyData, goData, keyMapping map[string]interface{}, sortMap map[string]string) { |
212 | 216 | for k, v := range keyMapping { |
213 | | - if v == nil { |
| 217 | + bV, bVOK := v.(bool) |
| 218 | + if v == nil || bVOK { |
214 | 219 | Debugf("checking values of '%s'\n", k) |
215 | 220 | } |
216 | 221 |
|
217 | 222 | pyVal, pyOk := pyData[k] |
218 | 223 | goVal, goOk := goData[k] |
| 224 | + |
| 225 | + // true means fields are optional (nullable), so if v is true and fileds are missing in both Py and go then this is OK |
| 226 | + if bVOK && bV && !pyOk && !goOk { |
| 227 | + Debugf("'%s' is not set in both responses, this is ok\n", k) |
| 228 | + continue |
| 229 | + } |
219 | 230 | if !pyOk { |
220 | 231 | t.Errorf("Missing key in Python response: %s", k) |
221 | 232 | continue |
@@ -588,12 +599,18 @@ func TestUserActiveSignatureAPI(t *testing.T) { |
588 | 599 | projectId := uuid.New().String() |
589 | 600 | key := "active_signature:" + userId |
590 | 601 | expire := time.Now().Add(time.Hour).Unix() |
591 | | - value, err := json.Marshal(map[string]interface{}{ |
| 602 | + iValue := map[string]interface{}{ |
592 | 603 | "user_id": userId, |
593 | 604 | "project_id": projectId, |
594 | 605 | "repository_id": fmt.Sprintf("%d", REPO_ID), |
595 | 606 | "pull_request_id": fmt.Sprintf("%d", PR_ID), |
596 | | - }) |
| 607 | + } |
| 608 | + if rand.Intn(2) == 0 { |
| 609 | + mrId := rand.Intn(100) |
| 610 | + iValue["merge_request_id"] = fmt.Sprintf("%d", mrId) |
| 611 | + iValue["return_url"] = fmt.Sprintf("https://gitlab.com/gitlab-org/gitlab/-/merge_requests/%d", mrId) |
| 612 | + } |
| 613 | + value, err := json.Marshal(iValue) |
597 | 614 | if err != nil { |
598 | 615 | t.Fatalf("failed to marshal value: %+v", err) |
599 | 616 | } |
|
0 commit comments