Skip to content

Commit

Permalink
refactor(js): migrate coffee to js
Browse files Browse the repository at this point in the history
  • Loading branch information
lgaticaq committed Sep 25, 2017
1 parent 7362d3c commit 0a44c6b
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 170 deletions.
2 changes: 0 additions & 2 deletions src/index.coffee

This file was deleted.

3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

module.exports = robot => robot.loadFile(__dirname, 'script.js')
46 changes: 0 additions & 46 deletions src/script.coffee

This file was deleted.

53 changes: 53 additions & 0 deletions src/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Description
// A Hubot script to check status of your m2m-status sims
//
// Dependencies:
// "m2m-status": "0.1.0"
//
// Configuration:
// M2M_USER, M2M_PASS
//
// Commands:
// hubot m2m check <sim> - Check status of sim
// hubot m2m check <icc> - Check status of icc
//
// Author:
// lgaticaq

const M2m = require('m2m-status')

module.exports = robot => {
const getStatus = (res, method) => {
res.send('Checking status...')
const client = new M2m({
user: process.env.M2M_USER,
password: process.env.M2M_PASS
})
return client[method](res.match[1])
}

const onResult = result => {
let msg = `Administration: ${result.status.admin ? 'OK' : 'Error'}\n`
msg += `GSM: ${result.status.gsm ? 'OK' : 'Error'}\n`
msg += `GPRS: ${result.status.gprs ? 'OK' : 'Error'}`
return msg
}

robot.respond(/m2m check (\+\d{11})/, res => {
getStatus(res, 'checkSim')
.then(result => res.send(onResult(result)))
.catch(err => {
res.send(`Error: ${err.message}`)
robot.emit('error', err)
})
})

robot.respond(/m2m check (\d{19})/, res =>
getStatus(res, 'checkIcc')
.then(result => res.send(onResult(result)))
.catch(err => {
res.send(`Error: ${err.message}`)
robot.emit('error', err)
})
)
}
122 changes: 0 additions & 122 deletions test/test.coffee

This file was deleted.

152 changes: 152 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
'use strict'

require('coffee-script/register')
const Helper = require('hubot-test-helper')
const { expect } = require('chai')
const proxyquire = require('proxyquire')

process.env.M2M_USER = 'user'
process.env.M2M_PASS = 'password'

class M2m {
constructor (username, password) {
this.username = username
this.password = password
}
checkSim (sim) {
return new Promise((resolve, reject) => {
if (sim === '+56999999999') {
return resolve({ status: { admin: true, gsm: true, gprs: true } })
} else if (sim === '+56888888888') {
return resolve({ status: { admin: false, gsm: false, gprs: false } })
} else {
return reject(new Error(`Sim ${sim} not found or not active`))
}
})
}
checkIcc (icc) {
return new Promise((resolve, reject) => {
if (icc === '1111111111111111111') {
return resolve({ status: { admin: true, gsm: true, gprs: true } })
} else if (icc === '2222222222222222222') {
return resolve({ status: { admin: false, gsm: false, gprs: false } })
} else {
return reject(new Error(`ICC ${icc} not found or not active`))
}
})
}
}

proxyquire('./../src/script.js', { 'm2m-status': M2m })

const helper = new Helper('./../src/index.js')

describe('hubot-m2m-status', function () {
beforeEach(() => {
this.room = helper.createRoom({ name: 'user' })
})

afterEach(() => this.room.destroy())

context('sim invalid', () => {
beforeEach(done => {
this.room.user.say('user', 'hubot m2m check +56111111111')
setTimeout(done, 100)
})

it('should get a error', () => {
expect(this.room.messages).to.eql([
['user', 'hubot m2m check +56111111111'],
['hubot', 'Checking status...'],
['hubot', 'Error: Sim +56111111111 not found or not active']
])
})
})

context('icc invalid', () => {
beforeEach(done => {
this.room.user.say('user', 'hubot m2m check 3333333333333333333')
setTimeout(done, 100)
})

it('should get a error', () => {
expect(this.room.messages).to.eql([
['user', 'hubot m2m check 3333333333333333333'],
['hubot', 'Checking status...'],
['hubot', 'Error: ICC 3333333333333333333 not found or not active']
])
})
})

context('sim valid data false', () => {
beforeEach(done => {
this.room.user.say('user', 'hubot m2m check +56888888888')
setTimeout(done, 100)
})

it('should get a sim status', () => {
let message = 'Administration: Error\n'
message += 'GSM: Error\n'
message += 'GPRS: Error'
expect(this.room.messages).to.eql([
['user', 'hubot m2m check +56888888888'],
['hubot', 'Checking status...'],
['hubot', message]
])
})
})

context('icc valid data false', () => {
beforeEach(done => {
this.room.user.say('user', 'hubot m2m check 2222222222222222222')
setTimeout(done, 100)
})

it('should get a sim status', () => {
let message = 'Administration: Error\n'
message += 'GSM: Error\n'
message += 'GPRS: Error'
expect(this.room.messages).to.eql([
['user', 'hubot m2m check 2222222222222222222'],
['hubot', 'Checking status...'],
['hubot', message]
])
})
})

context('sim valid data true', () => {
beforeEach(done => {
this.room.user.say('user', 'hubot m2m check +56999999999')
setTimeout(done, 100)
})

it('should get a sim status', () => {
let message = 'Administration: OK\n'
message += 'GSM: OK\n'
message += 'GPRS: OK'
expect(this.room.messages).to.eql([
['user', 'hubot m2m check +56999999999'],
['hubot', 'Checking status...'],
['hubot', message]
])
})
})

context('icc valid data true', () => {
beforeEach(done => {
this.room.user.say('user', 'hubot m2m check 1111111111111111111')
setTimeout(done, 100)
})

it('should get a sim status', () => {
let message = 'Administration: OK\n'
message += 'GSM: OK\n'
message += 'GPRS: OK'
expect(this.room.messages).to.eql([
['user', 'hubot m2m check 1111111111111111111'],
['hubot', 'Checking status...'],
['hubot', message]
])
})
})
})

0 comments on commit 0a44c6b

Please sign in to comment.