Skip to content

Commit

Permalink
ADD: added unit tests for index.test.ts to verify the logic provided …
Browse files Browse the repository at this point in the history
…to the google oauth resolver
  • Loading branch information
MennaTullahTaha committed May 11, 2024
1 parent b652da1 commit 1436240
Showing 1 changed file with 157 additions and 0 deletions.
157 changes: 157 additions & 0 deletions packages/backend/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,165 @@
import { PluginEnvironment } from './types';
import { resolverResult } from './plugins/plugins_helper/googleAuthResolver';
import {
AuthResolverCatalogUserQuery,
AuthResolverContext,
BackstageSignInResult,
ProfileInfo,
SignInInfo,
} from '@backstage/plugin-auth-node';
import { TokenParams } from '@backstage/plugin-auth-backend';
import { Entity } from '@backstage/catalog-model';

describe('test', () => {
it('unbreaks the test runner', () => {
const unbreaker = {} as PluginEnvironment;
expect(unbreaker).toBeTruthy();
});
});

describe('providers.google.create.signIn.resolver logic', () => {
it('should throw an exception for empty email address', async () => {
const mockProfile: ProfileInfo = {
email: '',
displayName: 'John Doe',
picture: 'https://example.com/avatar.jpg',
};

const mockSignInInfo: SignInInfo<any> = {
profile: mockProfile,
result: {}, // Placeholder for the authentication result
};

const mockTokenParams: TokenParams = {
claims: {
sub: 'user123',
ent: ['entity1', 'entity2'],
customClaim: 'value',
},
};

const mockContext: AuthResolverContext = {
issueToken: (params: TokenParams) => {
return new Promise<{ token: string }>(resolve => {
resolve({ token: 'fake token' });
});
},
findCatalogUser: function (
query: AuthResolverCatalogUserQuery,
): Promise<{ entity: Entity }> {
throw new Error('Function not implemented.');
},
signInWithCatalogUser: function (
query: AuthResolverCatalogUserQuery,
): Promise<BackstageSignInResult> {
throw new Error('Function not implemented.');
},
};

await expect(resolverResult(mockSignInInfo, mockContext)).rejects.toThrow(
'Login failed, user profile does not contain a valid email',
);
});

it('should throw an exception for invalid non empty email address', async () => {
const mockProfile: ProfileInfo = {
email: 'test.example.com',
displayName: 'John Doe',
picture: 'https://example.com/avatar.jpg',
};

const mockSignInInfo: SignInInfo<any> = {
profile: mockProfile,
result: {}, // Placeholder for the authentication result
};

const mockContext: AuthResolverContext = {
issueToken: (params: TokenParams) => {
return new Promise<{ token: string }>(resolve => {
resolve({ token: 'fake token' });
});
},
findCatalogUser: function (
query: AuthResolverCatalogUserQuery,
): Promise<{ entity: Entity }> {
throw new Error('Function not implemented.');
},
signInWithCatalogUser: function (
query: AuthResolverCatalogUserQuery,
): Promise<BackstageSignInResult> {
throw new Error('Function not implemented.');
},
};

await expect(resolverResult(mockSignInInfo, mockContext)).rejects.toThrow(
'Login failed, user profile does not contain a valid email',
);
});

it('should throw an exception for valid email address with incorrect domain', async () => {
const mockProfile: ProfileInfo = {
email: '[email protected]',
displayName: 'John Doe',
picture: 'https://example.com/avatar.jpg',
};

const mockSignInInfo: SignInInfo<any> = {
profile: mockProfile,
result: {}, // Placeholder for the authentication result
};

const mockContext: AuthResolverContext = {
issueToken: (params: TokenParams) => {
return new Promise<{ token: string }>(resolve => {
resolve({ token: 'fake token' });
});
},
findCatalogUser: function (
query: AuthResolverCatalogUserQuery,
): Promise<{ entity: Entity }> {
throw new Error('Function not implemented.');
},
signInWithCatalogUser: function (
query: AuthResolverCatalogUserQuery,
): Promise<BackstageSignInResult> {
throw new Error('Function not implemented.');
},
};

await expect(resolverResult(mockSignInInfo, mockContext)).rejects.toThrow(
`Login failed, '${mockProfile.email}' does not belong to the expected domain`,
);
});

it('should return a token for valid email address with correct domain', async () => {
const mockProfile: ProfileInfo = {
email: '[email protected]',
displayName: 'John Doe',
picture: 'https://example.com/avatar.jpg',
};

const mockSignInInfo: SignInInfo<any> = {
profile: mockProfile,
result: {}, // Placeholder for the authentication result
};

const mockContext: AuthResolverContext = {
issueToken: (params: TokenParams) => {
// Mock implementation for issueToken method
return { token: params.claims.sub + params.claims.ent };
},
findCatalogUser: (query: AuthResolverCatalogUserQuery) => {
// Mock implementation for findCatalogUser method
return '';
},
signInWithCatalogUser: (query: AuthResolverCatalogUserQuery) => {
// Mock implementation for signInWithCatalogUser method
return '';
},
};

return expect(resolverResult(mockSignInInfo, mockContext)).resolves.toEqual(
{ token: 'user:default/john_doeuser:default/john_doe' },
);
});
});

0 comments on commit 1436240

Please sign in to comment.