Skip to content

Commit 3771bfe

Browse files
Update the test
Signed-off-by: Lukasz Gryglicki <lgryglicki@cncf.io> Assisted by [OpenAI](https://platform.openai.com/) Assisted by [GitHub Copilot](https://github.com/features/copilot)
1 parent d526eeb commit 3771bfe

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

tests/py2go/api_test.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -927,18 +927,35 @@ func TestAllUserActiveSignatureAPI(t *testing.T) {
927927
}
928928
}
929929

930-
func runUserCompatAPIForUser(t *testing.T, userId string) {
931-
apiURL := PY_API_URL + fmt.Sprintf(UserCompatAPIPath[0], userId)
932-
Debugf("Py API call: %s\n", apiURL)
933-
oldResp, err := http.Get(apiURL)
930+
func authGet(t *testing.T, apiURL string) *http.Response {
931+
t.Helper()
932+
if TOKEN == "" {
933+
t.Fatalf("TOKEN environment variable is required for authenticated /v2/user tests")
934+
}
935+
req, err := http.NewRequest("GET", apiURL, nil)
936+
if err != nil {
937+
t.Fatalf("Failed to create request: %v", err)
938+
}
939+
req.Header.Set("Authorization", "Bearer "+TOKEN)
940+
if XACL != "" {
941+
req.Header.Set("X-ACL", XACL)
942+
}
943+
resp, err := http.DefaultClient.Do(req)
934944
if err != nil {
935945
t.Fatalf("Failed to call API: %v", err)
936946
}
947+
return resp
948+
}
949+
950+
func runUserCompatAPIForUser(t *testing.T, userId string) {
951+
apiURL := PY_API_URL + fmt.Sprintf(UserCompatAPIPath[0], userId)
952+
Debugf("Py API call: %s\n", apiURL)
953+
oldResp := authGet(t, apiURL)
937954
assert.Equal(t, http.StatusOK, oldResp.StatusCode, "Expected 200 from PY API")
938955
defer oldResp.Body.Close()
939956
oldBody, _ := io.ReadAll(oldResp.Body)
940957
var oldJSON interface{}
941-
err = json.Unmarshal(oldBody, &oldJSON)
958+
err := json.Unmarshal(oldBody, &oldJSON)
942959
assert.NoError(t, err)
943960
Debugf("Py raw response: %+v\n", string(oldBody))
944961
Debugf("Py response: %+v\n", oldJSON)
@@ -985,15 +1002,12 @@ func runUserCompatAPIForUser(t *testing.T, userId string) {
9851002
func runUserCompatAPIForUserExpectFail(t *testing.T, userId string) {
9861003
apiURL := PY_API_URL + fmt.Sprintf(UserCompatAPIPath[0], userId)
9871004
Debugf("Py API call: %s\n", apiURL)
988-
oldResp, err := http.Get(apiURL)
989-
if err != nil {
990-
t.Fatalf("Failed to call API: %v", err)
991-
}
1005+
oldResp := authGet(t, apiURL)
9921006
assert.Equal(t, http.StatusBadRequest, oldResp.StatusCode, "Expected 400 from Py API")
9931007
defer oldResp.Body.Close()
9941008
oldBody, _ := io.ReadAll(oldResp.Body)
9951009
var oldJSON interface{}
996-
err = json.Unmarshal(oldBody, &oldJSON)
1010+
err := json.Unmarshal(oldBody, &oldJSON)
9971011
assert.NoError(t, err)
9981012
Debugf("Py raw response: %+v\n", string(oldBody))
9991013
Debugf("Py response: %+v\n", oldJSON)

0 commit comments

Comments
 (0)