forked from chroe/gitlink-cli
139 lines
3.8 KiB
Go
Executable File
139 lines
3.8 KiB
Go
Executable File
package org
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gitlink-org/gitlink-cli/internal/client"
|
|
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
|
)
|
|
|
|
func TestOrgList(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "GET" && r.URL.Path == "/organizations.json" {
|
|
writeJSON(t, w, []map[string]interface{}{
|
|
{"id": float64(1), "name": "org1"},
|
|
{"id": float64(2), "name": "org2"},
|
|
})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runOrgShortcut(t, server, "list", map[string]string{})
|
|
if err != nil {
|
|
t.Fatalf("org list failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestOrgInfo(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "GET" && r.URL.Path == "/organizations/1.json" {
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"id": float64(1), "name": "myorg", "description": "A test org",
|
|
})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runOrgShortcut(t, server, "info", map[string]string{"id": "1"})
|
|
if err != nil {
|
|
t.Fatalf("org info failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestOrgMembers(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "GET" && r.URL.Path == "/organizations/1/organization_users.json" {
|
|
writeJSON(t, w, []map[string]interface{}{
|
|
{"id": float64(1), "login": "user1", "role": "admin"},
|
|
{"id": float64(2), "login": "user2", "role": "member"},
|
|
})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runOrgShortcut(t, server, "members", map[string]string{"id": "1"})
|
|
if err != nil {
|
|
t.Fatalf("org members failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestOrgCreate(t *testing.T) {
|
|
var createPayload map[string]interface{}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "POST" && r.URL.Path == "/organizations.json" {
|
|
createPayload = decodeJSON(t, r)
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"id": float64(99), "name": "new-org",
|
|
})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runOrgShortcut(t, server, "create", map[string]string{
|
|
"name": "new-org",
|
|
"description": "My new organization",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("org create failed: %v", err)
|
|
}
|
|
|
|
if createPayload["name"] != "new-org" {
|
|
t.Fatalf("expected name=new-org, got %v", createPayload["name"])
|
|
}
|
|
if createPayload["description"] != "My new organization" {
|
|
t.Fatalf("expected description='My new organization', got %v", createPayload["description"])
|
|
}
|
|
}
|
|
|
|
func runOrgShortcut(t *testing.T, server *httptest.Server, name string, args map[string]string) error {
|
|
t.Helper()
|
|
shortcut := findOrgShortcut(t, name)
|
|
ctx := &common.RuntimeContext{
|
|
Client: &client.Client{HTTP: server.Client(), BaseURL: server.URL},
|
|
Owner: "owner",
|
|
Repo: "repo",
|
|
Format: "json",
|
|
Args: args,
|
|
}
|
|
return shortcut.Run(ctx)
|
|
}
|
|
|
|
func findOrgShortcut(t *testing.T, name string) *common.Shortcut {
|
|
t.Helper()
|
|
for _, s := range Shortcuts() {
|
|
if s.Name == name {
|
|
return s
|
|
}
|
|
}
|
|
t.Fatalf("shortcut %q not found", name)
|
|
return nil
|
|
}
|
|
|
|
func decodeJSON(t *testing.T, r *http.Request) map[string]interface{} {
|
|
t.Helper()
|
|
var payload map[string]interface{}
|
|
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
|
t.Fatalf("failed to decode request body: %v", err)
|
|
}
|
|
return payload
|
|
}
|
|
|
|
func writeJSON(t *testing.T, w http.ResponseWriter, payload interface{}) {
|
|
t.Helper()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(payload); err != nil {
|
|
t.Fatalf("failed to write response: %v", err)
|
|
}
|
|
}
|