Skip to content

Commit

Permalink
[tests] more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Jun 21, 2024
1 parent fbdfd5b commit 1d92580
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/cli/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = ({ commandProcessor, root }) => {
params: '<file>',
options: {
'saveTo': {
description: 'Specify the bootloader file to add device protection to'
description: 'Specify the filename for the protected binary'
}
},
handler: (args) => {
Expand Down
40 changes: 37 additions & 3 deletions src/cmd/device-protection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ describe('DeviceProtectionCommands', () => {
expect(deviceProtectionCommands._getDeviceString).to.have.been.calledOnce;
expect(result).to.eql(expectedStatus);
});

it('throws an error while getting the protection status of the device', async () => {
const expectedStatus = {
protected: true,
overridden: false
};

sinon.stub(deviceProtectionCommands, '_getDeviceProtection').rejects(new Error('random error'));

let error;
try {
await deviceProtectionCommands.getStatus();
} catch (e) {
error = e;
}

expect(error).to.be.an.instanceOf(Error);
expect(error.message).to.include('Unable to get device status: random error');
});
});

describe('disableProtection', () => {
Expand All @@ -57,7 +76,7 @@ describe('DeviceProtectionCommands', () => {
expect(deviceProtectionCommands.api.unprotectDevice).to.have.been.calledTwice;
});

it('should disable protection on the device', async () => {
it('should disable protection on the device and turns to an open device', async () => {
sinon.stub(deviceProtectionCommands, '_getDeviceProtection')
.onFirstCall().resolves({ protected: true, overridden: false })
.onSecondCall().resolves({ protected: true, overridden: false });
Expand All @@ -81,12 +100,11 @@ describe('DeviceProtectionCommands', () => {
expect(deviceProtectionCommands._markAsDevelopmentDevice).to.have.been.calledOnce;
});

it('handles 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)');

// Call the method
await deviceProtectionCommands.disableProtection();

expect(deviceProtectionCommands._getDeviceProtection).to.have.been.calledOnce;
Expand Down Expand Up @@ -195,6 +213,22 @@ describe('DeviceProtectionCommands', () => {
expect(error).to.be.an.instanceOf(Error);
expect(error.message).to.include('Device protection feature is not supported on this device');
});

it('throws a random error', async () => {
deviceProtectionCommands.device = {
getProtectionState: sinon.stub().rejects(new Error('random error'))
};

let error;
try {
await deviceProtectionCommands._getDeviceProtection();
} catch (_e) {
error = _e;
}

expect(error).to.be.an.instanceOf(Error);
expect(error.message).to.include('random error');
});
});

describe('_flashBootloader', () => {
Expand Down

0 comments on commit 1d92580

Please sign in to comment.