code.gitea.io/gitea@v1.22.3/routers/private/hook_verification_test.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package private
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  
    10  	"code.gitea.io/gitea/models/unittest"
    11  	"code.gitea.io/gitea/modules/git"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  var testReposDir = "tests/repos/"
    17  
    18  func TestVerifyCommits(t *testing.T) {
    19  	unittest.PrepareTestEnv(t)
    20  
    21  	gitRepo, err := git.OpenRepository(context.Background(), testReposDir+"repo1_hook_verification")
    22  	defer gitRepo.Close()
    23  	assert.NoError(t, err)
    24  
    25  	objectFormat, err := gitRepo.GetObjectFormat()
    26  	assert.NoError(t, err)
    27  
    28  	testCases := []struct {
    29  		base, head string
    30  		verified   bool
    31  	}{
    32  		{"72920278f2f999e3005801e5d5b8ab8139d3641c", "d766f2917716d45be24bfa968b8409544941be32", true},
    33  		{objectFormat.EmptyObjectID().String(), "93eac826f6188f34646cea81bf426aa5ba7d3bfe", true}, // New branch with verified commit
    34  		{"9779d17a04f1e2640583d35703c62460b2d86e0a", "72920278f2f999e3005801e5d5b8ab8139d3641c", false},
    35  		{objectFormat.EmptyObjectID().String(), "9ce3f779ae33f31fce17fac3c512047b75d7498b", false}, // New branch with unverified commit
    36  	}
    37  
    38  	for _, tc := range testCases {
    39  		err = verifyCommits(tc.base, tc.head, gitRepo, nil)
    40  		if tc.verified {
    41  			assert.NoError(t, err)
    42  		} else {
    43  			assert.Error(t, err)
    44  		}
    45  	}
    46  }