Skip to content

Commit

Permalink
Merge pull request #282 from supertokens/testing/adds-tests-for-session
Browse files Browse the repository at this point in the history
[chore] Adds tests for GetSession
  • Loading branch information
rishabhpoddar authored May 18, 2023
2 parents b2b2c0c + 1582434 commit bcea5da
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

- Adds additional tests for the session recipe

## [0.12.1] - 2023-05-12

### Changes
Expand Down
57 changes: 57 additions & 0 deletions recipe/session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,63 @@ func TestSessionContainerOverride(t *testing.T) {
assert.Equal(t, 1, data["test"])
}

func TestGetSessionReturnsNilForJWTWithoutSessionClaims(t *testing.T) {
configValue := supertokens.TypeInput{
Supertokens: &supertokens.ConnectionInfo{
ConnectionURI: "http://localhost:8080",
},
AppInfo: supertokens.AppInfo{
AppName: "SuperTokens",
WebsiteDomain: "supertokens.io",
APIDomain: "api.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()
False := false
mux.HandleFunc("/getSession", func(rw http.ResponseWriter, r *http.Request) {
response, err := GetSession(r, rw, &sessmodels.VerifySessionOptions{
SessionRequired: &False,
})

assert.NoError(t, err)
assert.Nil(t, response)
})

testServer := httptest.NewServer(supertokens.Middleware(mux))
defer func() {
testServer.Close()
}()

validity := uint64(60)
response, err := CreateJWT(map[string]interface{}{}, &validity, nil)

if err != nil {
t.Error(err.Error())
}

jwt := response.OK.Jwt

req, err := http.NewRequest(http.MethodGet, testServer.URL+"/getSession", nil)
req.Header.Add("Authorization", "Bearer "+jwt)
assert.NoError(t, err)
_, err = http.DefaultClient.Do(req)

if err != nil {
t.Error(err.Error())
}
}

type MockResponseWriter struct{}

func (mw MockResponseWriter) Header() http.Header {
Expand Down

0 comments on commit bcea5da

Please sign in to comment.