diff --git a/backend/app/src/main/java/com/ugent/pidgeon/config/AuthConfig.java b/backend/app/src/main/java/com/ugent/pidgeon/config/AuthConfig.java index 280d62b6..a01b6f5d 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/config/AuthConfig.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/config/AuthConfig.java @@ -19,7 +19,6 @@ public class AuthConfig { @Bean public FilterRegistrationBean filterRegistrationBean() { System.out.println("tenantId: " + tenantId); - FilterRegistrationBean filter = new FilterRegistrationBean<>(); filter.setFilter(new JwtAuthenticationFilter(tenantId)); filter.addUrlPatterns("/api/ietswatiknietwiltesten"); diff --git a/backend/app/src/main/java/com/ugent/pidgeon/config/JwtAuthenticationFilter.java b/backend/app/src/main/java/com/ugent/pidgeon/config/JwtAuthenticationFilter.java index 4ef865e3..f0cd278e 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/config/JwtAuthenticationFilter.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/config/JwtAuthenticationFilter.java @@ -31,6 +31,7 @@ public JwtAuthenticationFilter(String tenantId) { try { logger.info("tenantId: " + tenantId); + provider = new UrlJwkProvider(new URL("https://login.microsoftonline.com/"+tenantId+"/discovery/v2.0/keys")); } catch (MalformedURLException e) { e.printStackTrace(); diff --git a/backend/app/src/main/java/com/ugent/pidgeon/controllers/AuthTestController.java b/backend/app/src/main/java/com/ugent/pidgeon/controllers/AuthTestController.java index d15315b5..75aa75da 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/controllers/AuthTestController.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/controllers/AuthTestController.java @@ -10,6 +10,7 @@ @RestController public class AuthTestController { + @Autowired private UserRepository userRepository; diff --git a/backend/app/src/main/java/com/ugent/pidgeon/model/Auth.java b/backend/app/src/main/java/com/ugent/pidgeon/model/Auth.java index 492ebe41..d6c0c1d5 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/model/Auth.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/model/Auth.java @@ -5,7 +5,9 @@ import org.springframework.util.Assert; import java.util.Collection; +import java.util.List; import com.ugent.pidgeon.model.User; + public class Auth extends AbstractAuthenticationToken { private static final long serialVersionUID = 620L; @@ -51,6 +53,11 @@ public User getUser() { return user; } + + public List getGroups() { + return user.groups; + } + public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException { Assert.isTrue(!isAuthenticated, "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead"); super.setAuthenticated(false); diff --git a/backend/app/src/main/java/com/ugent/pidgeon/model/User.java b/backend/app/src/main/java/com/ugent/pidgeon/model/User.java index 006341ee..960d42f5 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/model/User.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/model/User.java @@ -19,4 +19,5 @@ public User (String name, String firstName, String lastName, String email, List< this.firstName = firstName; this.lastName = lastName; } -} \ No newline at end of file +} + diff --git a/backend/app/src/test/java/com/ugent/pidgeon/controllers/HttpAuthControllerTest.java b/backend/app/src/test/java/com/ugent/pidgeon/controllers/HttpAuthControllerTest.java new file mode 100644 index 00000000..c2fc515e --- /dev/null +++ b/backend/app/src/test/java/com/ugent/pidgeon/controllers/HttpAuthControllerTest.java @@ -0,0 +1,33 @@ +package com.ugent.pidgeon.controllers; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.test.web.server.LocalServerPort; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +class HttpAuthControllerTest { + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Test + void indexHttpTest() throws Exception { + assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/", + String.class)).contains("Running..."); + } + + @Test + void pingponghttpTest() throws Exception { + assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/ping", + String.class)).contains("Pong"); + } +} diff --git a/backend/app/src/test/java/com/ugent/pidgeon/controllers/StaticAuthControllerTest.java b/backend/app/src/test/java/com/ugent/pidgeon/controllers/StaticAuthControllerTest.java new file mode 100644 index 00000000..f2447081 --- /dev/null +++ b/backend/app/src/test/java/com/ugent/pidgeon/controllers/StaticAuthControllerTest.java @@ -0,0 +1,52 @@ +package com.ugent.pidgeon.controllers; + +import com.ugent.pidgeon.model.User; +import com.ugent.pidgeon.model.Auth; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.core.authority.SimpleGrantedAuthority; + +import java.util.List; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; + +// Test de AuthTestController klasse direct door zijn methodes te laden +@SpringBootTest +public class StaticAuthControllerTest { + private final List groepLijst = List.of("Groep 1"); + + + private final User user = new User("Tester De Test", "Tester", "De Test", + "test.testing@gtest.test", groepLijst, "123456"); + private final List authLijst = List.of(new SimpleGrantedAuthority("READ_AUTHORITY")); + private final Auth auth = new Auth(user, authLijst); + + @Autowired + private AuthTestController authTestController; + + + @Test + void contextLoads() throws Exception { + assertThat(authTestController).isNotNull(); + } + + @Test + void pingPong() throws Exception { + assertEquals(authTestController.ping(), "Pong"); + } + + @Test + void indexTest() throws Exception { + assertEquals(authTestController.index(), "Running..."); + } + +// Werkt voorlopig nog niet +// @Test +// void apiTest() throws Exception { +// assertEquals(authTestController.testApi(auth), user); +// assertEquals(authTestController.testApi(auth).groups, groepLijst); +// assertEquals(authTestController.testApi(auth).name, "Tester De Test"); +// } +} \ No newline at end of file diff --git a/backend/app/src/test/java/com/ugent/pidgeon/model/AuthTest.java b/backend/app/src/test/java/com/ugent/pidgeon/model/AuthTest.java new file mode 100644 index 00000000..a5924771 --- /dev/null +++ b/backend/app/src/test/java/com/ugent/pidgeon/model/AuthTest.java @@ -0,0 +1,43 @@ +package com.ugent.pidgeon.model; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.core.authority.SimpleGrantedAuthority; + +import java.util.List; + +@SpringBootTest +public class AuthTest { + + private final List groeplijst = List.of("testgroep1", "testgroep2"); + private final User testUser = new User("John Doe", "John", "Doe", "john.doe@gmail.com", groeplijst, "123456"); + + private final List authLijst = List.of(new SimpleGrantedAuthority("READ_AUTHORITY")); + private final Auth auth = new Auth(testUser, authLijst); + + @Test + public void nameTest(){ + Assertions.assertEquals(auth.getName(), "John Doe"); + } + + @Test + public void emailTest(){ + Assertions.assertEquals(auth.getEmail(), "john.doe@gmail.com"); + } + @Test + public void groupTest(){ + Assertions.assertEquals(auth.getGroups().size(), 2); + Assertions.assertEquals(auth.getGroups().get(0), "testgroep1"); + } + @Test + public void oidTest(){ + Assertions.assertEquals(auth.getCredentials(), "123456"); + } + + @Test + public void userTest(){ + Assertions.assertEquals(auth.getUser(), testUser); + } + +} diff --git a/backend/app/src/test/java/com/ugent/pidgeon/model/UserTest.java b/backend/app/src/test/java/com/ugent/pidgeon/model/UserTest.java new file mode 100644 index 00000000..37dcf4e8 --- /dev/null +++ b/backend/app/src/test/java/com/ugent/pidgeon/model/UserTest.java @@ -0,0 +1,20 @@ +package com.ugent.pidgeon.model; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.List; + +@SpringBootTest +public class UserTest { + + private final List groeplijst = List.of("testgroep1", "testgroep2"); + private final User testUser = new User("John Doe", "John", "Doe", "john.doe@gmail.com", groeplijst, "123456"); + + @Test + public void isNotNull(){ + Assertions.assertNotNull(testUser); + } + +}