1 package dst.ass2.service.facade.test;
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;
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;
28 import dst.ass2.service.facade.ServiceFacadeApplication;
29 import dst.ass2.service.facade.ServiceFacadeApplicationConfig;
31 @RunWith(SpringRunner.class)
32 @SpringBootTest(classes = ServiceFacadeApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
33 @ActiveProfiles("AuthenticationResourceTest")
34 public class AuthenticationResourceTest {
39 private RestTemplate restTemplate;
40 private HttpHeaders headers;
44 headers = new HttpHeaders();
45 restTemplate = new RestTemplate();
47 SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
48 BufferingClientHttpRequestFactory bufferingClientHttpRequestFactory = new BufferingClientHttpRequestFactory(requestFactory);
49 requestFactory.setOutputStreaming(false);
50 restTemplate.setRequestFactory(bufferingClientHttpRequestFactory);
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");
61 HttpEntity<?> request = new HttpEntity<>(body, headers);
62 ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
64 assertThat(response.getStatusCode().series(), is(HttpStatus.Series.SUCCESSFUL));
65 assertNotNull(response.getBody());
66 assertThat(response.getBody(), is(ServiceFacadeApplicationConfig.MockAuthenticationClient.TOKEN));
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");
77 HttpEntity<?> request = new HttpEntity<>(body, headers);
81 status = restTemplate.postForEntity(url, request, String.class).getStatusCode();
82 } catch (HttpClientErrorException e) {
83 status = e.getStatusCode();
86 assertThat("Return an appropriate error code", status, allOf(
88 not(HttpStatus.NOT_FOUND),
89 not(HttpStatus.INTERNAL_SERVER_ERROR)
93 private String url(String uri) {
94 return "http://localhost:" + port + uri;