Some checks failed
ci / cargo test (abacus) (push) Has been cancelled
ci / cargo test (agent-runtime) (push) Has been cancelled
ci / cargo test (context-passport) (push) Has been cancelled
ci / cargo test (game-economy) (push) Has been cancelled
ci / cargo test (ledgerline) (push) Has been cancelled
ci / cargo test (mint) (push) Has been cancelled
ci / cargo test (rago) (push) Has been cancelled
ci / cargo test (relay) (push) Has been cancelled
ci / node suites (push) Has been cancelled
ci / cargo test (toolgate) (push) Has been cancelled
ci / cargo test (turnstile) (push) Has been cancelled
ci / cargo test (vend) (push) Has been cancelled
apps/gitea -> apps/rithub-server; compose services/containers renamed (rithub-server-app/postgres/act-runner) with explicit project name and volume names pinned to the exact existing Docker volumes (no data loss); our own GITEA_* compose/env var names -> RIT_*; new rit-admin CLI wrapper on railyard-box; giteaSession.mjs/giteaOAuth.mjs/giteaInfo.mjs -> ritSession/ritOAuth/ritInfo across 6 apps; GiteaForgeAdapter -> RitForgeAdapter + giteaSyncService/importGiteaRepos/syncGiteaRepos -> rit* in ritgram (tsc clean); apps/console/lib/gitea.ts -> lib/ritProvision.ts; Go internal/giteaclient -> internal/ritclient in rithub-async-import (go build/test clean); new /auth/rit + /forges/rit/connect routes with /gitea back-compat aliases; locale/swagger/footer/captcha template text scrubbed of remaining "Gitea" strings. Deployed to railyard-box: old containers stopped/removed, /srv/railyard-gitea renamed to /srv/rithub-server with a symlink kept, docker compose up -d from the new path, act_runner network-alias fix for its persisted instance URL. Live-verified: docker compose config diff showed zero unintended value changes, DB row counts unchanged (19 users, 635 repos), full route regression 200/expected, 0 "gitea" in headers/bodies on every checked page, real git clone+push over HTTPS and SSH with a disposable account (deleted after). Deferred on purpose (documented in plans/railyard.md): the forge discriminator / DB column names / Postgres role+volume names (data migrations, not a naming change) and the upstream GITEA__ env-to-ini keys and X-Gitea-* wire headers (hardcoded in the vendored engine binary). See plans/railyard.md RIT-OPS/RIT-BRD entry for the full old->new map and live evidence. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
228 lines
7.7 KiB
Go
228 lines
7.7 KiB
Go
package migrate
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/caelum0x/railyard/apps/rithub-async-import/internal/ritclient"
|
|
"github.com/caelum0x/railyard/apps/rithub-async-import/internal/jobs"
|
|
)
|
|
|
|
// fakeUpstream is a RepoCreator test double — no network, fully scripted.
|
|
type fakeUpstream struct {
|
|
authUser func(ctx context.Context, token string) (*ritclient.User, error)
|
|
createRepo func(ctx context.Context, token, owner string, opts ritclient.CreateRepoOptions, isOrg bool) (*ritclient.Repo, error)
|
|
updateDesc func(ctx context.Context, token, owner, name, description string) error
|
|
updateCalled bool
|
|
}
|
|
|
|
func (f *fakeUpstream) AuthenticatedUser(ctx context.Context, token string) (*ritclient.User, error) {
|
|
return f.authUser(ctx, token)
|
|
}
|
|
|
|
func (f *fakeUpstream) CreateEmptyRepo(ctx context.Context, token, owner string, opts ritclient.CreateRepoOptions, isOrg bool) (*ritclient.Repo, error) {
|
|
return f.createRepo(ctx, token, owner, opts, isOrg)
|
|
}
|
|
|
|
func (f *fakeUpstream) UpdateRepoDescription(ctx context.Context, token, owner, name, description string) error {
|
|
f.updateCalled = true
|
|
if f.updateDesc != nil {
|
|
return f.updateDesc(ctx, token, owner, name, description)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func newTestStore(t *testing.T) *jobs.Store {
|
|
t.Helper()
|
|
store, err := jobs.NewStore("") // in-memory, no persistence needed for tests
|
|
if err != nil {
|
|
t.Fatalf("jobs.NewStore: %v", err)
|
|
}
|
|
return store
|
|
}
|
|
|
|
func TestServeMigrate_SyncPassthrough_DefaultsForwardUnchanged(t *testing.T) {
|
|
var gotBody []byte
|
|
var gotAuth string
|
|
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotAuth = r.Header.Get("Authorization")
|
|
gotBody, _ = io.ReadAll(r.Body)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusCreated)
|
|
_, _ = w.Write([]byte(`{"full_name":"octocat/Hello-World"}`))
|
|
}))
|
|
defer upstream.Close()
|
|
|
|
h := &Handler{UpstreamBase: upstream.URL, Store: newTestStore(t)}
|
|
reqBody := []byte(`{"clone_addr":"https://github.com/octocat/Hello-World.git","repo_name":"Hello-World"}`)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/repos/migrate", bytes.NewReader(reqBody))
|
|
req.Header.Set("Authorization", "token sync-caller-token")
|
|
rec := httptest.NewRecorder()
|
|
|
|
h.ServeMigrate(rec, req)
|
|
|
|
if rec.Code != http.StatusCreated {
|
|
t.Fatalf("status = %d, want %d; body=%s", rec.Code, http.StatusCreated, rec.Body.String())
|
|
}
|
|
if rec.Body.String() != `{"full_name":"octocat/Hello-World"}` {
|
|
t.Fatalf("relayed body = %q", rec.Body.String())
|
|
}
|
|
if gotAuth != "token sync-caller-token" {
|
|
t.Fatalf("upstream saw Authorization = %q", gotAuth)
|
|
}
|
|
if string(gotBody) != string(reqBody) {
|
|
t.Fatalf("upstream saw body = %q, want %q", gotBody, reqBody)
|
|
}
|
|
}
|
|
|
|
func TestServeMigrate_Async_HappyPath(t *testing.T) {
|
|
store := newTestStore(t)
|
|
upstream := &fakeUpstream{
|
|
authUser: func(ctx context.Context, token string) (*ritclient.User, error) {
|
|
if token != "caller-token" {
|
|
t.Fatalf("AuthenticatedUser got token %q", token)
|
|
}
|
|
return &ritclient.User{Login: "caelum0x"}, nil
|
|
},
|
|
createRepo: func(ctx context.Context, token, owner string, opts ritclient.CreateRepoOptions, isOrg bool) (*ritclient.Repo, error) {
|
|
if isOrg {
|
|
t.Fatalf("expected a user repo, not org")
|
|
}
|
|
if opts.AutoInit {
|
|
t.Fatalf("placeholder repo must not auto-init")
|
|
}
|
|
if opts.Description != "[migrating] " {
|
|
t.Fatalf("description marker = %q", opts.Description)
|
|
}
|
|
return &ritclient.Repo{FullName: owner + "/" + opts.Name, HTMLURL: "https://rithub.dev/" + owner + "/" + opts.Name}, nil
|
|
},
|
|
}
|
|
|
|
workerDone := make(chan jobs.Job, 1)
|
|
h := &Handler{
|
|
Upstream: upstream,
|
|
Store: store,
|
|
Worker: func(ctx context.Context, job jobs.Job, req Request, token string) {
|
|
_ = store.Update(job.ID, func(j *jobs.Job) { j.Status = jobs.StatusDone })
|
|
final, _ := store.Get(job.ID)
|
|
workerDone <- final
|
|
},
|
|
}
|
|
|
|
reqBody := []byte(`{"clone_addr":"https://github.com/octocat/Hello-World.git","repo_name":"Hello-World","async":true}`)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/repos/migrate", bytes.NewReader(reqBody))
|
|
req.Header.Set("Authorization", "token caller-token")
|
|
rec := httptest.NewRecorder()
|
|
|
|
h.ServeMigrate(rec, req)
|
|
|
|
if rec.Code != http.StatusAccepted {
|
|
t.Fatalf("status = %d, want 202; body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
var resp map[string]any
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("decoding response: %v", err)
|
|
}
|
|
for _, field := range []string{"id", "status", "repo_owner", "repo_name", "html_url", "status_url"} {
|
|
if _, ok := resp[field]; !ok {
|
|
t.Fatalf("response missing field %q: %v", field, resp)
|
|
}
|
|
}
|
|
if resp["status"] != string(jobs.StatusCloning) {
|
|
t.Fatalf("initial status = %v, want %q", resp["status"], jobs.StatusCloning)
|
|
}
|
|
id := resp["id"].(string)
|
|
|
|
select {
|
|
case <-workerDone:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("worker never ran")
|
|
}
|
|
|
|
statusReq := httptest.NewRequest(http.MethodGet, "/api/v1/repos/migrate/status/"+id, nil)
|
|
statusReq.SetPathValue("id", id)
|
|
statusRec := httptest.NewRecorder()
|
|
h.ServeStatus(statusRec, statusReq)
|
|
|
|
if statusRec.Code != http.StatusOK {
|
|
t.Fatalf("status endpoint = %d, body=%s", statusRec.Code, statusRec.Body.String())
|
|
}
|
|
var job jobs.Job
|
|
if err := json.Unmarshal(statusRec.Body.Bytes(), &job); err != nil {
|
|
t.Fatalf("decoding job: %v", err)
|
|
}
|
|
if job.Status != jobs.StatusDone {
|
|
t.Fatalf("final status = %q, want %q", job.Status, jobs.StatusDone)
|
|
}
|
|
}
|
|
|
|
func TestServeMigrate_Async_RejectsMirror(t *testing.T) {
|
|
h := &Handler{Store: newTestStore(t)}
|
|
reqBody := []byte(`{"clone_addr":"https://github.com/octocat/Hello-World.git","repo_name":"hi","async":true,"mirror":true}`)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/repos/migrate", bytes.NewReader(reqBody))
|
|
req.Header.Set("Authorization", "token t")
|
|
rec := httptest.NewRecorder()
|
|
|
|
h.ServeMigrate(rec, req)
|
|
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want 400; body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestServeMigrate_Async_RequiresAuthHeader(t *testing.T) {
|
|
h := &Handler{Store: newTestStore(t)}
|
|
reqBody := []byte(`{"clone_addr":"https://github.com/octocat/Hello-World.git","repo_name":"hi","async":true}`)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/repos/migrate", bytes.NewReader(reqBody))
|
|
rec := httptest.NewRecorder()
|
|
|
|
h.ServeMigrate(rec, req)
|
|
|
|
if rec.Code != http.StatusUnauthorized {
|
|
t.Fatalf("status = %d, want 401; body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestServeMigrate_Async_RelaysUpstreamAuthFailure(t *testing.T) {
|
|
h := &Handler{
|
|
Store: newTestStore(t),
|
|
Upstream: &fakeUpstream{
|
|
authUser: func(ctx context.Context, token string) (*ritclient.User, error) {
|
|
return nil, &ritclient.APIError{Status: http.StatusUnauthorized, Body: []byte(`{"message":"bad credentials"}`)}
|
|
},
|
|
},
|
|
}
|
|
reqBody := []byte(`{"clone_addr":"https://github.com/octocat/Hello-World.git","repo_name":"hi","async":true}`)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/repos/migrate", bytes.NewReader(reqBody))
|
|
req.Header.Set("Authorization", "token bad-token")
|
|
rec := httptest.NewRecorder()
|
|
|
|
h.ServeMigrate(rec, req)
|
|
|
|
if rec.Code != http.StatusUnauthorized {
|
|
t.Fatalf("status = %d, want 401; body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
if rec.Body.String() != `{"message":"bad credentials"}` {
|
|
t.Fatalf("body = %q, want upstream body relayed verbatim", rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestServeStatus_NotFound(t *testing.T) {
|
|
h := &Handler{Store: newTestStore(t)}
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/repos/migrate/status/does-not-exist", nil)
|
|
req.SetPathValue("id", "does-not-exist")
|
|
rec := httptest.NewRecorder()
|
|
|
|
h.ServeStatus(rec, req)
|
|
|
|
if rec.Code != http.StatusNotFound {
|
|
t.Fatalf("status = %d, want 404", rec.Code)
|
|
}
|
|
}
|