Skip to content

Commit

Permalink
013/01: exclude integrationTest when build image
Browse files Browse the repository at this point in the history
  • Loading branch information
thuan committed Jan 13, 2024
1 parent a1ee848 commit fd30c64
Show file tree
Hide file tree
Showing 11 changed files with 715 additions and 674 deletions.
41 changes: 41 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,46 @@
<io.jsonwebtoken.jjwt-jackson.version>0.11.5</io.jsonwebtoken.jjwt-jackson.version>
<image>thuanvn2002/spotify:latest</image>
</properties>
<profiles>
<profile>
<id>unit-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version> <!-- Use the latest version -->

<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>integration-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M7</version> <!-- Use the latest version -->

<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<dependencies>
<dependency>
<groupId>com.cloudinary</groupId>
Expand Down Expand Up @@ -196,6 +236,7 @@
</to>
</configuration>
</plugin>

</plugins>

</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,67 +41,67 @@ public JwtAuthenticationFilter jwtAuthenticationFilter () {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
// .csrf((AbstractHttpConfigurer::disable))
// .cors(Customizer.withDefaults())
// .authorizeHttpRequests(
// authz ->
// authz
// .requestMatchers(
// "/api/v1/song/save",
// "/api/v1/song/update/**",
// "/api/v1/song/upload/**",
// "/api/v1/album/upload/**",
// "/api/v1/album/*/add/**",
// "/api/v1/album/*/remove/**",
// "/api/v1/album/*/add",
// "/api/v1/album/update/**"
// )
// .hasRole( "ARTIST")
// .requestMatchers(
// "/api/v1/role/**",
// "/api/v1/playlist/admin/**",
// "/api/v1/category/admin/**",
// "/api/v1/review/admin/**"
// )
// .hasRole("ADMIN")
// .requestMatchers(
// "/api/v1/song/find/by/sentiment/**",
// "/api/v1/song/increase/view/**",
// "/api/v1/user/increase/view/**",
// "/api/v1/user/*/playlists/followings",
// "/api/v1/user/*/add/**",
// "/api/v1/user/*/remove/**",
// "/api/v1/playlist/user/*/add/**",
// "/api/v1/playlist/user/*/remove/**",
// "/api/v1/playlist/*/create/playlist",
// "/api/v1/playlist/*/add/song/**",
// "/api/v1/playlist/*/remove/song/**",
// "/api/v1/playlist/upload/**",
// "/api/v1/follower/*/follow/**",
// "/api/v1/follower/*/cancel/**",
// "/api/v1/follower/*/followings",
// "/api/v1/follower/is/*/followed/**",
// "/api/v1/review/*/review/in/**"
// )
// .authenticated()
// .anyRequest()
// .permitAll())
// .sessionManagement(httpSecuritySessionManagementConfigurer -> httpSecuritySessionManagementConfigurer
// .sessionCreationPolicy(SessionCreationPolicy.STATELESS))
// .authenticationProvider(authenticationProvider)
// .addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
// .logout(httpSecurityLogoutConfigurer -> httpSecurityLogoutConfigurer.logoutUrl("/api/v1/auth/logout")
// .logoutSuccessHandler((request, response, authentication) -> SecurityContextHolder.clearContext()))
.csrf((AbstractHttpConfigurer::disable))
.cors(Customizer.withDefaults())
.authorizeHttpRequests(authorizationManagerRequestMatcherRegistry -> authorizationManagerRequestMatcherRegistry
.requestMatchers("/api/v1/allowAllByPhi/**").authenticated().anyRequest().permitAll())
.authorizeHttpRequests(
authz ->
authz
.requestMatchers(
"/api/v1/song/save",
"/api/v1/song/update/**",
"/api/v1/song/upload/**",
"/api/v1/album/upload/**",
"/api/v1/album/*/add/**",
"/api/v1/album/*/remove/**",
"/api/v1/album/*/add",
"/api/v1/album/update/**"
)
.hasRole( "ARTIST")
.requestMatchers(
"/api/v1/role/**",
"/api/v1/playlist/admin/**",
"/api/v1/category/admin/**",
"/api/v1/review/admin/**"
)
.hasRole("ADMIN")
.requestMatchers(
"/api/v1/song/find/by/sentiment/**",
"/api/v1/song/increase/view/**",
"/api/v1/user/increase/view/**",
"/api/v1/user/*/playlists/followings",
"/api/v1/user/*/add/**",
"/api/v1/user/*/remove/**",
"/api/v1/playlist/user/*/add/**",
"/api/v1/playlist/user/*/remove/**",
"/api/v1/playlist/*/create/playlist",
"/api/v1/playlist/*/add/song/**",
"/api/v1/playlist/*/remove/song/**",
"/api/v1/playlist/upload/**",
"/api/v1/follower/*/follow/**",
"/api/v1/follower/*/cancel/**",
"/api/v1/follower/*/followings",
"/api/v1/follower/is/*/followed/**",
"/api/v1/review/*/review/in/**"
)
.authenticated()
.anyRequest()
.permitAll())
.sessionManagement(httpSecuritySessionManagementConfigurer -> httpSecuritySessionManagementConfigurer
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authenticationProvider(authenticationProvider)
.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
.logout(httpSecurityLogoutConfigurer -> httpSecurityLogoutConfigurer.logoutUrl("/api/v1/auth/logout")
.logoutSuccessHandler((request, response, authentication) -> SecurityContextHolder.clearContext()))
// .csrf((AbstractHttpConfigurer::disable))
// .cors(Customizer.withDefaults())
// .authorizeHttpRequests(authorizationManagerRequestMatcherRegistry -> authorizationManagerRequestMatcherRegistry
// .requestMatchers("/api/v1/allowAllByPhi/**").authenticated().anyRequest().permitAll())
// .sessionManagement(httpSecuritySessionManagementConfigurer -> httpSecuritySessionManagementConfigurer
// .sessionCreationPolicy(SessionCreationPolicy.STATELESS))
// .authenticationProvider(authenticationProvider)
// .addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
// .logout(httpSecurityLogoutConfigurer -> httpSecurityLogoutConfigurer.logoutUrl("/api/v1/auth/logout")
// .logoutSuccessHandler((request, response, authentication) -> SecurityContextHolder.clearContext()))
;
return http.build();
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/spotify/app/AbstractTestcontainers.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static void beforeAll() {
Flyway flyway = Flyway
.configure()
.dataSource(
mySQLContainer.getJdbcUrl(),
mySQLContainer.getJdbcUrl().concat("?autoReconnect=true&useSSL=false"),
mySQLContainer.getUsername(),
mySQLContainer.getPassword()
).load();
Expand All @@ -40,9 +40,9 @@ static void beforeAll() {

@DynamicPropertySource
static void dynamicProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", mySQLContainer::getJdbcUrl);
String jdbcUrl = mySQLContainer.getJdbcUrl().concat("?autoReconnect=true&useSSL=false");
registry.add("spring.datasource.url", () -> jdbcUrl);
registry.add("spring.datasource.username", mySQLContainer::getUsername);
registry.add("spring.datasource.password", mySQLContainer::getPassword);
}

// @BeforeAll
Expand Down
128 changes: 64 additions & 64 deletions src/test/java/com/spotify/app/journey/AlbumIT.java
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
//package com.spotify.app.journey;
//
//import com.github.javafaker.Faker;
//import com.spotify.app.AbstractTestcontainers;
//import com.spotify.app.dto.AlbumDTO;
//import com.spotify.app.dto.request.AlbumRequest;
//import com.spotify.app.dto.response.UserNoAssociationResponse;
//import com.spotify.app.security.auth.AuthenticationRequest;
//import com.spotify.app.security.auth.AuthenticationResponse;
//import com.spotify.app.security.jwt.JwtService;
//import lombok.extern.slf4j.Slf4j;
//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.web.client.TestRestTemplate;
//import org.springframework.http.*;
//
//import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
//
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//@Slf4j
//public class AlbumIT extends AbstractTestcontainers {
//
// private static final String AUTH_PATH = "/api/v1/auth";
// private static final String ALBUM_PATH = "/api/v1/album";
//
// @Autowired
// private TestRestTemplate restTemplate;
// @Autowired
// private JwtService jwtService;
//
// @Test
// public void canArtistAddAlbum() {
// // given
// Faker faker = new Faker();
// String email = "taylor@gmail.com";
// String password = "thuan2023";
// AuthenticationRequest authRequest = new AuthenticationRequest(email,password);
// AuthenticationResponse authResponse = restTemplate.postForObject(AUTH_PATH+"/authenticate", authRequest, AuthenticationResponse.class);
// log.info(String.valueOf(authResponse));
// String token = authResponse.getAccessToken();
//
//
// Long userId = authResponse.getUser().id();
//
// AlbumRequest albumRequest = new AlbumRequest(faker.funnyName().name());
//
// HttpHeaders httpHeaders = new HttpHeaders();
// httpHeaders.set(HttpHeaders.ACCEPT , MediaType.APPLICATION_JSON_VALUE);
// httpHeaders.set("Authorization", "Bearer " + token);
//
// HttpEntity<?> request = new HttpEntity<>(albumRequest, httpHeaders);
//
// String url = String.format(ALBUM_PATH.concat("/%d/add"),userId);
// log.info(url);
//
// // when
// ResponseEntity<AlbumDTO> response = restTemplate.exchange(url, HttpMethod.POST ,request, AlbumDTO.class);
//
// // then
// assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
//
// }
//}
package com.spotify.app.journey;

import com.github.javafaker.Faker;
import com.spotify.app.AbstractTestcontainers;
import com.spotify.app.dto.AlbumDTO;
import com.spotify.app.dto.request.AlbumRequest;
import com.spotify.app.dto.response.UserNoAssociationResponse;
import com.spotify.app.security.auth.AuthenticationRequest;
import com.spotify.app.security.auth.AuthenticationResponse;
import com.spotify.app.security.jwt.JwtService;
import lombok.extern.slf4j.Slf4j;
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.web.client.TestRestTemplate;
import org.springframework.http.*;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
public class AlbumIT extends AbstractTestcontainers {

private static final String AUTH_PATH = "/api/v1/auth";
private static final String ALBUM_PATH = "/api/v1/album";

@Autowired
private TestRestTemplate restTemplate;
@Autowired
private JwtService jwtService;

@Test
public void canArtistAddAlbum() {
// given
Faker faker = new Faker();
String email = "taylor@gmail.com";
String password = "thuan2023";
AuthenticationRequest authRequest = new AuthenticationRequest(email,password);
AuthenticationResponse authResponse = restTemplate.postForObject(AUTH_PATH+"/authenticate", authRequest, AuthenticationResponse.class);
log.info(String.valueOf(authResponse));
String token = authResponse.getAccessToken();


Long userId = authResponse.getUser().id();

AlbumRequest albumRequest = new AlbumRequest(faker.funnyName().name());

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set(HttpHeaders.ACCEPT , MediaType.APPLICATION_JSON_VALUE);
httpHeaders.set("Authorization", "Bearer " + token);

HttpEntity<?> request = new HttpEntity<>(albumRequest, httpHeaders);

String url = String.format(ALBUM_PATH.concat("/%d/add"),userId);
log.info(url);

// when
ResponseEntity<AlbumDTO> response = restTemplate.exchange(url, HttpMethod.POST ,request, AlbumDTO.class);

// then
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);

}
}
Loading

0 comments on commit fd30c64

Please sign in to comment.