mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-23 05:42:33 +09:00
Remove external service dependencies in migration tests (#36866)
Fix #36859 Replace live third-party API calls in migration tests with a fixture-based HTTP mock server. Fixtures are committed so tests run offline by default; live recording is gated per service on an API-token env var. Co-authored-by: Claude (Opus 4.7) <noreply@anthropic.com>
This commit is contained in:
@@ -6,39 +6,34 @@ package migrations
|
||||
import (
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
base "code.gitea.io/gitea/modules/migration"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCodebaseDownloadRepo(t *testing.T) {
|
||||
// Skip tests if Codebase token is not found
|
||||
cloneUser := os.Getenv("CODEBASE_CLONE_USER")
|
||||
clonePassword := os.Getenv("CODEBASE_CLONE_PASSWORD")
|
||||
apiUser := os.Getenv("CODEBASE_API_USER")
|
||||
apiPassword := os.Getenv("CODEBASE_API_TOKEN")
|
||||
if apiUser == "" || apiPassword == "" {
|
||||
t.Skip("skipped test because a CODEBASE_ variable was not in the environment")
|
||||
}
|
||||
liveMode := apiUser != "" && apiPassword != ""
|
||||
|
||||
_, callerFile, _, _ := runtime.Caller(0)
|
||||
fixtureDir := filepath.Join(filepath.Dir(callerFile), "_mock_data/TestCodebaseDownloadRepo")
|
||||
mockServer := unittest.NewMockWebServer(t, "https://api3.codebasehq.com", fixtureDir, liveMode)
|
||||
|
||||
cloneAddr := "https://gitea-test.codebasehq.com/gitea-test/test.git"
|
||||
u, _ := url.Parse(cloneAddr)
|
||||
if cloneUser != "" {
|
||||
u.User = url.UserPassword(cloneUser, clonePassword)
|
||||
}
|
||||
projectURL, _ := url.Parse(cloneAddr)
|
||||
projectURL.User = nil
|
||||
|
||||
ctx := t.Context()
|
||||
factory := &CodebaseDownloaderFactory{}
|
||||
downloader, err := factory.New(ctx, base.MigrateOptions{
|
||||
CloneAddr: u.String(),
|
||||
AuthUsername: apiUser,
|
||||
AuthPassword: apiPassword,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating Codebase downloader: %v", err)
|
||||
}
|
||||
downloader := NewCodebaseDownloader(ctx, projectURL, "gitea-test", "test", apiUser, apiPassword)
|
||||
downloader.baseURL, _ = url.Parse(mockServer.URL)
|
||||
|
||||
repo, err := downloader.GetRepoInfo(ctx)
|
||||
assert.NoError(t, err)
|
||||
assertRepositoryEqual(t, &base.Repository{
|
||||
@@ -144,6 +139,6 @@ func TestCodebaseDownloadRepo(t *testing.T) {
|
||||
}, prs)
|
||||
|
||||
rvs, err := downloader.GetReviews(ctx, prs[0])
|
||||
assert.NoError(t, err)
|
||||
assert.Error(t, err)
|
||||
assert.Empty(t, rvs)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user