AuthRequestTest

This commit is contained in:
Milos Kozak 2019-03-12 19:55:47 +01:00
parent b7305f156f
commit bb22b00245
3 changed files with 92 additions and 0 deletions

View file

@ -38,6 +38,7 @@ class AuthRequest {
return; return;
} }
if (!confirmCode.equals(codeReceived)) { if (!confirmCode.equals(codeReceived)) {
processed = true;
if (L.isEnabled(L.SMS)) if (L.isEnabled(L.SMS))
log.debug("Wrong code"); log.debug("Wrong code");
plugin.sendSMS(new Sms(requester.phoneNumber, R.string.sms_wrongcode)); plugin.sendSMS(new Sms(requester.phoneNumber, R.string.sms_wrongcode));

View file

@ -104,6 +104,7 @@ public class AAPSMocker {
when(MainApp.gs(R.string.profile_per_unit)).thenReturn("/U"); when(MainApp.gs(R.string.profile_per_unit)).thenReturn("/U");
when(MainApp.gs(R.string.profile_carbs_per_unit)).thenReturn("g/U"); when(MainApp.gs(R.string.profile_carbs_per_unit)).thenReturn("g/U");
when(MainApp.gs(R.string.profile_ins_units_per_hout)).thenReturn("U/h"); when(MainApp.gs(R.string.profile_ins_units_per_hout)).thenReturn("U/h");
when(MainApp.gs(R.string.sms_wrongcode)).thenReturn("Wrong code. Command cancelled.");
} }
public static MainApp mockMainApp() { public static MainApp mockMainApp() {

View file

@ -0,0 +1,90 @@
package info.nightscout.androidaps.plugins.general.smsCommunicator;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.stubbing.Answer;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.Date;
import info.AAPSMocker;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.utils.T;
import static org.mockito.ArgumentMatchers.any;
import static org.powermock.api.mockito.PowerMockito.doAnswer;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({SmsCommunicatorPlugin.class, L.class, SP.class, MainApp.class, DateUtil.class})
public class AuthRequestTest {
SmsCommunicatorPlugin smsCommunicatorPlugin;
Sms sentSms;
boolean actionCalled = false;
@Test
public void doTests() {
Sms requester = new Sms("aNumber", "aText");
SmsAction action = new SmsAction() {
@Override
public void run() {
actionCalled = true;
}
};
// Check if SMS requesting code is sent
AuthRequest authRequest = new AuthRequest(smsCommunicatorPlugin, requester, "Request text", "ABC", action);
Assert.assertEquals(sentSms.phoneNumber, "aNumber");
Assert.assertEquals(sentSms.text, "Request text");
// wrong reply
actionCalled = false;
authRequest.action("EFG");
Assert.assertEquals(sentSms.phoneNumber, "aNumber");
Assert.assertEquals(sentSms.text, "Wrong code. Command cancelled.");
Assert.assertFalse(actionCalled);
// correct reply
authRequest = new AuthRequest(smsCommunicatorPlugin, requester, "Request text", "ABC", action);
actionCalled = false;
authRequest.action("ABC");
Assert.assertTrue(actionCalled);
// test timed out message
long now = 10000;
when(DateUtil.now()).thenReturn(now);
authRequest = new AuthRequest(smsCommunicatorPlugin, requester, "Request text", "ABC", action);
actionCalled = false;
when(DateUtil.now()).thenReturn(now + T.mins(Constants.SMS_CONFIRM_TIMEOUT).msecs() + 1);
authRequest.action("ABC");
Assert.assertFalse(actionCalled);
}
@Before
public void prepareTests() {
smsCommunicatorPlugin = mock(SmsCommunicatorPlugin.class);
doAnswer((Answer) invocation -> {
sentSms = invocation.getArgument(0);
return null;
}).when(smsCommunicatorPlugin).sendSMS(any(Sms.class));
AAPSMocker.mockMainApp();
AAPSMocker.mockApplicationContext();
AAPSMocker.mockSP();
AAPSMocker.mockL();
AAPSMocker.mockStrings();
mockStatic(DateUtil.class);
}
}