mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +01:00
Add unit tests
This commit is contained in:
@@ -6,6 +6,7 @@ import java.net.URI;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@ExtensionPoint(multi = false)
|
@ExtensionPoint(multi = false)
|
||||||
|
@FunctionalInterface
|
||||||
public interface LogoutRedirection {
|
public interface LogoutRedirection {
|
||||||
Optional<URI> afterLogoutRedirectTo();
|
Optional<URI> afterLogoutRedirectTo();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -204,10 +204,9 @@ export const logout = (link: string, history: History) => {
|
|||||||
})
|
})
|
||||||
.then(json => {
|
.then(json => {
|
||||||
if (json && json.logoutRedirect) {
|
if (json && json.logoutRedirect) {
|
||||||
location.href = json.logoutRedirect;
|
window.location.assign(json.logoutRedirect);
|
||||||
} else {
|
|
||||||
dispatch(logoutSuccess());
|
|
||||||
}
|
}
|
||||||
|
dispatch(logoutSuccess());
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
dispatch(fetchIndexResources());
|
dispatch(fetchIndexResources());
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ import reducer, {
|
|||||||
FETCH_ME,
|
FETCH_ME,
|
||||||
LOGOUT,
|
LOGOUT,
|
||||||
getLoginFailure,
|
getLoginFailure,
|
||||||
getLogoutFailure
|
getLogoutFailure,
|
||||||
} from "./auth";
|
} from "./auth";
|
||||||
|
|
||||||
import configureMockStore from "redux-mock-store";
|
import configureMockStore from "redux-mock-store";
|
||||||
@@ -224,6 +224,44 @@ describe("auth actions", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should dispatch logout success and redirect", () => {
|
||||||
|
fetchMock.deleteOnce("/api/v2/auth/access_token", {
|
||||||
|
status: 200,
|
||||||
|
body: { logoutRedirect: "http://example.com/cas/logout" }
|
||||||
|
});
|
||||||
|
|
||||||
|
fetchMock.getOnce("/api/v2/me", {
|
||||||
|
status: 401
|
||||||
|
});
|
||||||
|
|
||||||
|
fetchMock.getOnce("/api/v2/", {
|
||||||
|
_links: {
|
||||||
|
login: {
|
||||||
|
login: "/login"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
window.location.assign = jest.fn();
|
||||||
|
|
||||||
|
const expectedActions = [
|
||||||
|
{ type: LOGOUT_PENDING },
|
||||||
|
{ type: LOGOUT_SUCCESS },
|
||||||
|
{ type: FETCH_INDEXRESOURCES_PENDING }
|
||||||
|
];
|
||||||
|
|
||||||
|
const store = mockStore({});
|
||||||
|
|
||||||
|
return store.dispatch(logout("/auth/access_token")).then(() => {
|
||||||
|
expect(window.location.assign.mock.calls[0][0]).toBe(
|
||||||
|
"http://example.com/cas/logout"
|
||||||
|
);
|
||||||
|
expect(store.getActions()).toEqual(expectedActions);
|
||||||
|
|
||||||
|
// expect(window.location.href).toEqual("http://example.com/cas/logout");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("should dispatch logout failure", () => {
|
it("should dispatch logout failure", () => {
|
||||||
fetchMock.deleteOnce("/api/v2/auth/access_token", {
|
fetchMock.deleteOnce("/api/v2/auth/access_token", {
|
||||||
status: 500
|
status: 500
|
||||||
|
|||||||
@@ -23,10 +23,18 @@ import sonia.scm.security.DefaultAccessTokenCookieIssuer;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.util.Date;
|
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.assertEquals;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@@ -49,6 +57,8 @@ public class AuthenticationResourceTest {
|
|||||||
|
|
||||||
private AccessTokenCookieIssuer cookieIssuer = new DefaultAccessTokenCookieIssuer(mock(ScmConfiguration.class));
|
private AccessTokenCookieIssuer cookieIssuer = new DefaultAccessTokenCookieIssuer(mock(ScmConfiguration.class));
|
||||||
|
|
||||||
|
private MockHttpResponse response = new MockHttpResponse();
|
||||||
|
|
||||||
private static final String AUTH_JSON_TRILLIAN = "{\n" +
|
private static final String AUTH_JSON_TRILLIAN = "{\n" +
|
||||||
"\t\"cookie\": true,\n" +
|
"\t\"cookie\": true,\n" +
|
||||||
"\t\"grant_type\": \"password\",\n" +
|
"\t\"grant_type\": \"password\",\n" +
|
||||||
@@ -123,7 +133,6 @@ public class AuthenticationResourceTest {
|
|||||||
public void shouldAuthCorrectly() throws URISyntaxException {
|
public void shouldAuthCorrectly() throws URISyntaxException {
|
||||||
|
|
||||||
MockHttpRequest request = getMockHttpRequest(AUTH_JSON_TRILLIAN);
|
MockHttpRequest request = getMockHttpRequest(AUTH_JSON_TRILLIAN);
|
||||||
MockHttpResponse response = new MockHttpResponse();
|
|
||||||
|
|
||||||
dispatcher.invoke(request, response);
|
dispatcher.invoke(request, response);
|
||||||
|
|
||||||
@@ -134,7 +143,6 @@ public class AuthenticationResourceTest {
|
|||||||
public void shouldAuthCorrectlyWithFormencodedData() throws URISyntaxException {
|
public void shouldAuthCorrectlyWithFormencodedData() throws URISyntaxException {
|
||||||
|
|
||||||
MockHttpRequest request = getMockHttpRequestUrlEncoded(AUTH_FORMENCODED_TRILLIAN);
|
MockHttpRequest request = getMockHttpRequestUrlEncoded(AUTH_FORMENCODED_TRILLIAN);
|
||||||
MockHttpResponse response = new MockHttpResponse();
|
|
||||||
|
|
||||||
dispatcher.invoke(request, response);
|
dispatcher.invoke(request, response);
|
||||||
|
|
||||||
@@ -146,7 +154,6 @@ public class AuthenticationResourceTest {
|
|||||||
public void shouldNotAuthUserWithWrongPassword() throws URISyntaxException {
|
public void shouldNotAuthUserWithWrongPassword() throws URISyntaxException {
|
||||||
|
|
||||||
MockHttpRequest request = getMockHttpRequest(AUTH_JSON_TRILLIAN_WRONG_PW);
|
MockHttpRequest request = getMockHttpRequest(AUTH_JSON_TRILLIAN_WRONG_PW);
|
||||||
MockHttpResponse response = new MockHttpResponse();
|
|
||||||
|
|
||||||
dispatcher.invoke(request, response);
|
dispatcher.invoke(request, response);
|
||||||
|
|
||||||
@@ -156,7 +163,6 @@ public class AuthenticationResourceTest {
|
|||||||
@Test
|
@Test
|
||||||
public void shouldNotAuthNonexistingUser() throws URISyntaxException {
|
public void shouldNotAuthNonexistingUser() throws URISyntaxException {
|
||||||
MockHttpRequest request = getMockHttpRequest(AUTH_JSON_NOT_EXISTING_USER);
|
MockHttpRequest request = getMockHttpRequest(AUTH_JSON_NOT_EXISTING_USER);
|
||||||
MockHttpResponse response = new MockHttpResponse();
|
|
||||||
|
|
||||||
dispatcher.invoke(request, response);
|
dispatcher.invoke(request, response);
|
||||||
|
|
||||||
@@ -187,16 +193,43 @@ public class AuthenticationResourceTest {
|
|||||||
@SubjectAware(username = "trillian", password = "secret")
|
@SubjectAware(username = "trillian", password = "secret")
|
||||||
public void shouldSuccessfullyLogoutUser() throws URISyntaxException {
|
public void shouldSuccessfullyLogoutUser() throws URISyntaxException {
|
||||||
MockHttpRequest request = MockHttpRequest.delete("/" + AuthenticationResource.PATH + "/access_token");
|
MockHttpRequest request = MockHttpRequest.delete("/" + AuthenticationResource.PATH + "/access_token");
|
||||||
MockHttpResponse response = new MockHttpResponse();
|
|
||||||
|
|
||||||
dispatcher.invoke(request, response);
|
dispatcher.invoke(request, response);
|
||||||
assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus());
|
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 {
|
private void shouldReturnBadRequest(String requestBody) throws URISyntaxException {
|
||||||
MockHttpRequest request = getMockHttpRequest(requestBody);
|
MockHttpRequest request = getMockHttpRequest(requestBody);
|
||||||
MockHttpResponse response = new MockHttpResponse();
|
|
||||||
|
|
||||||
dispatcher.invoke(request, response);
|
dispatcher.invoke(request, response);
|
||||||
|
|
||||||
@@ -218,5 +251,4 @@ public class AuthenticationResourceTest {
|
|||||||
request.contentType(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
|
request.contentType(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user