Skip to content

Commit

Permalink
Fix: Placement of quickstarts tests (#702)
Browse files Browse the repository at this point in the history
* Adding unit tests

* Adding test run wrappers

* Fixing lint issue

* Fixing test

* Fixing placement of test

* Correcting placement of quickstarts tests

* More tests

---------

Co-authored-by: Will Vedder <[email protected]>
  • Loading branch information
willvedd and willvedd authored Apr 3, 2023
1 parent 9465e9d commit aaec788
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 62 deletions.
73 changes: 73 additions & 0 deletions internal/auth0/quickstarts_test.go
Original file line number Diff line number Diff line change
@@ -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: "<!-- markdownlint-disable MD041 -->\n<p>To run the sample follow these steps:</p>\n<ol></p>",
},
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: "<p>To run the sample follow these steps:</p>\n<ol>\n<li>Set the <strong>Allowed Callback URLs</strong></p>",
},
}

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())
})
}
31 changes: 31 additions & 0 deletions internal/cli/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"))
}
71 changes: 9 additions & 62 deletions internal/cli/quickstarts_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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: "<!-- markdownlint-disable MD041 -->\n<p>To run the sample follow these steps:</p>\n<ol></p>",
},
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: "<p>To run the sample follow these steps:</p>\n<ol>\n<li>Set the <strong>Allowed Callback URLs</strong></p>",
},
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"))
}

0 comments on commit aaec788

Please sign in to comment.