Skip to content

Commit

Permalink
add unit test for email service (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuyDang1108 authored Sep 27, 2024
1 parent d4158c1 commit e6bcf2e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ public class ReCaptchaServiceImpl implements ReCaptchaService {
@Value("${default.captcha-enable}")
private String captchaEnable;

private final RestTemplate restTemplate = new RestTemplate();

private static final String URL = "https://www.google.com/recaptcha/api/siteverify";

@Override
public boolean validateRecaptcha(String recaptchaResponse) {
if (captchaEnable.equals("false")) {
return true;
}
RestTemplate restTemplate = new RestTemplate();
String url = URL + "?secret=" + recaptchaSecretKey + "&response=" + recaptchaResponse;
String response = restTemplate.postForObject(url, null, String.class);
assert response != null;
JsonObject jsonObject = JsonParser.parseString(response).getAsJsonObject();
return jsonObject.get("success").getAsBoolean();
}
}
}
68 changes: 68 additions & 0 deletions src/test/java/com/fjb/sunrise/services/ReCaptchaServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.fjb.sunrise.services;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;

import org.junit.jupiter.api.Assertions;
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.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.RestTemplate;

@ExtendWith(SpringExtension.class)
@SpringBootTest
class ReCaptchaServiceTest {
@MockBean
private RestTemplate restTemplate;
@Autowired
private ReCaptchaService recaptchaService;

@Value("${default.recaptcha-secret-key}")
private String siteKey;
@Test
void validateRecaptcha_whenCaptchaDisabled_ThenReturnTrue() {
ReflectionTestUtils.setField(recaptchaService, "captchaEnable", "false");

boolean result = recaptchaService.validateRecaptcha("any-response");
Assertions.assertTrue(result);
}

@Test
void validateRecaptcha_whenCaptchaEnabledAndValidResponse_ThenReturnTrue() {
ReflectionTestUtils.setField(recaptchaService, "captchaEnable", "true");
ReflectionTestUtils.setField(recaptchaService, "recaptchaSecretKey", siteKey);
ReflectionTestUtils.setField(recaptchaService, "restTemplate", restTemplate);

String recaptchaResponse = "valid-response";
String mockApiResponse = "{\"success\": true}";
Mockito.when(restTemplate.postForObject(Mockito.anyString(), Mockito.isNull(), Mockito.eq(String.class)))
.thenReturn(mockApiResponse);

boolean result = recaptchaService.validateRecaptcha(recaptchaResponse);

Assertions.assertTrue(result);
}

@Test
void validateRecaptcha_whenCaptchaEnabledAndInvalidResponse_ThenReturnFalse() {
ReflectionTestUtils.setField(recaptchaService, "captchaEnable", "true");

String recaptchaResponse = "invalid-response";
String mockApiResponse = "{\"success\": false}";

Mockito.when(restTemplate.postForObject(Mockito.anyString(), Mockito.isNull(), Mockito.eq(String.class)))
.thenReturn(mockApiResponse);

boolean result = recaptchaService.validateRecaptcha(recaptchaResponse);

Assertions.assertFalse(result);
}
}

0 comments on commit e6bcf2e

Please sign in to comment.