Skip to content

Commit

Permalink
Update provider tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nkshah2 committed Oct 6, 2023
1 parent eb6aff9 commit 426d434
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions recipe/thirdparty/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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": "[email protected]",
"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")
}

0 comments on commit 426d434

Please sign in to comment.