-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Added network interceptor hook #386
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
962f684
feat: added hook for intercepting api calls made to the core
IamMayankThakur 57bd92a
added tests
IamMayankThakur c72f26c
updated changelog and bumped version
IamMayankThakur 1937c90
passed userContext to the querier functions
IamMayankThakur d99fae6
test: added more tests for network interceptor hook
IamMayankThakur 57bfd01
fix: failing ci - github actions
IamMayankThakur a326bfd
debug: added commaands to debug github actions
IamMayankThakur f591fd0
debug: added commaands to debug github actions
IamMayankThakur e85e522
fix: failing CI, because go binary was not deleted
IamMayankThakur 488f492
fix: failing CI, upgraded go version
IamMayankThakur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -9,6 +9,8 @@ jobs: | |
# When using actions/checkout in a custom container, the directory is not treated as a git repo and does not have a .git directory, therefore we need to initialize it as a git repo. This will allows us to track changes made after go mod tidy runs | ||
- name: Create a new git repository | ||
run: git init && git add --all && git -c user.name='test' -c user.email='[email protected]' commit -m 'init for pr action' | ||
- name: Install latest go | ||
run: wget https://go.dev/dl/go1.21.3.linux-amd64.tar.gz && rm -rf /usr/local/go && tar -C /usr/local -xzf go*.tar.gz && export PATH=$PATH:/usr/local/go/bin && rm go1.21.3.linux-amd64.tar.gz | ||
- name: Go mod tidy for root project | ||
run: go mod tidy | ||
- name: Go mod tidy for example apps | ||
|
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 |
---|---|---|
|
@@ -20,4 +20,6 @@ jobs: | |
with: | ||
node-version: '12' | ||
- run: git init && git add --all && git -c user.name='test' -c user.email='[email protected]' commit -m 'init for pr action' | ||
- name: Install latest go | ||
run: wget https://go.dev/dl/go1.21.3.linux-amd64.tar.gz && rm -rf /usr/local/go && tar -C /usr/local -xzf go*.tar.gz && export PATH=$PATH:/usr/local/go/bin && rm go1.21.3.linux-amd64.tar.gz | ||
- run: ./hooks/pre-commit.sh |
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,227 @@ | ||
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" | ||
) | ||
|
||
var isNetworkIntercepted = false | ||
|
||
func TestNetworkInterceptorDuringSignIn(t *testing.T) { | ||
configValue := supertokens.TypeInput{ | ||
Supertokens: &supertokens.ConnectionInfo{ | ||
ConnectionURI: "http://localhost:8080", | ||
NetworkInterceptor: func(request *http.Request, context supertokens.UserContext) *http.Request { | ||
isNetworkIntercepted = true | ||
return request | ||
}, | ||
}, | ||
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() | ||
|
||
res, err := unittesting.SignInRequest("[email protected]", "validpass123", testServer.URL) | ||
|
||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
assert.Equal(t, 200, res.StatusCode) | ||
assert.Equal(t, true, isNetworkIntercepted) | ||
} | ||
|
||
func TestNetworkInterceptorNotSet(t *testing.T) { | ||
isNetworkIntercepted = false | ||
configValue := supertokens.TypeInput{ | ||
Supertokens: &supertokens.ConnectionInfo{ | ||
ConnectionURI: "http://localhost:8080", | ||
}, | ||
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() | ||
|
||
res, err := unittesting.SignInRequest("[email protected]", "validpass123", testServer.URL) | ||
|
||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
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) | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a test where you modify the url of the request to the core, and then check that the function call to signIn returns an error with the right content (should have 404 status code, or the message should have a 404).
add a test where you modify the query params of the request to the core, and then check that the function to a GET request (like list roles for users), returns a 400 from the core
add a test where you modify the request body and then check that the function call to the core returns a 400