From a5450ca229f6bb18f2d42fa927c33b64749d3307 Mon Sep 17 00:00:00 2001 From: Deepjyoti Barman Date: Wed, 2 Oct 2024 08:36:49 +0530 Subject: [PATCH] Add support for normalizing connection URI for dashboard recipe --- recipe/dashboard/api/implementation.go | 11 +- recipe/dashboard/dashboardGet_test.go | 162 +++++++++++++++++++++++++ 2 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 recipe/dashboard/dashboardGet_test.go diff --git a/recipe/dashboard/api/implementation.go b/recipe/dashboard/api/implementation.go index 322a565f..f2a5f295 100644 --- a/recipe/dashboard/api/implementation.go +++ b/recipe/dashboard/api/implementation.go @@ -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" @@ -45,7 +46,15 @@ func MakeAPIImplementation() dashboardmodels.APIInterface { if err != nil { return "", err } - connectionURI := stInstance.SuperTokens.ConnectionURI + + connectionURIToNormalize := strings.Split(stInstance.SuperTokens.ConnectionURI, ";")[0] + + var normalizationError error + normalizedConnectionURI, normalizationError := supertokens.NewNormalisedURLDomain(connectionURIToNormalize) + if normalizationError != nil { + return "", normalizationError + } + connectionURI := normalizedConnectionURI.GetAsStringDangerous() normalizedDashboardPath, err := supertokens.NewNormalisedURLPath(constants.DashboardAPI) if err != nil { diff --git a/recipe/dashboard/dashboardGet_test.go b/recipe/dashboard/dashboardGet_test.go new file mode 100644 index 00000000..ef315ffa --- /dev/null +++ b/recipe/dashboard/dashboardGet_test.go @@ -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))) +}