Files
anonymous_github/backend/internal/server/providers_test.go
T

66 lines
2.4 KiB
Go

package server
import (
"context"
"errors"
"net/http/httptest"
"strings"
"testing"
)
func TestProviderResolution(t *testing.T) {
for _, c := range []struct {
source M
want string
fail bool
}{{M{}, "github", false}, {M{"type": "GitHubStream"}, "github", false}, {M{"type": "GitHubDownload"}, "github", false}, {M{"type": "Zip"}, "archive", false}, {M{"type": "Provider", "provider": "fixture"}, "fixture", false}, {M{"type": "Other"}, "", true}} {
got, e := sourceProvider(c.source)
if got != c.want || (e != nil) != c.fail {
t.Fatalf("%v: %s %v", c.source, got, e)
}
}
s := &Server{}
if _, e := s.providerAccess(M{"source": M{"provider": "fixture"}, "providerAccess": M{"provider": "github"}}); e == nil {
t.Fatal("accepted mismatched binding")
}
}
func TestResourceStatusErrors(t *testing.T) {
s := &Server{}
for _, c := range []struct {
kind, status, message, code string
http int
}{
{"repo", "queue", "rate_limited:1790000000000", "rate_limited", 425},
{"repo", "error", "", "repository_not_accessible", 500},
{"repo", "error", "repository_access_revoked", "repository_access_revoked", 500},
{"repo", "error", "repo_not_found", "repo_not_found", 404},
{"repo", "error", "branch_not_found", "branch_not_found", 404},
{"repo", "error", "commit_not_found", "commit_not_found", 404},
{"repo", "error", "repo_not_accessible", "repo_not_accessible", 403},
{"repo", "error", "repository_not_accessible", "repository_not_accessible", 403},
{"repo", "error", "token_expired", "token_expired", 401},
{"repo", "error", "not_connected", "not_connected", 401},
{"repo", "error", "github_rate_limit_exceeded", "github_rate_limit_exceeded", 429},
{"repo", "error", "github_unavailable", "github_unavailable", 502},
{"repo", "error", "repo_empty", "repo_empty", 409},
{"repo", "error", "quota_exceeded", "quota_exceeded", 402},
{"gist", "error", "", "gist_not_available", 500},
{"pr", "error", "", "pull_request_not_available", 500},
{"repo", "removed", "", "repository_expired", 410},
} {
e := s.checkResource(context.Background(), c.kind, M{"status": c.status, "statusMessage": c.message})
var api *APIError
if !errors.As(e, &api) || api.Code != c.code || api.Status != c.http {
t.Fatalf("%+v: %v", c, e)
}
if c.code == "rate_limited" {
w := httptest.NewRecorder()
writeError(w, e)
if !strings.Contains(w.Body.String(), `"resetAt":1790000000000`) {
t.Fatal(w.Body)
}
}
}
}