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:
@@ -4,32 +4,28 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"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 TestGogsDownloadRepo(t *testing.T) {
|
||||
// Skip tests if Gogs token is not found
|
||||
gogsPersonalAccessToken := os.Getenv("GOGS_READ_TOKEN")
|
||||
if len(gogsPersonalAccessToken) == 0 {
|
||||
t.Skip("skipped test because GOGS_READ_TOKEN was not in the environment")
|
||||
}
|
||||
token := os.Getenv("GOGS_READ_TOKEN")
|
||||
liveMode := token != ""
|
||||
|
||||
_, callerFile, _, _ := runtime.Caller(0)
|
||||
fixtureDir := filepath.Join(filepath.Dir(callerFile), "_mock_data/TestGogsDownloadRepo")
|
||||
mockServer := unittest.NewMockWebServer(t, "https://try.gogs.io", fixtureDir, liveMode)
|
||||
|
||||
resp, err := http.Get("https://try.gogs.io/lunnytest/TESTREPO")
|
||||
if err != nil || resp.StatusCode/100 != 2 {
|
||||
// skip and don't run test
|
||||
t.Skipf("visit test repo failed, ignored")
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
ctx := t.Context()
|
||||
downloader := NewGogsDownloader(ctx, "https://try.gogs.io", "", "", gogsPersonalAccessToken, "lunnytest", "TESTREPO")
|
||||
downloader := NewGogsDownloader(ctx, mockServer.URL, "", "", token, "lunnytest", "TESTREPO")
|
||||
repo, err := downloader.GetRepoInfo(ctx)
|
||||
assert.NoError(t, err)
|
||||
|
||||
@@ -37,8 +33,8 @@ func TestGogsDownloadRepo(t *testing.T) {
|
||||
Name: "TESTREPO",
|
||||
Owner: "lunnytest",
|
||||
Description: "",
|
||||
CloneURL: "https://try.gogs.io/lunnytest/TESTREPO.git",
|
||||
OriginalURL: "https://try.gogs.io/lunnytest/TESTREPO",
|
||||
CloneURL: mockServer.URL + "/lunnytest/TESTREPO.git",
|
||||
OriginalURL: mockServer.URL + "/lunnytest/TESTREPO",
|
||||
DefaultBranch: "master",
|
||||
}, repo)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user