feat(tools): branch_protection_get
This commit is contained in:
@@ -164,3 +164,37 @@ func TestDeleteBranchProtected(t *testing.T) {
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, err, gitea.ErrPermissionDenied)
|
||||
}
|
||||
|
||||
func TestGetBranchProtectionFound(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, "/api/v1/repos/o/r/branch_protections/main", r.URL.Path)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{
|
||||
"required_approvals": 2,
|
||||
"push_whitelist_usernames": ["alice"],
|
||||
"merge_whitelist_usernames": ["bob"]
|
||||
}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := gitea.NewClient(srv.URL, "tok")
|
||||
bp, err := c.GetBranchProtection(context.Background(), "o", "r", "main")
|
||||
require.NoError(t, err)
|
||||
assert.True(t, bp.Protected)
|
||||
assert.Equal(t, int64(2), bp.RequiredApprovals)
|
||||
assert.Equal(t, []string{"alice"}, bp.PushWhitelist)
|
||||
assert.Equal(t, []string{"bob"}, bp.MergeWhitelist)
|
||||
}
|
||||
|
||||
func TestGetBranchProtectionNotFoundReturnsUnprotected(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
_, _ = w.Write([]byte(`{"message":"not found"}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := gitea.NewClient(srv.URL, "tok")
|
||||
bp, err := c.GetBranchProtection(context.Background(), "o", "r", "feat/x")
|
||||
require.NoError(t, err)
|
||||
assert.False(t, bp.Protected)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user