gitlink-cli/shortcuts/user/user_test.go

66 lines
1.7 KiB
Go

package user
import (
"net/http"
"testing"
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
)
func TestUserMe(t *testing.T) {
server := common.NewTestServer(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && r.URL.Path == "/users/me.json" {
common.WriteJSON(t, w, map[string]interface{}{
"login": "alice",
"user_id": float64(42),
"name": "Alice",
})
} else {
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
}
})
defer server.Close()
ctx := common.NewTestContext(t, server, "", "", map[string]string{})
err := common.RunShortcut(t, Shortcuts(), "me", ctx)
if err != nil {
t.Fatalf("me failed: %v", err)
}
}
func TestUserInfo(t *testing.T) {
server := common.NewTestServer(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && r.URL.Path == "/users/bob.json" {
common.WriteJSON(t, w, map[string]interface{}{
"login": "bob",
"user_id": float64(7),
"name": "Bob",
})
} else {
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
}
})
defer server.Close()
ctx := common.NewTestContext(t, server, "", "", map[string]string{
"login": "bob",
})
err := common.RunShortcut(t, Shortcuts(), "info", ctx)
if err != nil {
t.Fatalf("info failed: %v", err)
}
}
func TestUserInfoMissingLogin(t *testing.T) {
server := common.NewTestServer(t, func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("no request should be made: %s %s", r.Method, r.URL.Path)
})
defer server.Close()
ctx := common.NewTestContext(t, server, "", "", map[string]string{})
err := common.RunShortcut(t, Shortcuts(), "info", ctx)
if err == nil {
t.Fatal("expected error for missing --login")
}
}