Skip to content

Commit 0a16e22

Browse files
refactor: Use testJSONBody helper for request body assertions in tests (#4183)
1 parent b7b1f86 commit 0a16e22

105 files changed

Lines changed: 943 additions & 1837 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

github/actions_oidc_test.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,17 @@ func TestActionsService_SetOrgOIDCSubjectClaimCustomTemplate(t *testing.T) {
8787
t.Parallel()
8888
client, mux, _ := setup(t)
8989

90+
input := &OIDCSubjectClaimCustomTemplate{
91+
IncludeClaimKeys: []string{"repo", "context"},
92+
}
93+
9094
mux.HandleFunc("/orgs/o/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
9195
testMethod(t, r, "PUT")
9296
testHeader(t, r, "Content-Type", "application/json")
93-
testBody(t, r, `{"include_claim_keys":["repo","context"]}`+"\n")
97+
testJSONBody(t, r, input)
9498
w.WriteHeader(http.StatusCreated)
9599
})
96100

97-
input := &OIDCSubjectClaimCustomTemplate{
98-
IncludeClaimKeys: []string{"repo", "context"},
99-
}
100101
ctx := t.Context()
101102
_, err := client.Actions.SetOrgOIDCSubjectClaimCustomTemplate(ctx, "o", input)
102103
if err != nil {
@@ -119,17 +120,18 @@ func TestActionsService_SetRepoOIDCSubjectClaimCustomTemplate(t *testing.T) {
119120
t.Parallel()
120121
client, mux, _ := setup(t)
121122

123+
input := &OIDCSubjectClaimCustomTemplate{
124+
UseDefault: Ptr(false),
125+
IncludeClaimKeys: []string{"repo", "context"},
126+
}
127+
122128
mux.HandleFunc("/repos/o/r/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
123129
testMethod(t, r, "PUT")
124130
testHeader(t, r, "Content-Type", "application/json")
125-
testBody(t, r, `{"use_default":false,"include_claim_keys":["repo","context"]}`+"\n")
131+
testJSONBody(t, r, input)
126132
w.WriteHeader(http.StatusCreated)
127133
})
128134

129-
input := &OIDCSubjectClaimCustomTemplate{
130-
UseDefault: Ptr(false),
131-
IncludeClaimKeys: []string{"repo", "context"},
132-
}
133135
ctx := t.Context()
134136
_, err := client.Actions.SetRepoOIDCSubjectClaimCustomTemplate(ctx, "o", "r", input)
135137
if err != nil {
@@ -152,16 +154,17 @@ func TestActionService_SetRepoOIDCSubjectClaimCustomTemplateToDefault(t *testing
152154
t.Parallel()
153155
client, mux, _ := setup(t)
154156

157+
input := &OIDCSubjectClaimCustomTemplate{
158+
UseDefault: Ptr(true),
159+
}
160+
155161
mux.HandleFunc("/repos/o/r/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
156162
testMethod(t, r, "PUT")
157163
testHeader(t, r, "Content-Type", "application/json")
158-
testBody(t, r, `{"use_default":true}`+"\n")
164+
testJSONBody(t, r, input)
159165
w.WriteHeader(http.StatusCreated)
160166
})
161167

162-
input := &OIDCSubjectClaimCustomTemplate{
163-
UseDefault: Ptr(true),
164-
}
165168
ctx := t.Context()
166169
_, err := client.Actions.SetRepoOIDCSubjectClaimCustomTemplate(ctx, "o", "r", input)
167170
if err != nil {

github/actions_permissions_enterprise_test.go

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package github
77

88
import (
9-
"encoding/json"
109
"fmt"
1110
"net/http"
1211
"testing"
@@ -55,14 +54,8 @@ func TestActionsService_UpdateActionsPermissionsInEnterprise(t *testing.T) {
5554
input := &ActionsPermissionsEnterprise{EnabledOrganizations: Ptr("all"), AllowedActions: Ptr("selected")}
5655

5756
mux.HandleFunc("/enterprises/e/actions/permissions", func(w http.ResponseWriter, r *http.Request) {
58-
var v *ActionsPermissionsEnterprise
59-
assertNilError(t, json.NewDecoder(r.Body).Decode(&v))
60-
6157
testMethod(t, r, "PUT")
62-
if !cmp.Equal(v, input) {
63-
t.Errorf("Request body = %+v, want %+v", v, input)
64-
}
65-
58+
testJSONBody(t, r, input)
6659
fmt.Fprint(w, `{"enabled_organizations": "all", "allowed_actions": "selected"}`)
6760
})
6861

@@ -140,28 +133,34 @@ func TestActionsService_SetEnabledOrgsInEnterprise(t *testing.T) {
140133
t.Parallel()
141134
client, mux, _ := setup(t)
142135

136+
input := []int64{123, 1234}
137+
143138
mux.HandleFunc("/enterprises/e/actions/permissions/organizations", func(w http.ResponseWriter, r *http.Request) {
144139
testMethod(t, r, "PUT")
145140
testHeader(t, r, "Content-Type", "application/json")
146-
testBody(t, r, `{"selected_organization_ids":[123,1234]}`+"\n")
141+
testJSONBody(t, r, struct {
142+
IDs []int64 `json:"selected_organization_ids"`
143+
}{
144+
IDs: input,
145+
})
147146
w.WriteHeader(http.StatusNoContent)
148147
})
149148

150149
ctx := t.Context()
151-
_, err := client.Actions.SetEnabledOrgsInEnterprise(ctx, "e", []int64{123, 1234})
150+
_, err := client.Actions.SetEnabledOrgsInEnterprise(ctx, "e", input)
152151
if err != nil {
153152
t.Errorf("Actions.SetEnabledOrgsInEnterprise returned error: %v", err)
154153
}
155154

156155
const methodName = "SetEnabledOrgsInEnterprise"
157156

158157
testBadOptions(t, methodName, func() (err error) {
159-
_, err = client.Actions.SetEnabledOrgsInEnterprise(ctx, "\n", []int64{123, 1234})
158+
_, err = client.Actions.SetEnabledOrgsInEnterprise(ctx, "\n", input)
160159
return err
161160
})
162161

163162
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
164-
return client.Actions.SetEnabledOrgsInEnterprise(ctx, "e", []int64{123, 1234})
163+
return client.Actions.SetEnabledOrgsInEnterprise(ctx, "e", input)
165164
})
166165
}
167166

@@ -260,14 +259,8 @@ func TestActionsService_UpdateActionsAllowedInEnterprise(t *testing.T) {
260259
input := &ActionsAllowed{GithubOwnedAllowed: Ptr(true), VerifiedAllowed: Ptr(false), PatternsAllowed: []string{"a/b"}}
261260

262261
mux.HandleFunc("/enterprises/e/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) {
263-
var v *ActionsAllowed
264-
assertNilError(t, json.NewDecoder(r.Body).Decode(&v))
265-
266262
testMethod(t, r, "PUT")
267-
if !cmp.Equal(v, input) {
268-
t.Errorf("Request body = %+v, want %+v", v, input)
269-
}
270-
263+
testJSONBody(t, r, input)
271264
fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`)
272265
})
273266

@@ -338,14 +331,8 @@ func TestActionsService_UpdateDefaultWorkflowPermissionsInEnterprise(t *testing.
338331
input := &DefaultWorkflowPermissionEnterprise{DefaultWorkflowPermissions: Ptr("read"), CanApprovePullRequestReviews: Ptr(true)}
339332

340333
mux.HandleFunc("/enterprises/e/actions/permissions/workflow", func(w http.ResponseWriter, r *http.Request) {
341-
var v *DefaultWorkflowPermissionEnterprise
342-
assertNilError(t, json.NewDecoder(r.Body).Decode(&v))
343-
344334
testMethod(t, r, "PUT")
345-
if !cmp.Equal(v, input) {
346-
t.Errorf("Request body = %+v, want %+v", v, input)
347-
}
348-
335+
testJSONBody(t, r, input)
349336
fmt.Fprint(w, `{ "default_workflow_permissions": "read", "can_approve_pull_request_reviews": true }`)
350337
})
351338

@@ -420,13 +407,8 @@ func TestActionsService_UpdateArtifactAndLogRetentionPeriodInEnterprise(t *testi
420407
input := &ArtifactPeriodOpt{Days: Ptr(90)}
421408

422409
mux.HandleFunc("/enterprises/e/actions/permissions/artifact-and-log-retention", func(w http.ResponseWriter, r *http.Request) {
423-
var v *ArtifactPeriodOpt
424-
assertNilError(t, json.NewDecoder(r.Body).Decode(&v))
425-
426410
testMethod(t, r, "PUT")
427-
if !cmp.Equal(v, input) {
428-
t.Errorf("Request body = %+v, want %+v", v, input)
429-
}
411+
testJSONBody(t, r, input)
430412
w.WriteHeader(http.StatusNoContent)
431413
})
432414

@@ -492,13 +474,8 @@ func TestActionsService_UpdateSelfHostedRunnerPermissionsInEnterprise(t *testing
492474
input := &SelfHostRunnerPermissionsEnterprise{DisableSelfHostedRunnersForAllOrgs: Ptr(false)}
493475

494476
mux.HandleFunc("/enterprises/e/actions/permissions/self-hosted-runners", func(w http.ResponseWriter, r *http.Request) {
495-
var v *SelfHostRunnerPermissionsEnterprise
496-
assertNilError(t, json.NewDecoder(r.Body).Decode(&v))
497-
498477
testMethod(t, r, "PUT")
499-
if !cmp.Equal(v, input) {
500-
t.Errorf("Request body = %+v, want %+v", v, input)
501-
}
478+
testJSONBody(t, r, input)
502479
w.WriteHeader(http.StatusNoContent)
503480
})
504481

@@ -573,13 +550,8 @@ func TestActionsService_UpdatePrivateRepoForkPRWorkflowSettingsInEnterprise(t *t
573550
}
574551

575552
mux.HandleFunc("/enterprises/e/actions/permissions/fork-pr-workflows-private-repos", func(w http.ResponseWriter, r *http.Request) {
576-
var v *WorkflowsPermissionsOpt
577-
assertNilError(t, json.NewDecoder(r.Body).Decode(&v))
578-
579553
testMethod(t, r, "PUT")
580-
if !cmp.Equal(v, input) {
581-
t.Errorf("Request body = %+v, want %+v", v, input)
582-
}
554+
testJSONBody(t, r, input)
583555
w.WriteHeader(http.StatusNoContent)
584556
})
585557

@@ -645,13 +617,8 @@ func TestActionsService_UpdateEnterpriseForkPRContributorApprovalPermissions(t *
645617
input := ContributorApprovalPermissions{ApprovalPolicy: "require_approval"}
646618

647619
mux.HandleFunc("/enterprises/e/actions/permissions/fork-pr-contributor-approval", func(w http.ResponseWriter, r *http.Request) {
648-
var v *ContributorApprovalPermissions
649-
assertNilError(t, json.NewDecoder(r.Body).Decode(&v))
650-
651620
testMethod(t, r, "PUT")
652-
if !cmp.Equal(v, &input) {
653-
t.Errorf("Request body = %+v, want %+v", v, &input)
654-
}
621+
testJSONBody(t, r, input)
655622
w.WriteHeader(http.StatusNoContent)
656623
})
657624

0 commit comments

Comments
 (0)