134 lines
3.9 KiB
Go
134 lines
3.9 KiB
Go
package main
|
|
|
|
// gitlab_extra_test.go — error taxonomy, insecure client seam, decode failures.
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestGitLabErrorType(t *testing.T) {
|
|
err := &GitLabError{msg: "upstream sad"}
|
|
if err.Error() != "upstream sad" {
|
|
t.Fatalf("GitLabError.Error = %q", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestNewGitLabWithClientInsecure(t *testing.T) {
|
|
store := openTestStore(t)
|
|
gl := NewGitLabWithClient(store, "https://gl.example/", true)
|
|
if !gl.insecure {
|
|
t.Fatal("insecure flag not set")
|
|
}
|
|
hc := gl.httpClient()
|
|
if hc.Transport == nil {
|
|
t.Fatal("insecure client must install a custom transport")
|
|
}
|
|
plain := NewGitLab(store, "https://gl.example/")
|
|
if plain.httpClient().Transport != nil {
|
|
t.Fatal("default client must not override the transport")
|
|
}
|
|
if !strings.HasSuffix(gl.baseURL, "example") || strings.HasSuffix(gl.baseURL, "/") {
|
|
t.Fatalf("baseURL trailing slash not trimmed: %q", gl.baseURL)
|
|
}
|
|
}
|
|
|
|
func TestGitLabConnectDecodeAndEmptyUser(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
body string
|
|
want string
|
|
}{
|
|
{"decode-failure", "not json", "decode"},
|
|
{"empty-username", `{}`, "no username"},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(tc.body))
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
gl := NewGitLab(openTestStore(t), up.URL)
|
|
_, err := gl.Connect(context.Background(), "pat")
|
|
if err == nil || !strings.Contains(err.Error(), tc.want) {
|
|
t.Fatalf("Connect = %v, want containing %q", err, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGitLabRequestFailures(t *testing.T) {
|
|
store := openTestStore(t)
|
|
|
|
// unparseable base URL → request construction error
|
|
gl := NewGitLab(store, "https://gl.example/\n")
|
|
if _, err := gl.Connect(context.Background(), "pat"); err == nil {
|
|
t.Fatal("connect against unparseable URL must fail")
|
|
}
|
|
|
|
// transport failure (cancelled context) wraps into GitLabError
|
|
gl2 := NewGitLab(store, "https://gl.example")
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
_, err := gl2.Connect(ctx, "pat")
|
|
if err == nil || !strings.Contains(err.Error(), "gitlab request failed") {
|
|
t.Fatalf("connect with dead context = %v, want GitLabError", err)
|
|
}
|
|
|
|
// store failures surface from token() and Disconnect
|
|
dead := openTestStore(t)
|
|
_ = dead.Close()
|
|
gl3 := NewGitLab(dead, "https://gl.example")
|
|
if _, err := gl3.Repos(context.Background()); err == nil || !strings.Contains(err.Error(), "closed") {
|
|
t.Fatalf("repos with closed store = %v", err)
|
|
}
|
|
if err := gl3.Disconnect(); err == nil {
|
|
t.Fatal("disconnect with closed store must fail")
|
|
}
|
|
}
|
|
|
|
func TestGitLabStatusStoreFailure(t *testing.T) {
|
|
dead := openTestStore(t)
|
|
_ = dead.Close()
|
|
gl := NewGitLab(dead, "https://gl.example")
|
|
status := gl.Status()
|
|
if status["connected"] != false {
|
|
t.Fatalf("status with broken store = %v, want connected=false", status)
|
|
}
|
|
}
|
|
|
|
func TestGitLabConnectStoreFailures(t *testing.T) {
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(`{"username":"alice"}`))
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
dead := openTestStore(t)
|
|
_ = dead.Close()
|
|
gl := NewGitLab(dead, up.URL)
|
|
if _, err := gl.Connect(context.Background(), "pat"); err == nil {
|
|
t.Fatal("Connect with closed store must fail at SetSetting")
|
|
}
|
|
}
|
|
|
|
func TestGitLabReposSkipsEmptyPaths(t *testing.T) {
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(`[{"path_with_namespace":"","name":"X"}]`))
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
store := openTestStore(t)
|
|
gl := NewGitLab(store, up.URL)
|
|
if err := store.SetSetting(settingGitLabToken, "pat"); err != nil {
|
|
t.Fatalf("set token: %v", err)
|
|
}
|
|
repos, err := gl.Repos(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("repos: %v", err)
|
|
}
|
|
if len(repos) != 0 {
|
|
t.Fatalf("repos = %+v, want empty (blank path skipped)", repos)
|
|
}
|
|
}
|