This repository has been archived on 2022-08-17. You can view files and clone it, but cannot push or open issues or pull requests.
dex/functional/dexctl_test.go
Eric Chiang 5052d8007f functional: clean up functional tests
Adjust logic and remove panics from functional tests.
2016-02-12 13:19:05 -08:00

88 lines
1.9 KiB
Go

package functional
import (
"bytes"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
)
var connConfigExample = []byte(`[
{
"type": "local",
"id": "local",
"passwordInfos": [
{
"userId":"elroy-id",
"passwordPlaintext": "bones"
},
{
"userId":"penny",
"passwordPlaintext": "kibble"
}
]
}
]`)
func TestDexctlCommands(t *testing.T) {
if strings.HasPrefix(dsn, "sqlite3://") {
t.Skip("only test dexctl conmand with postgres")
}
tempFile, err := ioutil.TempFile("", "dexctl_functional_tests_")
if err != nil {
t.Fatal(err)
}
connConfig := tempFile.Name()
defer os.Remove(connConfig)
if _, err := tempFile.Write(connConfigExample); err != nil {
t.Fatal(err)
}
tempFile.Close()
tests := []struct {
args []string
env []string
expErr bool
stdin io.Reader
}{
{
args: []string{"new-client", "https://example.com/callback"},
env: []string{"DEXCTL_DB_URL=" + dsn},
},
{
args: []string{"new-client", "--db-url", dsn, "https://example.com/callback"},
},
{
args: []string{"set-connector-configs", connConfig},
env: []string{"DEXCTL_DB_URL=" + dsn},
},
{
args: []string{"set-connector-configs", "--db-url", dsn, connConfig},
},
{
args: []string{"set-connector-configs", "--db-url", dsn, "-"},
stdin: bytes.NewReader(connConfigExample),
},
{
args: []string{"set-connector-configs", "-"},
env: []string{"DEXCTL_DB_URL=" + dsn},
stdin: bytes.NewReader(connConfigExample),
},
}
for _, tt := range tests {
cmd := exec.Command("../bin/dexctl", tt.args...)
cmd.Stdin = tt.stdin
cmd.Env = tt.env
out, err := cmd.CombinedOutput()
if !tt.expErr && err != nil {
t.Errorf("cmd 'dexctl %s' failed: %v %s", strings.Join(tt.args, " "), err, out)
} else if tt.expErr && err == nil {
t.Errorf("expected cmd 'dexctl %s' to fail", strings.Join(tt.args, " "))
}
}
}