-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da05442
commit e9fadb3
Showing
12 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Device { | ||
constructor(data = {}) { | ||
this.id = data.id; | ||
this.name = data.name; | ||
} | ||
} | ||
|
||
module.exports = Device; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class DeviceCreateOptions { | ||
constructor(data = {}) { | ||
this.name = data.name; | ||
this.sharedSecret = data.sharedSecret; | ||
} | ||
} | ||
|
||
module.exports = DeviceCreateOptions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const Device = require('./device'); | ||
|
||
class DeviceListResult { | ||
constructor(data = {}) { | ||
this.items = (data.items || []).map((i) => (new Device(i))); | ||
} | ||
} | ||
|
||
module.exports = DeviceListResult; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class OtpResult { | ||
constructor(data = {}) { | ||
this.code = data.code; | ||
this.expires = new Date(data.expires); | ||
} | ||
} | ||
|
||
module.exports = OtpResult; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
const DeviceListResult = require('../models/deviceListResult'); | ||
const Device = require('../models/device'); | ||
const OtpResult = require('../models/otpResult'); | ||
|
||
class Devices { | ||
constructor(client) { | ||
this.client = client; | ||
} | ||
|
||
list() { | ||
const self = this; | ||
const url = `api/devices`; | ||
|
||
return new Promise((resolve, reject) => { | ||
self.client.request.get(url, (err, response, body) => { | ||
if (err || response.statusCode !== 200) { | ||
return reject(err || self.client.httpError(response)); | ||
} | ||
resolve(new DeviceListResult(body)); | ||
}); | ||
}); | ||
} | ||
|
||
create(options) { | ||
const self = this; | ||
const url = `api/devices`; | ||
|
||
return new Promise((resolve, reject) => { | ||
self.client.request.post(url, { body: options }, (err, response, body) => { | ||
if (err || response.statusCode !== 200) { | ||
return reject(err || self.client.httpError(response)); | ||
} | ||
resolve(new Device(body)); | ||
}); | ||
}); | ||
} | ||
|
||
otp(query) { | ||
const self = this; | ||
|
||
if (!query || query.indexOf('-') > -1) { | ||
const url = `api/devices/${query}/otp`; | ||
|
||
return new Promise((resolve, reject) => { | ||
self.client.request.get(url, (err, response, body) => { | ||
if (err || response.statusCode !== 200) { | ||
return reject(err || self.client.httpError(response)); | ||
} | ||
resolve(new OtpResult(body)); | ||
}); | ||
}); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
const url = `api/devices/otp`; | ||
self.client.request.post(url, { body: { sharedSecret: query } }, (err, response, body) => { | ||
if (err || response.statusCode !== 200) { | ||
return reject(err || self.client.httpError(response)); | ||
} | ||
resolve(new OtpResult(body)); | ||
}); | ||
}); | ||
} | ||
|
||
del(deviceId) { | ||
const self = this; | ||
const url = `api/devices/${deviceId}`; | ||
|
||
return new Promise((resolve, reject) => { | ||
self.client.request.del(url, (err, response) => { | ||
if (err || response.statusCode !== 204) { | ||
return reject(err || self.client.httpError(response)); | ||
} | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = Devices; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const { assert } = require('chai'); | ||
const MailosaurClient = require('../lib/mailosaur'); | ||
|
||
const outputError = (done) => (err) => { | ||
// eslint-disable-next-line no-console | ||
console.log(err.errorType, err.httpStatusCode, err.httpResponseBody); | ||
done(err); | ||
}; | ||
|
||
describe('devices', () => { | ||
let client; | ||
const apiKey = process.env.MAILOSAUR_API_KEY; | ||
const baseUrl = process.env.MAILOSAUR_BASE_URL || 'https://mailosaur.com/'; | ||
|
||
before(() => { | ||
if (!apiKey) { | ||
throw new Error('Missing necessary environment variables - refer to README.md'); | ||
} | ||
|
||
client = new MailosaurClient(apiKey, baseUrl); | ||
}); | ||
|
||
describe('CRUD', () => { | ||
const deviceName = 'My test'; | ||
const sharedSecret = 'ONSWG4TFOQYTEMY='; | ||
let createdDevice; | ||
|
||
it('should create a new device', (done) => { | ||
client.devices.create({ | ||
name: deviceName, | ||
sharedSecret | ||
}).then((device) => { | ||
createdDevice = device; | ||
assert.isNotEmpty(createdDevice.id); | ||
assert.equal(createdDevice.name, deviceName); | ||
done(); | ||
}).catch(outputError(done)); | ||
}); | ||
|
||
it('retrieve an otp via device ID', (done) => { | ||
client.devices.otp(createdDevice.id) | ||
.then((otpResult) => { | ||
assert.isString(otpResult.code); | ||
assert.equal(otpResult.code.length, 6); | ||
done(); | ||
}) | ||
.catch(outputError(done)); | ||
}); | ||
|
||
it('should delete an existing device', (done) => { | ||
client.devices.list().then((result) => { | ||
assert.equal(result.items.length, 1); | ||
}).then(() => ( | ||
client.devices.del(createdDevice.id) | ||
)).then(() => ( | ||
client.devices.list() | ||
)) | ||
.then((result) => { | ||
assert.equal(result.items.length, 0); | ||
done(); | ||
}) | ||
.catch(outputError(done)); | ||
}); | ||
|
||
it('retrieve an otp via shared secret', (done) => { | ||
client.devices.otp(sharedSecret) | ||
.then((otpResult) => { | ||
assert.isString(otpResult.code); | ||
assert.equal(otpResult.code.length, 6); | ||
done(); | ||
}) | ||
.catch(outputError(done)); | ||
}); | ||
}); | ||
}); |