Skip to content

Commit

Permalink
security update
Browse files Browse the repository at this point in the history
  • Loading branch information
Knightplayzz committed Jun 13, 2024
1 parent 3963cb0 commit f0add13
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 38 deletions.
10 changes: 5 additions & 5 deletions test/auth/login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ describe('login function', () => {

test('should throw an error if fetch response is not ok', async () => {
fetch.mockResolvedValue(new Response(null, { status: 401 }));
context.getAuthToken.mockReturnValue('invalid-token');
context.getAuthToken.mockReturnValue('SampleToken');

await expect(login('valid-token')).rejects.toThrow('Authentication token is wrong.');
await expect(login('SampleToken')).rejects.toThrow('Authentication token is wrong.');
});

test('should set auth token and log in successfully if fetch response is ok', async () => {
fetch.mockResolvedValue(new Response(null, { status: 200 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

console.log = jest.fn(); // Mock console.log

await login('valid-token');
await login('SampleToken');

expect(context.setAuthToken).toHaveBeenCalledWith('valid-token');
expect(context.setAuthToken).toHaveBeenCalledWith('SampleToken');
expect(console.log).toHaveBeenCalledWith('You succesfully logged in.');
});
});
6 changes: 3 additions & 3 deletions test/brawlers/brawlers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ describe('brawlers function', () => {
test('should fetch brawlers information successfully', async () => {
const mockResponse = { items: [{ name: 'Shelly' }, { name: 'Colt' }] };
fetch.mockResolvedValue(new Response(JSON.stringify(mockResponse), { status: 200 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const data = await brawlers();

expect(fetch).toHaveBeenCalledWith('https://api.brawlstars.com/v1/brawlers', {
headers: { 'Authorization': 'Bearer valid-token' },
headers: { 'Authorization': 'Bearer SampleToken' },
});
expect(data).toEqual(mockResponse);
});

test('should handle fetch error', async () => {
fetch.mockResolvedValue(new Response(null, { status: 500 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

await expect(brawlers()).rejects.toThrow();
});
Expand Down
6 changes: 3 additions & 3 deletions test/brawlers/getBrawler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ describe('getBrawler function', () => {
const brawlerId = '123';
const mockResponse = { id: '123', name: 'Shelly' };
fetch.mockResolvedValue(new Response(JSON.stringify(mockResponse), { status: 200 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const data = await getBrawler(brawlerId);

expect(fetch).toHaveBeenCalledWith(`https://api.brawlstars.com/v1/brawlers/${encodeURIComponent(brawlerId)}`, {
headers: { 'Authorization': 'Bearer valid-token' },
headers: { 'Authorization': 'Bearer SampleToken' },
});
expect(data).toEqual(mockResponse);
});

test('should handle fetch error', async () => {
const brawlerId = '123';
fetch.mockRejectedValue(new Error('Failed to fetch'));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

await expect(getBrawler(brawlerId)).rejects.toThrow();
});
Expand Down
6 changes: 3 additions & 3 deletions test/clubs/getClub.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ describe('getClub function', () => {
test('should fetch club information successfully', async () => {
const mockResponse = { id: '123', name: 'Awesome Club' };
fetch.mockResolvedValue(new Response(JSON.stringify(mockResponse), { status: 200 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const clubTag = '#ABC123';
const data = await getClub(clubTag);

expect(fetch).toHaveBeenCalledWith(`https://api.brawlstars.com/v1/clubs/${encodeURIComponent(clubTag)}`, {
headers: { 'Authorization': 'Bearer valid-token' },
headers: { 'Authorization': 'Bearer SampleToken' },
});
expect(data).toEqual(mockResponse);
});

test('should handle fetch error and return error response', async () => {
const errorResponse = { reason: 'error', message: 'Server error' };
fetch.mockResolvedValue(new Response(JSON.stringify(errorResponse), { status: 500 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const clubTag = '#ABC123';
const result = await getClub(clubTag);
Expand Down
6 changes: 3 additions & 3 deletions test/events/getEvents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ describe('getEvents function', () => {
test('should fetch events successfully', async () => {
const mockResponse = [{ id: '1', name: 'Event 1' }, { id: '2', name: 'Event 2' }];
fetch.mockResolvedValue(new Response(JSON.stringify(mockResponse), { status: 200 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const data = await getEvents();

expect(fetch).toHaveBeenCalledWith('https://api.brawlstars.com/v1/events/rotation', {
headers: { 'Authorization': 'Bearer valid-token' },
headers: { 'Authorization': 'Bearer SampleToken' },
});
expect(data).toEqual(mockResponse);
});

test('should handle fetch error and return error response', async () => {
const errorResponse = { reason: 'error', message: 'Server error' };
fetch.mockResolvedValue(new Response(JSON.stringify(errorResponse), { status: 500 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const result = await getEvents();

Expand Down
6 changes: 3 additions & 3 deletions test/players/getBattlelog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ describe('getBattlelog function', () => {
test('should fetch battle log successfully', async () => {
const mockResponse = [{ battleId: '1', result: 'victory' }, { battleId: '2', result: 'defeat' }];
fetch.mockResolvedValue(new Response(JSON.stringify(mockResponse), { status: 200 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const playerTag = '#ABC123';
const data = await getBattlelog(playerTag);

expect(fetch).toHaveBeenCalledWith(`https://api.brawlstars.com/v1/players/${encodeURIComponent(playerTag)}/battlelog`, {
headers: { 'Authorization': 'Bearer valid-token' },
headers: { 'Authorization': 'Bearer SampleToken' },
});
expect(data).toEqual(mockResponse);
});

test('should handle fetch error and return error response', async () => {
const errorResponse = { reason: 'error', message: 'Server error' };
fetch.mockResolvedValue(new Response(JSON.stringify(errorResponse), { status: 500 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const playerTag = '#ABC123';
const result = await getBattlelog(playerTag);
Expand Down
6 changes: 3 additions & 3 deletions test/players/getPlayer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ describe('getPlayer function', () => {
test('should fetch player information successfully', async () => {
const mockResponse = { playerId: '123', name: 'PlayerName' };
fetch.mockResolvedValue(new Response(JSON.stringify(mockResponse), { status: 200 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const playerTag = '#ABC123';
const data = await getPlayer(playerTag);

expect(fetch).toHaveBeenCalledWith(`https://api.brawlstars.com/v1/players/${encodeURIComponent(playerTag)}`, {
headers: { 'Authorization': 'Bearer valid-token' },
headers: { 'Authorization': 'Bearer SampleToken' },
});
expect(data).toEqual(mockResponse);
});

test('should handle fetch error and return error response', async () => {
const errorResponse = { reason: 'error', message: 'Server error' };
fetch.mockResolvedValue(new Response(JSON.stringify(errorResponse), { status: 500 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const playerTag = '#ABC123';
const result = await getPlayer(playerTag);
Expand Down
6 changes: 3 additions & 3 deletions test/rankings/getRankingsBrawler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ describe('getRankingsBrawlers function', () => {
const brawlerId = '123';
const mockResponse = [{ playerId: '1', name: 'Player 1' }, { playerId: '2', name: 'Player 2' }];
fetch.mockResolvedValue(new Response(JSON.stringify(mockResponse), { status: 200 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const data = await getRankingsBrawlers(countryCode, brawlerId);

expect(fetch).toHaveBeenCalledWith(`https://api.brawlstars.com/v1/rankings/${encodeURIComponent(countryCode)}/brawlers/${encodeURIComponent(brawlerId)}`, {
headers: { 'Authorization': 'Bearer valid-token' },
headers: { 'Authorization': 'Bearer SampleToken' },
});
expect(data).toEqual(mockResponse);
});
Expand All @@ -54,7 +54,7 @@ describe('getRankingsBrawlers function', () => {
const brawlerId = '123';
const errorResponse = { reason: 'error', message: 'Server error' };
fetch.mockResolvedValue(new Response(JSON.stringify(errorResponse), { status: 500 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const result = await getRankingsBrawlers(countryCode, brawlerId);

Expand Down
6 changes: 3 additions & 3 deletions test/rankings/getRankingsClubs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ describe('getRankingsClubs function', () => {
const countryCode = 'US';
const mockResponse = [{ clubId: '1', name: 'Club 1' }, { clubId: '2', name: 'Club 2' }];
fetch.mockResolvedValue(new Response(JSON.stringify(mockResponse), { status: 200 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const data = await getRankingsClubs(countryCode);

expect(fetch).toHaveBeenCalledWith(`https://api.brawlstars.com/v1/rankings/${encodeURIComponent(countryCode)}/clubs`, {
headers: { 'Authorization': 'Bearer valid-token' },
headers: { 'Authorization': 'Bearer SampleToken' },
});
expect(data).toEqual(mockResponse);
});
Expand All @@ -39,7 +39,7 @@ describe('getRankingsClubs function', () => {
const countryCode = 'US';
const errorResponse = { reason: 'error', message: 'Server error' };
fetch.mockResolvedValue(new Response(JSON.stringify(errorResponse), { status: 500 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const result = await getRankingsClubs(countryCode);

Expand Down
6 changes: 3 additions & 3 deletions test/rankings/getRankingsPlayers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ describe('getRankingsPlayers function', () => {
const countryCode = 'US';
const mockResponse = [{ playerId: '1', name: 'Player 1' }, { playerId: '2', name: 'Player 2' }];
fetch.mockResolvedValue(new Response(JSON.stringify(mockResponse), { status: 200 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const data = await getRankingsPlayers(countryCode);

expect(fetch).toHaveBeenCalledWith(`https://api.brawlstars.com/v1/rankings/${encodeURIComponent(countryCode)}/players`, {
headers: { 'Authorization': 'Bearer valid-token' },
headers: { 'Authorization': 'Bearer SampleToken' },
});
expect(data).toEqual(mockResponse);
});
Expand All @@ -39,7 +39,7 @@ describe('getRankingsPlayers function', () => {
const countryCode = 'US';
const errorResponse = { reason: 'error', message: 'Server error' };
fetch.mockResolvedValue(new Response(JSON.stringify(errorResponse), { status: 500 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const result = await getRankingsPlayers(countryCode);

Expand Down
6 changes: 3 additions & 3 deletions test/rankings/getRankingsPowerplay..test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ describe('getRankingsPowerplay function', () => {
const seasonId = '2023';
const mockResponse = [{ playerId: '1', name: 'Player 1' }, { playerId: '2', name: 'Player 2' }];
fetch.mockResolvedValue(new Response(JSON.stringify(mockResponse), { status: 200 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const data = await getRankingsPowerplay(countryCode, seasonId);

expect(fetch).toHaveBeenCalledWith(`https://api.brawlstars.com/v1/rankings/${encodeURIComponent(countryCode)}/players`, {
headers: { 'Authorization': 'Bearer valid-token' },
headers: { 'Authorization': 'Bearer SampleToken' },
});
expect(data).toEqual(mockResponse);
});
Expand All @@ -54,7 +54,7 @@ describe('getRankingsPowerplay function', () => {
const seasonId = '2023';
const errorResponse = { reason: 'error', message: 'Server error' };
fetch.mockResolvedValue(new Response(JSON.stringify(errorResponse), { status: 500 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const result = await getRankingsPowerplay(countryCode, seasonId);

Expand Down
6 changes: 3 additions & 3 deletions test/rankings/getRankingsPowerplaySeasons.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ describe('getRankingsPowerplaySeasons function', () => {
test('should fetch powerplay seasons successfully', async () => {
const mockResponse = [{ seasonId: '1', name: 'Season 1' }, { seasonId: '2', name: 'Season 2' }];
fetch.mockResolvedValue(new Response(JSON.stringify(mockResponse), { status: 200 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const countryCode = 'US';
const data = await getRankingsPowerplaySeasons(countryCode);

expect(fetch).toHaveBeenCalledWith(`https://api.brawlstars.com/v1/rankings/${encodeURIComponent(countryCode)}/powerplay/seasons`, {
headers: { 'Authorization': 'Bearer valid-token' },
headers: { 'Authorization': 'Bearer SampleToken' },
});
expect(data).toEqual(mockResponse);
});

test('should handle fetch error and return error response', async () => {
const errorResponse = { reason: 'error', message: 'Server error' };
fetch.mockResolvedValue(new Response(JSON.stringify(errorResponse), { status: 500 }));
context.getAuthToken.mockReturnValue('valid-token');
context.getAuthToken.mockReturnValue('SampleToken');

const countryCode = 'US';
const result = await getRankingsPowerplaySeasons(countryCode);
Expand Down

0 comments on commit f0add13

Please sign in to comment.