Skip to content

Commit

Permalink
Add initial unit test for cli.IsLoggedIn()
Browse files Browse the repository at this point in the history
  • Loading branch information
rene00 committed Feb 26, 2021
1 parent e76223a commit e4503c6
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package cli

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"

"github.com/auth0/auth0-cli/internal/display"
"github.com/google/go-cmp/cmp"
"github.com/olekukonko/tablewriter"
"github.com/stretchr/testify/assert"
)

// TODO(cyx): think about whether we should extract this function in the
Expand Down Expand Up @@ -42,3 +47,43 @@ func expectTable(t testing.TB, got string, header []string, data [][]string) {
t.Fatal(cmp.Diff(want, got))
}
}

func TestIsLoggedIn(t *testing.T) {
tests := []struct {
defaultTenant string
tenants map[string]tenant
want bool
desc string
}{
{"", map[string]tenant{}, false, "no tenants"},
{"t0", map[string]tenant{}, false, "tenant is set but no tenants map"},
{"t0", map[string]tenant{"t0": tenant{}}, false, "tenants map set but invalid token"},
}

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
tmpFile, err := ioutil.TempFile(os.TempDir(), "isLoggedIn-")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpFile.Name())

type Config struct {
DefaultTenant string `json:"default_tenant"`
Tenants map[string]tenant `json:"tenants"`
}

b, err := json.Marshal(&Config{test.defaultTenant, test.tenants})
if err != nil {
t.Fatal(err)
}

if err = ioutil.WriteFile(tmpFile.Name(), b, 0400); err != nil {
t.Fatal(err)
}

c := cli{renderer: display.NewRenderer(), path: tmpFile.Name()}
assert.Equal(t, test.want, c.isLoggedIn())
})
}
}

0 comments on commit e4503c6

Please sign in to comment.