]> git.somenet.org - pub/jan/dst18.git/blob - ass2-service/facade/src/test/java/dst/ass2/service/facade/test/AuthenticationResourceTest.java
Add template for assignment 2
[pub/jan/dst18.git] / ass2-service / facade / src / test / java / dst / ass2 / service / facade / test / AuthenticationResourceTest.java
1 package dst.ass2.service.facade.test;
2
3 import static org.hamcrest.CoreMatchers.allOf;
4 import static org.hamcrest.CoreMatchers.is;
5 import static org.hamcrest.CoreMatchers.not;
6 import static org.junit.Assert.assertNotNull;
7 import static org.junit.Assert.assertThat;
8
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 import org.springframework.boot.context.embedded.LocalServerPort;
13 import org.springframework.boot.test.context.SpringBootTest;
14 import org.springframework.http.HttpEntity;
15 import org.springframework.http.HttpHeaders;
16 import org.springframework.http.HttpStatus;
17 import org.springframework.http.MediaType;
18 import org.springframework.http.ResponseEntity;
19 import org.springframework.http.client.BufferingClientHttpRequestFactory;
20 import org.springframework.http.client.SimpleClientHttpRequestFactory;
21 import org.springframework.test.context.ActiveProfiles;
22 import org.springframework.test.context.junit4.SpringRunner;
23 import org.springframework.util.LinkedMultiValueMap;
24 import org.springframework.util.MultiValueMap;
25 import org.springframework.web.client.HttpClientErrorException;
26 import org.springframework.web.client.RestTemplate;
27
28 import dst.ass2.service.facade.ServiceFacadeApplication;
29 import dst.ass2.service.facade.ServiceFacadeApplicationConfig;
30
31 @RunWith(SpringRunner.class)
32 @SpringBootTest(classes = ServiceFacadeApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
33 @ActiveProfiles("AuthenticationResourceTest")
34 public class AuthenticationResourceTest {
35
36     @LocalServerPort
37     private int port;
38
39     private RestTemplate restTemplate;
40     private HttpHeaders headers;
41
42     @Before
43     public void setUp() {
44         headers = new HttpHeaders();
45         restTemplate = new RestTemplate();
46
47         SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
48         BufferingClientHttpRequestFactory bufferingClientHttpRequestFactory = new BufferingClientHttpRequestFactory(requestFactory);
49         requestFactory.setOutputStreaming(false);
50         restTemplate.setRequestFactory(bufferingClientHttpRequestFactory);
51     }
52
53     @Test
54     public void authenticate_withValidUser_returnsOkAndToken() throws Exception {
55         headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
56         String url = url("/auth/authenticate");
57         MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
58         body.add("email", "junit@example.com");
59         body.add("password", "junit");
60
61         HttpEntity<?> request = new HttpEntity<>(body, headers);
62         ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
63
64         assertThat(response.getStatusCode().series(), is(HttpStatus.Series.SUCCESSFUL));
65         assertNotNull(response.getBody());
66         assertThat(response.getBody(), is(ServiceFacadeApplicationConfig.MockAuthenticationClient.TOKEN));
67     }
68
69     @Test
70     public void authenticate_withInvalidUser_returnsAppropriateCode() throws Exception {
71         headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
72         String url = url("/auth/authenticate");
73         MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
74         body.add("email", "nonexisting@example.com");
75         body.add("password", "wrong");
76
77         HttpEntity<?> request = new HttpEntity<>(body, headers);
78         HttpStatus status;
79
80         try {
81             status = restTemplate.postForEntity(url, request, String.class).getStatusCode();
82         } catch (HttpClientErrorException e) {
83             status = e.getStatusCode();
84         }
85
86         assertThat("Return an appropriate error code", status, allOf(
87                 not(HttpStatus.OK),
88                 not(HttpStatus.NOT_FOUND),
89                 not(HttpStatus.INTERNAL_SERVER_ERROR)
90         ));
91     }
92
93     private String url(String uri) {
94         return "http://localhost:" + port + uri;
95     }
96
97 }