Skip to content

Commit

Permalink
Merge pull request #7 from HackYeahKabanosy/improvements-and-tests
Browse files Browse the repository at this point in the history
Improvements and tests
  • Loading branch information
pvgomes authored Sep 29, 2024
2 parents 0b0f9a9 + a1c7c7f commit 9d44bdb
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .env-dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
NODE_ENV=develop
MONGO_URL= mongodb://localhost:27017/fraud-free-api?retryWrites=true&w=majority
GPT_SECRET_KEY=sk
GPT_SECRET_KEY=sk
VT_SECRET_KEY=alk
18 changes: 0 additions & 18 deletions src/app/modules/provider/provider.service.spec.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/app/modules/provider/providers/trustpilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ export class TrustPilot {
const reviewSummary = $('[class^="styles_clickable"] span').text().trim();
const [totalReviews, ratingDescription] = reviewSummary.split('•').map(part => part.trim());

const finalRatingDescription = ratingDescription || "no customer reviews for this site";

return {
companyName,
totalReviews,
trustScore,
ratingDescription,
ratingDescription: finalRatingDescription,
rating,
};
}
}

53 changes: 51 additions & 2 deletions src/app/modules/provider/providers/trustpilot.ts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ describe('TrustPilot Class', () => {
const enrichedUrl = trustPilot['enrichUrl'](inputUrl);
expect(enrichedUrl).toBe(inputUrl);
});

it('should handle URLs without schema', () => {
const inputUrl = 'www.trustpilot.com/review/amazon.pl';
const enrichedUrl = trustPilot['enrichUrl'](inputUrl);
expect(enrichedUrl).toBe('https://www.trustpilot.com/review/www.trustpilot.com/review/amazon.pl');
});
});

describe('factory', () => {
Expand All @@ -36,7 +42,9 @@ describe('TrustPilot Class', () => {
<div data-rating-typography="true">4.5</div>
<span class="typography_display-s__qOjh6">Amazon</span>
<img alt="TrustScore 4.5 out of 5">
<span role="link" tabindex="0" class="styles_clickable__zQWyh"><span class="typography_body-l__KUYFJ typography_appearance-subtle__8_H2l styles_text__W4hWi">163&nbsp; • &nbsp;Bad</span></span>
<span class="styles_clickable__zQWyh">
<span class="typography_body-l__KUYFJ typography_appearance-subtle__8_H2l styles_text__W4hWi">163&nbsp; • &nbsp;Bad</span>
</span>
`;

(axios.get as jest.Mock).mockResolvedValue({ data: html });
Expand All @@ -52,8 +60,49 @@ describe('TrustPilot Class', () => {
});
});

it('should handle missing ratingDescription gracefully', async () => {
const html = `
<div data-rating-typography="true">4.5</div>
<span class="typography_display-s__qOjh6">Amazon</span>
<img alt="TrustScore 4.5 out of 5">
<span class="styles_clickable__zQWyh"><span class="typography_body-l__KUYFJ typography_appearance-subtle__8_H2l styles_text__W4hWi">163&nbsp; • &nbsp;</span></span>
`;

(axios.get as jest.Mock).mockResolvedValue({ data: html });

const data = await trustPilot.factory('amazon.pl');

expect(data).toEqual({
companyName: 'Amazon',
totalReviews: '163',
trustScore: '4.5',
ratingDescription: 'no customer reviews for this site', // default message
rating: '4.5',
});
});

it('should handle case with no total reviews', async () => {
const html = `
<div data-rating-typography="true">4.5</div>
<span class="typography_display-s__qOjh6">Amazon</span>
<img alt="TrustScore 4.5 out of 5">
<span class="styles_clickable__zQWyh"><span class="typography_body-l__KUYFJ typography_appearance-subtle__8_H2l styles_text__W4hWi"> • &nbsp;Bad</span></span>
`;

(axios.get as jest.Mock).mockResolvedValue({ data: html });

const data = await trustPilot.factory('amazon.pl');

expect(data).toEqual({
companyName: 'Amazon',
totalReviews: '', // no total reviews
trustScore: '4.5',
ratingDescription: 'Bad',
rating: '4.5',
});
});

it('should handle request errors gracefully', async () => {
// Mock axios.get to throw an error
(axios.get as jest.Mock).mockRejectedValue(new Error('Request failed'));

const result = await trustPilot.factory('invalidurl.com');
Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/provider/providers/virus-total.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import axios from 'axios';
import { config } from 'src/config/config';

export class VirusTotal {
private apiKey: string;

constructor() {
// Store your VirusTotal API key
this.apiKey =
'b4a6e161f5b9c62a6ad9a217ccfd72c5aad1e3b1e4e4ce98ba2782532f1dbc52';
this.apiKey = config.vtSecret;
}

async factory(url: string) {
Expand Down
1 change: 1 addition & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export const config = {
env: process.env.NODE_ENV,
mongoUrl: process.env.MONGO_URL,
gptSecret: process.env.GPT_SECRET_KEY,
vtSecret: process.env.VT_SECRET_KEY
};

0 comments on commit 9d44bdb

Please sign in to comment.