Skip to content

Commit

Permalink
Merge pull request #432 from supertokens/feat/normalise-dashboard-con…
Browse files Browse the repository at this point in the history
…nection-url

feat: normalise dashboard connection url
  • Loading branch information
rishabhpoddar authored Oct 2, 2024
2 parents 60e0dc2 + 4449b38 commit 2112cb9
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

## [0.25.1] - 2024-10-02

- Adds support for normalizing the connection URI's before returning them in dashboard GET response.

## [0.25.0] - 2024-09-25

### Changes
Expand Down
18 changes: 17 additions & 1 deletion recipe/dashboard/api/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package api

import (
"strconv"
"strings"

"github.com/supertokens/supertokens-golang/recipe/dashboard/constants"
"github.com/supertokens/supertokens-golang/recipe/dashboard/dashboardmodels"
Expand Down Expand Up @@ -45,7 +46,22 @@ func MakeAPIImplementation() dashboardmodels.APIInterface {
if err != nil {
return "", err
}
connectionURI := stInstance.SuperTokens.ConnectionURI

// We are splitting the passed URI here so that if multiple URI's are passed
// separated by a colon, the first one is returned.
connectionURIToNormalize := strings.Split(stInstance.SuperTokens.ConnectionURI, ";")[0]

// This normalizes the URI to make sure that it has things like protocol etc
// injected into it before it is returned.
var normalizationError error
normalizedConnectionURI, normalizationError := supertokens.NewNormalisedURLDomain(connectionURIToNormalize)
if normalizationError != nil {
// In case of failures, we want to return a 500 here, mainly because that
// is what we return if the connectionURI is invalid which is the case here
// if normalization fails.
return "", normalizationError
}
connectionURI := normalizedConnectionURI.GetAsStringDangerous()

normalizedDashboardPath, err := supertokens.NewNormalisedURLPath(constants.DashboardAPI)
if err != nil {
Expand Down
162 changes: 162 additions & 0 deletions recipe/dashboard/dashboardGet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package dashboard

import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/supertokens/supertokens-golang/recipe/emailpassword"

"github.com/stretchr/testify/assert"
"github.com/supertokens/supertokens-golang/recipe/dashboard/dashboardmodels"
"github.com/supertokens/supertokens-golang/supertokens"
"github.com/supertokens/supertokens-golang/test/unittesting"
)

func TestThatDashboardGetNormalizesConnectionURIWithoutHTTP(t *testing.T) {
connectionURI := "http://localhost:8080"
connectionURIWithoutProtocol := strings.Replace(connectionURI, "http://", "", -1)
config := supertokens.TypeInput{
OnSuperTokensAPIError: func(err error, req *http.Request, res http.ResponseWriter) {
print(err)
},
Supertokens: &supertokens.ConnectionInfo{
ConnectionURI: connectionURIWithoutProtocol,
},
AppInfo: supertokens.AppInfo{
APIDomain: "api.supertokens.io",
AppName: "SuperTokens",
WebsiteDomain: "supertokens.io",
},
RecipeList: []supertokens.Recipe{
emailpassword.Init(nil),
Init(&dashboardmodels.TypeInput{
ApiKey: "testapikey",
}),
},
}

BeforeEach()
unittesting.StartUpST("localhost", "8080")
defer AfterEach()
err := supertokens.Init(config)
if err != nil {
t.Error(err.Error())
}

mux := http.NewServeMux()
testServer := httptest.NewServer(supertokens.Middleware(mux))
defer testServer.Close()

req, err := http.NewRequest(http.MethodGet, testServer.URL+"/auth/dashboard", strings.NewReader(`{}`))
req.Header.Set("Authorization", "Bearer testapikey")
res, err := http.DefaultClient.Do(req)
assert.Equal(t, res.StatusCode, 200)

if err != nil {
t.Error(err.Error())
}

body, _ := io.ReadAll(res.Body)
assert.True(t, strings.Contains(string(body), fmt.Sprintf("window.connectionURI = \"%s\"", connectionURI)))
}

func TestThatDashboardGetNormalizesConnectionURIWithoutHTTPS(t *testing.T) {
connectionURI := "https://try.supertokens.com"
connectionURIWithoutProtocol := strings.Replace(connectionURI, "https://", "", -1)
config := supertokens.TypeInput{
OnSuperTokensAPIError: func(err error, req *http.Request, res http.ResponseWriter) {
print(err)
},
Supertokens: &supertokens.ConnectionInfo{
ConnectionURI: connectionURIWithoutProtocol,
},
AppInfo: supertokens.AppInfo{
APIDomain: "api.supertokens.io",
AppName: "SuperTokens",
WebsiteDomain: "supertokens.io",
},
RecipeList: []supertokens.Recipe{
emailpassword.Init(nil),
Init(&dashboardmodels.TypeInput{
ApiKey: "testapikey",
}),
},
}

BeforeEach()
unittesting.StartUpST("localhost", "8080")
defer AfterEach()
err := supertokens.Init(config)
if err != nil {
t.Error(err.Error())
}

mux := http.NewServeMux()
testServer := httptest.NewServer(supertokens.Middleware(mux))
defer testServer.Close()

req, err := http.NewRequest(http.MethodGet, testServer.URL+"/auth/dashboard", strings.NewReader(`{}`))
req.Header.Set("Authorization", "Bearer testapikey")
res, err := http.DefaultClient.Do(req)
assert.Equal(t, res.StatusCode, 200)

if err != nil {
t.Error(err.Error())
}

body, _ := io.ReadAll(res.Body)
assert.True(t, strings.Contains(string(body), fmt.Sprintf("window.connectionURI = \"%s\"", connectionURI)))
}

func TestThatDashboardGetReturnsFirstURIWhenMultipleArePassed(t *testing.T) {
firstConnectionURI := "http://localhost:8080"
secondConnectionURI := "https://try.supertokens.com"
multiplConnectionURIs := fmt.Sprintf("%s;%s", firstConnectionURI, secondConnectionURI)
config := supertokens.TypeInput{
OnSuperTokensAPIError: func(err error, req *http.Request, res http.ResponseWriter) {
print(err)
},
Supertokens: &supertokens.ConnectionInfo{
ConnectionURI: multiplConnectionURIs,
},
AppInfo: supertokens.AppInfo{
APIDomain: "api.supertokens.io",
AppName: "SuperTokens",
WebsiteDomain: "supertokens.io",
},
RecipeList: []supertokens.Recipe{
emailpassword.Init(nil),
Init(&dashboardmodels.TypeInput{
ApiKey: "testapikey",
}),
},
}

BeforeEach()
unittesting.StartUpST("localhost", "8080")
defer AfterEach()
err := supertokens.Init(config)
if err != nil {
t.Error(err.Error())
}

mux := http.NewServeMux()
testServer := httptest.NewServer(supertokens.Middleware(mux))
defer testServer.Close()

req, err := http.NewRequest(http.MethodGet, testServer.URL+"/auth/dashboard", strings.NewReader(`{}`))
req.Header.Set("Authorization", "Bearer testapikey")
res, err := http.DefaultClient.Do(req)
assert.Equal(t, res.StatusCode, 200)

if err != nil {
t.Error(err.Error())
}

body, _ := io.ReadAll(res.Body)
assert.True(t, strings.Contains(string(body), fmt.Sprintf("window.connectionURI = \"%s\"", firstConnectionURI)))
}
2 changes: 1 addition & 1 deletion supertokens/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
)

// VERSION current version of the lib
const VERSION = "0.25.0"
const VERSION = "0.25.1"

var (
cdiSupported = []string{"3.1"}
Expand Down

0 comments on commit 2112cb9

Please sign in to comment.