forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/release/v1.7' into develop
- Loading branch information
Showing
15 changed files
with
722 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integrations | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
|
||
api "code.gitea.io/sdk/gitea" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type APITestContext struct { | ||
Reponame string | ||
Session *TestSession | ||
Token string | ||
Username string | ||
ExpectedCode int | ||
} | ||
|
||
func NewAPITestContext(t *testing.T, username, reponame string) APITestContext { | ||
session := loginUser(t, username) | ||
token := getTokenForLoggedInUser(t, session) | ||
return APITestContext{ | ||
Session: session, | ||
Token: token, | ||
Username: username, | ||
Reponame: reponame, | ||
} | ||
} | ||
|
||
func (ctx APITestContext) GitPath() string { | ||
return fmt.Sprintf("%s/%s.git", ctx.Username, ctx.Reponame) | ||
} | ||
|
||
func doAPICreateRepository(ctx APITestContext, empty bool, callback ...func(*testing.T, api.Repository)) func(*testing.T) { | ||
return func(t *testing.T) { | ||
createRepoOption := &api.CreateRepoOption{ | ||
AutoInit: !empty, | ||
Description: "Temporary repo", | ||
Name: ctx.Reponame, | ||
Private: true, | ||
Gitignores: "", | ||
License: "WTFPL", | ||
Readme: "Default", | ||
} | ||
req := NewRequestWithJSON(t, "POST", "/api/v1/user/repos?token="+ctx.Token, createRepoOption) | ||
if ctx.ExpectedCode != 0 { | ||
ctx.Session.MakeRequest(t, req, ctx.ExpectedCode) | ||
return | ||
} | ||
resp := ctx.Session.MakeRequest(t, req, http.StatusCreated) | ||
|
||
var repository api.Repository | ||
DecodeJSON(t, resp, &repository) | ||
if len(callback) > 0 { | ||
callback[0](t, repository) | ||
} | ||
} | ||
} | ||
|
||
func doAPIGetRepository(ctx APITestContext, callback ...func(*testing.T, api.Repository)) func(*testing.T) { | ||
return func(t *testing.T) { | ||
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", ctx.Username, ctx.Reponame, ctx.Token) | ||
|
||
req := NewRequest(t, "GET", urlStr) | ||
if ctx.ExpectedCode != 0 { | ||
ctx.Session.MakeRequest(t, req, ctx.ExpectedCode) | ||
return | ||
} | ||
resp := ctx.Session.MakeRequest(t, req, http.StatusOK) | ||
|
||
var repository api.Repository | ||
DecodeJSON(t, resp, &repository) | ||
if len(callback) > 0 { | ||
callback[0](t, repository) | ||
} | ||
} | ||
} | ||
|
||
func doAPIDeleteRepository(ctx APITestContext) func(*testing.T) { | ||
return func(t *testing.T) { | ||
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", ctx.Username, ctx.Reponame, ctx.Token) | ||
|
||
req := NewRequest(t, "DELETE", urlStr) | ||
if ctx.ExpectedCode != 0 { | ||
ctx.Session.MakeRequest(t, req, ctx.ExpectedCode) | ||
return | ||
} | ||
ctx.Session.MakeRequest(t, req, http.StatusNoContent) | ||
} | ||
} | ||
|
||
func doAPICreateUserKey(ctx APITestContext, keyname, keyFile string, callback ...func(*testing.T, api.PublicKey)) func(*testing.T) { | ||
return func(t *testing.T) { | ||
urlStr := fmt.Sprintf("/api/v1/user/keys?token=%s", ctx.Token) | ||
|
||
dataPubKey, err := ioutil.ReadFile(keyFile + ".pub") | ||
assert.NoError(t, err) | ||
req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateKeyOption{ | ||
Title: keyname, | ||
Key: string(dataPubKey), | ||
}) | ||
if ctx.ExpectedCode != 0 { | ||
ctx.Session.MakeRequest(t, req, ctx.ExpectedCode) | ||
return | ||
} | ||
resp := ctx.Session.MakeRequest(t, req, http.StatusCreated) | ||
var publicKey api.PublicKey | ||
DecodeJSON(t, resp, &publicKey) | ||
if len(callback) > 0 { | ||
callback[0](t, publicKey) | ||
} | ||
} | ||
} | ||
|
||
func doAPIDeleteUserKey(ctx APITestContext, keyID int64) func(*testing.T) { | ||
return func(t *testing.T) { | ||
urlStr := fmt.Sprintf("/api/v1/user/keys/%d?token=%s", keyID, ctx.Token) | ||
|
||
req := NewRequest(t, "DELETE", urlStr) | ||
if ctx.ExpectedCode != 0 { | ||
ctx.Session.MakeRequest(t, req, ctx.ExpectedCode) | ||
return | ||
} | ||
ctx.Session.MakeRequest(t, req, http.StatusNoContent) | ||
} | ||
} | ||
|
||
func doAPICreateDeployKey(ctx APITestContext, keyname, keyFile string, readOnly bool) func(*testing.T) { | ||
return func(t *testing.T) { | ||
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/keys?token=%s", ctx.Username, ctx.Reponame, ctx.Token) | ||
|
||
dataPubKey, err := ioutil.ReadFile(keyFile + ".pub") | ||
assert.NoError(t, err) | ||
req := NewRequestWithJSON(t, "POST", urlStr, api.CreateKeyOption{ | ||
Title: keyname, | ||
Key: string(dataPubKey), | ||
ReadOnly: readOnly, | ||
}) | ||
|
||
if ctx.ExpectedCode != 0 { | ||
ctx.Session.MakeRequest(t, req, ctx.ExpectedCode) | ||
return | ||
} | ||
ctx.Session.MakeRequest(t, req, http.StatusCreated) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integrations | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"code.gitea.io/git" | ||
"code.gitea.io/gitea/modules/setting" | ||
"github.com/Unknwon/com" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func withKeyFile(t *testing.T, keyname string, callback func(string)) { | ||
keyFile := filepath.Join(setting.AppDataPath, keyname) | ||
err := exec.Command("ssh-keygen", "-f", keyFile, "-t", "rsa", "-N", "").Run() | ||
assert.NoError(t, err) | ||
|
||
//Setup ssh wrapper | ||
os.Setenv("GIT_SSH_COMMAND", | ||
"ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i "+ | ||
filepath.Join(setting.AppWorkPath, keyFile)) | ||
os.Setenv("GIT_SSH_VARIANT", "ssh") | ||
|
||
callback(keyFile) | ||
|
||
defer os.RemoveAll(keyFile) | ||
defer os.RemoveAll(keyFile + ".pub") | ||
} | ||
|
||
func createSSHUrl(gitPath string, u *url.URL) *url.URL { | ||
u2 := *u | ||
u2.Scheme = "ssh" | ||
u2.User = url.User("git") | ||
u2.Host = fmt.Sprintf("%s:%d", setting.SSH.ListenHost, setting.SSH.ListenPort) | ||
u2.Path = gitPath | ||
return &u2 | ||
} | ||
|
||
func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL)) { | ||
prepareTestEnv(t) | ||
s := http.Server{ | ||
Handler: mac, | ||
} | ||
|
||
u, err := url.Parse(setting.AppURL) | ||
assert.NoError(t, err) | ||
listener, err := net.Listen("tcp", u.Host) | ||
assert.NoError(t, err) | ||
|
||
defer func() { | ||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) | ||
s.Shutdown(ctx) | ||
cancel() | ||
}() | ||
|
||
go s.Serve(listener) | ||
//Started by config go ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs) | ||
|
||
callback(t, u) | ||
} | ||
|
||
func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) { | ||
return func(t *testing.T) { | ||
assert.NoError(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{})) | ||
assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md"))) | ||
} | ||
} | ||
|
||
func doGitCloneFail(dstLocalPath string, u *url.URL) func(*testing.T) { | ||
return func(t *testing.T) { | ||
assert.Error(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{})) | ||
assert.False(t, com.IsExist(filepath.Join(dstLocalPath, "README.md"))) | ||
} | ||
} | ||
|
||
func doGitInitTestRepository(dstPath string) func(*testing.T) { | ||
return func(t *testing.T) { | ||
// Init repository in dstPath | ||
assert.NoError(t, git.InitRepository(dstPath, false)) | ||
assert.NoError(t, ioutil.WriteFile(filepath.Join(dstPath, "README.md"), []byte(fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s", dstPath)), 0644)) | ||
assert.NoError(t, git.AddChanges(dstPath, true)) | ||
signature := git.Signature{ | ||
Email: "test@example.com", | ||
Name: "test", | ||
When: time.Now(), | ||
} | ||
assert.NoError(t, git.CommitChanges(dstPath, git.CommitChangesOptions{ | ||
Committer: &signature, | ||
Author: &signature, | ||
Message: "Initial Commit", | ||
})) | ||
} | ||
} | ||
|
||
func doGitAddRemote(dstPath, remoteName string, u *url.URL) func(*testing.T) { | ||
return func(t *testing.T) { | ||
_, err := git.NewCommand("remote", "add", remoteName, u.String()).RunInDir(dstPath) | ||
assert.NoError(t, err) | ||
} | ||
} | ||
|
||
func doGitPushTestRepository(dstPath, remoteName, branch string) func(*testing.T) { | ||
return func(t *testing.T) { | ||
_, err := git.NewCommand("push", "-u", remoteName, branch).RunInDir(dstPath) | ||
assert.NoError(t, err) | ||
} | ||
} | ||
|
||
func doGitPushTestRepositoryFail(dstPath, remoteName, branch string) func(*testing.T) { | ||
return func(t *testing.T) { | ||
_, err := git.NewCommand("push", "-u", remoteName, branch).RunInDir(dstPath) | ||
assert.Error(t, err) | ||
} | ||
} |
Oops, something went wrong.