Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improved non token supported device handling #597

Merged
merged 7 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/appium-tizen-tv-driver/lib/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class TizenTVDriver extends BaseDriver {
`but it may be ignorable. Proceeding the app installation.`);
}
if (_.isArray(installedPackages) && !installedPackages.includes(caps.appPackage)) {
throw new errors.SessionNotCreatedError(`${caps.appPackage} does not exist on the device.`);
log.info(`${caps.appPackage} might not exist on the device, or the TV model is old thus no installed app information existed.`);
}
}

Expand Down
7 changes: 6 additions & 1 deletion packages/appium-tizen-tv-driver/lib/rc-pair.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ export async function pairRemote({host, port}) {
console.log(token); // eslint-disable-line no-console
return;
}
throw new Error(`Could not retrieve token; please try allowing the remote again`);

if (await rc.isTokenSupportedDevice()) {
throw new Error(`Could not retrieve token; please try allowing the remote again`);
}

console.log('The device may not support token-based authentication. Allowing the pop-up notification is sufficient.'); // eslint-disable-line no-console
} finally {
await rc.disconnect();
}
Expand Down
44 changes: 42 additions & 2 deletions packages/tizen-remote/lib/tizen-remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import pRetry from 'p-retry';
import WebSocket from 'ws';
import {KeyCommand, TextCommand} from './command';
import {Keys} from './keys';
import got from 'got';

export {Keys};

Expand Down Expand Up @@ -288,6 +289,12 @@ export class TizenRemote extends createdTypedEmitterClass() {
*/
#tokenCache;

/**
* If the target device supports token.
* @type {boolean | undefined}
*/
#tokenSupportCache;

/**
* Whether or not to persist tokens to the cache
* @type {boolean}
Expand Down Expand Up @@ -337,6 +344,8 @@ export class TizenRemote extends createdTypedEmitterClass() {
this.#handshakeTimeout = opts.handshakeTimeout ?? constants.DEFAULT_HANDSHAKE_TIMEOUT;
this.#handshakeRetries = opts.handshakeRetries ?? constants.DEFAULT_HANDSHAKE_RETRIES;
this.#tokenTimeout = opts.tokenTimeout ?? constants.DEFAULT_TOKEN_TIMEOUT;

this.#tokenSupportCache = undefined;
}

/**
Expand Down Expand Up @@ -374,6 +383,33 @@ export class TizenRemote extends createdTypedEmitterClass() {
*/
get url() {
return this.#url.toString();
}

/**
* Return True if the target device has 'TokenAuthSupport' param for the api/v2 endpoint.
* No 'TokenAuthSupport' indicates the device does not require "token".
* @returns {Promise<boolean>}
*/
async isTokenSupportedDevice() {
if (_.isBoolean(this.#tokenSupportCache)) {
return this.#tokenSupportCache;
}
try {
const deviceData = await got.get(`http://${this.#host}:8001/api/v2/`).json();
this.#tokenSupportCache = this._getDeviceSupportsTokens(deviceData) === 'true';
return this.#tokenSupportCache;
} catch {
// defaults to true for newer TVs.
return true;
}
}
/**
* Private. Accessible for testing
* @param {any} jsonBody
* @returns {'true'|'false'|undefined}
*/
_getDeviceSupportsTokens(jsonBody) {
return jsonBody?.device?.TokenAuthSupport;
}

/**
Expand Down Expand Up @@ -689,7 +725,7 @@ export class TizenRemote extends createdTypedEmitterClass() {
*
* If a new token must be requested, expect to wait _at least_ thirty (30) seconds.
* @param {NoConnectOption & {force?: boolean}} opts
* @returns {Promise<string>}
* @returns {Promise<string | undefined>}
*/
async getToken({noConnect = false, force = false} = {}) {
if (!force) {
Expand Down Expand Up @@ -719,7 +755,11 @@ export class TizenRemote extends createdTypedEmitterClass() {
this.emit(Event.TOKEN, token);
return token;
}
throw new Error(`Could not get token; server responded with: ${format('%O', res)}`);
if (await this.isTokenSupportedDevice()) {
throw new Error(`Could not get token; server responded with: ${format('%O', res)}`);
}
this.#debug('The device may not support token as old model.');
return;
} finally {
this.#onWs(WsEvent.MESSAGE, this.#updateTokenListener, {context: this});
}
Expand Down
1 change: 1 addition & 0 deletions packages/tizen-remote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@types/ws": "8.5.5",
"debug": "4.3.4",
"delay": "4.4.1",
"got": "11.8.6",
"lodash": "4.17.21",
"p-retry": "4.6.2",
"strict-event-emitter-types": "2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/tizen-remote/test/e2e/tizen-remote.e2e.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ describe('websocket behavior', function () {
/** @type {import('../../lib/types').TizenRemoteOptions} */
let remoteOpts;

/** @type {string} */
/** @type {string|undefined} */
let token;

/**
* Connects, gets a token, disconnects.
* @param {number} port
* @returns {Promise<string>}
* @returns {Promise<string|undefined>}
*/
async function getInitialToken(port) {
const remote = new TizenRemote(HOST, {
Expand Down
130 changes: 130 additions & 0 deletions packages/tizen-remote/test/unit/tizen-remote.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,5 +362,135 @@ describe('TizenRemote', function () {
});
});
});

describe('isTokenSupportedDevice()', function () {
beforeEach(function () {
remote = new TizenRemote('host');
});
describe('with TokenAuthSupport', function () {
it('should true', function () {
const r = remote._getDeviceSupportsTokens({
'device': {
'FrameTVSupport': 'false',
'GamePadSupport': 'true',
'ImeSyncedSupport': 'true',
'Language': 'en_US',
'OS': 'Tizen',
'PowerState': 'on',
'TokenAuthSupport': 'true',
'VoiceSupport': 'true',
'WallScreenRatio': '0',
'WallService': 'false',
'countryCode': 'CA',
'description': 'Samsung DTV RCR',
'developerIP': '192.168.11.147',
'developerMode': '1',
'duid': 'uuid:94a93b85-fe59-46aa-9007-6d25b52df02b',
'firmwareVersion': 'Unknown',
'id': 'uuid:94a93b85-fe59-46aa-9007-6d25b52df02b',
'ip': '192.168.11.41',
'model': '19_MUSEM_UHD',
'modelName': 'UN49RU8000FXZC',
'name': 'tv',
'networkType': 'wired',
'resolution': '3840x2160',
'smartHubAgreement': 'true',
'type': 'Samsung SmartTV',
'udn': 'uuid:94a93b85-fe59-46aa-9007-6d25b52df02b',
'wifiMac': '00:7c:2d:d5:22:f3'
},
'id': 'uuid:94a93b85-fe59-46aa-9007-6d25b52df02b',
'isSupport': '{"DMP_DRM_PLAYREADY":"false","DMP_DRM_WIDEVINE":"false","DMP_available":"true","EDEN_available":"true","FrameTVSupport":"false","ImeSyncedSupport":"true","TokenAuthSupport":"true","remote_available":"true","remote_fourDirections":"true","remote_touchPad":"true","remote_voiceControl":"true"}\n',
'name': 'tv',
'remote': '1.0',
'type': 'Samsung SmartTV',
'uri': 'http://192.168.11.41:8001/api/v2/',
'version': '2.0.25'
});
expect(r, 'to equal', 'true');
});
it('should false', function () {
const r = remote._getDeviceSupportsTokens({
'device': {
'FrameTVSupport': 'false',
'GamePadSupport': 'true',
'ImeSyncedSupport': 'true',
'Language': 'en_US',
'OS': 'Tizen',
'PowerState': 'on',
'TokenAuthSupport': 'false',
'VoiceSupport': 'true',
'WallScreenRatio': '0',
'WallService': 'false',
'countryCode': 'CA',
'description': 'Samsung DTV RCR',
'developerIP': '192.168.11.147',
'developerMode': '1',
'duid': 'uuid:94a93b85-fe59-46aa-9007-6d25b52df02b',
'firmwareVersion': 'Unknown',
'id': 'uuid:94a93b85-fe59-46aa-9007-6d25b52df02b',
'ip': '192.168.11.41',
'model': '19_MUSEM_UHD',
'modelName': 'UN49RU8000FXZC',
'name': 'tv',
'networkType': 'wired',
'resolution': '3840x2160',
'smartHubAgreement': 'true',
'type': 'Samsung SmartTV',
'udn': 'uuid:94a93b85-fe59-46aa-9007-6d25b52df02b',
'wifiMac': '00:7c:2d:d5:22:f3'
},
'id': 'uuid:94a93b85-fe59-46aa-9007-6d25b52df02b',
'isSupport': '{"DMP_DRM_PLAYREADY":"false","DMP_DRM_WIDEVINE":"false","DMP_available":"true","EDEN_available":"true","FrameTVSupport":"false","ImeSyncedSupport":"true","TokenAuthSupport":"false","remote_available":"true","remote_fourDirections":"true","remote_touchPad":"true","remote_voiceControl":"true"}\n',
'name': 'tv',
'remote': '1.0',
'type': 'Samsung SmartTV',
'uri': 'http://192.168.11.41:8001/api/v2/',
'version': '2.0.25'
});
expect(r, 'to equal', 'false');
});
it('should null', function () {
const r = remote._getDeviceSupportsTokens({
'device': {
'FrameTVSupport': 'false',
'GamePadSupport': 'true',
'ImeSyncedSupport': 'true',
'Language': 'en_US',
'OS': 'Tizen',
'PowerState': 'on',
'VoiceSupport': 'true',
'WallScreenRatio': '0',
'WallService': 'false',
'countryCode': 'CA',
'description': 'Samsung DTV RCR',
'developerIP': '192.168.11.147',
'developerMode': '1',
'duid': 'uuid:94a93b85-fe59-46aa-9007-6d25b52df02b',
'firmwareVersion': 'Unknown',
'id': 'uuid:94a93b85-fe59-46aa-9007-6d25b52df02b',
'ip': '192.168.11.41',
'model': '19_MUSEM_UHD',
'modelName': 'UN49RU8000FXZC',
'name': 'tv',
'networkType': 'wired',
'resolution': '3840x2160',
'smartHubAgreement': 'true',
'type': 'Samsung SmartTV',
'udn': 'uuid:94a93b85-fe59-46aa-9007-6d25b52df02b',
'wifiMac': '00:7c:2d:d5:22:f3'
},
'id': 'uuid:94a93b85-fe59-46aa-9007-6d25b52df02b',
'isSupport': '{"DMP_DRM_PLAYREADY":"false","DMP_DRM_WIDEVINE":"false","DMP_available":"true","EDEN_available":"true","FrameTVSupport":"false","ImeSyncedSupport":"true","remote_available":"true","remote_fourDirections":"true","remote_touchPad":"true","remote_voiceControl":"true"}\n',
'name': 'tv',
'remote': '1.0',
'type': 'Samsung SmartTV',
'uri': 'http://192.168.11.41:8001/api/v2/',
'version': '2.0.25'
});
expect(r, 'to equal', undefined);
});
});
});
});
});