maidn-cli/cmd/e2e_mutate_test.go

47 lines
2 KiB
Go

package cmd
import (
"io"
"net/http"
"strings"
"testing"
"github.com/Pingu-Studio/MaidnCLI/internal/e2emutate"
"github.com/spf13/cobra"
)
type commandMutationHTTP struct {
calls []*http.Request
}
func (f *commandMutationHTTP) Do(request *http.Request) (*http.Response, error) {
f.calls = append(f.calls, request)
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader("")), Header: make(http.Header)}, nil
}
func TestE2EMutateCommandWiringUsesOnlyTokenReferences(t *testing.T) {
originalMutator := e2eMutator
originalURL, originalOwner, originalRepo, originalBranch, originalSHA := e2eMutateForgejoURL, e2eMutateOwner, e2eMutateRepo, e2eMutateBranch, e2eMutateSHA
originalEnv, originalFile, originalOpenPR := e2eMutateTokenEnv, e2eMutateTokenFile, e2eMutateOpenPR
t.Cleanup(func() {
e2eMutator = originalMutator
e2eMutateForgejoURL, e2eMutateOwner, e2eMutateRepo, e2eMutateBranch, e2eMutateSHA = originalURL, originalOwner, originalRepo, originalBranch, originalSHA
e2eMutateTokenEnv, e2eMutateTokenFile, e2eMutateOpenPR = originalEnv, originalFile, originalOpenPR
})
command, _, err := rootCmd.Find([]string{"e2e-mutate"})
if err != nil || command != e2eMutateCmd || command.Flags().Lookup("token") != nil {
t.Fatalf("e2e-mutate command or token flags are not wired safely: %v", err)
}
fake := &commandMutationHTTP{}
e2eMutator = func() e2emutate.Mutator { return e2emutate.Mutator{HTTP: fake} }
e2eMutateForgejoURL, e2eMutateOwner = "https://git.example.test", "test-org-2"
e2eMutateRepo, e2eMutateBranch = "maidn-e2e-repo", "maidn-e2e-branch"
e2eMutateSHA = "0123456789abcdef0123456789abcdef01234567"
e2eMutateTokenEnv, e2eMutateTokenFile, e2eMutateOpenPR = "E2E_MUTATE_TEST_TOKEN", "", false
t.Setenv(e2eMutateTokenEnv, "test-token")
if err := runE2EMutate(&cobra.Command{}, nil); err != nil || len(fake.calls) != 1 || fake.calls[0].Method != http.MethodPatch {
t.Fatalf("runE2EMutate() = %v, calls = %#v", err, fake.calls)
}
}