diff --git a/internal/auth0/quickstarts_test.go b/internal/auth0/quickstarts_test.go new file mode 100644 index 000000000..f97d9b99d --- /dev/null +++ b/internal/auth0/quickstarts_test.go @@ -0,0 +1,73 @@ +package auth0 + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +var mockQuickStarts = Quickstarts{ + Quickstart{ + Name: "Express", + AppType: "webapp", + URL: "/docs/quickstart/webapp/express", + Logo: "https://cdn2.auth0.com/docs/1.13412.0/img/platforms/javascript.svg", + DownloadLink: "/docs/package/v2?repo=auth0-express-webapp-sample&branch=master&path=01-Login", + DownloadInstructions: "\n

To run the sample follow these steps:

\n

    ", + }, + Quickstart{ + Name: "Flutter", + AppType: "native", + URL: "/docs/quickstart/native/flutter", + Logo: "https://cdn2.auth0.com/docs/1.13412.0/img/platforms/flutter.svg", + DownloadLink: "/docs/package/v2?repo=auth0-flutter-samples&branch=main&path=sample", + DownloadInstructions: "

    To run the sample follow these steps:

    \n
      \n
    1. Set the Allowed Callback URLs

      ", + }, +} + +func TestFilterByType(t *testing.T) { + t.Run("filter quickstarts by known types", func(t *testing.T) { + res, err := mockQuickStarts.FilterByType("webapp") + assert.Len(t, res, 1) + assert.Equal(t, res[0].Name, "Express") + assert.NoError(t, err) + + res, err = mockQuickStarts.FilterByType("native") + assert.Len(t, res, 1) + assert.Equal(t, res[0].Name, "Flutter") + assert.NoError(t, err) + }) + + t.Run("filter quickstarts by an unknown type", func(t *testing.T) { + res, err := mockQuickStarts.FilterByType("some-unknown-type") + assert.Nil(t, res) + assert.Error(t, err) + assert.Equal(t, fmt.Sprintf("unable to find any quickstarts for: %s", "some-unknown-type"), err.Error()) + }) +} + +func TestStacks(t *testing.T) { + t.Run("get quickstart stacks from quickstarts list", func(t *testing.T) { + res := mockQuickStarts.Stacks() + assert.Equal(t, res, []string{"Express", "Flutter"}) + + res = Quickstarts{}.Stacks() + assert.Len(t, res, 0) + }) +} + +func TestFindByStack(t *testing.T) { + t.Run("find quickstart stack by known app type", func(t *testing.T) { + res, err := mockQuickStarts.FindByStack("Express") + assert.NoError(t, err) + assert.Equal(t, "Express", res.Name) + }) + + t.Run("find quickstart stack by known app type", func(t *testing.T) { + res, err := mockQuickStarts.FindByStack("some-non-existent-qs-type") + assert.Error(t, err) + assert.Empty(t, res) + assert.Equal(t, fmt.Sprintf("quickstart not found for %s", "some-non-existent-qs-type"), err.Error()) + }) +} diff --git a/internal/cli/apps_test.go b/internal/cli/apps_test.go index b82188b08..d15b4542b 100644 --- a/internal/cli/apps_test.go +++ b/internal/cli/apps_test.go @@ -7,6 +7,7 @@ import ( "github.com/auth0/go-auth0/management" "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" "github.com/auth0/auth0-cli/internal/auth0" "github.com/auth0/auth0-cli/internal/auth0/mock" @@ -88,3 +89,33 @@ func TestAppsListCmd(t *testing.T) { }) } } + +func TestFormatAppSettingsPath(t *testing.T) { + assert.Empty(t, formatAppSettingsPath("")) + assert.Equal(t, "applications/app-id-1/settings", formatAppSettingsPath("app-id-1")) +} + +func TestTypeFor(t *testing.T) { + testAppType := appTypeNative + expected := "Native" + assert.Equal(t, &expected, typeFor(&testAppType)) + + testAppType = appTypeSPA + expected = "Single Page Web Application" + assert.Equal(t, &expected, typeFor(&testAppType)) + + testAppType = appTypeRegularWeb + expected = "Regular Web Application" + assert.Equal(t, &expected, typeFor(&testAppType)) + + testAppType = appTypeNonInteractive + expected = "Machine to Machine" + assert.Equal(t, &expected, typeFor(&testAppType)) + + testAppType = "some-unknown-api-type" + assert.Nil(t, typeFor(&testAppType)) +} + +func TestCommaSeparatedStringToSlice(t *testing.T) { + assert.Equal(t, []string{"foo"}, commaSeparatedStringToSlice("foo")) +} diff --git a/internal/cli/quickstarts_test.go b/internal/cli/quickstarts_test.go index edf8e5aae..34a8dd0dc 100644 --- a/internal/cli/quickstarts_test.go +++ b/internal/cli/quickstarts_test.go @@ -1,12 +1,9 @@ package cli import ( - "fmt" "testing" "github.com/stretchr/testify/assert" - - "github.com/auth0/auth0-cli/internal/auth0" ) func TestQuickstartsTypeFor(t *testing.T) { @@ -17,67 +14,17 @@ func TestQuickstartsTypeFor(t *testing.T) { assert.Equal(t, "generic", quickstartsTypeFor("some-unknown-value")) } -var mockQuickStarts = auth0.Quickstarts{ - auth0.Quickstart{ - Name: "Express", - AppType: "webapp", - URL: "/docs/quickstart/webapp/express", - Logo: "https://cdn2.auth0.com/docs/1.13412.0/img/platforms/javascript.svg", - DownloadLink: "/docs/package/v2?repo=auth0-express-webapp-sample&branch=master&path=01-Login", - DownloadInstructions: "\n

      To run the sample follow these steps:

      \n

        ", - }, - auth0.Quickstart{ - Name: "Flutter", - AppType: "native", - URL: "/docs/quickstart/native/flutter", - Logo: "https://cdn2.auth0.com/docs/1.13412.0/img/platforms/flutter.svg", - DownloadLink: "/docs/package/v2?repo=auth0-flutter-samples&branch=main&path=sample", - DownloadInstructions: "

        To run the sample follow these steps:

        \n
          \n
        1. Set the Allowed Callback URLs

          ", - }, +func TestDefaultCallbackURLFor(t *testing.T) { + assert.Equal(t, "http://localhost:3000/api/auth/callback", defaultCallbackURLFor("next.js")) + assert.Equal(t, "http://localhost:3000", defaultCallbackURLFor("all-other-quickstart-application-types")) } -func TestFilterByType(t *testing.T) { - t.Run("filter quickstarts by known types", func(t *testing.T) { - res, err := mockQuickStarts.FilterByType(qsWebApp) - assert.Len(t, res, 1) - assert.Equal(t, res[0].Name, "Express") - assert.NoError(t, err) - - res, err = mockQuickStarts.FilterByType(qsNative) - assert.Len(t, res, 1) - assert.Equal(t, res[0].Name, "Flutter") - assert.NoError(t, err) - }) - - t.Run("filter quickstarts by an unknown type", func(t *testing.T) { - res, err := mockQuickStarts.FilterByType("some-unknown-type") - assert.Nil(t, res) - assert.Error(t, err) - assert.Equal(t, fmt.Sprintf("unable to find any quickstarts for: %s", "some-unknown-type"), err.Error()) - }) +func TestDefaultURLFor(t *testing.T) { + assert.Equal(t, "http://localhost:4200", defaultURLFor("angular")) + assert.Equal(t, "http://localhost:3000", defaultURLFor("all-other-quickstart-application-types")) } -func TestStacks(t *testing.T) { - t.Run("get quickstart stacks from quickstarts list", func(t *testing.T) { - res := mockQuickStarts.Stacks() - assert.Equal(t, res, []string{"Express", "Flutter"}) - - res = auth0.Quickstarts{}.Stacks() - assert.Len(t, res, 0) - }) -} - -func TestFindByStack(t *testing.T) { - t.Run("find quickstart stack by known app type", func(t *testing.T) { - res, err := mockQuickStarts.FindByStack("Express") - assert.NoError(t, err) - assert.Equal(t, "Express", res.Name) - }) - - t.Run("find quickstart stack by an unknown app type", func(t *testing.T) { - res, err := mockQuickStarts.FindByStack("some-non-existent-qs-type") - assert.Error(t, err) - assert.Empty(t, res) - assert.Equal(t, fmt.Sprintf("quickstart not found for %s", "some-non-existent-qs-type"), err.Error()) - }) +func TestUrlPromptFor(t *testing.T) { + assert.Equal(t, "Quickstarts use localhost, do you want to add http://localhost:3000/api/auth/callback to the list\n of allowed callback URLs and http://localhost:3000 to the list of allowed logout URLs?", urlPromptFor("generic", "Next.js")) + assert.Equal(t, "Quickstarts use localhost, do you want to add http://localhost:3000 to the list\n of allowed callback URLs and logout URLs?", urlPromptFor("generic", "Laravel API")) }