Skip to content

Commit

Permalink
Fix tests failing due to mocked strategies
Browse files Browse the repository at this point in the history
The "this" context would become innacessible with the previous mocking
approach.
  • Loading branch information
rodrigoborgesdeoliveira committed Jun 23, 2024
1 parent a29ff4f commit 632a46e
Showing 1 changed file with 47 additions and 22 deletions.
69 changes: 47 additions & 22 deletions tests/strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,84 @@ declare global {
}
}

class SuccessStrategy extends Strategy {
private handler(token: string): void {
const payload = { sub: 1 };
this.done(null, payload);
}

public verifyGoogleIdToken(idToken: string): void {
this.handler(idToken);
}

public verifyGoogleAccessToken(accessToken: string): void {
this.handler(accessToken);
}
}

class ErrorStrategy extends Strategy {
private handler(token: string): void {
this.done({ message: 'Error message' });
}

public verifyGoogleIdToken(idToken: string): void {
this.handler(idToken);
}

public verifyGoogleAccessToken(accessToken: string): void {
this.handler(accessToken);
}
}

class NoParsedTokenStrategy extends Strategy {
private handler(token: string): void {
this.done(null, false, { message: 'Error message' });
}

public verifyGoogleIdToken(idToken: string): void {
this.handler(idToken);
}

public verifyGoogleAccessToken(accessToken: string): void {
this.handler(accessToken);
}
}

describe('Strategy', () => {
const verify = (parsedToken: any, googleId: any, done: (...args: any[]) => void) => {
return done(null, { id: '1234' }, { scope: 'read' });
};

const mockToken = '123456790-POIHANPRI-KNJYHHKIIH';

const strategy = new Strategy(
const strategy = new SuccessStrategy(
{
clientID: 'DUMMY_CLIENT_ID',
},
verify,
);

strategy.verifyGoogleIdToken = (idToken: string) => {
const payload = { sub: 1 };
strategy.done(null, payload);
};

const strategyWClientIDArray = new Strategy(
const strategyWClientIDArray = new SuccessStrategy(
{
clientID: ['DUMMY_CLIENT_ID_1', 'DUMMY_CLIENT_ID_2', 'DUMMY_CLIENT_ID', 'DUMMY_CLIENT_ID_3'],
},
verify,
);

strategyWClientIDArray.verifyGoogleIdToken = (idToken: string) => {
const payload = { sub: 1 };
strategy.done(null, payload);
};

const strategyNoParsedToken = new Strategy(
const strategyNoParsedToken = new NoParsedTokenStrategy(
{
clientID: 'DUMMY_CLIENT_ID',
},
verify,
);

strategyNoParsedToken.verifyGoogleIdToken = (idToken: string) => {
strategy.done(null, false, { message: 'Error message' });
};;

const strategyTokenError = new Strategy(
const strategyTokenError = new ErrorStrategy(
{
clientID: 'DUMMY_CLIENT_ID',
},
verify,
);

strategyTokenError.verifyGoogleIdToken = (idToken: string) => {
strategy.done({ message: 'Error message' });
};

it('should be named google-verify-token', () => {
expect(strategy.name).to.equal('google-verify-token');
});
Expand Down

0 comments on commit 632a46e

Please sign in to comment.