code.gitea.io/gitea@v1.21.7/tests/integration/api_repo_git_commits_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  	"net/http"
     8  	"testing"
     9  
    10  	auth_model "code.gitea.io/gitea/models/auth"
    11  	"code.gitea.io/gitea/models/unittest"
    12  	user_model "code.gitea.io/gitea/models/user"
    13  	api "code.gitea.io/gitea/modules/structs"
    14  	"code.gitea.io/gitea/tests"
    15  
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func compareCommitFiles(t *testing.T, expect []string, files []*api.CommitAffectedFiles) {
    20  	var actual []string
    21  	for i := range files {
    22  		actual = append(actual, files[i].Filename)
    23  	}
    24  	assert.ElementsMatch(t, expect, actual)
    25  }
    26  
    27  func TestAPIReposGitCommits(t *testing.T) {
    28  	defer tests.PrepareTestEnv(t)()
    29  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    30  	// Login as User2.
    31  	session := loginUser(t, user.Name)
    32  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
    33  
    34  	// check invalid requests
    35  	req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/12345?token="+token, user.Name)
    36  	MakeRequest(t, req, http.StatusNotFound)
    37  
    38  	req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/..?token="+token, user.Name)
    39  	MakeRequest(t, req, http.StatusUnprocessableEntity)
    40  
    41  	req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/branch-not-exist?token="+token, user.Name)
    42  	MakeRequest(t, req, http.StatusNotFound)
    43  
    44  	for _, ref := range [...]string{
    45  		"master", // Branch
    46  		"v1.1",   // Tag
    47  		"65f1",   // short sha
    48  		"65f1bf27bc3bf70f64657658635e66094edbcb4d", // full sha
    49  	} {
    50  		req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/%s?token="+token, user.Name, ref)
    51  		MakeRequest(t, req, http.StatusOK)
    52  	}
    53  }
    54  
    55  func TestAPIReposGitCommitList(t *testing.T) {
    56  	defer tests.PrepareTestEnv(t)()
    57  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    58  	// Login as User2.
    59  	session := loginUser(t, user.Name)
    60  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
    61  
    62  	// Test getting commits (Page 1)
    63  	req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo20/commits?token="+token+"&not=master&sha=remove-files-a", user.Name)
    64  	resp := MakeRequest(t, req, http.StatusOK)
    65  
    66  	var apiData []api.Commit
    67  	DecodeJSON(t, resp, &apiData)
    68  
    69  	assert.Len(t, apiData, 2)
    70  	assert.EqualValues(t, "cfe3b3c1fd36fba04f9183287b106497e1afe986", apiData[0].CommitMeta.SHA)
    71  	compareCommitFiles(t, []string{"link_hi", "test.csv"}, apiData[0].Files)
    72  	assert.EqualValues(t, "c8e31bc7688741a5287fcde4fbb8fc129ca07027", apiData[1].CommitMeta.SHA)
    73  	compareCommitFiles(t, []string{"test.csv"}, apiData[1].Files)
    74  
    75  	assert.EqualValues(t, resp.Header().Get("X-Total"), "2")
    76  }
    77  
    78  func TestAPIReposGitCommitListNotMaster(t *testing.T) {
    79  	defer tests.PrepareTestEnv(t)()
    80  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    81  	// Login as User2.
    82  	session := loginUser(t, user.Name)
    83  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
    84  
    85  	// Test getting commits (Page 1)
    86  	req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token, user.Name)
    87  	resp := MakeRequest(t, req, http.StatusOK)
    88  
    89  	var apiData []api.Commit
    90  	DecodeJSON(t, resp, &apiData)
    91  
    92  	assert.Len(t, apiData, 3)
    93  	assert.EqualValues(t, "69554a64c1e6030f051e5c3f94bfbd773cd6a324", apiData[0].CommitMeta.SHA)
    94  	compareCommitFiles(t, []string{"readme.md"}, apiData[0].Files)
    95  	assert.EqualValues(t, "27566bd5738fc8b4e3fef3c5e72cce608537bd95", apiData[1].CommitMeta.SHA)
    96  	compareCommitFiles(t, []string{"readme.md"}, apiData[1].Files)
    97  	assert.EqualValues(t, "5099b81332712fe655e34e8dd63574f503f61811", apiData[2].CommitMeta.SHA)
    98  	compareCommitFiles(t, []string{"readme.md"}, apiData[2].Files)
    99  
   100  	assert.EqualValues(t, resp.Header().Get("X-Total"), "3")
   101  }
   102  
   103  func TestAPIReposGitCommitListPage2Empty(t *testing.T) {
   104  	defer tests.PrepareTestEnv(t)()
   105  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
   106  	// Login as User2.
   107  	session := loginUser(t, user.Name)
   108  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
   109  
   110  	// Test getting commits (Page=2)
   111  	req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token+"&page=2", user.Name)
   112  	resp := MakeRequest(t, req, http.StatusOK)
   113  
   114  	var apiData []api.Commit
   115  	DecodeJSON(t, resp, &apiData)
   116  
   117  	assert.Len(t, apiData, 0)
   118  }
   119  
   120  func TestAPIReposGitCommitListDifferentBranch(t *testing.T) {
   121  	defer tests.PrepareTestEnv(t)()
   122  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
   123  	// Login as User2.
   124  	session := loginUser(t, user.Name)
   125  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
   126  
   127  	// Test getting commits (Page=1, Branch=good-sign)
   128  	req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token+"&sha=good-sign", user.Name)
   129  	resp := MakeRequest(t, req, http.StatusOK)
   130  
   131  	var apiData []api.Commit
   132  	DecodeJSON(t, resp, &apiData)
   133  
   134  	assert.Len(t, apiData, 1)
   135  	assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA)
   136  	compareCommitFiles(t, []string{"readme.md"}, apiData[0].Files)
   137  }
   138  
   139  func TestAPIReposGitCommitListWithoutSelectFields(t *testing.T) {
   140  	defer tests.PrepareTestEnv(t)()
   141  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
   142  	// Login as User2.
   143  	session := loginUser(t, user.Name)
   144  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
   145  
   146  	// Test getting commits without files, verification, and stats
   147  	req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token+"&sha=good-sign&stat=false&files=false&verification=false", user.Name)
   148  	resp := MakeRequest(t, req, http.StatusOK)
   149  
   150  	var apiData []api.Commit
   151  	DecodeJSON(t, resp, &apiData)
   152  
   153  	assert.Len(t, apiData, 1)
   154  	assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA)
   155  	assert.Equal(t, (*api.CommitStats)(nil), apiData[0].Stats)
   156  	assert.Equal(t, (*api.PayloadCommitVerification)(nil), apiData[0].RepoCommit.Verification)
   157  	assert.Equal(t, ([]*api.CommitAffectedFiles)(nil), apiData[0].Files)
   158  }
   159  
   160  func TestDownloadCommitDiffOrPatch(t *testing.T) {
   161  	defer tests.PrepareTestEnv(t)()
   162  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
   163  	// Login as User2.
   164  	session := loginUser(t, user.Name)
   165  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
   166  
   167  	// Test getting diff
   168  	reqDiff := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/git/commits/f27c2b2b03dcab38beaf89b0ab4ff61f6de63441.diff?token="+token, user.Name)
   169  	resp := MakeRequest(t, reqDiff, http.StatusOK)
   170  	assert.EqualValues(t,
   171  		"commit f27c2b2b03dcab38beaf89b0ab4ff61f6de63441\nAuthor: User2 <user2@example.com>\nDate:   Sun Aug 6 19:55:01 2017 +0200\n\n    good signed commit\n\ndiff --git a/readme.md b/readme.md\nnew file mode 100644\nindex 0000000..458121c\n--- /dev/null\n+++ b/readme.md\n@@ -0,0 +1 @@\n+good sign\n",
   172  		resp.Body.String())
   173  
   174  	// Test getting patch
   175  	reqPatch := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/git/commits/f27c2b2b03dcab38beaf89b0ab4ff61f6de63441.patch?token="+token, user.Name)
   176  	resp = MakeRequest(t, reqPatch, http.StatusOK)
   177  	assert.EqualValues(t,
   178  		"From f27c2b2b03dcab38beaf89b0ab4ff61f6de63441 Mon Sep 17 00:00:00 2001\nFrom: User2 <user2@example.com>\nDate: Sun, 6 Aug 2017 19:55:01 +0200\nSubject: [PATCH] good signed commit\n\n---\n readme.md | 1 +\n 1 file changed, 1 insertion(+)\n create mode 100644 readme.md\n\ndiff --git a/readme.md b/readme.md\nnew file mode 100644\nindex 0000000..458121c\n--- /dev/null\n+++ b/readme.md\n@@ -0,0 +1 @@\n+good sign\n",
   179  		resp.Body.String())
   180  }
   181  
   182  func TestGetFileHistory(t *testing.T) {
   183  	defer tests.PrepareTestEnv(t)()
   184  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
   185  	// Login as User2.
   186  	session := loginUser(t, user.Name)
   187  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
   188  
   189  	req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?path=readme.md&token="+token+"&sha=good-sign", user.Name)
   190  	resp := MakeRequest(t, req, http.StatusOK)
   191  
   192  	var apiData []api.Commit
   193  	DecodeJSON(t, resp, &apiData)
   194  
   195  	assert.Len(t, apiData, 1)
   196  	assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA)
   197  	compareCommitFiles(t, []string{"readme.md"}, apiData[0].Files)
   198  
   199  	assert.EqualValues(t, resp.Header().Get("X-Total"), "1")
   200  }
   201  
   202  func TestGetFileHistoryNotOnMaster(t *testing.T) {
   203  	defer tests.PrepareTestEnv(t)()
   204  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
   205  	// Login as User2.
   206  	session := loginUser(t, user.Name)
   207  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
   208  
   209  	req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo20/commits?path=test.csv&token="+token+"&sha=add-csv&not=master", user.Name)
   210  	resp := MakeRequest(t, req, http.StatusOK)
   211  
   212  	var apiData []api.Commit
   213  	DecodeJSON(t, resp, &apiData)
   214  
   215  	assert.Len(t, apiData, 1)
   216  	assert.Equal(t, "c8e31bc7688741a5287fcde4fbb8fc129ca07027", apiData[0].CommitMeta.SHA)
   217  	compareCommitFiles(t, []string{"test.csv"}, apiData[0].Files)
   218  
   219  	assert.EqualValues(t, resp.Header().Get("X-Total"), "1")
   220  }