mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 00:45:44 +01:00
Add unit tests
This commit is contained in:
@@ -23,10 +23,18 @@ import sonia.scm.security.DefaultAccessTokenCookieIssuer;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
|
||||
import static java.net.URI.create;
|
||||
import static java.util.Optional.empty;
|
||||
import static java.util.Optional.of;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -49,6 +57,8 @@ public class AuthenticationResourceTest {
|
||||
|
||||
private AccessTokenCookieIssuer cookieIssuer = new DefaultAccessTokenCookieIssuer(mock(ScmConfiguration.class));
|
||||
|
||||
private MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
private static final String AUTH_JSON_TRILLIAN = "{\n" +
|
||||
"\t\"cookie\": true,\n" +
|
||||
"\t\"grant_type\": \"password\",\n" +
|
||||
@@ -123,7 +133,6 @@ public class AuthenticationResourceTest {
|
||||
public void shouldAuthCorrectly() throws URISyntaxException {
|
||||
|
||||
MockHttpRequest request = getMockHttpRequest(AUTH_JSON_TRILLIAN);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
@@ -134,7 +143,6 @@ public class AuthenticationResourceTest {
|
||||
public void shouldAuthCorrectlyWithFormencodedData() throws URISyntaxException {
|
||||
|
||||
MockHttpRequest request = getMockHttpRequestUrlEncoded(AUTH_FORMENCODED_TRILLIAN);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
@@ -146,7 +154,6 @@ public class AuthenticationResourceTest {
|
||||
public void shouldNotAuthUserWithWrongPassword() throws URISyntaxException {
|
||||
|
||||
MockHttpRequest request = getMockHttpRequest(AUTH_JSON_TRILLIAN_WRONG_PW);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
@@ -156,7 +163,6 @@ public class AuthenticationResourceTest {
|
||||
@Test
|
||||
public void shouldNotAuthNonexistingUser() throws URISyntaxException {
|
||||
MockHttpRequest request = getMockHttpRequest(AUTH_JSON_NOT_EXISTING_USER);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
@@ -187,16 +193,43 @@ public class AuthenticationResourceTest {
|
||||
@SubjectAware(username = "trillian", password = "secret")
|
||||
public void shouldSuccessfullyLogoutUser() throws URISyntaxException {
|
||||
MockHttpRequest request = MockHttpRequest.delete("/" + AuthenticationResource.PATH + "/access_token");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleLogoutRedirection() throws URISyntaxException, UnsupportedEncodingException {
|
||||
mockResourceWithLogoutRedirection(of(create("http://example.com/cas/logout")));
|
||||
|
||||
MockHttpRequest request = MockHttpRequest.delete("/" + AuthenticationResource.PATH + "/access_token");
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||
assertThat(response.getContentAsString(), containsString("http://example.com/cas/logout"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleDisabledLogoutRedirection() throws URISyntaxException {
|
||||
mockResourceWithLogoutRedirection(empty());
|
||||
|
||||
MockHttpRequest request = MockHttpRequest.delete("/" + AuthenticationResource.PATH + "/access_token");
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus());
|
||||
}
|
||||
|
||||
private void mockResourceWithLogoutRedirection(Optional<URI> target) {
|
||||
dispatcher.getRegistry().removeRegistrations(AuthenticationResource.class);
|
||||
AuthenticationResource authenticationResource =
|
||||
new AuthenticationResource(accessTokenBuilderFactory, cookieIssuer, () -> target);
|
||||
dispatcher.getRegistry().addSingletonResource(authenticationResource);
|
||||
}
|
||||
|
||||
private void shouldReturnBadRequest(String requestBody) throws URISyntaxException {
|
||||
MockHttpRequest request = getMockHttpRequest(requestBody);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
@@ -218,5 +251,4 @@ public class AuthenticationResourceTest {
|
||||
request.contentType(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
|
||||
return request;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user