fix and create unit tests for anonymous user

This commit is contained in:
Eduard Heimbuch
2019-10-14 11:12:29 +02:00
parent adfaf6166c
commit ce2ea950a8
4 changed files with 20 additions and 17 deletions

View File

@@ -11,7 +11,6 @@ import reducer, {
getLoginFailure,
getLogoutFailure,
getMe,
isAuthenticated,
isFetchMePending,
isLoginPending,
isLogoutPending,
@@ -35,10 +34,7 @@ import reducer, {
import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk";
import fetchMock from "fetch-mock";
import {
FETCH_INDEXRESOURCES_PENDING,
FETCH_INDEXRESOURCES_SUCCESS
} from "./indexResource";
import {FETCH_INDEXRESOURCES_PENDING, FETCH_INDEXRESOURCES_SUCCESS} from "./indexResource";
const me = {
name: "tricia",
@@ -284,16 +280,6 @@ describe("auth actions", () => {
describe("auth selectors", () => {
const error = new Error("yo it failed");
it("should be false, if authenticated is undefined or false", () => {
expect(isAuthenticated({})).toBe(false);
expect(isAuthenticated({ auth: {} })).toBe(false);
expect(isAuthenticated({ auth: { authenticated: false } })).toBe(false);
});
it("should be true, if authenticated is true", () => {
expect(isAuthenticated({ auth: { authenticated: true } })).toBe(true);
});
it("should return me", () => {
expect(getMe({ auth: { me } })).toBe(me);
});

View File

@@ -137,7 +137,6 @@ public class ConfigResourceTest {
assertTrue(response.getContentAsString().contains("\"proxyPassword\":\"newPassword\""));
assertTrue(response.getContentAsString().contains("\"self\":{\"href\":\"/v2/config"));
assertTrue("link not found", response.getContentAsString().contains("\"update\":{\"href\":\"/v2/config"));
verify(userManager).create(SCMContext.ANONYMOUS);
}
@Test

View File

@@ -2,6 +2,7 @@ package sonia.scm.api.v2.resources;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.sdorra.shiro.SubjectAware;
import com.google.common.collect.ImmutableList;
import com.google.inject.util.Providers;
import de.otto.edison.hal.HalRepresentation;
@@ -169,7 +170,9 @@ public class RepositoryPermissionRootResourceTest extends RepositoryTestBase {
@TestFactory
@DisplayName("test endpoints on missing permissions and user is not Admin")
@SubjectAware(username = "trillian")
Stream<DynamicTest> missedPermissionUserForbiddenTestFactory() {
when(subject.getPrincipal()).thenReturn("user");
doThrow(AuthorizationException.class).when(repositoryManager).get(any(NamespaceAndName.class));
return createDynamicTestsToAssertResponses(
requestGETPermission.expectedResponseStatus(403),
@@ -179,6 +182,20 @@ public class RepositoryPermissionRootResourceTest extends RepositoryTestBase {
requestPUTPermission.expectedResponseStatus(403));
}
@TestFactory
@DisplayName("test endpoints on missing permissions and user is not Admin")
@SubjectAware(username = "trillian")
Stream<DynamicTest> missedPermissionAnonymousUnauthorizedTestFactory() {
when(subject.getPrincipal()).thenReturn("_anonymous");
doThrow(AuthorizationException.class).when(repositoryManager).get(any(NamespaceAndName.class));
return createDynamicTestsToAssertResponses(
requestGETPermission.expectedResponseStatus(401),
requestPOSTPermission.expectedResponseStatus(401),
requestGETAllPermissions.expectedResponseStatus(401),
requestDELETEPermission.expectedResponseStatus(401),
requestPUTPermission.expectedResponseStatus(401));
}
@Test
public void userWithPermissionWritePermissionShouldGetAllPermissionsWithCreateAndUpdateLinks() throws URISyntaxException {
createUserWithRepositoryAndPermissions(TEST_PERMISSIONS, PERMISSION_WRITE);

View File

@@ -40,6 +40,7 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.MDC;
import sonia.scm.AbstractTestBase;
import sonia.scm.SCMContext;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
@@ -117,7 +118,7 @@ public class MDCFilterTest extends AbstractTestBase {
filter.doFilter(request, response, chain);
assertNotNull(chain.ctx);
assertEquals("anonymous", chain.ctx.get(MDCFilter.MDC_USERNAME));
assertEquals(SCMContext.USER_ANONYMOUS, chain.ctx.get(MDCFilter.MDC_USERNAME));
}
private static class MDCCapturingFilterChain implements FilterChain {