From 426d434ef499e6db7ebb96f312364251b0133bfc Mon Sep 17 00:00:00 2001 From: Nemi Shah Date: Fri, 6 Oct 2023 13:37:46 +0530 Subject: [PATCH] Update provider tests --- recipe/thirdparty/provider_test.go | 122 +++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/recipe/thirdparty/provider_test.go b/recipe/thirdparty/provider_test.go index 4c9c85be..73b1f82b 100644 --- a/recipe/thirdparty/provider_test.go +++ b/recipe/thirdparty/provider_test.go @@ -17,9 +17,12 @@ package thirdparty import ( + "encoding/json" "errors" + "github.com/supertokens/supertokens-golang/recipe/session" "io" "io/ioutil" + "net" "net/http" "net/http/httptest" "net/url" @@ -779,3 +782,122 @@ func TestThatSignInUpFailsIfValidateAccessTokenReturnsError(t *testing.T) { respString = strings.Replace(respString, "\n", "", -1) assert.Equal(t, respString, "Invalid access token") } + +func TestThatSignInUpWorksIfValidateAccessTokenDoesNotReturnError(t *testing.T) { + overrideValidateCalled := false + configValue := supertokens.TypeInput{ + Supertokens: &supertokens.ConnectionInfo{ + ConnectionURI: "http://localhost:8080", + }, + AppInfo: supertokens.AppInfo{ + APIDomain: "api.supertokens.io", + AppName: "SuperTokens", + WebsiteDomain: "supertokens.io", + }, + RecipeList: []supertokens.Recipe{ + session.Init(nil), + Init( + &tpmodels.TypeInput{ + SignInAndUpFeature: tpmodels.TypeInputSignInAndUp{ + Providers: []tpmodels.ProviderInput{ + { + Config: tpmodels.ProviderConfig{ + ThirdPartyId: "custom", + TokenEndpoint: "http://127.0.0.1:8083/tokenendpoint", + UserInfoEndpoint: "http://127.0.0.1:8083/userinfo", + UserInfoMap: tpmodels.TypeUserInfoMap{ + FromUserInfoAPI: tpmodels.TypeUserInfoMapFields{ + UserId: "userId", + Email: "email", + EmailVerified: "emailVerified", + }, + }, + Clients: []tpmodels.ProviderClientConfig{ + { + ClientID: "test", + ClientSecret: "test-secret", + Scope: []string{"test-scope-1", "test-scope-2"}, + }, + }, + ValidateAccessToken: func(accessToken string, clientConfig tpmodels.ProviderConfigForClientType, userContext supertokens.UserContext) error { + overrideValidateCalled = true + if accessToken != "accesstoken" { + return errors.New("Invalid access token") + } + + return nil + }, + }, + }, + }, + }, + }, + ), + }, + } + + BeforeEach() + unittesting.StartUpST("localhost", "8080") + defer AfterEach() + err := supertokens.Init(configValue) + + if err != nil { + t.Error(err.Error()) + } + + mux := http.NewServeMux() + + mux.HandleFunc("/tokenendpoint", func(rw http.ResponseWriter, r *http.Request) { + data := map[string]interface{}{ + "access_token": "accesstoken", + "id_token": "idtoken", + } + rw.Header().Set("Content-Type", "application/json") + rw.WriteHeader(http.StatusCreated) + json.NewEncoder(rw).Encode(data) + }) + + mux.HandleFunc("/userinfo", func(rw http.ResponseWriter, r *http.Request) { + data := map[string]interface{}{ + "userId": "testiserid", + "email": "testinguser@supertokens.com", + "emailVerified": "true", + } + rw.Header().Set("Content-Type", "application/json") + rw.WriteHeader(http.StatusCreated) + json.NewEncoder(rw).Encode(data) + }) + + l, err := net.Listen("tcp", "127.0.0.1:8083") + if err != nil { + t.Error(err.Error()) + } + + testServer := httptest.NewUnstartedServer(supertokens.Middleware(mux)) + testServer.Listener.Close() + testServer.Listener = l + + // Start the server. + testServer.Start() + defer testServer.Close() + + req, err := http.NewRequest(http.MethodPost, testServer.URL+"/auth/signinup", strings.NewReader(`{"thirdPartyId": "custom", "redirectURIInfo": {"redirectURIOnProviderDashboard": "http://127.0.0.1/callback", "redirectURIQueryParams": {"code": "abcdefghj"}}}`)) + if err != nil { + t.Error(err.Error()) + } + + res, err := http.DefaultClient.Do(req) + + dataInBytes, err := ioutil.ReadAll(res.Body) + if err != nil { + t.Error(err.Error()) + } + + var response map[string]string + + err = json.Unmarshal(dataInBytes, &response) + + assert.Equal(t, res.StatusCode, 200) + assert.True(t, overrideValidateCalled) + assert.Equal(t, response["status"], "OK") +}