Skip to content

Commit

Permalink
FIXUP: implement auth service
Browse files Browse the repository at this point in the history
  • Loading branch information
skgndi12 committed Dec 26, 2023
1 parent eb9b34c commit d9c695c
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions api/test/core/services/auth/auth.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ describe('Test auth service', () => {
const oauthClientId = 'test_client_id';
const oauthRedirectUri = '127.0.0.1:0/api/v1/google/sign-in/token';
const oauthAuthEndpoint = 'https://accounts.google.com/o/oauth2/auth';
const nonce = '2fc312e4-0c04-4f93-b0bf-c9e81f244814';
const state = '8a5c8a05-0a20-49f9-92a5-a6f83d5617e9';
const response_type = 'code';
const protocol = 'http';
const scope = 'openid email';
const access_type = 'online';
const prompt = 'consent select_account';
const authConfig: AuthConfig = { oauthStateExpirationMinutes: 10 };
let keyValueRepository: KeyValueRepository;
let googleHandler: GoogleHandler;
let authService: AuthService;
const state = '8a5c8a05-0a20-49f9-92a5-a6f83d5617e9';

beforeEach(() => {
keyValueRepository = {
Expand All @@ -25,7 +31,9 @@ describe('Test auth service', () => {
};
googleHandler = {
buildOidcRequest: jest.fn(() => {
return `https://accounts.google.com/o/oauth2/auth?client_id=test_client_id&nonce=2fc312e4-0c04-4f93-b0bf-c9e81f244814&response_type=code&redirect_uri=http%3A%2F%2F127.0.0.1%3A0%2Fapi%2Fv1%2Fgoogle%2Fsign-in%2Ftoken&scope=openid%20email&state=${state}&access_type=online&prompt=consent%20select_account`;
return encodeURI(
`${oauthAuthEndpoint}?client_id=${oauthClientId}&nonce=${nonce}&response_type=${response_type}&redirect_uri=${protocol}://${oauthRedirectUri}&scope=${scope}&state=${state}&access_type=${access_type}&prompt=${prompt}`
);
})
};
jest.spyOn(crypto, 'randomUUID').mockReturnValue(state);
Expand All @@ -37,20 +45,19 @@ describe('Test auth service', () => {
});

it('should return valid OIDC request', () => {
const givenProtocol = 'http';
const givenReferrer = 'http://127.0.0.1/home';
const expectedQueyObject = {
client_id: oauthClientId,
response_type: 'code',
redirect_uri: `${givenProtocol}://${oauthRedirectUri}`,
state: state,
scope: 'openid email',
access_type: 'online',
prompt: 'consent select_account'
response_type,
redirect_uri: `${protocol}://${oauthRedirectUri}`,
state,
scope,
access_type,
prompt
};

const redirectUrl = authService.initiateGoogleSignIn(
givenProtocol,
protocol,
givenReferrer
);
const [authEndpoint, queryString] = redirectUrl.split('?');
Expand All @@ -72,6 +79,6 @@ describe('Test auth service', () => {
);

expect(googleHandler.buildOidcRequest).toBeCalledTimes(1);
expect(googleHandler.buildOidcRequest).toBeCalledWith(givenProtocol, state);
expect(googleHandler.buildOidcRequest).toBeCalledWith(protocol, state);
});
});

0 comments on commit d9c695c

Please sign in to comment.