Skip to content

Commit

Permalink
3) added security to client service, but worker service is affected i…
Browse files Browse the repository at this point in the history
…n tests (only!)
  • Loading branch information
Fuud committed Aug 7, 2022
1 parent cfbebf6 commit 7c8abae
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
1) Initial
code [rev:2b8fcd50](https://github.com/Fuud/integration-tests-article/commit/2b8fcd509151ebc83c24d2b4e9fd0b665eb82ded)
2) Integration test module contains classpath from both microservices.
2) Integration test module contains classpath from both
microservices [rev:cfbebf68](https://github.com/Fuud/integration-tests-article/commit/cfbebf68c0876dc2bfaaca8cb3074d7c6275d414)
3) added security to client service, but worker service is affected in tests (only!)
4 changes: 4 additions & 0 deletions client-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fuud.client.service.worker.WorkerResponseDto;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

Expand All @@ -16,8 +17,14 @@ public ClientServiceEndpoint(ClientServiceConfig config, RestTemplateBuilder res
}

@PostMapping("/task")
public String placeTask(@RequestBody ClientRequest request){
public String placeTask(@RequestBody ClientRequest request) {
return restTemplate.postForObject(config.getWorkerUrl(), request, WorkerResponseDto.class).getJobId();
}

@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public void handleException(Throwable t) {
t.printStackTrace();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package fuud.client.service;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

import static org.springframework.security.config.Customizer.withDefaults;

@Configuration
public class SecurityConfiguration {

@Bean
public UserDetailsService userDetailsService() {
UserDetails user = User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authz) -> authz
.anyRequest().authenticated()
)
.httpBasic(withDefaults());

http.csrf().disable();
return http.build();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import fuud.client.service.ClientServiceApplication;
import fuud.worker.service.WorkerServiceApplication;
import org.junit.Test;
import org.springframework.http.HttpHeaders;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Base64;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand All @@ -22,12 +24,13 @@ public void testTaskSubmission() throws Exception {
HttpRequest.newBuilder()
.method("POST", HttpRequest.BodyPublishers.ofString("{ \"data\":\"my-data\"}"))
.header("Content-Type", "application/json")
.header(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes()))
.uri(URI.create("http://localhost:8080/task"))
.build(),
HttpResponse.BodyHandlers.ofString()
);

assertEquals(response.statusCode(), 200);
assertEquals(200, response.statusCode());
assertFalse(response.body().isBlank());
}
}

0 comments on commit 7c8abae

Please sign in to comment.