From 1cb8215aea9c3dbfa1e39873c3cc882165145860 Mon Sep 17 00:00:00 2001 From: birkon <birkon@stud.ntnu.no> Date: Mon, 24 Apr 2023 12:38:45 +0200 Subject: [PATCH] added test for authentication --- pom.xml | 6 ++ .../controller/AuthenticationController.java | 2 +- .../AuthenticationControllerTest.java | 98 +++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 src/test/java/ntnu/idatt2016/v233/SmartMat/controller/AuthenticationControllerTest.java diff --git a/pom.xml b/pom.xml index 2d824c62..e29a22f5 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 71a6a5df..8791924b 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 00000000..b5b5c071 --- /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()); + } + +} -- GitLab