supertokens / supertokens-auth-react

ReactJS authentication module for SuperTokens
https://supertokens.com
Other
270 stars 84 forks source link

Any easy way to mock out sessions? #694

Open nodiesBlade opened 1 year ago

nodiesBlade commented 1 year ago

Hi, we're creating tests for one of our web applications, and there doesn't seem to be an easy way to mock out Sessions where SessionAuth HoC is respected.

While we can mock out the auth endpoints, I'm not so sure how to mock out the actual session response. If we cannot mock out the actual session, then the SessionAuth HoC will block us from testing other components. Plus, we'd like to actually test Auth flows

https://github.com/supertokens/supertokens-core/issues/115 recommends spinning up a local supertokenscore with inmemory db, however, this is really cumberstone and complex for our use case.

rishabhpoddar commented 1 year ago

What you can do is to override the session recipe functions on the frontend like doesSessionExist or getAccessTokenPayloadSecurely etc to return mocked information if it's testing env. This should solve your issue

nodiesBlade commented 1 year ago

Do you have an example?

I'm not too familiar with the AuthSession component and what it uses for valid session.

rishabhpoddar commented 1 year ago

We don't have a specific example for this, but do checkout this: https://supertokens.com/docs/session/advanced-customizations/frontend-functions-override/usage

You need to override all the session recipe functions like doesSessionExist and return the appropriate values from the functions.