Skip to content

Commit

Permalink
Rework E2E tests to avoid Open Device
Browse files Browse the repository at this point in the history
  • Loading branch information
monkbroc committed Jun 27, 2024
1 parent fa277b4 commit a94f8fb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/cli/device-protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = ({ commandProcessor, root }) => {
examples: {
'$0 $command': 'Puts a Protected Device to Service Mode',
},
epilogue: 'A Protected Device in Service Mode allows any command to be performed on it that can be performed on an open device like flashing firmware or serial monitor.'
epilogue: 'A Protected Device in Service Mode allows any command to be performed on it that can be performed on an Open Device like flashing firmware or serial monitor.'
});

commandProcessor.createCommand(deviceProtection, 'enable', 'Enables device protection', {
Expand Down
26 changes: 11 additions & 15 deletions src/cmd/device-protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
* Retrieves and displays the protection status of the device.
*
* This method assumes the device is in normal mode and not in DFU mode. It retrieves the current protection status and
* constructs a message indicating whether the device is protected, in service mode, or not protected.
* constructs a message indicating whether the device is Protected, in Service Mode, or Open
* The device protection status is then displayed in the console.
*
* @async
Expand All @@ -49,11 +49,11 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
let helper;

if (s.overridden) {
res = 'Protected device (service mode)';
helper = `Run ${chalk.yellow('particle device-protection enable')} to take the device out of service mode.`;
res = 'Protected Device (Service Mode)';
helper = `Run ${chalk.yellow('particle device-protection enable')} to take the device out of Service Mode.`;
} else if (s.protected) {
res = 'Protected device';
helper = `Run ${chalk.yellow('particle device-protection disable')} to put the device in service mode.`;
res = 'Protected Device';
helper = `Run ${chalk.yellow('particle device-protection disable')} to put the device in Service Mode.`;
} else {
res = 'Open device';
helper = `Run ${chalk.yellow('particle device-protection enable')} to protect the device.`;
Expand All @@ -77,14 +77,11 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
/**
* Disables protection on the device.
*
* This method checks the current protection status of the device and proceeds to disable protection
* if the device is protected. If the device is not protected or is already in service mode,
* appropriate messages are logged to the console. If the `open` parameter is true, the device will be
* flashed with an unprotected bootloader and marked as a development device to prevent re-enabling protection.
* This method checks the current protection status of the device and proceeds to put the device in Service Mode
* if the device is protected. If the device is not protected or is already in Service Mode,
* appropriate messages are logged to the console.
*
* @async
* @param {Object} [options={}] - Options for disabling protection.
* @param {boolean} [options.open] - If true, flashes an unprotected bootloader and marks the device as a development device to make it an open device.
* @returns {Promise<void>}
* @throws {Error} - Throws an error if any of the async operations fail.
*/
Expand Down Expand Up @@ -122,7 +119,6 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
await this.device.unprotectDevice({ action: 'confirm', serverSignature, serverPublicKeyFingerprint });

addToOutput.push(`${deviceStr} is now in Service Mode.${os.EOL}A Protected Device stays in Service Mode for a total of 20 reboots or 24 hours.${os.EOL}`);

} catch (error) {
throw new Error(`Failed to disable device protection: ${error.message}${os.EOL}`);
}
Expand Down Expand Up @@ -157,12 +153,12 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {

if (s.overridden) {
await this.device.unprotectDevice({ action: 'reset' });
addToOutput.push(`${deviceStr} is now a protected device.${os.EOL}`);
addToOutput.push(`${deviceStr} is now a Protected Device.${os.EOL}`);
return;
}

if (s.protected) {
addToOutput.push(`${deviceStr} is already a protected device.${os.EOL}`);
addToOutput.push(`${deviceStr} is already a Protected Device.${os.EOL}`);
return;
}

Expand All @@ -179,7 +175,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
}

await this._flashBootloader(protectedBinary);
addToOutput.push(`${deviceStr} is now a protected device.${os.EOL}`);
addToOutput.push(`${deviceStr} is now a Protected Device.${os.EOL}`);

const success = await this._markAsDevelopmentDevice(false);
addToOutput.push(success ?
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/device-protection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('DeviceProtectionCommands', () => {
expect(deviceProtectionCommands.api.unprotectDevice).to.have.been.calledTwice;
});

it('handles already open devices', async () => {
it('handles already Open Devices', async () => {
sinon.stub(deviceProtectionCommands, '_getDeviceProtection')
.onFirstCall().resolves({ protected: false, overridden: false });
sinon.stub(deviceProtectionCommands, '_getDeviceString').resolves('[123456789abcdef] (Product 12345)');
Expand Down Expand Up @@ -105,7 +105,7 @@ describe('DeviceProtectionCommands', () => {
});


it('handles already protected devices', async () => {
it('handles already Protected Devices', async () => {
sinon.stub(deviceProtectionCommands, '_getDeviceProtection').resolves({
protected: true,
overridden: false
Expand All @@ -119,7 +119,7 @@ describe('DeviceProtectionCommands', () => {
expect(deviceProtectionCommands._isDeviceProtectionActiveInProduct).to.not.have.been.called;
});

it('protects a service mode device', async () => {
it('protects a Service Mode device', async () => {
sinon.stub(deviceProtectionCommands, '_getDeviceProtection').resolves({
protected: true,
overridden: true
Expand All @@ -134,7 +134,7 @@ describe('DeviceProtectionCommands', () => {
expect(deviceProtectionCommands._isDeviceProtectionActiveInProduct).to.not.have.been.called;
});

it('does not protect an open device if it is not in a product', async () => {
it('does not protect an Open Device if it is not in a product', async () => {
sinon.stub(deviceProtectionCommands, '_getDeviceProtection').resolves({
protected: false,
overridden: false
Expand Down
56 changes: 10 additions & 46 deletions test/e2e/device-protection.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ const {
PRODUCT_01_DEVICE_01_ID,
PRODUCT_01_ID
} = require('../lib/env');
const { delay } = require('../../src/lib/utilities');

const FLASH_TIME = 15000;

describe('Device Protection Commands [@device,@device-protection]', () => {
describe.only('Device Protection Commands [@device,@device-protection]', () => {
const help = [
'Manage device protection',
'Usage: particle device-protection <command>',
Expand All @@ -25,14 +22,8 @@ describe('Device Protection Commands [@device,@device-protection]', () => {
''
];


before(async () => {
await cli.setTestProfileAndLogin();
// Ensure device starts as an Open Device
await cli.run(['device-protection', 'disable', '--open']);

// give time to flash the device
await delay(FLASH_TIME);
});

after(async () => {
Expand Down Expand Up @@ -65,72 +56,45 @@ describe('Device Protection Commands [@device,@device-protection]', () => {
});

describe('DeviceProtection Commands', () => {
it('Gets the current device protection status', async () => {
const { stdout, stderr, exitCode } = await cli.run(['device-protection', 'status']);

expect(stdout).to.include(`[${PRODUCT_01_DEVICE_01_ID}] (Product ${PRODUCT_01_ID}): Open device\nRun particle device-protection enable to protect the device.`);
expect(stderr).to.equal('');
expect(exitCode).to.equal(0);
});

it('Attempts to disable protection status on an open device', async () => {
const { stdout, stderr, exitCode } = await cli.run(['device-protection', 'disable']);

expect(stdout).to.include(`[${PRODUCT_01_DEVICE_01_ID}] (Product ${PRODUCT_01_ID}) is not a protected device.`);
expect(stderr).to.equal('');
expect(exitCode).to.equal(0);
});

it('Enables protection status on the device', async () => {
// Put device in Service Mode
await cli.run(['device-protection', 'disable']);

const { stdout, stderr, exitCode } = await cli.run(['device-protection', 'enable']);

expect(stdout).to.include(`[${PRODUCT_01_DEVICE_01_ID}] (Product ${PRODUCT_01_ID}) is now a protected device.\nDevice removed from development mode to maintain current settings.`);
expect(stdout).to.include(`[${PRODUCT_01_DEVICE_01_ID}] (Product ${PRODUCT_01_ID}) is now a Protected Device.\nDevice removed from development mode to maintain current settings.`);
expect(stderr).to.equal('');
expect(exitCode).to.equal(0);

// give time to flash the device
await delay(FLASH_TIME);
});

it('Gets the current status of a Protected Device', async () => {
const { stdout, stderr, exitCode } = await cli.run(['device-protection', 'status']);

expect(stdout).to.include(`[${PRODUCT_01_DEVICE_01_ID}] (Product ${PRODUCT_01_ID}): Protected device\nRun particle device-protection disable to put the device in service mode.`);
expect(stdout).to.include(`[${PRODUCT_01_DEVICE_01_ID}] (Product ${PRODUCT_01_ID}): Protected Device\nRun particle device-protection disable to put the device in Service Mode.`);
expect(stderr).to.equal('');
expect(exitCode).to.equal(0);
});

it('Attempts to enable protection status on a protected device', async () => {
it('Attempts to enable protection status on a Protected Device', async () => {
const { stdout, stderr, exitCode } = await cli.run(['device-protection', 'enable']);

expect(stdout).to.include(`[${PRODUCT_01_DEVICE_01_ID}] (Product ${PRODUCT_01_ID}) is already a protected device.`);
expect(stdout).to.include(`[${PRODUCT_01_DEVICE_01_ID}] (Product ${PRODUCT_01_ID}) is already a Protected Device.`);
expect(stderr).to.equal('');
expect(exitCode).to.equal(0);
});

it('Puts the device in service mode', async () => {
const { stdout, stderr, exitCode } = await cli.run(['device-protection', 'disable']);

expect(stdout).to.include(`[${PRODUCT_01_DEVICE_01_ID}] (Product ${PRODUCT_01_ID}) is now in service mode.\nA protected device stays in service mode for a total of 20 reboots or 24 hours.`);
expect(stdout).to.include(`[${PRODUCT_01_DEVICE_01_ID}] (Product ${PRODUCT_01_ID}) is now in Service Mode.\nA Protected Device stays in Service Mode for a total of 20 reboots or 24 hours.`);
expect(stderr).to.equal('');
expect(exitCode).to.equal(0);
});

it('Turns the device to an open device', async () => {
const { stdout, stderr, exitCode } = await cli.run(['device-protection', 'disable', '--open']);

expect(stdout).to.include(`[${PRODUCT_01_DEVICE_01_ID}] (Product ${PRODUCT_01_ID}) is now an open device.\nDevice placed in development mode to maintain current settings.`);
expect(stderr).to.equal('');
expect(exitCode).to.equal(0);

// give time to flash the device
await delay(FLASH_TIME);
});

it('Gets the current status of the device', async () => {
const { stdout, stderr, exitCode } = await cli.run(['device-protection', 'status']);

expect(stdout).to.include(`[${PRODUCT_01_DEVICE_01_ID}] (Product ${PRODUCT_01_ID}): Open device\nRun particle device-protection enable to protect the device.`);
expect(stdout).to.include(`[${PRODUCT_01_DEVICE_01_ID}] (Product ${PRODUCT_01_ID}): Protected Device (Service Mode)\nRun particle device-protection enable to protect the device.`);
expect(stderr).to.equal('');
expect(exitCode).to.equal(0);
});
Expand Down

0 comments on commit a94f8fb

Please sign in to comment.