Skip to content

Commit

Permalink
Don't clear development mode if not set
Browse files Browse the repository at this point in the history
  • Loading branch information
monkbroc committed Jun 28, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent df4af0e commit 7a2de6e
Showing 2 changed files with 38 additions and 12 deletions.
18 changes: 12 additions & 6 deletions src/cmd/device-protection.js
Original file line number Diff line number Diff line change
@@ -180,11 +180,13 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {

if (onlineMode) {
const success = await this._markAsDevelopmentDevice(false);
addToOutput.push(success ?
// TODO: Improve these lines
`Device removed from development mode to maintain current settings.${os.EOL}` :
`Failed to remove device from development mode. Device protection may be disabled on next cloud connection.${os.EOL}`
);
if (typeof success !== 'undefined') {
addToOutput.push(success ?
// TODO: Improve these lines
`Device removed from development mode to maintain current settings.${os.EOL}` :
`Failed to remove device from development mode. Device protection may be disabled on next cloud connection.${os.EOL}`
);
}
}
}));
} catch (error) {
@@ -257,11 +259,15 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
*
* @async
* @param {boolean} state - The state to set for the development device.
* @returns {Promise<boolean>} True if the device was successfully marked, false otherwise.
* @returns {Promise<boolean|undefined>} Undefined if no need to change mode, true if the mode was successfully changed, false otherwise.
*/
async _markAsDevelopmentDevice(state) {
try {
if (this.productId) {
const data = await this.api.getDeviceAttributes(this.deviceId, this.productId);
if (data.development === state) {
return;
}
await this.api.markAsDevelopmentDevice(this.deviceId, state, this.productId);
return true;
}
32 changes: 26 additions & 6 deletions src/cmd/device-protection.test.js
Original file line number Diff line number Diff line change
@@ -209,16 +209,18 @@ describe('DeviceProtectionCommands', () => {
});

describe('_markAsDevelopmentDevice', () => {
it('should mark the device as a development device', async () => {
it('clears the device as a development device', async () => {
let attributes = { development: true };
deviceProtectionCommands.productId = 12345;
deviceProtectionCommands.api = {
getDeviceAttributes: sinon.stub().resolves(attributes),
markAsDevelopmentDevice: sinon.stub().resolves()
};

let error;
let res;
try {
res = await deviceProtectionCommands._markAsDevelopmentDevice(true);
res = await deviceProtectionCommands._markAsDevelopmentDevice(false);
} catch (e) {
error = e;
}
@@ -227,12 +229,30 @@ describe('DeviceProtectionCommands', () => {
expect(res).to.eql(true);
});

it('should return false if the product ID is not available', async () => {
deviceProtectionCommands.productId = null;
it('does not clear development mode if not set', async () => {
let attributes = { development: false };
deviceProtectionCommands.productId = 12345;
deviceProtectionCommands.api = {
getDeviceAttributes: sinon.stub().resolves(attributes),
markAsDevelopmentDevice: sinon.stub().resolves()
};

let error;
let res;
try {
res = await deviceProtectionCommands._markAsDevelopmentDevice(false);
} catch (e) {
error = e;
}

expect(deviceProtectionCommands.api.markAsDevelopmentDevice).to.not.have.been.called;
expect(error).to.be.undefined;
expect(res).to.be.undefined;
});

it('returns false if the product ID is not available', async () => {
deviceProtectionCommands.productId = null;

let error;
let res;
try {
@@ -245,10 +265,10 @@ describe('DeviceProtectionCommands', () => {
expect(res).to.eql(false);
});

it('should return false if an error occurs', async () => {
it('returns false if an error occurs', async () => {
deviceProtectionCommands.productId = 12345;
deviceProtectionCommands.api = {
markAsDevelopmentDevice: sinon.stub().rejects(new Error('random error'))
getDeviceAttributes: sinon.stub().rejects(new Error('random error'))
};

let error;

0 comments on commit 7a2de6e

Please sign in to comment.