add tests for PUMP DISCONNECT sms command

This commit is contained in:
Lexsus 2020-05-29 19:15:11 +03:00
parent 9a1993c5f6
commit 4af06620fb
2 changed files with 62 additions and 12 deletions

View file

@ -480,18 +480,26 @@ class SmsCommunicatorPlugin @Inject constructor(
})
} else if (splitted.size == 3) {
if (splitted[1].equals("DISCONNECT", ignoreCase = true)) {
val minutes = SafeParse.stringToInt(splitted[2])
val passCode = generatePasscode()
val reply = String.format(resourceHelper.gs(R.string.smscommunicator_pumpdisconnectwithcode), minutes, passCode)
receivedSms.processed = true
messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction() {
override fun run() {
val profile = profileFunction.getProfile()
loopPlugin.disconnectPump(minutes, profile)
rxBus.send(EventRefreshOverview("SMS_PUMP_DISCONNECT"))
sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_pumpdisconnected)))
}
})
var duration = SafeParse.stringToInt(splitted[2])
duration = Math.max(0, duration)
duration = Math.min(120, duration)
if (duration == 0) {
receivedSms.processed = true
sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_wrongduration)))
return
} else {
val passCode = generatePasscode()
val reply = String.format(resourceHelper.gs(R.string.smscommunicator_pumpdisconnectwithcode), duration, passCode)
receivedSms.processed = true
messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction() {
override fun run() {
val profile = profileFunction.getProfile()
loopPlugin.disconnectPump(duration, profile)
rxBus.send(EventRefreshOverview("SMS_PUMP_DISCONNECT"))
sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_pumpdisconnected)))
}
})
}
} else {
sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat)))
return

View file

@ -226,9 +226,12 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
`when`(resourceHelper.gs(R.string.smscommunicator_loopdisablereplywithcode)).thenReturn("To disable loop reply with code %1\$s")
`when`(resourceHelper.gs(R.string.smscommunicator_loopenablereplywithcode)).thenReturn("To enable loop reply with code %1\$s")
`when`(resourceHelper.gs(R.string.smscommunicator_loopresumereplywithcode)).thenReturn("To resume loop reply with code %1\$s")
`when`(resourceHelper.gs(R.string.smscommunicator_pumpdisconnectwithcode)).thenReturn("To disconnect pump for %1d minutes reply with code %2\$s")
`when`(resourceHelper.gs(R.string.smscommunicator_pumpconnectwithcode)).thenReturn("To connect pump reply with code %1\$s")
`when`(resourceHelper.gs(R.string.smscommunicator_reconnect)).thenReturn("Pump reconnected")
`when`(resourceHelper.gs(R.string.smscommunicator_pumpconnectfail)).thenReturn("Connection to pump failed")
`when`(resourceHelper.gs(R.string.smscommunicator_pumpdisconnected)).thenReturn("Pump disconnected")
}
@Test
@ -547,6 +550,45 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text)
Assert.assertEquals("Pump reconnected", smsCommunicatorPlugin.messages[3].text)
//PUMP DISCONNECT 1 2: wrong format
smsCommunicatorPlugin.messages = ArrayList()
sms = Sms("1234", "PUMP DISCONNECT 1 2")
smsCommunicatorPlugin.processSms(sms)
Assert.assertFalse(sms.ignored)
Assert.assertEquals("PUMP DISCONNECT 1 2", smsCommunicatorPlugin.messages[0].text)
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages[1].text)
//PUMP DISCONNECT 0
smsCommunicatorPlugin.messages = ArrayList()
sms = Sms("1234", "PUMP DISCONNECT 0")
smsCommunicatorPlugin.processSms(sms)
Assert.assertFalse(sms.ignored)
Assert.assertEquals("Wrong duration", smsCommunicatorPlugin.messages[1].text)
//PUMP DISCONNECT 30
smsCommunicatorPlugin.messages = ArrayList()
sms = Sms("1234", "PUMP DISCONNECT 30")
smsCommunicatorPlugin.processSms(sms)
Assert.assertFalse(sms.ignored)
Assert.assertEquals("PUMP DISCONNECT 30", smsCommunicatorPlugin.messages[0].text)
Assert.assertTrue(smsCommunicatorPlugin.messages[1].text.contains("To disconnect pump for"))
passCode = smsCommunicatorPlugin.messageToConfirm?.confirmCode!!
smsCommunicatorPlugin.processSms(Sms("1234", passCode))
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text)
Assert.assertEquals("Pump disconnected", smsCommunicatorPlugin.messages[3].text)
//PUMP DISCONNECT 30
smsCommunicatorPlugin.messages = ArrayList()
sms = Sms("1234", "PUMP DISCONNECT 200")
smsCommunicatorPlugin.processSms(sms)
Assert.assertFalse(sms.ignored)
Assert.assertEquals("PUMP DISCONNECT 200", smsCommunicatorPlugin.messages[0].text)
Assert.assertTrue(smsCommunicatorPlugin.messages[1].text.contains("To disconnect pump for"))
passCode = smsCommunicatorPlugin.messageToConfirm?.confirmCode!!
smsCommunicatorPlugin.processSms(Sms("1234", passCode))
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text)
Assert.assertEquals("Pump disconnected", smsCommunicatorPlugin.messages[3].text)
//HELP
smsCommunicatorPlugin.messages = ArrayList()
sms = Sms("1234", "HELP")