Unverified Commit 83a427b9 authored by AMIT KUMAR's avatar AMIT KUMAR Committed by GitHub
Browse files

SB-27415 feat:fixing email subject issue (#980)

* Issue #SB-27415 feat:fixing email subject issue
Showing with 204 additions and 100 deletions
+204 -100
...@@ -16,6 +16,12 @@ import org.sunbird.validator.BaseRequestValidator; ...@@ -16,6 +16,12 @@ import org.sunbird.validator.BaseRequestValidator;
public class OtpRequestValidator extends BaseRequestValidator { public class OtpRequestValidator extends BaseRequestValidator {
private final List<String> allowedTemplate =
Arrays.asList(
JsonKey.RESET_PASSWORD_TEMPLATE_ID,
JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID,
JsonKey.CONTACT_UPDATE_TEMPLATE_ID);
public void validateGenerateOtpRequest(Request otpRequest) { public void validateGenerateOtpRequest(Request otpRequest) {
commonValidation(otpRequest, false); commonValidation(otpRequest, false);
validateTemplateId(otpRequest); validateTemplateId(otpRequest);
...@@ -23,9 +29,7 @@ public class OtpRequestValidator extends BaseRequestValidator { ...@@ -23,9 +29,7 @@ public class OtpRequestValidator extends BaseRequestValidator {
private void validateTemplateId(Request otpRequest) { private void validateTemplateId(Request otpRequest) {
String templateId = (String) otpRequest.getRequest().get(JsonKey.TEMPLATE_ID); String templateId = (String) otpRequest.getRequest().get(JsonKey.TEMPLATE_ID);
if (StringUtils.isNotBlank(templateId) if (StringUtils.isNotBlank(templateId) && !allowedTemplate.contains(templateId)) {
&& !templateId.equalsIgnoreCase(JsonKey.TEMPLATE_ID_VALUE)
&& !templateId.equalsIgnoreCase(JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID)) {
throw new ProjectCommonException( throw new ProjectCommonException(
ResponseCode.invalidIdentifier.getErrorCode(), ResponseCode.invalidIdentifier.getErrorCode(),
ProjectUtil.formatMessage( ProjectUtil.formatMessage(
......
...@@ -27,7 +27,6 @@ import play.mvc.Http; ...@@ -27,7 +27,6 @@ import play.mvc.Http;
import play.mvc.Result; import play.mvc.Result;
import play.mvc.Results; import play.mvc.Results;
import util.Attrs; import util.Attrs;
import util.Common;
import util.RequestInterceptor; import util.RequestInterceptor;
public class OnRequestHandler implements ActionCreator { public class OnRequestHandler implements ActionCreator {
...@@ -83,8 +82,7 @@ public class OnRequestHandler implements ActionCreator { ...@@ -83,8 +82,7 @@ public class OnRequestHandler implements ActionCreator {
} }
result = delegate.call(request); result = delegate.call(request);
} else if (JsonKey.UNAUTHORIZED.equals(message)) { } else if (JsonKey.UNAUTHORIZED.equals(message)) {
result = result = onDataValidationError(request, ResponseCode.UNAUTHORIZED.getResponseCode());
onDataValidationError(request, message, ResponseCode.UNAUTHORIZED.getResponseCode());
} else { } else {
result = delegate.call(request); result = delegate.call(request);
} }
...@@ -144,17 +142,9 @@ public class OnRequestHandler implements ActionCreator { ...@@ -144,17 +142,9 @@ public class OnRequestHandler implements ActionCreator {
* send some key in header. * send some key in header.
* *
* @param request Request * @param request Request
* @param errorMessage String
* @return CompletionStage<Result> * @return CompletionStage<Result>
*/ */
public CompletionStage<Result> onDataValidationError( public CompletionStage<Result> onDataValidationError(Http.Request request, int responseCode) {
Http.Request request, String errorMessage, int responseCode) {
String context = Common.getFromRequest(request, Attrs.CONTEXT);
logger.info(
"onDataValidationError: Data error found with context info : "
+ context
+ " , Error Msg: "
+ errorMessage);
Response resp = Response resp =
BaseController.createFailureResponse( BaseController.createFailureResponse(
request, ResponseCode.unAuthorized, ResponseCode.UNAUTHORIZED); request, ResponseCode.unAuthorized, ResponseCode.UNAUTHORIZED);
......
package controllers.otp.validator;
import org.junit.Test;
import org.sunbird.exception.ProjectCommonException;
import org.sunbird.keys.JsonKey;
import org.sunbird.request.Request;
public class OtpRequestValidatorTest {
@Test(expected = ProjectCommonException.class)
public void testValidateGenerateOtpRequest() {
Request otpRequest = new Request();
otpRequest.getRequest().put(JsonKey.KEY, "xyz@xyz.com");
otpRequest.getRequest().put(JsonKey.TYPE, "email");
otpRequest.getRequest().put(JsonKey.TEMPLATE_ID, "invalidTemplateID");
OtpRequestValidator otpRequestValidator = new OtpRequestValidator();
otpRequestValidator.validateGenerateOtpRequest(otpRequest);
}
}
...@@ -472,7 +472,7 @@ public final class JsonKey { ...@@ -472,7 +472,7 @@ public final class JsonKey {
public static final String CATEGORY = "category"; public static final String CATEGORY = "category";
public static final String TEMPLATE_ID = "templateId"; public static final String TEMPLATE_ID = "templateId";
public static final String TEMPLATE_OPTIONS = "templateOptions"; public static final String TEMPLATE_OPTIONS = "templateOptions";
public static final String TEMPLATE_ID_VALUE = "resetPasswordWithOtp"; public static final String RESET_PASSWORD_TEMPLATE_ID = "resetPasswordWithOtp";
public static final String VERSION_3 = "v3"; public static final String VERSION_3 = "v3";
public static final String VERSION_4 = "v4"; public static final String VERSION_4 = "v4";
public static final String WARD_LOGIN_OTP_TEMPLATE_ID = "wardLoginOTP"; public static final String WARD_LOGIN_OTP_TEMPLATE_ID = "wardLoginOTP";
...@@ -599,6 +599,12 @@ public final class JsonKey { ...@@ -599,6 +599,12 @@ public final class JsonKey {
public static final String IDS = "ids"; public static final String IDS = "ids";
public static final String NOTIFICATIONS = "notifications"; public static final String NOTIFICATIONS = "notifications";
public static final String FEEDS = "feeds"; public static final String FEEDS = "feeds";
public static final String EMAIL_VERIFICATION_SUBJECT = "OTP to verify Email";
public static final String CONTACT_UPDATE_TEMPLATE_ID = "otpContactUpdateTemplate";
public static final String OTP_CONTACT_UPDATE_TEMPLATE_EMAIL = "otpContactUpdateTemplateEmail";
public static final String OTP_CONTACT_UPDATE_TEMPLATE_SMS = "otpContactUpdateTemplateSms";
public static final String CONTACT_DETAILS_UPDATE_VERIFICATION_SUBJECT =
"OTP to edit Diksha Profile";
private JsonKey() {} private JsonKey() {}
} }
...@@ -52,7 +52,6 @@ public class OTPActor extends BaseActor { ...@@ -52,7 +52,6 @@ public class OTPActor extends BaseActor {
logger.debug(request.getRequestContext(), "OTPActor:generateOTP method call start."); logger.debug(request.getRequestContext(), "OTPActor:generateOTP method call start.");
String type = (String) request.getRequest().get(JsonKey.TYPE); String type = (String) request.getRequest().get(JsonKey.TYPE);
String key = (String) request.getRequest().get(JsonKey.KEY); String key = (String) request.getRequest().get(JsonKey.KEY);
String userId = (String) request.getRequest().get(JsonKey.USER_ID); String userId = (String) request.getRequest().get(JsonKey.USER_ID);
if (StringUtils.isNotBlank(userId)) { if (StringUtils.isNotBlank(userId)) {
key = otpService.getEmailPhoneByUserId(userId, type, request.getRequestContext()); key = otpService.getEmailPhoneByUserId(userId, type, request.getRequestContext());
......
...@@ -5,7 +5,6 @@ import java.util.HashMap; ...@@ -5,7 +5,6 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import org.apache.commons.lang3.StringUtils;
import org.sunbird.actor.core.BaseActor; import org.sunbird.actor.core.BaseActor;
import org.sunbird.datasecurity.impl.LogMaskServiceImpl; import org.sunbird.datasecurity.impl.LogMaskServiceImpl;
import org.sunbird.keys.JsonKey; import org.sunbird.keys.JsonKey;
...@@ -35,7 +34,7 @@ public class SendOTPActor extends BaseActor { ...@@ -35,7 +34,7 @@ public class SendOTPActor extends BaseActor {
String type = (String) request.getRequest().get(JsonKey.TYPE); String type = (String) request.getRequest().get(JsonKey.TYPE);
String key = (String) request.getRequest().get(JsonKey.KEY); String key = (String) request.getRequest().get(JsonKey.KEY);
String otp = (String) request.getRequest().get(JsonKey.OTP); String otp = (String) request.getRequest().get(JsonKey.OTP);
String template = (String) request.getRequest().get(JsonKey.TEMPLATE_ID); String templateId = (String) request.getRequest().get(JsonKey.TEMPLATE_ID);
if (JsonKey.EMAIL.equalsIgnoreCase(type) if (JsonKey.EMAIL.equalsIgnoreCase(type)
|| JsonKey.PREV_USED_EMAIL.equalsIgnoreCase(type) || JsonKey.PREV_USED_EMAIL.equalsIgnoreCase(type)
|| JsonKey.RECOVERY_EMAIL.equalsIgnoreCase(type)) { || JsonKey.RECOVERY_EMAIL.equalsIgnoreCase(type)) {
...@@ -46,14 +45,14 @@ public class SendOTPActor extends BaseActor { ...@@ -46,14 +45,14 @@ public class SendOTPActor extends BaseActor {
+ logMaskService.maskEmail(key) + logMaskService.maskEmail(key)
+ " or userId " + " or userId "
+ userId); + userId);
sendOTPViaEmail(key, otp, userId, template, request.getRequestContext()); sendOTPViaEmail(key, otp, templateId, request.getRequestContext());
} else if (JsonKey.PHONE.equalsIgnoreCase(type) } else if (JsonKey.PHONE.equalsIgnoreCase(type)
|| JsonKey.PREV_USED_PHONE.equalsIgnoreCase(type) || JsonKey.PREV_USED_PHONE.equalsIgnoreCase(type)
|| JsonKey.RECOVERY_PHONE.equalsIgnoreCase(type)) { || JsonKey.RECOVERY_PHONE.equalsIgnoreCase(type)) {
logger.info( logger.info(
request.getRequestContext(), request.getRequestContext(),
"SendOTPActor:sendOTP : Sending OTP via sms for Key = " + logMaskService.maskPhone(key)); "SendOTPActor:sendOTP : Sending OTP via sms for Key = " + logMaskService.maskPhone(key));
sendOTPViaSMS(key, otp, template, request.getRequestContext()); sendOTPViaSMS(key, otp, templateId, request.getRequestContext());
} else { } else {
logger.info(request.getRequestContext(), "SendOTPActor:sendOTP : No Email/Phone provided."); logger.info(request.getRequestContext(), "SendOTPActor:sendOTP : No Email/Phone provided.");
} }
...@@ -62,20 +61,13 @@ public class SendOTPActor extends BaseActor { ...@@ -62,20 +61,13 @@ public class SendOTPActor extends BaseActor {
sender().tell(response, self()); sender().tell(response, self());
} }
private void sendOTPViaEmail( private void sendOTPViaEmail(String key, String otp, String templateId, RequestContext context) {
String key, String otp, String otpType, String template, RequestContext context) {
Map<String, Object> emailTemplateMap = new HashMap<>(); Map<String, Object> emailTemplateMap = new HashMap<>();
emailTemplateMap.put(JsonKey.EMAIL, key); emailTemplateMap.put(JsonKey.EMAIL, key);
emailTemplateMap.put(JsonKey.OTP, otp); emailTemplateMap.put(JsonKey.OTP, otp);
emailTemplateMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES, OTPUtil.getOTPExpirationInMinutes()); emailTemplateMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES, OTPUtil.getOTPExpirationInMinutes());
emailTemplateMap.put(JsonKey.TEMPLATE_ID, template); emailTemplateMap.put(JsonKey.TEMPLATE_ID, templateId);
Request emailRequest; Request emailRequest = OTPUtil.getRequestToSendOTPViaEmail(emailTemplateMap, context);
if (StringUtils.isBlank(otpType)) {
emailRequest = OTPUtil.getRequestToSendOTPViaEmail(emailTemplateMap, context);
} else {
emailRequest =
OTPUtil.getRequestToSendOTPViaEmail(emailTemplateMap, JsonKey.RESET_PASSWORD, context);
}
emailRequest.setRequestContext(context); emailRequest.setRequestContext(context);
logger.info( logger.info(
context, context,
......
...@@ -82,21 +82,22 @@ public final class OTPUtil { ...@@ -82,21 +82,22 @@ public final class OTPUtil {
return false; return false;
} }
Map<String, String> smsTemplate = new HashMap<>(); Map<String, String> smsTemplate = new HashMap<>();
String template = (String) otpMap.get(JsonKey.TEMPLATE_ID); String templateId = (String) otpMap.get(JsonKey.TEMPLATE_ID);
smsTemplate.put(JsonKey.OTP, (String) otpMap.get(JsonKey.OTP)); smsTemplate.put(JsonKey.OTP, (String) otpMap.get(JsonKey.OTP));
smsTemplate.put( smsTemplate.put(
JsonKey.OTP_EXPIRATION_IN_MINUTES, (String) otpMap.get(JsonKey.OTP_EXPIRATION_IN_MINUTES)); JsonKey.OTP_EXPIRATION_IN_MINUTES, (String) otpMap.get(JsonKey.OTP_EXPIRATION_IN_MINUTES));
smsTemplate.put( smsTemplate.put(
JsonKey.INSTALLATION_NAME, JsonKey.INSTALLATION_NAME,
ProjectUtil.getConfigValue(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME)); ProjectUtil.getConfigValue(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME));
String sms; String sms = "";
if (StringUtils.isBlank(template)) { if (StringUtils.isBlank(templateId)) {
sms = otpService.getSmsBody(JsonKey.VERIFY_PHONE_OTP_TEMPLATE, smsTemplate, context); sms = otpService.getSmsBody(JsonKey.VERIFY_PHONE_OTP_TEMPLATE, smsTemplate, context);
} else if (StringUtils.isNotBlank(template) } else if (StringUtils.equals(JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID, templateId)) {
&& StringUtils.equals(template, JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID)) {
sms = otpService.getSmsBody(JsonKey.OTP_PHONE_WARD_LOGIN_TEMPLATE, smsTemplate, context); sms = otpService.getSmsBody(JsonKey.OTP_PHONE_WARD_LOGIN_TEMPLATE, smsTemplate, context);
} else { } else if (StringUtils.equals(JsonKey.RESET_PASSWORD_TEMPLATE_ID, templateId)) {
sms = otpService.getSmsBody(JsonKey.OTP_PHONE_RESET_PASSWORD_TEMPLATE, smsTemplate, context); sms = otpService.getSmsBody(JsonKey.OTP_PHONE_RESET_PASSWORD_TEMPLATE, smsTemplate, context);
} else if (StringUtils.equals(JsonKey.CONTACT_UPDATE_TEMPLATE_ID, templateId)) {
sms = otpService.getSmsBody(JsonKey.OTP_CONTACT_UPDATE_TEMPLATE_SMS, smsTemplate, context);
} }
logger.debug(context, "OTPUtil:sendOTPViaSMS: SMS text = " + sms); logger.debug(context, "OTPUtil:sendOTPViaSMS: SMS text = " + sms);
...@@ -128,33 +129,30 @@ public final class OTPUtil { ...@@ -128,33 +129,30 @@ public final class OTPUtil {
} }
public static Request getRequestToSendOTPViaEmail( public static Request getRequestToSendOTPViaEmail(
Map<String, Object> emailTemplateMap, String otpType, RequestContext context) { Map<String, Object> emailTemplateMap, RequestContext context) {
Request request = null; Request request;
if ((StringUtils.isBlank((String) emailTemplateMap.get(JsonKey.EMAIL)))) { if ((StringUtils.isBlank((String) emailTemplateMap.get(JsonKey.EMAIL)))) {
return request; return null;
} }
String templateId = (String) emailTemplateMap.get(JsonKey.TEMPLATE_ID);
String envName = ProjectUtil.getConfigValue(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME); String envName = ProjectUtil.getConfigValue(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME);
String emailSubject = null;
if (JsonKey.RESET_PASSWORD.equalsIgnoreCase(otpType)) {
emailSubject = ProjectUtil.getConfigValue(JsonKey.SUNBIRD_RESET_PASS_MAIL_SUBJECT);
} else {
// default fallback for all other otpType
emailSubject = ProjectUtil.getConfigValue(JsonKey.ONBOARDING_MAIL_SUBJECT);
}
emailTemplateMap.put(JsonKey.SUBJECT, ProjectUtil.formatMessage(emailSubject, envName));
List<String> reciptientsMail = new ArrayList<>(); List<String> reciptientsMail = new ArrayList<>();
reciptientsMail.add((String) emailTemplateMap.get(JsonKey.EMAIL)); reciptientsMail.add((String) emailTemplateMap.get(JsonKey.EMAIL));
emailTemplateMap.put(JsonKey.RECIPIENT_EMAILS, reciptientsMail); emailTemplateMap.put(JsonKey.RECIPIENT_EMAILS, reciptientsMail);
if (StringUtils.isBlank((String) emailTemplateMap.get(JsonKey.TEMPLATE_ID))) { if (StringUtils.isBlank(templateId)) {
emailTemplateMap.put(JsonKey.EMAIL_TEMPLATE_TYPE, JsonKey.OTP); emailTemplateMap.put(JsonKey.EMAIL_TEMPLATE_TYPE, JsonKey.OTP);
} else if (StringUtils.isNotBlank((String) emailTemplateMap.get(JsonKey.TEMPLATE_ID)) emailTemplateMap.put(JsonKey.SUBJECT, JsonKey.EMAIL_VERIFICATION_SUBJECT);
&& StringUtils.equals( } else if (StringUtils.equalsIgnoreCase(JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID, templateId)) {
(String) emailTemplateMap.get(JsonKey.TEMPLATE_ID),
JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID)) {
emailTemplateMap.put(JsonKey.EMAIL_TEMPLATE_TYPE, JsonKey.OTP_EMAIL_WARD_LOGIN_TEMPLATE); emailTemplateMap.put(JsonKey.EMAIL_TEMPLATE_TYPE, JsonKey.OTP_EMAIL_WARD_LOGIN_TEMPLATE);
} else { String emailSubject = ProjectUtil.getConfigValue(JsonKey.ONBOARDING_MAIL_SUBJECT);
// send otp to email while reseting password from portal emailTemplateMap.put(JsonKey.SUBJECT, ProjectUtil.formatMessage(emailSubject, envName));
} else if (StringUtils.equalsIgnoreCase(JsonKey.RESET_PASSWORD_TEMPLATE_ID, templateId)) {
emailTemplateMap.put(JsonKey.EMAIL_TEMPLATE_TYPE, JsonKey.OTP_EMAIL_RESET_PASSWORD_TEMPLATE); emailTemplateMap.put(JsonKey.EMAIL_TEMPLATE_TYPE, JsonKey.OTP_EMAIL_RESET_PASSWORD_TEMPLATE);
emailTemplateMap.put(
JsonKey.SUBJECT, ProjectUtil.getConfigValue(JsonKey.SUNBIRD_RESET_PASS_MAIL_SUBJECT));
} else if (StringUtils.equalsIgnoreCase(JsonKey.CONTACT_UPDATE_TEMPLATE_ID, templateId)) {
emailTemplateMap.put(JsonKey.EMAIL_TEMPLATE_TYPE, JsonKey.OTP_CONTACT_UPDATE_TEMPLATE_EMAIL);
emailTemplateMap.put(JsonKey.SUBJECT, JsonKey.CONTACT_DETAILS_UPDATE_VERIFICATION_SUBJECT);
} }
emailTemplateMap.put(JsonKey.INSTALLATION_NAME, envName); emailTemplateMap.put(JsonKey.INSTALLATION_NAME, envName);
request = new Request(); request = new Request();
...@@ -164,11 +162,6 @@ public final class OTPUtil { ...@@ -164,11 +162,6 @@ public final class OTPUtil {
return request; return request;
} }
public static Request getRequestToSendOTPViaEmail(
Map<String, Object> emailTemplateMap, RequestContext context) {
return getRequestToSendOTPViaEmail(emailTemplateMap, null, context);
}
public static String getOTPExpirationInMinutes() { public static String getOTPExpirationInMinutes() {
String expirationInSeconds = ProjectUtil.getConfigValue(JsonKey.SUNBIRD_OTP_EXPIRATION); String expirationInSeconds = ProjectUtil.getConfigValue(JsonKey.SUNBIRD_OTP_EXPIRATION);
int otpExpiration = Integer.parseInt(expirationInSeconds); int otpExpiration = Integer.parseInt(expirationInSeconds);
......
package org.sunbird.util.otp; package org.sunbird.util.otp;
import static org.powermock.api.mockito.PowerMockito.when;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -18,11 +22,6 @@ import org.sunbird.request.RequestContext; ...@@ -18,11 +22,6 @@ import org.sunbird.request.RequestContext;
import org.sunbird.service.otp.OTPService; import org.sunbird.service.otp.OTPService;
import org.sunbird.util.ProjectUtil; import org.sunbird.util.ProjectUtil;
import java.util.HashMap;
import java.util.Map;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ @PrepareForTest({
OTPService.class, OTPService.class,
...@@ -54,17 +53,24 @@ public class OTPUtilTest { ...@@ -54,17 +53,24 @@ public class OTPUtilTest {
when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString");
OTPService otpService = PowerMockito.mock(OTPService.class); OTPService otpService = PowerMockito.mock(OTPService.class);
PowerMockito.whenNew(OTPService.class).withNoArguments().thenReturn(otpService); PowerMockito.whenNew(OTPService.class).withNoArguments().thenReturn(otpService);
when(otpService.getSmsBody(Mockito.anyString(),Mockito.anyMap(),Mockito.any(RequestContext.class))).thenReturn("some sms text"); when(otpService.getSmsBody(
Mockito.anyString(), Mockito.anyMap(), Mockito.any(RequestContext.class)))
.thenReturn("some sms text");
ISmsProvider smsProvider = PowerMockito.mock(ISmsProvider.class); ISmsProvider smsProvider = PowerMockito.mock(ISmsProvider.class);
PowerMockito.mockStatic(SMSFactory.class); PowerMockito.mockStatic(SMSFactory.class);
when(SMSFactory.getInstance()).thenReturn(smsProvider); when(SMSFactory.getInstance()).thenReturn(smsProvider);
when(smsProvider.send(Mockito.anyString(),Mockito.anyString(),Mockito.anyString(), Mockito.any(RequestContext.class))).thenReturn(true); when(smsProvider.send(
Mockito.anyString(),
Mockito.anyString(),
Mockito.anyString(),
Mockito.any(RequestContext.class)))
.thenReturn(true);
Map<String, Object> otpMap = new HashMap<>(); Map<String, Object> otpMap = new HashMap<>();
otpMap.put(JsonKey.PHONE,"9742511111"); otpMap.put(JsonKey.PHONE, "9742511111");
otpMap.put(JsonKey.TEMPLATE_ID,"someTemplateId"); otpMap.put(JsonKey.TEMPLATE_ID, "someTemplateId");
otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES,"30"); otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES, "30");
otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME,"displayName"); otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME, "displayName");
otpMap.put(JsonKey.COUNTRY_CODE,"91"); otpMap.put(JsonKey.COUNTRY_CODE, "91");
boolean bool = OTPUtil.sendOTPViaSMS(otpMap, new RequestContext()); boolean bool = OTPUtil.sendOTPViaSMS(otpMap, new RequestContext());
Assert.assertTrue(bool); Assert.assertTrue(bool);
} }
...@@ -75,16 +81,23 @@ public class OTPUtilTest { ...@@ -75,16 +81,23 @@ public class OTPUtilTest {
when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString");
OTPService otpService = PowerMockito.mock(OTPService.class); OTPService otpService = PowerMockito.mock(OTPService.class);
PowerMockito.whenNew(OTPService.class).withNoArguments().thenReturn(otpService); PowerMockito.whenNew(OTPService.class).withNoArguments().thenReturn(otpService);
when(otpService.getSmsBody(Mockito.anyString(),Mockito.anyMap(),Mockito.any(RequestContext.class))).thenReturn("some sms text"); when(otpService.getSmsBody(
Mockito.anyString(), Mockito.anyMap(), Mockito.any(RequestContext.class)))
.thenReturn("some sms text");
ISmsProvider smsProvider = PowerMockito.mock(ISmsProvider.class); ISmsProvider smsProvider = PowerMockito.mock(ISmsProvider.class);
PowerMockito.mockStatic(SMSFactory.class); PowerMockito.mockStatic(SMSFactory.class);
when(SMSFactory.getInstance()).thenReturn(smsProvider); when(SMSFactory.getInstance()).thenReturn(smsProvider);
when(smsProvider.send(Mockito.anyString(),Mockito.anyString(),Mockito.anyString(), Mockito.any(RequestContext.class))).thenReturn(true); when(smsProvider.send(
Mockito.anyString(),
Mockito.anyString(),
Mockito.anyString(),
Mockito.any(RequestContext.class)))
.thenReturn(true);
Map<String, Object> otpMap = new HashMap<>(); Map<String, Object> otpMap = new HashMap<>();
otpMap.put(JsonKey.PHONE,"9742511111"); otpMap.put(JsonKey.PHONE, "9742511111");
otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES,"30"); otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES, "30");
otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME,"displayName"); otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME, "displayName");
otpMap.put(JsonKey.COUNTRY_CODE,"91"); otpMap.put(JsonKey.COUNTRY_CODE, "91");
boolean bool = OTPUtil.sendOTPViaSMS(otpMap, new RequestContext()); boolean bool = OTPUtil.sendOTPViaSMS(otpMap, new RequestContext());
Assert.assertTrue(bool); Assert.assertTrue(bool);
} }
...@@ -95,17 +108,80 @@ public class OTPUtilTest { ...@@ -95,17 +108,80 @@ public class OTPUtilTest {
when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString");
OTPService otpService = PowerMockito.mock(OTPService.class); OTPService otpService = PowerMockito.mock(OTPService.class);
PowerMockito.whenNew(OTPService.class).withNoArguments().thenReturn(otpService); PowerMockito.whenNew(OTPService.class).withNoArguments().thenReturn(otpService);
when(otpService.getSmsBody(Mockito.anyString(),Mockito.anyMap(),Mockito.any(RequestContext.class))).thenReturn("some sms text"); when(otpService.getSmsBody(
Mockito.anyString(), Mockito.anyMap(), Mockito.any(RequestContext.class)))
.thenReturn("some sms text");
ISmsProvider smsProvider = PowerMockito.mock(ISmsProvider.class); ISmsProvider smsProvider = PowerMockito.mock(ISmsProvider.class);
PowerMockito.mockStatic(SMSFactory.class); PowerMockito.mockStatic(SMSFactory.class);
when(SMSFactory.getInstance()).thenReturn(smsProvider); when(SMSFactory.getInstance()).thenReturn(smsProvider);
when(smsProvider.send(Mockito.anyString(),Mockito.anyString(),Mockito.anyString(), Mockito.any(RequestContext.class))).thenReturn(true); when(smsProvider.send(
Mockito.anyString(),
Mockito.anyString(),
Mockito.anyString(),
Mockito.any(RequestContext.class)))
.thenReturn(true);
Map<String, Object> otpMap = new HashMap<>(); Map<String, Object> otpMap = new HashMap<>();
otpMap.put(JsonKey.PHONE,"9742511111"); otpMap.put(JsonKey.PHONE, "9742511111");
otpMap.put(JsonKey.TEMPLATE_ID,JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID); otpMap.put(JsonKey.TEMPLATE_ID, JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID);
otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES,"30"); otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES, "30");
otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME,"displayName"); otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME, "displayName");
otpMap.put(JsonKey.COUNTRY_CODE,"91"); otpMap.put(JsonKey.COUNTRY_CODE, "91");
boolean bool = OTPUtil.sendOTPViaSMS(otpMap, new RequestContext());
Assert.assertTrue(bool);
}
@Test
public void sendOTPViaSMSTest4() throws Exception {
PowerMockito.mockStatic(ProjectUtil.class);
when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString");
OTPService otpService = PowerMockito.mock(OTPService.class);
PowerMockito.whenNew(OTPService.class).withNoArguments().thenReturn(otpService);
when(otpService.getSmsBody(
Mockito.anyString(), Mockito.anyMap(), Mockito.any(RequestContext.class)))
.thenReturn("some sms text");
ISmsProvider smsProvider = PowerMockito.mock(ISmsProvider.class);
PowerMockito.mockStatic(SMSFactory.class);
when(SMSFactory.getInstance()).thenReturn(smsProvider);
when(smsProvider.send(
Mockito.anyString(),
Mockito.anyString(),
Mockito.anyString(),
Mockito.any(RequestContext.class)))
.thenReturn(true);
Map<String, Object> otpMap = new HashMap<>();
otpMap.put(JsonKey.PHONE, "9742511111");
otpMap.put(JsonKey.TEMPLATE_ID, JsonKey.RESET_PASSWORD_TEMPLATE_ID);
otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES, "30");
otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME, "displayName");
otpMap.put(JsonKey.COUNTRY_CODE, "91");
boolean bool = OTPUtil.sendOTPViaSMS(otpMap, new RequestContext());
Assert.assertTrue(bool);
}
@Test
public void sendOTPViaSMSTest5() throws Exception {
PowerMockito.mockStatic(ProjectUtil.class);
when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString");
OTPService otpService = PowerMockito.mock(OTPService.class);
PowerMockito.whenNew(OTPService.class).withNoArguments().thenReturn(otpService);
when(otpService.getSmsBody(
Mockito.anyString(), Mockito.anyMap(), Mockito.any(RequestContext.class)))
.thenReturn("some sms text");
ISmsProvider smsProvider = PowerMockito.mock(ISmsProvider.class);
PowerMockito.mockStatic(SMSFactory.class);
when(SMSFactory.getInstance()).thenReturn(smsProvider);
when(smsProvider.send(
Mockito.anyString(),
Mockito.anyString(),
Mockito.anyString(),
Mockito.any(RequestContext.class)))
.thenReturn(true);
Map<String, Object> otpMap = new HashMap<>();
otpMap.put(JsonKey.PHONE, "9742511111");
otpMap.put(JsonKey.TEMPLATE_ID, JsonKey.CONTACT_UPDATE_TEMPLATE_ID);
otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES, "30");
otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME, "displayName");
otpMap.put(JsonKey.COUNTRY_CODE, "91");
boolean bool = OTPUtil.sendOTPViaSMS(otpMap, new RequestContext()); boolean bool = OTPUtil.sendOTPViaSMS(otpMap, new RequestContext());
Assert.assertTrue(bool); Assert.assertTrue(bool);
} }
...@@ -113,38 +189,39 @@ public class OTPUtilTest { ...@@ -113,38 +189,39 @@ public class OTPUtilTest {
@Test @Test
public void sendOTPViaSMSWithoutPhoneTest() { public void sendOTPViaSMSWithoutPhoneTest() {
Map<String, Object> otpMap = new HashMap<>(); Map<String, Object> otpMap = new HashMap<>();
otpMap.put(JsonKey.TEMPLATE_ID,"someTemplateId"); otpMap.put(JsonKey.TEMPLATE_ID, "someTemplateId");
otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES,"30"); otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES, "30");
otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME,"displayName"); otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME, "displayName");
otpMap.put(JsonKey.COUNTRY_CODE,"91"); otpMap.put(JsonKey.COUNTRY_CODE, "91");
boolean bool = OTPUtil.sendOTPViaSMS(otpMap, new RequestContext()); boolean bool = OTPUtil.sendOTPViaSMS(otpMap, new RequestContext());
Assert.assertFalse(bool); Assert.assertFalse(bool);
} }
//@Test // @Test
public void getEmailPhoneByUserIdTest() throws Exception { public void getEmailPhoneByUserIdTest() throws Exception {
Map<String, Object> user = new HashMap<>(); Map<String, Object> user = new HashMap<>();
user.put(JsonKey.USER_ID,"1235-4578-156645"); user.put(JsonKey.USER_ID, "1235-4578-156645");
user.put(JsonKey.EMAIL,"xyz@xyz.com"); user.put(JsonKey.EMAIL, "xyz@xyz.com");
user.put(JsonKey.PHONE,"9999999999"); user.put(JsonKey.PHONE, "9999999999");
OTPService otpService = PowerMockito.mock(OTPService.class); OTPService otpService = PowerMockito.mock(OTPService.class);
PowerMockito.whenNew(OTPService.class).withNoArguments().thenReturn(otpService); PowerMockito.whenNew(OTPService.class).withNoArguments().thenReturn(otpService);
String value = otpService.getEmailPhoneByUserId("1235-4578-156645",JsonKey.EMAIL, new RequestContext()); String value =
otpService.getEmailPhoneByUserId("1235-4578-156645", JsonKey.EMAIL, new RequestContext());
Assert.assertNotNull(value); Assert.assertNotNull(value);
} }
@Test(expected = ProjectCommonException.class) @Test(expected = ProjectCommonException.class)
public void getEmailPhoneByUserId2Test() { public void getEmailPhoneByUserId2Test() {
OTPService otpService = new OTPService(); OTPService otpService = new OTPService();
otpService.getEmailPhoneByUserId("1235-4578-156645",JsonKey.EMAIL, new RequestContext()); otpService.getEmailPhoneByUserId("1235-4578-156645", JsonKey.EMAIL, new RequestContext());
} }
@Test(expected = ProjectCommonException.class) @Test(expected = ProjectCommonException.class)
public void getEmailPhoneByUserId3Test() { public void getEmailPhoneByUserId3Test() {
Map<String, Object> user = new HashMap<>(); Map<String, Object> user = new HashMap<>();
user.put(JsonKey.USER_ID,"1235-4578-156645"); user.put(JsonKey.USER_ID, "1235-4578-156645");
OTPService otpService = new OTPService(); OTPService otpService = new OTPService();
otpService.getEmailPhoneByUserId("1235-4578-156645",JsonKey.EMAIL, new RequestContext()); otpService.getEmailPhoneByUserId("1235-4578-156645", JsonKey.EMAIL, new RequestContext());
} }
@Test @Test
...@@ -153,7 +230,7 @@ public class OTPUtilTest { ...@@ -153,7 +230,7 @@ public class OTPUtilTest {
when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString");
Map<String, Object> emailTemplateMap = new HashMap<>(); Map<String, Object> emailTemplateMap = new HashMap<>();
emailTemplateMap.put(JsonKey.EMAIL,"xyz@xyz.com"); emailTemplateMap.put(JsonKey.EMAIL, "xyz@xyz.com");
Request request = OTPUtil.getRequestToSendOTPViaEmail(emailTemplateMap, new RequestContext()); Request request = OTPUtil.getRequestToSendOTPViaEmail(emailTemplateMap, new RequestContext());
Assert.assertNotNull(request); Assert.assertNotNull(request);
...@@ -176,8 +253,32 @@ public class OTPUtilTest { ...@@ -176,8 +253,32 @@ public class OTPUtilTest {
when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString");
Map<String, Object> emailTemplateMap = new HashMap<>(); Map<String, Object> emailTemplateMap = new HashMap<>();
emailTemplateMap.put(JsonKey.EMAIL,"xyz@xyz.com"); emailTemplateMap.put(JsonKey.EMAIL, "xyz@xyz.com");
emailTemplateMap.put(JsonKey.TEMPLATE_ID,JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID); emailTemplateMap.put(JsonKey.TEMPLATE_ID, JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID);
Request request = OTPUtil.getRequestToSendOTPViaEmail(emailTemplateMap, new RequestContext());
Assert.assertNotNull(request);
}
@Test
public void getRequestToSendOTPViaEmailTest4() {
PowerMockito.mockStatic(ProjectUtil.class);
when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString");
Map<String, Object> emailTemplateMap = new HashMap<>();
emailTemplateMap.put(JsonKey.EMAIL, "xyz@xyz.com");
emailTemplateMap.put(JsonKey.TEMPLATE_ID, JsonKey.RESET_PASSWORD_TEMPLATE_ID);
Request request = OTPUtil.getRequestToSendOTPViaEmail(emailTemplateMap, new RequestContext());
Assert.assertNotNull(request);
}
@Test
public void getRequestToSendOTPViaEmailTest5() {
PowerMockito.mockStatic(ProjectUtil.class);
when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString");
Map<String, Object> emailTemplateMap = new HashMap<>();
emailTemplateMap.put(JsonKey.EMAIL, "xyz@xyz.com");
emailTemplateMap.put(JsonKey.TEMPLATE_ID, JsonKey.CONTACT_UPDATE_TEMPLATE_ID);
Request request = OTPUtil.getRequestToSendOTPViaEmail(emailTemplateMap, new RequestContext()); Request request = OTPUtil.getRequestToSendOTPViaEmail(emailTemplateMap, new RequestContext());
Assert.assertNotNull(request); Assert.assertNotNull(request);
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment