Commit fa65507d authored by Lukáš Chudíček's avatar Lukáš Chudíček
Browse files

test: tests working with security

parent fb5df9f9
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.EventListener;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
@@ -18,6 +19,7 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@Profile("prod")
public class SecurityConfig {
    private static final Logger log = LoggerFactory.getLogger(App.class);
    private static final String SECURITY_SCHEME_OAUTH2 = "MUNI";
+2 −0
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ server:
    include-message: always

spring:
  profiles:
    active: prod
  jpa:
    open-in-view: false
    hibernate:
+28 −0
Original line number Diff line number Diff line
package cz.muni.fi.iamdb.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@Profile("test")
public class SecurityConfig {
    /**
     * Configure access restrictions to the API.
     * Introspection of opaque access token is configured, introspection endpoint is defined in application.yml.
     */
    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {

        http
                .authorizeHttpRequests(x -> x
                        .anyRequest().permitAll()
                )
//                .oauth2ResourceServer(oauth2 -> oauth2.opaqueToken(Customizer.withDefaults()))
        ;
        return http.build();
    }
}
+136 −136
Original line number Diff line number Diff line
//package cz.muni.fi.iamdb.integration;
//
//import com.fasterxml.jackson.databind.JsonNode;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import jakarta.transaction.Transactional;
//import org.junit.jupiter.api.Test;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.test.annotation.Rollback;
//import org.springframework.test.context.ActiveProfiles;
//import org.springframework.test.web.servlet.MockMvc;
//
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
//
//@SpringBootTest
//@AutoConfigureMockMvc
//@ActiveProfiles("test")
//@Rollback
//public class IntegrationTest {
//
//    @Autowired
//    private MockMvc mockMvc;
//
//
//    @Test
//    @Transactional
//    void testCreate() throws Exception {
//        String userJson = """
//                {
//                    "name": "alice",
//                    "email": "valid@mail.com",
//                    "hashed_password": "the_hash",
//                    "salt": "the_salt",
//                    "role": "USER"
//                }
//                """;
//        mockMvc.perform(post("/users")
//                        .contentType("application/json")
//                        .content(userJson))
//                .andExpect(status().isCreated())
//                .andExpect(jsonPath("$.id").exists());
//    }
//
//    @Test
//    @Transactional
//    void testFindAll() throws Exception {
//        mockMvc.perform(get("/users"))
//                .andExpect(status().isOk())
//                .andExpect(jsonPath("$").isArray());
//    }
//
//    @Test
//    @Transactional
//    void testFindById() throws Exception {
//        // setup
//        String userJson = """
//                {
//                    "name": "alice",
//                    "email": "valid@mail.com",
//                    "hashed_password": "the_hash",
//                    "salt": "the_salt",
//                    "role": "USER"
//                }
//                """;
//
//
//        String jsonResponse = mockMvc.perform(post("/users")
//                        .contentType("application/json")
//                        .content(userJson)).andReturn().getResponse().getContentAsString();
//        JsonNode jsonNode = new ObjectMapper().readTree(jsonResponse);
//        String insertedUuidButString = jsonNode.get("id").asText();
//
//        // get & assert
//        mockMvc.perform(get("/users/" + insertedUuidButString))
//                .andExpect(status().isOk())
//                .andExpect(jsonPath("$.id").value(insertedUuidButString));
//    }
//
//    @Test
//    @Transactional
//    void testDelete() throws Exception {
//        // setup
//        String userJson = """
//                {
//                    "name": "alice",
//                    "email": "valid@mail.com",
//                    "hashed_password": "the_hash",
//                    "salt": "the_salt",
//                    "role": "USER"
//                }
//                """;
//
//
//        String jsonResponse = mockMvc.perform(post("/users")
//                .contentType("application/json")
//                .content(userJson)).andReturn().getResponse().getContentAsString();
//        JsonNode jsonNode = new ObjectMapper().readTree(jsonResponse);
//        String insertedUuidButString = jsonNode.get("id").asText();
//
//        // act
//        mockMvc.perform(delete("/users/" + insertedUuidButString))
//                .andExpect(status().isNoContent());
//
//        mockMvc.perform(delete("/users/" + insertedUuidButString))
//                .andExpect(status().isNoContent()); // delete idempotent
//    }
//
//    @Test
//    @Transactional
//    void testDeleteNonexistent() throws Exception {
//        mockMvc.perform(delete("/users/00000000-0000-0000-0000-000000000000"))
//                .andExpect(status().isNoContent()); // delete idempotent
//    }
//
//    @Test
//    void testCreateInvalid() throws Exception {
//        String userJson = """
//                {
//                    "name": "alice",
//                    "hashed_password": "the_hash",
//                    "salt": "the_salt",
//                    "role": "USER"
//                }
//                """;
//
//        mockMvc.perform(post("/users")
//                .contentType("application/json")
//                .content(userJson))
//                // FUCK WHOLE JPA
//                .andExpect(status().isInternalServerError());
//    }
//}
package cz.muni.fi.iamdb.integration;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;

@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
@ActiveProfiles("test")
@Rollback
public class IntegrationTest {

    @Autowired
    private MockMvc mockMvc;


    @Test
    @Transactional
    void testCreate() throws Exception {
        String userJson = """
                {
                    "name": "alice",
                    "email": "valid@mail.com",
                    "hashed_password": "the_hash",
                    "salt": "the_salt",
                    "role": "USER"
                }
                """;
        mockMvc.perform(post("/users")
                        .contentType("application/json")
                        .content(userJson))
                .andExpect(status().isCreated())
                .andExpect(jsonPath("$.id").exists());
    }

    @Test
    @Transactional
    void testFindAll() throws Exception {
        mockMvc.perform(get("/users"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$").isArray());
    }

    @Test
    @Transactional
    void testFindById() throws Exception {
        // setup
        String userJson = """
                {
                    "name": "alice",
                    "email": "valid@mail.com",
                    "hashed_password": "the_hash",
                    "salt": "the_salt",
                    "role": "USER"
                }
                """;


        String jsonResponse = mockMvc.perform(post("/users")
                        .contentType("application/json")
                        .content(userJson)).andReturn().getResponse().getContentAsString();
        JsonNode jsonNode = new ObjectMapper().readTree(jsonResponse);
        String insertedUuidButString = jsonNode.get("id").asText();

        // get & assert
        mockMvc.perform(get("/users/" + insertedUuidButString))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.id").value(insertedUuidButString));
    }

    @Test
    @Transactional
    void testDelete() throws Exception {
        // setup
        String userJson = """
                {
                    "name": "alice",
                    "email": "valid@mail.com",
                    "hashed_password": "the_hash",
                    "salt": "the_salt",
                    "role": "USER"
                }
                """;


        String jsonResponse = mockMvc.perform(post("/users")
                .contentType("application/json")
                .content(userJson)).andReturn().getResponse().getContentAsString();
        JsonNode jsonNode = new ObjectMapper().readTree(jsonResponse);
        String insertedUuidButString = jsonNode.get("id").asText();

        // act
        mockMvc.perform(delete("/users/" + insertedUuidButString))
                .andExpect(status().isNoContent());

        mockMvc.perform(delete("/users/" + insertedUuidButString))
                .andExpect(status().isNoContent()); // delete idempotent
    }

    @Test
    @Transactional
    void testDeleteNonexistent() throws Exception {
        mockMvc.perform(delete("/users/00000000-0000-0000-0000-000000000000"))
                .andExpect(status().isNoContent()); // delete idempotent
    }

    @Test
    void testCreateInvalid() throws Exception {
        String userJson = """
                {
                    "name": "alice",
                    "hashed_password": "the_hash",
                    "salt": "the_salt",
                    "role": "USER"
                }
                """;

        mockMvc.perform(post("/users")
                .contentType("application/json")
                .content(userJson))
                // FUCK WHOLE JPA
                .andExpect(status().isInternalServerError());
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ import cz.muni.fi.iamdb.facade.UserFacade;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
@@ -18,6 +19,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@AutoConfigureMockMvc(addFilters = false)
@WebMvcTest(controllers = UserRestController.class)
class UserRestControllerTest {

Loading