717 lines
21 KiB
Go
717 lines
21 KiB
Go
package forge
|
|
|
|
import (
|
|
"context"
|
|
json "github.com/goccy/go-json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"dappco.re/go/core/forge/types"
|
|
)
|
|
|
|
func TestRepoService_ListTopics_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("expected GET, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/topics" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode(types.TopicName{TopicNames: []string{"go", "forge"}})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
topics, err := f.Repos.ListTopics(context.Background(), "core", "go-forge")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(topics, []string{"go", "forge"}) {
|
|
t.Fatalf("got %#v", topics)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_UpdateTopics_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPut {
|
|
t.Errorf("expected PUT, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/topics" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
var body types.RepoTopicOptions
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Errorf("decode body: %v", err)
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(body.Topics, []string{"go", "forge"}) {
|
|
t.Fatalf("got %#v", body.Topics)
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
if err := f.Repos.UpdateTopics(context.Background(), "core", "go-forge", []string{"go", "forge"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_AddTopic_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPut {
|
|
t.Errorf("expected PUT, got %s", r.Method)
|
|
}
|
|
if r.URL.EscapedPath() != "/api/v1/repos/core/go-forge/topics/release%20candidate" {
|
|
t.Errorf("wrong path: %s", r.URL.EscapedPath())
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
if err := f.Repos.AddTopic(context.Background(), "core", "go-forge", "release candidate"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_DeleteTopic_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodDelete {
|
|
t.Errorf("expected DELETE, got %s", r.Method)
|
|
}
|
|
if r.URL.EscapedPath() != "/api/v1/repos/core/go-forge/topics/release%20candidate" {
|
|
t.Errorf("wrong path: %s", r.URL.EscapedPath())
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
if err := f.Repos.DeleteTopic(context.Background(), "core", "go-forge", "release candidate"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_ListIssueTemplates_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("expected GET, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/issue_templates" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Header().Set("X-Total-Count", "1")
|
|
json.NewEncoder(w).Encode([]types.IssueTemplate{
|
|
{
|
|
Name: "bug report",
|
|
Title: "Bug report",
|
|
Content: "Describe the problem",
|
|
},
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
templates, err := f.Repos.ListIssueTemplates(context.Background(), "core", "go-forge")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(templates) != 1 {
|
|
t.Fatalf("got %d templates, want 1", len(templates))
|
|
}
|
|
if templates[0].Name != "bug report" || templates[0].Title != "Bug report" {
|
|
t.Fatalf("got %#v", templates[0])
|
|
}
|
|
}
|
|
|
|
func TestRepoService_GetNewPinAllowed_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("expected GET, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/new_pin_allowed" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode(types.NewIssuePinsAllowed{
|
|
Issues: true,
|
|
PullRequests: false,
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
result, err := f.Repos.GetNewPinAllowed(context.Background(), "core", "go-forge")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !result.Issues || result.PullRequests {
|
|
t.Fatalf("got %#v", result)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_UpdateAvatar_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
t.Errorf("expected POST, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/avatar" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
var body types.UpdateRepoAvatarOption
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Fatalf("decode body: %v", err)
|
|
}
|
|
if body.Image != "iVBORw0KGgoAAAANSUhEUg==" {
|
|
t.Fatalf("got image=%q", body.Image)
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
if err := f.Repos.UpdateAvatar(context.Background(), "core", "go-forge", &types.UpdateRepoAvatarOption{
|
|
Image: "iVBORw0KGgoAAAANSUhEUg==",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_DeleteAvatar_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodDelete {
|
|
t.Errorf("expected DELETE, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/avatar" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
if err := f.Repos.DeleteAvatar(context.Background(), "core", "go-forge"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_ListPushMirrors_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("expected GET, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/push_mirrors" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Header().Set("X-Total-Count", "2")
|
|
json.NewEncoder(w).Encode([]types.PushMirror{
|
|
{RemoteName: "mirror-a"},
|
|
{RemoteName: "mirror-b"},
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
mirrors, err := f.Repos.ListPushMirrors(context.Background(), "core", "go-forge")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(mirrors) != 2 || mirrors[0].RemoteName != "mirror-a" || mirrors[1].RemoteName != "mirror-b" {
|
|
t.Fatalf("got %#v", mirrors)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_GetPushMirror_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("expected GET, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/push_mirrors/mirror-a" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode(types.PushMirror{
|
|
RemoteName: "mirror-a",
|
|
RemoteAddress: "ssh://git@example.com/core/go-forge.git",
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
mirror, err := f.Repos.GetPushMirror(context.Background(), "core", "go-forge", "mirror-a")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if mirror.RemoteName != "mirror-a" {
|
|
t.Fatalf("got remote_name=%q", mirror.RemoteName)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_CreatePushMirror_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
t.Errorf("expected POST, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/push_mirrors" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
var body types.CreatePushMirrorOption
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Fatalf("decode body: %v", err)
|
|
}
|
|
if body.RemoteAddress != "ssh://git@example.com/core/go-forge.git" || !body.SyncOnCommit {
|
|
t.Fatalf("got %#v", body)
|
|
}
|
|
json.NewEncoder(w).Encode(types.PushMirror{
|
|
RemoteName: "mirror-a",
|
|
RemoteAddress: body.RemoteAddress,
|
|
SyncOnCommit: body.SyncOnCommit,
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
mirror, err := f.Repos.CreatePushMirror(context.Background(), "core", "go-forge", &types.CreatePushMirrorOption{
|
|
RemoteAddress: "ssh://git@example.com/core/go-forge.git",
|
|
RemoteUsername: "git",
|
|
RemotePassword: "secret",
|
|
Interval: "1h",
|
|
SyncOnCommit: true,
|
|
UseSSH: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if mirror.RemoteName != "mirror-a" || !mirror.SyncOnCommit {
|
|
t.Fatalf("got %#v", mirror)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_DeletePushMirror_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodDelete {
|
|
t.Errorf("expected DELETE, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/push_mirrors/mirror-a" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
if err := f.Repos.DeletePushMirror(context.Background(), "core", "go-forge", "mirror-a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_SyncPushMirrors_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
t.Errorf("expected POST, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/push_mirrors-sync" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
if err := f.Repos.SyncPushMirrors(context.Background(), "core", "go-forge"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_GetSubscription_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("expected GET, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/subscription" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode(types.WatchInfo{
|
|
Subscribed: true,
|
|
Ignored: false,
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
result, err := f.Repos.GetSubscription(context.Background(), "core", "go-forge")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !result.Subscribed || result.Ignored {
|
|
t.Fatalf("got %#v", result)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_ListStargazers_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("expected GET, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/stargazers" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode([]types.User{{UserName: "alice"}, {UserName: "bob"}})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
users, err := f.Repos.ListStargazers(context.Background(), "core", "go-forge")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(users) != 2 || users[0].UserName != "alice" || users[1].UserName != "bob" {
|
|
t.Fatalf("got %#v", users)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_ListSubscribers_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("expected GET, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/subscribers" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode([]types.User{{UserName: "charlie"}})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
users, err := f.Repos.ListSubscribers(context.Background(), "core", "go-forge")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(users) != 1 || users[0].UserName != "charlie" {
|
|
t.Fatalf("got %#v", users)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_GetSigningKey_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("expected GET, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/signing-key.gpg" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
w.Write([]byte("-----BEGIN PGP PUBLIC KEY BLOCK-----\n..."))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
key, err := f.Repos.GetSigningKey(context.Background(), "core", "go-forge")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := "-----BEGIN PGP PUBLIC KEY BLOCK-----\n..."
|
|
if key != want {
|
|
t.Fatalf("got %q, want %q", key, want)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_ListAssignees_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("expected GET, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/assignees" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode([]types.User{{UserName: "alice"}, {UserName: "bob"}})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
users, err := f.Repos.ListAssignees(context.Background(), "core", "go-forge")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(users) != 2 || users[0].UserName != "alice" || users[1].UserName != "bob" {
|
|
t.Fatalf("got %#v", users)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_IterAssignees_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("expected GET, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/assignees" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Header().Set("X-Total-Count", "2")
|
|
json.NewEncoder(w).Encode([]types.User{{UserName: "alice"}, {UserName: "bob"}})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
var names []string
|
|
for user, err := range f.Repos.IterAssignees(context.Background(), "core", "go-forge") {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
names = append(names, user.UserName)
|
|
}
|
|
if len(names) != 2 || names[0] != "alice" || names[1] != "bob" {
|
|
t.Fatalf("got %#v", names)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_ListCollaborators_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("expected GET, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/collaborators" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode([]types.User{{UserName: "alice"}})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
users, err := f.Repos.ListCollaborators(context.Background(), "core", "go-forge")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(users) != 1 || users[0].UserName != "alice" {
|
|
t.Fatalf("got %#v", users)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_AddCollaborator_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPut {
|
|
t.Errorf("expected PUT, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/collaborators/alice" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
var body types.AddCollaboratorOption
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Fatalf("decode body: %v", err)
|
|
}
|
|
if body.Permission != "write" {
|
|
t.Fatalf("got permission=%q, want %q", body.Permission, "write")
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
if err := f.Repos.AddCollaborator(context.Background(), "core", "go-forge", "alice", &types.AddCollaboratorOption{Permission: "write"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_DeleteCollaborator_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodDelete {
|
|
t.Errorf("expected DELETE, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/collaborators/alice" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
if err := f.Repos.DeleteCollaborator(context.Background(), "core", "go-forge", "alice"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_CheckCollaborator_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("expected GET, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/collaborators/alice" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
ok, err := f.Repos.CheckCollaborator(context.Background(), "core", "go-forge", "alice")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !ok {
|
|
t.Fatal("expected collaborator check to return true")
|
|
}
|
|
}
|
|
|
|
func TestRepoService_GetCollaboratorPermission_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("expected GET, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/collaborators/alice/permission" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode(types.RepoCollaboratorPermission{
|
|
Permission: "write",
|
|
RoleName: "collaborator",
|
|
User: &types.User{UserName: "alice"},
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
perm, err := f.Repos.GetCollaboratorPermission(context.Background(), "core", "go-forge", "alice")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if perm.Permission != "write" || perm.User == nil || perm.User.UserName != "alice" {
|
|
t.Fatalf("got %#v", perm)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_Watch_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPut {
|
|
t.Errorf("expected PUT, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/subscription" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode(types.WatchInfo{
|
|
Subscribed: true,
|
|
Ignored: false,
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
result, err := f.Repos.Watch(context.Background(), "core", "go-forge")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !result.Subscribed || result.Ignored {
|
|
t.Fatalf("got %#v", result)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_Unwatch_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodDelete {
|
|
t.Errorf("expected DELETE, got %s", r.Method)
|
|
}
|
|
if r.URL.Path != "/api/v1/repos/core/go-forge/subscription" {
|
|
t.Errorf("wrong path: %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
if err := f.Repos.Unwatch(context.Background(), "core", "go-forge"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestRepoService_PathParamsAreEscaped_Good(t *testing.T) {
|
|
owner := "acme org"
|
|
repo := "my/repo"
|
|
org := "team alpha"
|
|
|
|
t.Run("ListOrgRepos", func(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.EscapedPath() != "/api/v1/orgs/team%20alpha/repos" {
|
|
t.Errorf("got path %q, want %q", r.URL.EscapedPath(), "/api/v1/orgs/team%20alpha/repos")
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode([]types.Repository{})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
_, err := f.Repos.ListOrgRepos(context.Background(), org)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
|
|
t.Run("Fork", func(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
want := "/api/v1/repos/acme%20org/my%2Frepo/forks"
|
|
if r.URL.EscapedPath() != want {
|
|
t.Errorf("got path %q, want %q", r.URL.EscapedPath(), want)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode(types.Repository{Name: repo})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := NewForge(srv.URL, "tok")
|
|
if _, err := f.Repos.Fork(context.Background(), owner, repo, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
}
|