Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove CC of team on password resets. #1225

Merged
merged 2 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions core/src/main/java/io/aiven/klaw/service/MailUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import io.aiven.klaw.config.ManageDatabase;
import io.aiven.klaw.dao.RegisterUserInfo;
import io.aiven.klaw.dao.Team;
import io.aiven.klaw.dao.UserInfo;
import io.aiven.klaw.helpers.HandleDbRequests;
import io.aiven.klaw.helpers.KwConstants;
import io.aiven.klaw.helpers.UtilMethods;
import io.aiven.klaw.model.enums.ApiResultStatus;
import io.aiven.klaw.model.enums.MailType;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -178,7 +180,7 @@ void sendMailResetPwd(
formattedStr = String.format(passwordReset, username, pwd);
subject = KLAW_ACCESS_PASSWORD_RESET_REQUESTED;

sendMail(username, dbHandle, formattedStr, subject, false, null, tenantId, loginUrl);
sendPwdResetMail(username, dbHandle, formattedStr, subject, false, null, tenantId, loginUrl);
}

void sendMailRegisteredUserSaas(
Expand Down Expand Up @@ -333,7 +335,7 @@ private void sendMail(
if (registrationRequest) {
emailId = otherMailId;
} else {
emailId = dbHandle.getUsersInfo(username).getMailid();
emailId = getEmailAddressFromUsername(username);
}

try {
Expand All @@ -355,6 +357,48 @@ private void sendMail(
});
}

private void sendPwdResetMail(
String username,
HandleDbRequests dbHandle,
String formattedStr,
String subject,
boolean registrationRequest,
String otherMailId,
int tenantId,
String loginUrl) {

CompletableFuture.runAsync(
() -> {
String emailId;

try {
emailId = getEmailAddressFromUsername(username);

if (emailId != null) {
emailService.sendSimpleMessage(
emailId, null, subject, formattedStr, tenantId, loginUrl);
} else {
log.error("Email id not found. Notification not sent !!");
}
} catch (Exception e) {
log.error("Email id not found. Notification not sent !! ", e);
}
});
}

public String getEmailAddressFromUsername(String username) {

Optional<UserInfo> user =
manageDatabase.selectAllCachedUserInfo().stream()
.filter(u -> u.getUsername().equals(username))
.findFirst();
if (user.isPresent()) {
return user.get().getMailid();
} else {
return null;
}
}

public String sendMailToSaasAdmin(int tenantId, String userName, String period, String loginUrl) {
String mailtext =
"Tenant extension : Tenant " + tenantId + " username " + userName + " period " + period;
Expand Down
57 changes: 55 additions & 2 deletions core/src/test/java/io/aiven/klaw/service/MailUtilsTest.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,77 @@
package io.aiven.klaw.service;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.when;

import io.aiven.klaw.config.ManageDatabase;
import io.aiven.klaw.dao.UserInfo;
import io.aiven.klaw.helpers.HandleDbRequests;
import io.aiven.klaw.helpers.KwConstants;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
public class MailUtilsTest {

public static final String LOGIN_URL = "https://localhost:9097";
@Mock UserDetails userDetails;

private MailUtils mailService;
@Mock HandleDbRequests handleDbRequests;

@Mock EmailService emailService;

@Mock ManageDatabase manageDatabase;

@InjectMocks private MailUtils mailService;

@BeforeEach
public void setUp() throws Exception {
mailService = new MailUtils();
// mailService = new MailUtils();
}

@Test
public void getUserDetails() {}

@Test
public void resetPasswordEmail_noCCTeam() throws InterruptedException {

String username = "Octopus";
UserInfo info = new UserInfo();
info.setUsername(username);
info.setMailid("Octopus.klaw@mailid");
when(manageDatabase.selectAllCachedUserInfo()).thenReturn(List.of(info));
when(manageDatabase.getKwPropertyValue(eq("klaw.mail.passwordreset.content"), eq(101)))
.thenReturn(KwConstants.MAIL_PASSWORDRESET_CONTENT);
mailService.sendMailResetPwd(username, "KlawPassword", handleDbRequests, 101, LOGIN_URL);

Thread.sleep(1000);
Mockito.verify(emailService, timeout(1000).times(1))
.sendSimpleMessage(
eq(info.getMailid()), eq(null), anyString(), anyString(), eq(101), eq(LOGIN_URL));
}

@Test
public void resetPasswordEmail_noSuchUser() {

String username = "Octopus";
UserInfo info = new UserInfo();
info.setUsername("Octi");
info.setMailid("Octi.klaw@mailid");
when(manageDatabase.selectAllCachedUserInfo()).thenReturn(List.of(info));
when(manageDatabase.getKwPropertyValue(eq("klaw.mail.passwordreset.content"), eq(101)))
.thenReturn(KwConstants.MAIL_PASSWORDRESET_CONTENT);
mailService.sendMailResetPwd(username, "KlawPassword", handleDbRequests, 101, LOGIN_URL);
Mockito.verify(emailService, timeout(1000).times(0))
.sendSimpleMessage(
eq(info.getMailid()), eq(null), anyString(), anyString(), eq(101), eq(LOGIN_URL));
}
}