code.gitea.io/gitea@v1.21.7/tests/integration/api_repo_hook_test.go (about)

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