Skip to content

Commit

Permalink
Use EnvService For BaseUrlForEmail (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
bibekaryal86 authored Jan 13, 2025
1 parent 2e26981 commit 7cc3218
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,23 @@ public Map<String, String> getRedirectUrls() {

@CacheEvict("redirectUrls")
public void evictRedirectUrlCache() {}

@CacheEvict("baseUrlForLinkInEmail")
public void evictBaseUrlForLinkInEmailCache() {}

@Cacheable("baseUrlForLinkInEmail")
public String getBaseUrlForLinkInEmail() {
final boolean isDevelopment = environment.matchesProfiles("development");
final String envDetailsName = "baseUrlForLinkInEmail";
final List<EnvDetails> envDetails = getAuthServiceEnvProperties();
EnvDetails withRedirectUrls =
envDetails.stream()
.filter(envDetail -> envDetail.getName().equals(envDetailsName))
.findFirst()
.orElse(null);
if (withRedirectUrls == null) {
return null;
}
return withRedirectUrls.getMapValue().get(isDevelopment ? "development" : "production");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static auth.service.app.util.JwtUtils.decodeAuthCredentials;
import static java.util.concurrent.CompletableFuture.runAsync;

import auth.service.app.connector.EnvServiceConnector;
import auth.service.app.exception.ElementMissingException;
import auth.service.app.exception.JwtInvalidException;
import auth.service.app.model.dto.ProfilePasswordRequest;
Expand Down Expand Up @@ -59,17 +60,21 @@ public class ProfileBasicAuthController {
private final EmailService emailService;
private final TokenService tokenService;
private final AuditService auditService;
private final EnvServiceConnector envServiceConnector;

@PostMapping("/{platformId}/create")
public ResponseEntity<ProfileResponse> createProfile(
@PathVariable final Long platformId,
@Valid @RequestBody final ProfileRequest profileRequest,
final HttpServletRequest request) {
try {
final String baseUrl = getBaseUrlForLinkInEmail(request);
String baseUrlForLinkInEmail = envServiceConnector.getBaseUrlForLinkInEmail();
if (baseUrlForLinkInEmail == null) {
baseUrlForLinkInEmail = getBaseUrlForLinkInEmail(request);
}
final PlatformEntity platformEntity = circularDependencyService.readPlatform(platformId);
final ProfileEntity profileEntity =
profileService.createProfile(platformEntity, profileRequest, baseUrl);
profileService.createProfile(platformEntity, profileRequest, baseUrlForLinkInEmail);
runAsync(
() ->
auditService.auditProfile(
Expand Down Expand Up @@ -259,9 +264,14 @@ public ResponseEntity<ResponseMetadata> validateProfileInit(
try {
final PlatformProfileRoleEntity platformProfileRoleEntity =
platformProfileRoleService.readPlatformProfileRole(platformId, email);
final String baseUrl = getBaseUrlForLinkInEmail(request);
String baseUrlForLinkInEmail = envServiceConnector.getBaseUrlForLinkInEmail();
if (baseUrlForLinkInEmail == null) {
baseUrlForLinkInEmail = getBaseUrlForLinkInEmail(request);
}
emailService.sendProfileValidationEmail(
platformProfileRoleEntity.getPlatform(), platformProfileRoleEntity.getProfile(), baseUrl);
platformProfileRoleEntity.getPlatform(),
platformProfileRoleEntity.getProfile(),
baseUrlForLinkInEmail);
runAsync(
() ->
auditService.auditProfile(
Expand Down Expand Up @@ -302,9 +312,14 @@ public ResponseEntity<ResponseMetadata> resetProfileInit(
try {
final PlatformProfileRoleEntity platformProfileRoleEntity =
platformProfileRoleService.readPlatformProfileRole(platformId, email);
final String baseUrl = getBaseUrlForLinkInEmail(request);
String baseUrlForLinkInEmail = envServiceConnector.getBaseUrlForLinkInEmail();
if (baseUrlForLinkInEmail == null) {
baseUrlForLinkInEmail = getBaseUrlForLinkInEmail(request);
}
emailService.sendProfileResetEmail(
platformProfileRoleEntity.getPlatform(), platformProfileRoleEntity.getProfile(), baseUrl);
platformProfileRoleEntity.getPlatform(),
platformProfileRoleEntity.getProfile(),
baseUrlForLinkInEmail);
runAsync(
() ->
auditService.auditProfile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static auth.service.app.util.CommonUtils.getBaseUrlForLinkInEmail;
import static java.util.concurrent.CompletableFuture.runAsync;

import auth.service.app.connector.EnvServiceConnector;
import auth.service.app.model.annotation.CheckPermission;
import auth.service.app.model.dto.ProfileEmailRequest;
import auth.service.app.model.dto.ProfilePasswordRequest;
Expand Down Expand Up @@ -45,6 +46,7 @@ public class ProfileController {
private final EntityDtoConvertUtils entityDtoConvertUtils;
private final PermissionCheck permissionCheck;
private final AuditService auditService;
private final EnvServiceConnector envServiceConnector;

@GetMapping
public ResponseEntity<ProfileResponse> readProfiles(
Expand Down Expand Up @@ -136,13 +138,19 @@ public ResponseEntity<ProfileResponse> updateProfileEmail(
final HttpServletRequest request) {
try {
permissionCheck.checkProfileAccess("", id);
final String baseUrl = getBaseUrlForLinkInEmail(request);
String baseUrlForLinkInEmail = envServiceConnector.getBaseUrlForLinkInEmail();
if (baseUrlForLinkInEmail == null) {
baseUrlForLinkInEmail = getBaseUrlForLinkInEmail(request);
}
final PlatformProfileRoleEntity platformProfileRoleEntity =
platformProfileRoleService.readPlatformProfileRole(
platformId, profileEmailRequest.getOldEmail());
final ProfileEntity profileEntity =
profileService.updateProfileEmail(
id, profileEmailRequest, platformProfileRoleEntity.getPlatform(), baseUrl);
id,
profileEmailRequest,
platformProfileRoleEntity.getPlatform(),
baseUrlForLinkInEmail);
runAsync(
() ->
auditService.auditProfile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -23,7 +24,7 @@
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;

public class AuthEnvServiceConnectorTest extends BaseTest {
public class EnvServiceConnectorTest extends BaseTest {

private EnvServiceConnector envServiceConnector;
private MockWebServer server;
Expand All @@ -44,6 +45,7 @@ void setUp() throws Exception {
void tearDown() throws Exception {
server.shutdown();
envServiceConnector.evictRedirectUrlCache();
envServiceConnector.evictBaseUrlForLinkInEmailCache();
}

@Test
Expand Down Expand Up @@ -101,4 +103,46 @@ void testGetRedirectUrls_Unauthorized() {
});
assertEquals(1, server.getRequestCount());
}

@Test
void testGetBaseUrlForLinkInEmail_Development() {
when(environment.matchesProfiles("development")).thenReturn(true);
server.enqueue(
new MockResponse()
.setResponseCode(200)
.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.setBody(Objects.requireNonNull(FixtureReader.readFixture(responseJsonFileName))));
String result = envServiceConnector.getBaseUrlForLinkInEmail();

assertNotNull(result);
assertEquals(
TestData.getEnvDetailsResponse().getLast().getMapValue().get("development"), result);
}

@Test
void testGetBaseUrlForLinkInEmail_Production() {
when(environment.matchesProfiles("development")).thenReturn(false);
server.enqueue(
new MockResponse()
.setResponseCode(200)
.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.setBody(Objects.requireNonNull(FixtureReader.readFixture(responseJsonFileName))));
String result = envServiceConnector.getBaseUrlForLinkInEmail();

assertNotNull(result);
assertEquals(
TestData.getEnvDetailsResponse().getLast().getMapValue().get("production"), result);
}

@Test
void testGetBaseUrlForLinkInEmail_Null() {
when(environment.matchesProfiles("development")).thenReturn(true);
server.enqueue(
new MockResponse()
.setResponseCode(200)
.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.setBody("[]"));
String result = envServiceConnector.getBaseUrlForLinkInEmail();
assertNull(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import auth.service.BaseTest;
import auth.service.app.connector.EnvServiceConnector;
import auth.service.app.model.dto.ProfileDto;
import auth.service.app.model.dto.ProfilePasswordRequest;
import auth.service.app.model.dto.ProfilePasswordTokenResponse;
Expand Down Expand Up @@ -71,6 +73,7 @@ public class ProfileBasicAuthControllerTest extends BaseTest {
@MockitoBean private AuditService auditService;
@MockitoBean private EmailService emailService;
@MockitoBean private ApplicationEventPublisher applicationEventPublisher;
@MockitoBean private EnvServiceConnector envServiceConnector;

@Autowired private ProfileRepository profileRepository;
@Autowired private PlatformRepository platformRepository;
Expand Down Expand Up @@ -127,6 +130,7 @@ static void setUp(
void setUpBeforeEach() {
clearInvocations(applicationEventPublisher);
doNothing().when(applicationEventPublisher).publishEvent(any(ProfileEvent.class));
when(envServiceConnector.getBaseUrlForLinkInEmail()).thenReturn(null);
}

@AfterAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import auth.service.BaseTest;
import auth.service.app.connector.EnvServiceConnector;
import auth.service.app.model.dto.ProfileDto;
import auth.service.app.model.dto.ProfileEmailRequest;
import auth.service.app.model.dto.ProfilePasswordRequest;
Expand All @@ -31,6 +33,7 @@
import java.util.Collections;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
Expand All @@ -48,6 +51,7 @@ public class ProfileControllerTest extends BaseTest {

@MockitoBean private ApplicationEventPublisher applicationEventPublisher;
@MockitoBean private AuditService auditService;
@MockitoBean private EnvServiceConnector envServiceConnector;

@BeforeAll
static void setUpBeforeAll() {
Expand All @@ -63,6 +67,11 @@ static void setUpBeforeAll() {
TestData.getBearerAuthCredentialsForTest(platformEntity, profileDtoWithPermission);
}

@BeforeEach
void setUpBeforeEach() {
when(envServiceConnector.getBaseUrlForLinkInEmail()).thenReturn(null);
}

@AfterEach
void tearDown() {
reset(applicationEventPublisher, auditService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@
"trackcase-service": "https://trackcase-spa.com/home/"
}
}
,
{
"name": "baseUrlForLinkInEmail",
"stringValue": "",
"listValue": [],
"mapValue": {
"development": "http://localhost:8000/authsvc",
"production": "https://gatewaysvc.appspot.com/authsvc"
}
}
]

0 comments on commit 7cc3218

Please sign in to comment.