You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api_repo_hook_test.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "testing"
  8. auth_model "code.gitea.io/gitea/models/auth"
  9. repo_model "code.gitea.io/gitea/models/repo"
  10. "code.gitea.io/gitea/models/unittest"
  11. user_model "code.gitea.io/gitea/models/user"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/tests"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func TestAPICreateHook(t *testing.T) {
  17. defer tests.PrepareTestEnv(t)()
  18. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 37})
  19. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  20. // user1 is an admin user
  21. session := loginUser(t, "user1")
  22. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
  23. completeURL := func(lastSegment string) string {
  24. return fmt.Sprintf("/api/v1/repos/%s/%s/%s?token=%s", owner.Name, repo.Name, lastSegment, token)
  25. }
  26. req := NewRequestWithJSON(t, "POST", completeURL("hooks"), api.CreateHookOption{
  27. Type: "gitea",
  28. Config: api.CreateHookOptionConfig{
  29. "content_type": "json",
  30. "url": "http://example.com/",
  31. },
  32. AuthorizationHeader: "Bearer s3cr3t",
  33. })
  34. resp := MakeRequest(t, req, http.StatusCreated)
  35. var apiHook *api.Hook
  36. DecodeJSON(t, resp, &apiHook)
  37. assert.Equal(t, "http://example.com/", apiHook.Config["url"])
  38. assert.Equal(t, "Bearer s3cr3t", apiHook.AuthorizationHeader)
  39. }