diff --git a/pom.xml b/pom.xml index 2d824c622ec8fcf1fa8fd3d039617514f3a88e35..e29a22f589dfda759c4bed43b59646092edbf875 100644 --- a/pom.xml +++ b/pom.xml @@ -76,6 +76,12 @@ <version>42.6.0</version> </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + </dependency> + + </dependencies> <build> diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/AuthenticationController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/AuthenticationController.java index 71a6a5df7bf4c831645d0d3f2e1e403f7dcd2000..8791924bbf026cfee268abafc81bdb52f47bcb04 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/AuthenticationController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/AuthenticationController.java @@ -19,7 +19,7 @@ import org.springframework.web.bind.annotation.RestController; * * @author Anders * @version 1.0 - * @since 04.04.2023 + * @since 19.04.2023 */ @RestController diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/AuthenticationControllerTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/AuthenticationControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..b5b5c071d411dfc9e95518a3fb56c15e8e426449 --- /dev/null +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/AuthenticationControllerTest.java @@ -0,0 +1,98 @@ +package ntnu.idatt2016.v233.SmartMat.controller; + +import ntnu.idatt2016.v233.SmartMat.dto.enums.Authority; +import ntnu.idatt2016.v233.SmartMat.dto.request.LoginRequest; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.http.*; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.web.client.DefaultResponseErrorHandler; +import org.springframework.web.client.RestTemplate; + +import java.time.Duration; +import java.util.Collection; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) + +public class AuthenticationControllerTest { + + @MockBean + private AuthenticationManager authenticationManager; + + + @Test + public void token_validCredentials_shouldReturnToken() { + LoginRequest loginRequest = new LoginRequest("kari123", "sjokoladekake"); + + when(authenticationManager.authenticate(any(Authentication.class))).thenReturn(new Authentication() { + @Override + public Collection<? extends GrantedAuthority> getAuthorities() { + return List.of(new SimpleGrantedAuthority(Authority.USER.toString())); + } + + @Override + public Object getCredentials() { + return "test"; + } + + @Override + public Object getDetails() { + return "test"; + } + + @Override + public Object getPrincipal() { + return "test"; + } + + @Override + public boolean isAuthenticated() { + return true; + } + + @Override + public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException { + + } + + @Override + public String getName() { + return "test"; + } + }); + + + + RestTemplateBuilder rb = new RestTemplateBuilder(); + rb.setConnectTimeout(Duration.ofSeconds(10)); + rb.setReadTimeout(Duration.ofSeconds(10)); + rb.requestFactory(() -> new HttpComponentsClientHttpRequestFactory()); + rb.errorHandler(new DefaultResponseErrorHandler(){ + protected boolean hasError(HttpStatus statusCode) { + return statusCode.is5xxServerError(); + } + }); + + + RestTemplate restTemplate = rb.build(); + + + ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://localhost:8080/api/auth/credentials", loginRequest, String.class); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertNotNull(responseEntity.getBody()); + assertFalse(responseEntity.getBody().isEmpty()); + } + +}