-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added more tests for network interceptor hook
- Loading branch information
1 parent
1937c90
commit d99fae6
Showing
1 changed file
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
package emailpassword | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/supertokens/supertokens-golang/recipe/userroles" | ||
"github.com/supertokens/supertokens-golang/supertokens" | ||
"github.com/supertokens/supertokens-golang/test/unittesting" | ||
) | ||
|
@@ -96,3 +99,129 @@ func TestNetworkInterceptorNotSet(t *testing.T) { | |
assert.Equal(t, 200, res.StatusCode) | ||
assert.Equal(t, false, isNetworkIntercepted) | ||
} | ||
|
||
func TestNetworkInterceptorIncorrectCoreURL(t *testing.T) { | ||
isNetworkIntercepted = false | ||
configValue := supertokens.TypeInput{ | ||
Supertokens: &supertokens.ConnectionInfo{ | ||
ConnectionURI: "http://localhost:8080", | ||
NetworkInterceptor: func(request *http.Request, context supertokens.UserContext) *http.Request { | ||
isNetworkIntercepted = true | ||
newRequest := request | ||
newRequest.URL.Path = "/public/recipe/incorrect/path" | ||
return newRequest | ||
}, | ||
}, | ||
AppInfo: supertokens.AppInfo{ | ||
AppName: "SuperTokens", | ||
APIDomain: "api.supertokens.io", | ||
WebsiteDomain: "supertokens.io", | ||
}, | ||
RecipeList: []supertokens.Recipe{ | ||
Init(nil), | ||
}, | ||
} | ||
BeforeEach() | ||
|
||
unittesting.StartUpST("localhost", "8080") | ||
|
||
defer AfterEach() | ||
|
||
err := supertokens.Init(configValue) | ||
|
||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
mux := http.NewServeMux() | ||
testServer := httptest.NewServer(supertokens.Middleware(mux)) | ||
defer testServer.Close() | ||
|
||
_, err = SignIn("public", "[email protected]", "validpass123") | ||
|
||
assert.NotNil(t, err, "there should be an error") | ||
assert.Contains(t, err.Error(), "status code: 404") | ||
assert.Equal(t, true, isNetworkIntercepted) | ||
} | ||
|
||
func TestNetworkInterceptorIncorrectQueryParams(t *testing.T) { | ||
isNetworkIntercepted = false | ||
configValue := supertokens.TypeInput{ | ||
Supertokens: &supertokens.ConnectionInfo{ | ||
ConnectionURI: "http://localhost:8080", | ||
NetworkInterceptor: func(r *http.Request, context supertokens.UserContext) *http.Request { | ||
isNetworkIntercepted = true | ||
newRequest := r | ||
q := url.Values{} | ||
newRequest.URL.RawQuery = q.Encode() | ||
return newRequest | ||
}, | ||
}, | ||
AppInfo: supertokens.AppInfo{ | ||
AppName: "SuperTokens", | ||
APIDomain: "api.supertokens.io", | ||
WebsiteDomain: "supertokens.io", | ||
}, | ||
RecipeList: []supertokens.Recipe{ | ||
Init(nil), | ||
userroles.Init(nil), | ||
}, | ||
} | ||
BeforeEach() | ||
|
||
unittesting.StartUpST("localhost", "8080") | ||
|
||
defer AfterEach() | ||
|
||
supertokens.Init(configValue) | ||
|
||
mux := http.NewServeMux() | ||
testServer := httptest.NewServer(supertokens.Middleware(mux)) | ||
defer testServer.Close() | ||
|
||
resp, _ := SignUp("public", "[email protected]", "validpass123") | ||
_, err := userroles.GetRolesForUser("public", resp.OK.User.ID) | ||
assert.NotNil(t, err, "should err, because userId is not passed") | ||
assert.Contains(t, err.Error(), "status code: 400") | ||
assert.Equal(t, true, isNetworkIntercepted) | ||
} | ||
|
||
func TestNetworkInterceptorRequestBody(t *testing.T) { | ||
isNetworkIntercepted = false | ||
configValue := supertokens.TypeInput{ | ||
Supertokens: &supertokens.ConnectionInfo{ | ||
ConnectionURI: "http://localhost:8080", | ||
NetworkInterceptor: func(r *http.Request, context supertokens.UserContext) *http.Request { | ||
isNetworkIntercepted = true | ||
newBody := bytes.NewReader([]byte(`{"newKey": "newValue"}`)) | ||
req, _ := http.NewRequest(r.Method, r.URL.String(), newBody) | ||
req.Header = r.Header | ||
return req | ||
}, | ||
}, | ||
AppInfo: supertokens.AppInfo{ | ||
AppName: "SuperTokens", | ||
APIDomain: "api.supertokens.io", | ||
WebsiteDomain: "supertokens.io", | ||
}, | ||
RecipeList: []supertokens.Recipe{ | ||
Init(nil), | ||
}, | ||
} | ||
BeforeEach() | ||
|
||
unittesting.StartUpST("localhost", "8080") | ||
|
||
defer AfterEach() | ||
|
||
supertokens.Init(configValue) | ||
|
||
mux := http.NewServeMux() | ||
testServer := httptest.NewServer(supertokens.Middleware(mux)) | ||
defer testServer.Close() | ||
|
||
_, err := SignIn("public", "[email protected]", "validpass123") | ||
assert.NotNil(t, err, "should err, because request body is incorrect") | ||
assert.Contains(t, err.Error(), "status code: 400") | ||
assert.Equal(t, true, isNetworkIntercepted) | ||
} |