Skip to content
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

How can I stub out parts of the web authentication flow for unit testing purposes? #318

Open
erawhctim opened this issue Oct 28, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@erawhctim
Copy link

Describe the feature request?

I have a wrapper class that encapsulates Okta SDK functionality & facilitates okta login/auth for the rest of my app.
I'd like to write tests for that wrapper class, replacing the key parts of the Okta SDK with stubs/fakes, but the SDK types' visibility makes that difficult. How can I stub out WebAuthentication().login(...)?

I'm able to provide my own WebAuthentication instance in my tests, which is good. WebAuthentication.authorizationCodeFlow is a public var (also good!). If AuthorizationCodeFlow was an interface I could implement myself (or if AuthFoundationDefaults exposes an AuthorizationCodeFlow factory lambda I could override), I think I could stub authorizationCodeFlow.resume(uri, flowContext) and be able to write the tests I want.

Alternatively, if WebAuthentication itself was an interface I could implement, I could stub the entire login call; I think that would suffice as well.

New or Affected Resource(s)

Affected resources.... web authentication? Not sure how to answer this.

Provide a documentation link

N/A

Additional Information?

If there's an additional path forward you can come up with that I've missed, please let me know!

@erawhctim erawhctim added the enhancement New feature or request label Oct 28, 2024
@rajdeepnanua-okta
Copy link
Contributor

Hi @erawhctim, there are a few ways that you can make test-specific code for this in the current state:

  1. Create an Interface (ie LoginManager), and create an app-specific and test-specific class. The app-specific class can call WebAuthentication, and the test-specific class can return stubbed credentials
  2. Use mockk to stub out WebAuthentication. See the section on constructor mocking in mockk here: https://mockk.io/#constructor-mocks

I can work on creating new interfaces as well for the login flows for easier test stubbing

@erawhctim
Copy link
Author

If adding new interfaces is a viable option for a future release, that would be great! Would rather not have to rely on mocking.

Thanks for your help

@rajdeepnanua-okta
Copy link
Contributor

This is definitely something I can accommodate, and I'll have this ready in a future release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants