Skip to content
quarry Artifact [ae0132e0dc]
Login

Artifact [ae0132e0dc]

Artifact ae0132e0dc4a2ac043465ba17efe1e537c835973976e199e049ddb3f2b3dca66:


package ops

import (
	"reflect"
	"testing"
)

type askIn struct {
	Name     string `json:"name"`
	Role     string `json:"role"`
	Days     int    `json:"days"`
	Password string `json:"password"`
	Value    string `json:"value" secret:"once"`
}

// A sentence can only name fields the input has and that hold no
// credential.
func TestAskSentencesCannotCarryASecret(t *testing.T) {
	in := reflect.TypeOf(askIn{})
	for ask, ok := range map[string]bool{
		"Create agent {name}":   true,
		"no fields at all":      true,
		"Give {name} {nothing}": false,
		"Log in as {password}":  false,
		"Show {value}":          false,
	} {
		if err := checkAsk(ask, in); (err == nil) != ok {
			t.Errorf("FAIL: checkAsk(%q) = %v, want ok=%v", ask, err, ok)
		}
	}
}

// A sentence reads with the call's values, and a call that leaves one
// of its fields empty has no sentence rather than one with a hole.
func TestASentenceIsFilledOrAbsent(t *testing.T) {
	d := &Descriptor{Ask: "Give {name} {role} for {days} days"}
	if got := d.Sentence(askIn{Name: "rajeev", Role: "admin", Days: 30}); got != "Give rajeev admin for 30 days" {
		t.Errorf("FAIL: got %q", got)
	}
	if got := d.Sentence(&askIn{Name: "rajeev", Role: "admin", Days: 30}); got != "Give rajeev admin for 30 days" {
		t.Errorf("FAIL: through a pointer: got %q", got)
	}
	if got := d.Sentence(askIn{Name: "rajeev", Days: 30}); got != "" {
		t.Errorf("FAIL: a sentence with a hole: %q", got)
	}
	if got := (&Descriptor{}).Sentence(askIn{Name: "x"}); got != "" {
		t.Errorf("FAIL: an op with no sentence read %q", got)
	}
}