From 0cced8014555dfeed6047e898c46f8fd689dc42b Mon Sep 17 00:00:00 2001 From: Wouter van der Plas Date: Sat, 13 Jan 2024 00:02:17 +0100 Subject: [PATCH] added some more unit tests --- src/utils/api/token.ts | 6 +-- tests/unit/utils/api/getToken.spec.ts | 53 +++++++++++++++++++++------ tests/unit/utils/api/upload.spec.ts | 10 +++++ 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/src/utils/api/token.ts b/src/utils/api/token.ts index 5286d42..ce8ce75 100644 --- a/src/utils/api/token.ts +++ b/src/utils/api/token.ts @@ -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; } @@ -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; } @@ -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('; '); } diff --git a/tests/unit/utils/api/getToken.spec.ts b/tests/unit/utils/api/getToken.spec.ts index 20d2b8e..0289390 100644 --- a/tests/unit/utils/api/getToken.spec.ts +++ b/tests/unit/utils/api/getToken.spec.ts @@ -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; @@ -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'); - }); }); diff --git a/tests/unit/utils/api/upload.spec.ts b/tests/unit/utils/api/upload.spec.ts index 7609aaa..4c80c7b 100644 --- a/tests/unit/utils/api/upload.spec.ts +++ b/tests/unit/utils/api/upload.spec.ts @@ -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();