fix: Cannot read property 'protocol' of undefined (#5377)

* add the buildUrlImage mirroring the web and added unit tests

* add the comments

* rename the file buildImageURL

* minor tweak iurl definition

* remove the old logic of tmp.image and user only the buildImageUrl

* add the url polyfill to work properly on react native

* minor tweak unit test

* refactor isValidUrl

* fix the e2e tests
This commit is contained in:
Reinaldo Neto 2023-12-13 12:50:03 -03:00 committed by GitHub
parent fd1827f618
commit 7934141d31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 291 additions and 13 deletions

View File

@ -27,7 +27,7 @@ export interface IUrlFromServer {
headers: {
contentType: string;
};
parsedUrl: {
parsedUrl?: {
host: string;
hash: any;
pathname: string;

View File

@ -0,0 +1,20 @@
import { buildImageURL } from './buildImageURL';
// https://github.com/RocketChat/Rocket.Chat/blob/5c145e3170f04e341be93a2a60f09b6cbdc46c73/apps/meteor/tests/unit/client/views/room/MessageList/lib/buildImageURL.spec.ts#L8
describe('buildImageURL', () => {
const testCases = [
[
'https://open.rocket.chat/avatar/rocket.cat',
'https://open.rocket.chat/avatar/rocket.cat',
'https://open.rocket.chat/direct/NNNNnnnnNNNNnnnnfrocket.cat'
],
['https://open.rocket.chat/assets/favicon_512.png', 'assets/favicon_512.png', 'https://open.rocket.chat/channel/general'],
['https://open.rocket.chat/assets/favicon_512.png', '/assets/favicon_512.png', 'https://open.rocket.chat/channel/general'],
['https://open.rocket.chat/assets/favicon_512.png', '//assets/favicon_512.png', 'https://open.rocket.chat/channel/general/']
] as const;
it.each(testCases)('should return %s for %s', (expectedResult, metaImgUrl, linkUrl) => {
const result = buildImageURL(linkUrl, metaImgUrl);
expect(result).toBe(expectedResult);
});
});

View File

@ -0,0 +1,16 @@
import { URL } from 'react-native-url-polyfill';
import { isValidUrl } from './isValidUrl';
// https://github.com/RocketChat/Rocket.Chat/blob/5c145e3170f04e341be93a2a60f09b6cbdc46c73/apps/meteor/client/components/message/content/urlPreviews/buildImageURL.ts#L3
export const buildImageURL = (url: string, imageUrl: string): string => {
if (isValidUrl(imageUrl)) {
return imageUrl;
}
const { origin } = new URL(url);
const imgURL = `${origin}/${imageUrl}`;
const normalizedUrl = imgURL.replace(/([^:]\/)\/+/gm, '$1');
return normalizedUrl;
};

View File

@ -0,0 +1,9 @@
import { URL } from 'react-native-url-polyfill';
export const isValidUrl = (link: string): boolean => {
try {
return Boolean(new URL(link));
} catch (error) {
return false;
}
};

View File

@ -0,0 +1,234 @@
import { IUrl, IUrlFromServer } from '../../../definitions';
import parseUrls from './parseUrls';
const tmpImageValidLink = {
urls: [
{
url: 'https://meet.google.com/cbr-hysk-azn?pli=1&authuser=1',
meta: {
pageTitle: 'Meet',
description:
'Real-time meetings by Google. Using your browser, share your video, desktop, and presentations with teammates and customers.',
twitterCard: 'summary',
ogUrl: 'https://meet.google.com',
ogType: 'website',
ogTitle: 'Meet',
ogDescription:
'Real-time meetings by Google. Using your browser, share your video, desktop, and presentations with teammates and customers.',
ogImage: 'https://fonts.gstatic.com/s/i/productlogos/meet_2020q4/v1/web-96dp/logo_meet_2020q4_color_2x_web_96dp.png'
},
headers: {
contentType: 'text/html; charset=utf-8'
},
parsedUrl: {
host: 'meet.google.com',
hash: null,
pathname: '/cbr-hysk-azn',
protocol: 'https:',
port: null,
query: 'pli=1&authuser=1',
search: '?pli=1&authuser=1',
hostname: 'meet.google.com'
}
}
],
expectedResult: [
{
_id: 0,
title: 'Meet',
description:
'Real-time meetings by Google. Using your browser, share your video, desktop, and presentations with teammates and customers.',
image: 'https://fonts.gstatic.com/s/i/productlogos/meet_2020q4/v1/web-96dp/logo_meet_2020q4_color_2x_web_96dp.png',
url: 'https://meet.google.com/cbr-hysk-azn?pli=1&authuser=1'
}
]
} as { urls: IUrlFromServer[]; expectedResult: IUrl[] };
const tmpImagePointingToAnAsset = {
urls: [
{
url: 'https://open.rocket.chat/',
meta: {
pageTitle: 'Rocket.Chat',
msapplicationTileImage: 'assets/tile_144.png',
msapplicationConfig: 'images/browserconfig.xml',
ogImage: 'assets/favicon_512.png',
twitterImage: 'assets/favicon_512.png',
appleMobileWebAppTitle: 'Rocket.Chat',
fbAppId: '835103589938459'
},
headers: {
contentType: 'text/html; charset=utf-8'
}
}
],
expectedResult: [
{
_id: 0,
title: 'Rocket.Chat',
image: 'https://open.rocket.chat/assets/favicon_512.png',
url: 'https://open.rocket.chat/'
}
]
} as unknown as { urls: IUrlFromServer[]; expectedResult: IUrl[] };
const tmpImagePointingToAnAssetThatStartsWithSlashWithoutParsedUrl = {
urls: [
{
url: 'https://open.rocket.chat/',
meta: {
pageTitle: 'Rocket.Chat',
msapplicationTileImage: 'assets/tile_144.png',
msapplicationConfig: 'images/browserconfig.xml',
ogImage: '/assets/favicon_512.png',
twitterImage: '/assets/favicon_512.png',
appleMobileWebAppTitle: 'Rocket.Chat',
fbAppId: '835103589938459'
},
headers: {
contentType: 'text/html; charset=utf-8'
}
}
],
expectedResult: [
{
_id: 0,
title: 'Rocket.Chat',
image: 'https://open.rocket.chat/assets/favicon_512.png',
url: 'https://open.rocket.chat/'
}
]
} as unknown as { urls: IUrlFromServer[]; expectedResult: IUrl[] };
const tmpImagePointingToAnAssetThatStartsWithSlashWithParsedUrl = {
urls: [
{
url: 'https://open.rocket.chat/',
meta: {
pageTitle: 'Rocket.Chat',
msapplicationTileImage: 'assets/tile_144.png',
msapplicationConfig: 'images/browserconfig.xml',
ogImage: '/assets/favicon_512.png',
twitterImage: '/assets/favicon_512.png',
appleMobileWebAppTitle: 'Rocket.Chat',
fbAppId: '835103589938459'
},
headers: {
contentType: 'text/html; charset=utf-8'
},
parsedUrl: {
hash: '',
host: 'open.rocket.chat',
hostname: 'open.rocket.chat',
pathname: '/',
port: '',
protocol: 'https:',
search: '',
query: 'pli=1&authuser=1'
}
}
],
expectedResult: [
{
_id: 0,
title: 'Rocket.Chat',
image: 'https://open.rocket.chat/assets/favicon_512.png',
url: 'https://open.rocket.chat/'
}
]
} as unknown as { urls: IUrlFromServer[]; expectedResult: IUrl[] };
const tmpImagePointingToAnAssetThatStartsWithDoubleSlashWithParsedUrl = {
urls: [
{
url: 'https://open.rocket.chat/',
meta: {
pageTitle: 'Rocket.Chat',
msapplicationTileImage: 'assets/tile_144.png',
msapplicationConfig: 'images/browserconfig.xml',
ogImage: '//assets/favicon_512.png',
twitterImage: '//assets/favicon_512.png',
appleMobileWebAppTitle: 'Rocket.Chat',
fbAppId: '835103589938459'
},
headers: {
contentType: 'text/html; charset=utf-8'
},
parsedUrl: {
host: 'open.rocket.chat',
hash: null,
protocol: 'https:',
port: null,
hostname: 'open.rocket.chat'
}
}
],
expectedResult: [
{
_id: 0,
title: 'Rocket.Chat',
image: 'https://open.rocket.chat/assets/favicon_512.png',
url: 'https://open.rocket.chat/'
}
]
} as unknown as { urls: IUrlFromServer[]; expectedResult: IUrl[] };
const tmpImagePointingToAnAssetThatStartsWithDoubleSlashWithoutParsedUrl = {
urls: [
{
url: 'https://open.rocket.chat/',
meta: {
pageTitle: 'Rocket.Chat',
msapplicationTileImage: 'assets/tile_144.png',
msapplicationConfig: 'images/browserconfig.xml',
ogImage: '//assets/favicon_512.png',
twitterImage: '//assets/favicon_512.png',
appleMobileWebAppTitle: 'Rocket.Chat',
fbAppId: '835103589938459'
},
headers: {
contentType: 'text/html; charset=utf-8'
}
}
],
expectedResult: [
{
_id: 0,
title: 'Rocket.Chat',
image: 'https://open.rocket.chat/assets/favicon_512.png',
url: 'https://open.rocket.chat/'
}
]
} as unknown as { urls: IUrlFromServer[]; expectedResult: IUrl[] };
describe('parseUrls function', () => {
it('test when a tmp.image is a valid link', () => {
const result = parseUrls(tmpImageValidLink.urls);
expect(result).toEqual(tmpImageValidLink.expectedResult);
});
it('test when a tmp.image is assets/favicon_512.png', () => {
const result = parseUrls(tmpImagePointingToAnAsset.urls);
expect(result).toEqual(tmpImagePointingToAnAsset.expectedResult);
});
it('test when a tmp.image is /assets/favicon_512.png and url with parsedUrl, parsedUrl.protocol and parsedUrl.host', () => {
const result = parseUrls(tmpImagePointingToAnAssetThatStartsWithSlashWithParsedUrl.urls);
expect(result).toEqual(tmpImagePointingToAnAssetThatStartsWithSlashWithParsedUrl.expectedResult);
});
it('test when a tmp.image is /assets/favicon_512.png and url without parsedUrl', () => {
const result = parseUrls(tmpImagePointingToAnAssetThatStartsWithSlashWithoutParsedUrl.urls);
expect(result).toEqual(tmpImagePointingToAnAssetThatStartsWithSlashWithoutParsedUrl.expectedResult);
});
it('test when a tmp.image is //assets/favicon_512.png and url with parsedUrl', () => {
const result = parseUrls(tmpImagePointingToAnAssetThatStartsWithDoubleSlashWithParsedUrl.urls);
expect(result).toEqual(tmpImagePointingToAnAssetThatStartsWithDoubleSlashWithParsedUrl.expectedResult);
});
it('test when a tmp.image is //assets/favicon_512.png and url without parsedUrl', () => {
const result = parseUrls(tmpImagePointingToAnAssetThatStartsWithDoubleSlashWithoutParsedUrl.urls);
expect(result).toEqual(tmpImagePointingToAnAssetThatStartsWithDoubleSlashWithoutParsedUrl.expectedResult);
});
});

View File

@ -1,4 +1,5 @@
import { IUrl, IUrlFromServer } from '../../../definitions';
import { buildImageURL } from './buildImageURL';
export default (urls: IUrlFromServer[]): IUrl[] =>
urls
@ -15,11 +16,7 @@ export default (urls: IUrlFromServer[]): IUrl[] =>
}
tmp.image = decodedOgImage || meta.twitterImage || meta.oembedThumbnailUrl;
if (tmp.image) {
if (tmp.image.indexOf('//') === 0) {
tmp.image = `${url.parsedUrl.protocol}${tmp.image}`;
} else if (tmp.image.indexOf('/') === 0 && url.parsedUrl && url.parsedUrl.host) {
tmp.image = `${url.parsedUrl.protocol}//${url.parsedUrl.host}${tmp.image}`;
}
tmp.image = buildImageURL(url.url, tmp.image);
}
tmp.url = url.url;
return tmp;

View File

@ -57,7 +57,9 @@ describe('Join protected room', () => {
.withTimeout(5000);
});
it('should join room', async () => {
// Users on servers version 6.5 cannot access the protected room
// TODO: remove the skip when the backend fixes the problem
it.skip('should join room', async () => {
await openJoinCode();
await element(by.id('join-code-input')).replaceText(joinCode);
await element(by.id('join-code-submit')).tap();
@ -71,7 +73,7 @@ describe('Join protected room', () => {
await expect(element(by.id('room-view-join'))).toBeNotVisible();
});
it('should send message', async () => {
it.skip('should send message', async () => {
await mockMessage(`${random()}message`);
});
});

View File

@ -504,24 +504,24 @@ describe('Room actions screen', () => {
it('should set/remove as mute', async () => {
await openActionSheet(otherUser.username);
await element(by[textMatcher]('Mute')).atIndex(0).tap();
await element(by[textMatcher]('Disable writing in room')).atIndex(0).tap();
await waitFor(element(by[textMatcher]('Are you sure?')))
.toExist()
.withTimeout(5000);
await element(by[textMatcher]('Mute').and(by.type(alertButtonType))).tap();
await element(by[textMatcher]('Disable writing in room').and(by.type(alertButtonType))).tap();
await waitForToast();
await openActionSheet(otherUser.username);
await element(by[textMatcher]('Unmute')).atIndex(0).tap();
await element(by[textMatcher]('Enable writing in room')).atIndex(0).tap();
await waitFor(element(by[textMatcher]('Are you sure?')))
.toExist()
.withTimeout(5000);
await element(by[textMatcher]('Unmute').and(by.type(alertButtonType))).tap();
await element(by[textMatcher]('Enable writing in room').and(by.type(alertButtonType))).tap();
await waitForToast();
await openActionSheet(otherUser.username);
// Tests if Remove as mute worked
await waitFor(element(by[textMatcher]('Mute')))
await waitFor(element(by[textMatcher]('Disable writing in room')))
.toExist()
.withTimeout(5000);
await closeActionSheet();