Skip to content

Commit

Permalink
Add device management methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-mailosaur committed May 12, 2022
1 parent d7d1304 commit 6f0082c
Show file tree
Hide file tree
Showing 5 changed files with 387 additions and 125 deletions.
88 changes: 88 additions & 0 deletions src/mailosaurCommands.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,58 @@ export interface UsageTransactionListResult {
items?: UsageTransaction[];
}

/**
* Mailosaur virtual security device.
*/
export interface Device {
/**
* Unique identifier for the device.
*/
id?: string;
/**
* The name of the device.
*/
name?: string;
}

/**
* Options used to create a new Mailosaur virtual security device.
*/
export interface DeviceCreateOptions {
/**
* A name used to identify the device.
*/
name?: string;
/**
* The base32-encoded shared secret for this device.
*/
sharedSecret?: string;
}

/**
* The result of the device listing operation.
*/
export interface DeviceListResult {
/**
* The individual devices forming the result.
*/
items?: Device[];
}

/**
* Mailosaur virtual security device OTP result.
*/
export interface OtpResult {
/**
* The current one-time password.
*/
code?: string;
/**
* The expiry date/time of the current one-time password.
*/
expires?: Date;
}

declare global {
namespace Cypress {
interface Chainable {
Expand Down Expand Up @@ -856,6 +908,42 @@ declare global {
*/
mailosaurGetUsageTransactions(
): Cypress.Chainable<UsageTransactionListResult>;

/**
* Returns a list of your virtual security devices.
*/
mailosaurListDevices(
): Cypress.Chainable<DeviceListResult>;

/**
* Creates a new virtual security device.
*/
mailosaurCreateDevice(
/**
* Options used to create a new Mailosaur virtual security device.
*/
options: DeviceCreateOptions
): Cypress.Chainable<Device>;

/**
* Retrieves the current one-time password for a saved device, or given base32-encoded shared secret.
*/
mailosaurGetDeviceOtp(
/**
* Either the unique identifier of the device, or a base32-encoded shared secret.
*/
query: string
): Cypress.Chainable<OtpResult>;

/**
* Permanently delete a device. This operation cannot be undone.
*/
mailosaurDeleteDevice(
/**
* The unique identifier of the device.
*/
deviceId: string
): Cypress.Chainable<null>;
}
}
}
26 changes: 26 additions & 0 deletions src/mailosaurCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class MailosaurCommands {
'mailosaurGenerateEmailAddress',
'mailosaurGetUsageLimits',
'mailosaurGetUsageTransactions',
'mailosaurListDevices',
'mailosaurCreateDevice',
'mailosaurGetDeviceOtp',
'mailosaurDeleteDevice',
];
}

Expand Down Expand Up @@ -237,6 +241,28 @@ class MailosaurCommands {
mailosaurGetUsageTransactions() {
return this.request.get('api/usage/transactions');
}

mailosaurListDevices() {
return this.request.get('api/devices');
}

mailosaurCreateDevice(options) {
return this.request.post('api/devices', options);
}

mailosaurGetDeviceOtp(query) {
if (!query || query.indexOf('-') > -1) {
return this.request.get(`api/devices/${query}/otp`);
}

return this.request.post('api/devices/otp', {
sharedSecret: query,
});
}

mailosaurDeleteDevice(deviceId) {
return this.request.del(`api/devices/${deviceId}`);
}
}

module.exports = MailosaurCommands;
49 changes: 49 additions & 0 deletions test/react-app/cypress/integration/devices.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-disable no-unused-expressions */
describe('Mailosaur device commands', () => {
const deviceName = 'My test';
const sharedSecret = 'ONSWG4TFOQYTEMY=';

let createdDevice;

it('.mailosaurCreateDevice should create a new device', (done) => {
cy.mailosaurCreateDevice({
name: deviceName,
sharedSecret,
}).then((device) => {
createdDevice = device;
expect(createdDevice.id).to.be.ok;
expect(createdDevice.name).to.equal(deviceName);
done();
});
});

it('.mailosaurGetDeviceOtp should retrieve an otp via device ID', (done) => {
cy.mailosaurGetDeviceOtp(createdDevice.id).then((otpResult) => {
expect(otpResult.code).to.be.a('string');
expect(otpResult.code).to.have.lengthOf(6);
done();
});
});

it('.mailosaurDeleteDevice should delete an existing device', (done) => {
cy.mailosaurListDevices().then((result) => {
expect(result.items).to.have.lengthOf(1);
}).then(() => (
cy.mailosaurDeleteDevice(createdDevice.id)
)).then(() => (
cy.mailosaurListDevices()
))
.then((result) => {
expect(result.items).to.have.lengthOf(0);
done();
});
});

it('.mailosaurGetDeviceOtp should retrieve an otp via shared secret', (done) => {
cy.mailosaurGetDeviceOtp(sharedSecret).then((otpResult) => {
expect(otpResult.code).to.be.a('string');
expect(otpResult.code).to.have.lengthOf(6);
done();
});
});
});
Loading

0 comments on commit 6f0082c

Please sign in to comment.