initial working read only state
This commit is contained in:
98
auth_test.go
Normal file
98
auth_test.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestResolveTokenPrefersEnvironment(t *testing.T) {
|
||||
called := false
|
||||
token, err := resolveTokenWith(
|
||||
context.Background(),
|
||||
"https://api.github.com/graphql",
|
||||
func(name string) string {
|
||||
if name == "GH_TOKEN" {
|
||||
return "from-environment"
|
||||
}
|
||||
return ""
|
||||
},
|
||||
func(context.Context, string, ...string) ([]byte, error) {
|
||||
called = true
|
||||
return nil, nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if token != "from-environment" {
|
||||
t.Fatalf("token = %q", token)
|
||||
}
|
||||
if called {
|
||||
t.Fatal("gh was called even though an environment token was available")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveTokenFallsBackToActiveGHAccount(t *testing.T) {
|
||||
var command string
|
||||
var args []string
|
||||
token, err := resolveTokenWith(
|
||||
context.Background(),
|
||||
"https://api.github.com/graphql",
|
||||
func(string) string { return "" },
|
||||
func(_ context.Context, name string, values ...string) ([]byte, error) {
|
||||
command, args = name, values
|
||||
return []byte("from-gh\n"), nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if token != "from-gh" {
|
||||
t.Fatalf("token = %q", token)
|
||||
}
|
||||
if command != "gh" || !reflect.DeepEqual(args, []string{"auth", "token", "--hostname", "github.com"}) {
|
||||
t.Fatalf("command = %q, args = %#v", command, args)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveTokenUsesEnterpriseHostname(t *testing.T) {
|
||||
var args []string
|
||||
_, err := resolveTokenWith(
|
||||
context.Background(),
|
||||
"https://github.example.com/api/graphql",
|
||||
func(string) string { return "" },
|
||||
func(_ context.Context, _ string, values ...string) ([]byte, error) {
|
||||
args = values
|
||||
return []byte("token"), nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(args, []string{"auth", "token", "--hostname", "github.example.com"}) {
|
||||
t.Fatalf("args = %#v", args)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveTokenExplainsGHAuthenticationFailure(t *testing.T) {
|
||||
_, err := resolveTokenWith(
|
||||
context.Background(),
|
||||
"https://api.github.com/graphql",
|
||||
func(string) string { return "" },
|
||||
func(context.Context, string, ...string) ([]byte, error) {
|
||||
return nil, errors.New("exit status 4")
|
||||
},
|
||||
)
|
||||
if err == nil || !strings.Contains(err.Error(), "gh auth login --hostname github.com") {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGitHubHostRejectsInvalidEndpoint(t *testing.T) {
|
||||
if _, err := githubHost("not-a-url"); err == nil {
|
||||
t.Fatal("expected invalid endpoint error")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user