Skip to content

Commit

Permalink
added some more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Wouter van der Plas committed Jan 12, 2024
1 parent 5648cc3 commit 0cced80
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/utils/api/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function loginUrl(server: string, origin?: string): string {
const serverURL = new URL(server);
const url = `${serverURL.origin}/w/api.php?action=login&format=json`;
if (origin) {
return `${url}&origin=${origin}`;
return `${url}&origin=${encodeURIComponent(origin)}`;
}
return url;
}
Expand All @@ -45,7 +45,7 @@ export function tokenUrl(server: string, origin?: string): string {
const serverURL = new URL(server);
const url = `${serverURL.origin}/w/api.php?action=query&meta=tokens&type=csrf&format=json`;
if (origin) {
return `${url}&origin=${origin}`;
return `${url}&origin=${encodeURIComponent(origin)}`;
}
return url;
}
Expand All @@ -60,7 +60,7 @@ export function tokenUrl(server: string, origin?: string): string {
* @param {string[] | string} cookies an array or a string of cookies
* @returns {string} the joined cookies
*/
function joinCookies(cookies: string[] | string): string {
export function joinCookies(cookies: string[] | string): string {
if (Array.isArray(cookies)) {
return cookies.join('; ');
}
Expand Down
53 changes: 42 additions & 11 deletions tests/unit/utils/api/getToken.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
import axios from 'axios';
import getToken, { loginUrl } from '../../../../src/utils/api/token';
import getToken, { joinCookies, loginUrl, tokenUrl } from '../../../../src/utils/api/token';

describe('loginUrl', () => {
it('should return the correct url', () => {
// normal urls
expect(loginUrl('https://www.wikidata.org')).toBe('https://www.wikidata.org/w/api.php?action=login&format=json');
expect(loginUrl('https://test.wikidata.org')).toBe('https://test.wikidata.org/w/api.php?action=login&format=json');

// urls with a path
expect(loginUrl('https://www.wikidata.org/wiki/Main_Page')).toBe('https://www.wikidata.org/w/api.php?action=login&format=json');
expect(loginUrl('https://test.wikidata.org/wiki/Main_Page')).toBe('https://test.wikidata.org/w/api.php?action=login&format=json');

// urls with a provided origin
expect(loginUrl('https://www.wikidata.org', 'https://example.org')).toBe('https://www.wikidata.org/w/api.php?action=login&format=json&origin=https%3A%2F%2Fexample.org');
expect(loginUrl('https://test.wikidata.org', 'https://example.org')).toBe('https://test.wikidata.org/w/api.php?action=login&format=json&origin=https%3A%2F%2Fexample.org');
expect(loginUrl('https://www.wikidata.org/wiki/Main_Page', 'https://example.org')).toBe('https://www.wikidata.org/w/api.php?action=login&format=json&origin=https%3A%2F%2Fexample.org');
});
});

describe('tokenUrl', () => {
it('should return the correct url', () => {
// normal urls
expect(tokenUrl('https://www.wikidata.org')).toBe('https://www.wikidata.org/w/api.php?action=query&meta=tokens&type=csrf&format=json');
expect(tokenUrl('https://test.wikidata.org')).toBe('https://test.wikidata.org/w/api.php?action=query&meta=tokens&type=csrf&format=json');

// urls with a path
expect(tokenUrl('https://www.wikidata.org/wiki/Main_Page')).toBe('https://www.wikidata.org/w/api.php?action=query&meta=tokens&type=csrf&format=json');
expect(tokenUrl('https://test.wikidata.org/wiki/Main_Page')).toBe('https://test.wikidata.org/w/api.php?action=query&meta=tokens&type=csrf&format=json');

// urls with a provided origin
expect(tokenUrl('https://www.wikidata.org', 'https://example.org')).toBe('https://www.wikidata.org/w/api.php?action=query&meta=tokens&type=csrf&format=json&origin=https%3A%2F%2Fexample.org');
expect(tokenUrl('https://test.wikidata.org', 'https://example.org')).toBe('https://test.wikidata.org/w/api.php?action=query&meta=tokens&type=csrf&format=json&origin=https%3A%2F%2Fexample.org');
expect(tokenUrl('https://www.wikidata.org/wiki/Main_Page', 'https://example.org')).toBe('https://www.wikidata.org/w/api.php?action=query&meta=tokens&type=csrf&format=json&origin=https%3A%2F%2Fexample.org');
});
});

describe('joinCookies', () => {
it('should return the correct string', () => {
expect(joinCookies(['a', 'b', 'c'])).toBe('a; b; c');
expect(joinCookies('a; b; c')).toBe('a; b; c');
});
});

jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;
Expand Down Expand Up @@ -31,14 +72,4 @@ describe('getToken', () => {
// @ts-expect-error testing
await expect(() => getToken('a', 'a', null)).rejects.toThrow();
});

it('should return the correct url', () => {
// normal urls
expect(loginUrl('https://www.wikidata.org')).toBe('https://www.wikidata.org/w/api.php?action=login&format=json');
expect(loginUrl('https://test.wikidata.org')).toBe('https://test.wikidata.org/w/api.php?action=login&format=json');

// urls with a path
expect(loginUrl('https://www.wikidata.org/wiki/Main_Page')).toBe('https://www.wikidata.org/w/api.php?action=login&format=json');
expect(loginUrl('https://test.wikidata.org/wiki/Main_Page')).toBe('https://test.wikidata.org/w/api.php?action=login&format=json');
});
});
10 changes: 10 additions & 0 deletions tests/unit/utils/api/upload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ describe('validateAuthentication', () => {
describe('upload', () => {
const item = Item.fromNothing();

let consoleErrorSpy: jest.SpyInstance;

beforeEach(() => {
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
});

afterEach(() => {
consoleErrorSpy.mockRestore();
});

describe('uploading', () => {
it('should use the anonymous key if there is no key, but the anonymous key is set', async () => {
const axiosMock = jest.fn();
Expand Down

0 comments on commit 0cced80

Please sign in to comment.