Skip to content

Commit

Permalink
test: fix inconsistent mocks and add test case for empty branch prote…
Browse files Browse the repository at this point in the history
…ction checks

Signed-off-by: Luis Davim <luis.davim@sendoso.com>
  • Loading branch information
luisdavim committed Sep 18, 2022
1 parent f211e09 commit e9a0dd6
Showing 1 changed file with 124 additions and 0 deletions.
124 changes: 124 additions & 0 deletions github/repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,130 @@ func TestRepositoriesService_UpdateBranchProtection_Checks(t *testing.T) {
}
}

func TestRepositoriesService_UpdateBranchProtection_StrictNoChecks(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

input := &ProtectionRequest{
RequiredStatusChecks: &RequiredStatusChecks{
Strict: true,
Checks: []*RequiredStatusCheck{},
},
RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{
DismissStaleReviews: true,
DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
Users: &[]string{"uu"},
Teams: &[]string{"tt"},
},
BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{
Users: []string{"uuu"},
Teams: []string{"ttt"},
Apps: []string{"aaa"},
},
},
Restrictions: &BranchRestrictionsRequest{
Users: []string{"u"},
Teams: []string{"t"},
Apps: []string{"a"},
},
}

mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
v := new(ProtectionRequest)
json.NewDecoder(r.Body).Decode(v)

testMethod(t, r, "PUT")
if !cmp.Equal(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}

// TODO: remove custom Accept header when this API fully launches
testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview)
fmt.Fprintf(w, `{
"required_status_checks":{
"strict":true,
"contexts":[],
"checks": []
},
"required_pull_request_reviews":{
"dismissal_restrictions":{
"users":[{
"id":3,
"login":"uu"
}],
"teams":[{
"id":4,
"slug":"tt"
}]
},
"dismiss_stale_reviews":true,
"require_code_owner_reviews":true,
"bypass_pull_request_allowances": {
"users":[{"id":10,"login":"uuu"}],
"teams":[{"id":20,"slug":"ttt"}],
"apps":[{"id":30,"slug":"aaa"}]
}
},
"restrictions":{
"users":[{"id":1,"login":"u"}],
"teams":[{"id":2,"slug":"t"}],
"apps":[{"id":3,"slug":"a"}]
}
}`)
})

ctx := context.Background()
protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input)
if err != nil {
t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err)
}

want := &Protection{
RequiredStatusChecks: &RequiredStatusChecks{
Strict: true,
Contexts: []string{},
Checks: []*RequiredStatusCheck{},
},
RequiredPullRequestReviews: &PullRequestReviewsEnforcement{
DismissStaleReviews: true,
DismissalRestrictions: &DismissalRestrictions{
Users: []*User{
{Login: String("uu"), ID: Int64(3)},
},
Teams: []*Team{
{Slug: String("tt"), ID: Int64(4)},
},
},
RequireCodeOwnerReviews: true,
BypassPullRequestAllowances: &BypassPullRequestAllowances{
Users: []*User{
{Login: String("uuu"), ID: Int64(10)},
},
Teams: []*Team{
{Slug: String("ttt"), ID: Int64(20)},
},
Apps: []*App{
{Slug: String("aaa"), ID: Int64(30)},
},
},
},
Restrictions: &BranchRestrictions{
Users: []*User{
{Login: String("u"), ID: Int64(1)},
},
Teams: []*Team{
{Slug: String("t"), ID: Int64(2)},
},
Apps: []*App{
{Slug: String("a"), ID: Int64(3)},
},
},
}
if !cmp.Equal(protection, want) {
t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want)
}
}

func TestRepositoriesService_RemoveBranchProtection(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down

0 comments on commit e9a0dd6

Please sign in to comment.