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

     1  // Copyright 2019 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  const testHookContent = `#!/bin/bash
    22  
    23  echo Hello, World!
    24  `
    25  
    26  func TestAPIListGitHooks(t *testing.T) {
    27  	defer tests.PrepareTestEnv(t)()
    28  
    29  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 37})
    30  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
    31  
    32  	// user1 is an admin user
    33  	session := loginUser(t, "user1")
    34  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
    35  	req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/hooks/git?token=%s",
    36  		owner.Name, repo.Name, token)
    37  	resp := MakeRequest(t, req, http.StatusOK)
    38  	var apiGitHooks []*api.GitHook
    39  	DecodeJSON(t, resp, &apiGitHooks)
    40  	assert.Len(t, apiGitHooks, 3)
    41  	for _, apiGitHook := range apiGitHooks {
    42  		if apiGitHook.Name == "pre-receive" {
    43  			assert.True(t, apiGitHook.IsActive)
    44  			assert.Equal(t, testHookContent, apiGitHook.Content)
    45  		} else {
    46  			assert.False(t, apiGitHook.IsActive)
    47  			assert.Empty(t, apiGitHook.Content)
    48  		}
    49  	}
    50  }
    51  
    52  func TestAPIListGitHooksNoHooks(t *testing.T) {
    53  	defer tests.PrepareTestEnv(t)()
    54  
    55  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
    56  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
    57  
    58  	// user1 is an admin user
    59  	session := loginUser(t, "user1")
    60  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
    61  	req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/hooks/git?token=%s",
    62  		owner.Name, repo.Name, token)
    63  	resp := MakeRequest(t, req, http.StatusOK)
    64  	var apiGitHooks []*api.GitHook
    65  	DecodeJSON(t, resp, &apiGitHooks)
    66  	assert.Len(t, apiGitHooks, 3)
    67  	for _, apiGitHook := range apiGitHooks {
    68  		assert.False(t, apiGitHook.IsActive)
    69  		assert.Empty(t, apiGitHook.Content)
    70  	}
    71  }
    72  
    73  func TestAPIListGitHooksNoAccess(t *testing.T) {
    74  	defer tests.PrepareTestEnv(t)()
    75  
    76  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
    77  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
    78  
    79  	session := loginUser(t, owner.Name)
    80  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
    81  	req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/hooks/git?token=%s",
    82  		owner.Name, repo.Name, token)
    83  	MakeRequest(t, req, http.StatusForbidden)
    84  }
    85  
    86  func TestAPIGetGitHook(t *testing.T) {
    87  	defer tests.PrepareTestEnv(t)()
    88  
    89  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 37})
    90  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
    91  
    92  	// user1 is an admin user
    93  	session := loginUser(t, "user1")
    94  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
    95  	req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/hooks/git/pre-receive?token=%s",
    96  		owner.Name, repo.Name, token)
    97  	resp := MakeRequest(t, req, http.StatusOK)
    98  	var apiGitHook *api.GitHook
    99  	DecodeJSON(t, resp, &apiGitHook)
   100  	assert.True(t, apiGitHook.IsActive)
   101  	assert.Equal(t, testHookContent, apiGitHook.Content)
   102  }
   103  
   104  func TestAPIGetGitHookNoAccess(t *testing.T) {
   105  	defer tests.PrepareTestEnv(t)()
   106  
   107  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
   108  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
   109  
   110  	session := loginUser(t, owner.Name)
   111  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
   112  	req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/hooks/git/pre-receive?token=%s",
   113  		owner.Name, repo.Name, token)
   114  	MakeRequest(t, req, http.StatusForbidden)
   115  }
   116  
   117  func TestAPIEditGitHook(t *testing.T) {
   118  	defer tests.PrepareTestEnv(t)()
   119  
   120  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
   121  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
   122  
   123  	// user1 is an admin user
   124  	session := loginUser(t, "user1")
   125  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
   126  
   127  	urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/hooks/git/pre-receive?token=%s",
   128  		owner.Name, repo.Name, token)
   129  	req := NewRequestWithJSON(t, "PATCH", urlStr, &api.EditGitHookOption{
   130  		Content: testHookContent,
   131  	})
   132  	resp := MakeRequest(t, req, http.StatusOK)
   133  	var apiGitHook *api.GitHook
   134  	DecodeJSON(t, resp, &apiGitHook)
   135  	assert.True(t, apiGitHook.IsActive)
   136  	assert.Equal(t, testHookContent, apiGitHook.Content)
   137  
   138  	req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/hooks/git/pre-receive?token=%s",
   139  		owner.Name, repo.Name, token)
   140  	resp = MakeRequest(t, req, http.StatusOK)
   141  	var apiGitHook2 *api.GitHook
   142  	DecodeJSON(t, resp, &apiGitHook2)
   143  	assert.True(t, apiGitHook2.IsActive)
   144  	assert.Equal(t, testHookContent, apiGitHook2.Content)
   145  }
   146  
   147  func TestAPIEditGitHookNoAccess(t *testing.T) {
   148  	defer tests.PrepareTestEnv(t)()
   149  
   150  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
   151  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
   152  
   153  	session := loginUser(t, owner.Name)
   154  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
   155  	urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/hooks/git/pre-receive?token=%s",
   156  		owner.Name, repo.Name, token)
   157  	req := NewRequestWithJSON(t, "PATCH", urlStr, &api.EditGitHookOption{
   158  		Content: testHookContent,
   159  	})
   160  	MakeRequest(t, req, http.StatusForbidden)
   161  }
   162  
   163  func TestAPIDeleteGitHook(t *testing.T) {
   164  	defer tests.PrepareTestEnv(t)()
   165  
   166  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 37})
   167  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
   168  
   169  	// user1 is an admin user
   170  	session := loginUser(t, "user1")
   171  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
   172  
   173  	req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/hooks/git/pre-receive?token=%s",
   174  		owner.Name, repo.Name, token)
   175  	MakeRequest(t, req, http.StatusNoContent)
   176  
   177  	req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/hooks/git/pre-receive?token=%s",
   178  		owner.Name, repo.Name, token)
   179  	resp := MakeRequest(t, req, http.StatusOK)
   180  	var apiGitHook2 *api.GitHook
   181  	DecodeJSON(t, resp, &apiGitHook2)
   182  	assert.False(t, apiGitHook2.IsActive)
   183  	assert.Empty(t, apiGitHook2.Content)
   184  }
   185  
   186  func TestAPIDeleteGitHookNoAccess(t *testing.T) {
   187  	defer tests.PrepareTestEnv(t)()
   188  
   189  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
   190  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
   191  
   192  	session := loginUser(t, owner.Name)
   193  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
   194  	req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/hooks/git/pre-receive?token=%s",
   195  		owner.Name, repo.Name, token)
   196  	MakeRequest(t, req, http.StatusForbidden)
   197  }