I’m trying to add a fake token into the token manager within a unit test in an angular application and can’t seem to find a feasible way to do this properly with the latest version of the Okta angular SDK.
Using the following (updated) versions of the Okta libraries, what method is best to do this in a unit test?
Context:
- okta/okta-angular: 5.1.1
- okta/okta-auth-js: 6.0.0
- angular 11.2.15
In example, here would be my current flow, where I’m at:
1.) First, I spy on the OKTA_AUTH instance I inject through the constructor:
Implementation:
constructor(@Inject(OKTA_AUTH) public oktaAuthClient: OktaAuth) { }
Test:
TestBed.configureTestingModule({
imports: [ OktaAuthModule ],
providers: [
{ provide: OKTA_CONFIG, multi: false, useFactory: () => {
const oktaAuth = new OktaAuth({
issuer: "https://test-host/oauth2/default",
clientId: "test-client-id",
redirectUri: window.location.origin + '/login/callback',
pkce: true,
tokenManager: {
storage: 'sessionStorage',
autoRenew: true
}
});
return {oktaAuth};
}},
],
});
2.) Attempt to spy on token manager and mock a return to the “get” method on it:
Implementation:
const tokenManager: TokenManager = this.oktaAuthClient.tokenManager;
const accessToken = await tokenManager.get('accessToken') as AccessToken;
Test:
const oktaAuthSpyL1 = jasmine.createSpyObj<OktaAuth>('OktaAuth', ['tokenManager']);
const oktaAuthSpyL2 = jasmine.createSpyObj('oktaAuthSpyL1', ['get'])
Not sure what I’m missing, but this doesn’t seem to work at all and the token is always undefined in the test scope. How to do I get the tokenManager.get() to return a token in a test?
Methods I’ve tried to add a token to the token manager:
setTokens:
const fakeToken: AccessToken = {
claims: { sub: "sub" },
scopes: ["openid" ],
userinfoUrl: "x",
authorizeUrl: "x",
expiresAt: 7200,
accessToken: "valid-token",
tokenType: "Bearer",
};
const tokens = {} as Tokens;
tokens.accessToken = fakeToken;
oktaAuthSpyL2.setTokens(tokens);
tokenManager.add:
const fakeToken: AccessToken = {
claims: { sub: "sub" },
scopes: ["openid" ],
userinfoUrl: "x",
authorizeUrl: "x",
expiresAt: 7200,
accessToken: "valid-token",
tokenType: "Bearer",
};
// assume the level 2 spy here has access to the token manager methods
oktaAuthSpyL2.add('accessToken', fakeToken);
const accessToken = await oktaAuthSpyL2.get('accessToken') as AccessToken;
console.info(accessToken); // undefined