Skip to content

Commit

Permalink
chore: add test for meta-tag-extractor.uc
Browse files Browse the repository at this point in the history
  • Loading branch information
hoeppner-dataport committed Nov 3, 2023
1 parent b86f891 commit 13e134b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { UnauthorizedException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { setupEntities, userFactory } from '@shared/testing';
import { LegacyLogger } from '@src/core/logger';
import { AuthorizationService } from '@src/modules/authorization';
import { MetaTagExtractorService } from '../service';
import { MetaTagExtractorUc } from './meta-tag-extractor.uc';

describe(MetaTagExtractorUc.name, () => {
let module: TestingModule;
let uc: MetaTagExtractorUc;
let authorizationService: DeepMocked<AuthorizationService>;
let metaTagExtractorService: DeepMocked<MetaTagExtractorService>;

beforeAll(async () => {
module = await Test.createTestingModule({
providers: [
MetaTagExtractorUc,
{
provide: MetaTagExtractorService,
useValue: createMock<MetaTagExtractorService>(),
},
{
provide: AuthorizationService,
useValue: createMock<AuthorizationService>(),
},
{
provide: LegacyLogger,
useValue: createMock<LegacyLogger>(),
},
],
}).compile();

uc = module.get(MetaTagExtractorUc);
authorizationService = module.get(AuthorizationService);
metaTagExtractorService = module.get(MetaTagExtractorService);

await setupEntities();
});

afterAll(async () => {
await module.close();
});

afterEach(() => {
jest.resetAllMocks();
});

describe('fetchMetaData', () => {
describe('when user exists', () => {
const setup = () => {
const user = userFactory.build();
authorizationService.getUserWithPermissions.mockResolvedValueOnce(user);

return { user };
};

it('should check if the user is a valid user', async () => {
const { user } = setup();

authorizationService.getUserWithPermissions.mockResolvedValueOnce(user);

const url = 'https://www.example.com/great-example';
await uc.fetchMetaData(user.id, url);

expect(authorizationService.getUserWithPermissions).toHaveBeenCalledWith(user.id);
});

it('should call meta tag extractor service', async () => {
const { user } = setup();

const url = 'https://www.example.com/great-example';
await uc.fetchMetaData(user.id, url);

expect(metaTagExtractorService.fetchMetaData).toHaveBeenCalledWith(url);
});
});

describe('when user does not exist', () => {
const setup = () => {
const user = userFactory.build();
authorizationService.getUserWithPermissions.mockRejectedValue(false);

return { user };
};

it('should throw an UnauthorizedException', async () => {
const { user } = setup();

const url = 'https://www.example.com/great-example';
await expect(uc.fetchMetaData(user.id, url)).rejects.toThrow(UnauthorizedException);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ export class MetaTagExtractorUc {
async fetchMetaData(userId: EntityId, url: string): Promise<MetaData> {
this.logger.debug({ action: 'fetchMetaData', userId });

const user = await this.authorizationService.getUserWithPermissions(userId);
if (!user) {
try {
await this.authorizationService.getUserWithPermissions(userId);
} catch (error) {
throw new UnauthorizedException();
}

Expand Down

0 comments on commit 13e134b

Please sign in to comment.