|
|
|
@ -12,19 +12,24 @@ import ( |
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo" |
|
|
|
|
"code.gitea.io/gitea/models/unittest" |
|
|
|
|
user_model "code.gitea.io/gitea/models/user" |
|
|
|
|
"code.gitea.io/gitea/modules/setting" |
|
|
|
|
api "code.gitea.io/gitea/modules/structs" |
|
|
|
|
"code.gitea.io/gitea/modules/test" |
|
|
|
|
"code.gitea.io/gitea/tests" |
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
const testHookContent = `#!/bin/bash |
|
|
|
|
func TestAPIGitHooks(t *testing.T) { |
|
|
|
|
defer tests.PrepareTestEnv(t)() |
|
|
|
|
defer test.MockVariableValue(&setting.DisableGitHooks, false)() |
|
|
|
|
|
|
|
|
|
echo Hello, World! |
|
|
|
|
const testHookContent = `#!/bin/bash |
|
|
|
|
echo "TestGitHookScript" |
|
|
|
|
` |
|
|
|
|
|
|
|
|
|
func TestAPIListGitHooks(t *testing.T) { |
|
|
|
|
defer tests.PrepareTestEnv(t)() |
|
|
|
|
t.Run("ListGitHooks", func(t *testing.T) { |
|
|
|
|
defer tests.PrintCurrentTest(t)() |
|
|
|
|
|
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 37}) |
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) |
|
|
|
@ -47,10 +52,10 @@ func TestAPIListGitHooks(t *testing.T) { |
|
|
|
|
assert.Empty(t, apiGitHook.Content) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
func TestAPIListGitHooksNoHooks(t *testing.T) { |
|
|
|
|
defer tests.PrepareTestEnv(t)() |
|
|
|
|
t.Run("NoGitHooks", func(t *testing.T) { |
|
|
|
|
defer tests.PrintCurrentTest(t)() |
|
|
|
|
|
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) |
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) |
|
|
|
@ -68,10 +73,10 @@ func TestAPIListGitHooksNoHooks(t *testing.T) { |
|
|
|
|
assert.False(t, apiGitHook.IsActive) |
|
|
|
|
assert.Empty(t, apiGitHook.Content) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
func TestAPIListGitHooksNoAccess(t *testing.T) { |
|
|
|
|
defer tests.PrepareTestEnv(t)() |
|
|
|
|
t.Run("ListGitHooksNoAccess", func(t *testing.T) { |
|
|
|
|
defer tests.PrintCurrentTest(t)() |
|
|
|
|
|
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) |
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) |
|
|
|
@ -81,10 +86,10 @@ func TestAPIListGitHooksNoAccess(t *testing.T) { |
|
|
|
|
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/hooks/git", owner.Name, repo.Name). |
|
|
|
|
AddTokenAuth(token) |
|
|
|
|
MakeRequest(t, req, http.StatusForbidden) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
func TestAPIGetGitHook(t *testing.T) { |
|
|
|
|
defer tests.PrepareTestEnv(t)() |
|
|
|
|
t.Run("GetGitHook", func(t *testing.T) { |
|
|
|
|
defer tests.PrintCurrentTest(t)() |
|
|
|
|
|
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 37}) |
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) |
|
|
|
@ -99,10 +104,9 @@ func TestAPIGetGitHook(t *testing.T) { |
|
|
|
|
DecodeJSON(t, resp, &apiGitHook) |
|
|
|
|
assert.True(t, apiGitHook.IsActive) |
|
|
|
|
assert.Equal(t, testHookContent, apiGitHook.Content) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestAPIGetGitHookNoAccess(t *testing.T) { |
|
|
|
|
defer tests.PrepareTestEnv(t)() |
|
|
|
|
}) |
|
|
|
|
t.Run("GetGitHookNoAccess", func(t *testing.T) { |
|
|
|
|
defer tests.PrintCurrentTest(t)() |
|
|
|
|
|
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) |
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) |
|
|
|
@ -112,10 +116,10 @@ func TestAPIGetGitHookNoAccess(t *testing.T) { |
|
|
|
|
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/hooks/git/pre-receive", owner.Name, repo.Name). |
|
|
|
|
AddTokenAuth(token) |
|
|
|
|
MakeRequest(t, req, http.StatusForbidden) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
func TestAPIEditGitHook(t *testing.T) { |
|
|
|
|
defer tests.PrepareTestEnv(t)() |
|
|
|
|
t.Run("EditGitHook", func(t *testing.T) { |
|
|
|
|
defer tests.PrintCurrentTest(t)() |
|
|
|
|
|
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) |
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) |
|
|
|
@ -142,10 +146,10 @@ func TestAPIEditGitHook(t *testing.T) { |
|
|
|
|
DecodeJSON(t, resp, &apiGitHook2) |
|
|
|
|
assert.True(t, apiGitHook2.IsActive) |
|
|
|
|
assert.Equal(t, testHookContent, apiGitHook2.Content) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
func TestAPIEditGitHookNoAccess(t *testing.T) { |
|
|
|
|
defer tests.PrepareTestEnv(t)() |
|
|
|
|
t.Run("EditGitHookNoAccess", func(t *testing.T) { |
|
|
|
|
defer tests.PrintCurrentTest(t)() |
|
|
|
|
|
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) |
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) |
|
|
|
@ -157,10 +161,10 @@ func TestAPIEditGitHookNoAccess(t *testing.T) { |
|
|
|
|
Content: testHookContent, |
|
|
|
|
}).AddTokenAuth(token) |
|
|
|
|
MakeRequest(t, req, http.StatusForbidden) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
func TestAPIDeleteGitHook(t *testing.T) { |
|
|
|
|
defer tests.PrepareTestEnv(t)() |
|
|
|
|
t.Run("DeleteGitHook", func(t *testing.T) { |
|
|
|
|
defer tests.PrintCurrentTest(t)() |
|
|
|
|
|
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 37}) |
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) |
|
|
|
@ -180,10 +184,10 @@ func TestAPIDeleteGitHook(t *testing.T) { |
|
|
|
|
DecodeJSON(t, resp, &apiGitHook2) |
|
|
|
|
assert.False(t, apiGitHook2.IsActive) |
|
|
|
|
assert.Empty(t, apiGitHook2.Content) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
func TestAPIDeleteGitHookNoAccess(t *testing.T) { |
|
|
|
|
defer tests.PrepareTestEnv(t)() |
|
|
|
|
t.Run("DeleteGitHookNoAccess", func(t *testing.T) { |
|
|
|
|
defer tests.PrintCurrentTest(t)() |
|
|
|
|
|
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) |
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) |
|
|
|
@ -193,4 +197,5 @@ func TestAPIDeleteGitHookNoAccess(t *testing.T) { |
|
|
|
|
req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/hooks/git/pre-receive", owner.Name, repo.Name). |
|
|
|
|
AddTokenAuth(token) |
|
|
|
|
MakeRequest(t, req, http.StatusForbidden) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|