import org.sonar.api.web.WidgetProperty;
import org.sonar.api.web.WidgetPropertyType;
import org.sonar.api.web.WidgetScope;
+import org.sonar.server.user.MockUserSession;
import java.util.List;
import static com.google.common.collect.Iterables.getOnlyElement;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.lessThan;
-import static org.hamcrest.Matchers.greaterThan;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertThat;
+import static org.sonar.server.component.ComponentTesting.newProjectDto;
public class ViewProxyTest {
@Test
public void compareTo() {
- assertThat(new ViewProxy<FakeView>(new FakeView("aaa")).compareTo(new ViewProxy<FakeView>(new FakeView("bbb"))), lessThan(0));
- assertThat(new ViewProxy<FakeView>(new FakeView("aaa")).compareTo(new ViewProxy<FakeView>(new FakeView("aaa"))), is(0));
- assertThat(new ViewProxy<FakeView>(new FakeView("bbb")).compareTo(new ViewProxy<FakeView>(new FakeView("aaa"))), greaterThan(0));
+ assertThat(new ViewProxy<FakeView>(new FakeView("aaa")).compareTo(new ViewProxy<FakeView>(new FakeView("bbb")))).isLessThan(0);
+ assertThat(new ViewProxy<FakeView>(new FakeView("aaa")).compareTo(new ViewProxy<FakeView>(new FakeView("aaa")))).isZero();
+ assertThat(new ViewProxy<FakeView>(new FakeView("bbb")).compareTo(new ViewProxy<FakeView>(new FakeView("aaa")))).isGreaterThan(0);
}
@Test
View view = new MyView();
ViewProxy proxy = new ViewProxy<View>(view);
- assertThat(proxy.getTarget(), is(view));
- assertArrayEquals(proxy.getSections(), new String[]{NavigationSection.RESOURCE});
- assertArrayEquals(proxy.getUserRoles(), new String[]{UserRole.ADMIN});
+ assertThat(proxy.getTarget()).isEqualTo(view);
+ assertThat(proxy.getSections()).isEqualTo(new String[] {NavigationSection.RESOURCE});
+ assertThat(proxy.getUserRoles()).isEqualTo(new String[] {UserRole.ADMIN});
}
@Test
View view = new MyView();
ViewProxy proxy = new ViewProxy<View>(view);
- assertThat(proxy.getTarget(), is(view));
- assertArrayEquals(proxy.getSections(), new String[]{NavigationSection.HOME});
- assertThat(proxy.getUserRoles().length, org.hamcrest.Matchers.is(0));
+ assertThat(proxy.getTarget()).isEqualTo(view);
+ assertThat(proxy.getSections()).isEqualTo(new String[] {NavigationSection.HOME});
+ assertThat(proxy.getUserRoles()).isEmpty();
}
@Test
ViewProxy proxy = new ViewProxy<MyView>(new MyView());
- assertThat(proxy.isDefaultTab(), is(true));
- assertThat(proxy.getDefaultTabForMetrics().length, is(0));
+ assertThat(proxy.isDefaultTab()).isTrue();
+ assertThat(proxy.getDefaultTabForMetrics()).isEmpty();
}
@Test
}
ViewProxy proxy = new ViewProxy<MyView>(new MyView());
- assertThat(proxy.isDefaultTab(), is(false));
- assertThat(proxy.getDefaultTabForMetrics().length, is(0));
+ assertThat(proxy.isDefaultTab()).isFalse();
+ assertThat(proxy.getDefaultTabForMetrics()).isEmpty();
}
@Test
}
ViewProxy proxy = new ViewProxy<MyView>(new MyView());
- assertThat(proxy.isDefaultTab(), is(false));
- assertThat(proxy.getDefaultTabForMetrics(), is(new String[]{"ncloc", "coverage"}));
+ assertThat(proxy.isDefaultTab()).isFalse();
+ assertThat(proxy.getDefaultTabForMetrics()).isEqualTo(new String[] {"ncloc", "coverage"});
}
@Test
@Test
public void widgetShouldRequireMandatoryProperties() {
ViewProxy<Widget> proxy = new ViewProxy<Widget>(new EditableWidget());
- assertThat(proxy.hasRequiredProperties(), is(true));
+ assertThat(proxy.hasRequiredProperties()).isTrue();
}
@Test
public void widgetShouldDefineOnlyOptionalProperties() {
ViewProxy<Widget> proxy = new ViewProxy<Widget>(new WidgetWithOptionalProperties());
- assertThat(proxy.hasRequiredProperties(), is(false));
+ assertThat(proxy.hasRequiredProperties()).isFalse();
}
@Test
}
ViewProxy proxy = new ViewProxy<MyView>(new MyView());
- assertThat(proxy.acceptsAvailableMeasures(new String[]{"lines", "ncloc", "coverage"}), is(true));
+ assertThat(proxy.acceptsAvailableMeasures(new String[] {"lines", "ncloc", "coverage"})).isTrue();
}
@Test
}
ViewProxy proxy = new ViewProxy<MyView>(new MyView());
- assertThat(proxy.acceptsAvailableMeasures(new String[]{"lines", "ncloc", "coverage"}), is(true));
- assertThat(proxy.acceptsAvailableMeasures(new String[]{"lines", "coverage"}), is(false));
+ assertThat(proxy.acceptsAvailableMeasures(new String[] {"lines", "ncloc", "coverage"})).isTrue();
+ assertThat(proxy.acceptsAvailableMeasures(new String[] {"lines", "coverage"})).isFalse();
}
@Test
}
ViewProxy proxy = new ViewProxy<MyView>(new MyView());
- assertThat(proxy.acceptsAvailableMeasures(new String[]{"lines", "coverage"}), is(true));
- assertThat(proxy.acceptsAvailableMeasures(new String[]{"complexity", "coverage"}), is(false));
+ assertThat(proxy.acceptsAvailableMeasures(new String[] {"lines", "coverage"})).isTrue();
+ assertThat(proxy.acceptsAvailableMeasures(new String[] {"complexity", "coverage"})).isFalse();
}
@Test
ViewProxy proxy = new ViewProxy<MyView>(new MyView());
// ok, mandatory measures and 1 needed measure
- assertThat(proxy.acceptsAvailableMeasures(new String[]{"lines", "ncloc", "coverage", "duplications"}), is(true));
+ assertThat(proxy.acceptsAvailableMeasures(new String[] {"lines", "ncloc", "coverage", "duplications"})).isTrue();
// ko, one of the needed measures but not all of the mandatory ones
- assertThat(proxy.acceptsAvailableMeasures(new String[]{"lines", "coverage", "duplications"}), is(false));
+ assertThat(proxy.acceptsAvailableMeasures(new String[] {"lines", "coverage", "duplications"})).isFalse();
// ko, mandatory measures but no one of the needed measures
- assertThat(proxy.acceptsAvailableMeasures(new String[]{"lines", "nloc", "coverage", "dsm"}), is(false));
+ assertThat(proxy.acceptsAvailableMeasures(new String[] {"lines", "nloc", "coverage", "dsm"})).isFalse();
}
+ @Test
+ public void is_authorized_by_default() {
+
+ @NavigationSection(NavigationSection.HOME)
+ class MyView extends FakeView {
+ MyView() {
+ super("fake");
+ }
+ }
+
+ ViewProxy proxy = new ViewProxy<View>(new MyView());
+
+ MockUserSession.set();
+ assertThat(proxy.isUserAuthorized()).isTrue();
+ }
+
+ @Test
+ public void is_authorized_on_any_permission() {
+
+ @NavigationSection(NavigationSection.HOME)
+ @UserRole({"polop", "palap"})
+ class MyView extends FakeView {
+ MyView() {
+ super("fake");
+ }
+ }
+
+ ViewProxy proxy = new ViewProxy<View>(new MyView());
+
+ MockUserSession.set().setGlobalPermissions("palap");
+ assertThat(proxy.isUserAuthorized()).isTrue();
+ }
+
+ @Test
+ public void is_not_authorized() {
+
+ @NavigationSection(NavigationSection.HOME)
+ @UserRole({"polop", "palap"})
+ class MyView extends FakeView {
+ MyView() {
+ super("fake");
+ }
+ }
+
+ ViewProxy proxy = new ViewProxy<View>(new MyView());
+
+ MockUserSession.set().setGlobalPermissions("pilip");
+ assertThat(proxy.isUserAuthorized()).isFalse();
+ }
+
+ @Test
+ public void is_authorized_by_default_on_component() {
+
+ @NavigationSection(NavigationSection.RESOURCE)
+ class MyView extends FakeView {
+ MyView() {
+ super("fake");
+ }
+ }
+
+ ViewProxy proxy = new ViewProxy<View>(new MyView());
+
+ MockUserSession.set();
+ assertThat(proxy.isUserAuthorized(newProjectDto("abcd"))).isTrue();
+ }
+
+ @Test
+ public void is_authorized_on_any_permission_on_component() {
+
+ @NavigationSection(NavigationSection.RESOURCE)
+ @UserRole({"polop", "palap"})
+ class MyView extends FakeView {
+ MyView() {
+ super("fake");
+ }
+ }
+
+ ViewProxy proxy = new ViewProxy<View>(new MyView());
+
+ MockUserSession.set().addProjectUuidPermissions("palap", "abcd");
+ assertThat(proxy.isUserAuthorized(newProjectDto("abcd"))).isTrue();
+ }
+
+ @Test
+ public void is_not_authorized_on_component() {
+
+ @NavigationSection(NavigationSection.RESOURCE)
+ @UserRole({"polop", "palap"})
+ class MyView extends FakeView {
+ MyView() {
+ super("fake");
+ }
+ }
+
+ ViewProxy proxy = new ViewProxy<View>(new MyView());
+
+ MockUserSession.set().addProjectUuidPermissions("pilip", "abcd");
+ assertThat(proxy.isUserAuthorized(newProjectDto("abcd"))).isFalse();
+ }
+
+ @Test
+ public void is_authorized_on_component_viewer_bypass() {
+
+ @NavigationSection(NavigationSection.RESOURCE)
+ @UserRole(UserRole.VIEWER)
+ class MyView extends FakeView {
+ MyView() {
+ super("fake");
+ }
+ }
+
+ ViewProxy proxy = new ViewProxy<View>(new MyView());
+
+ MockUserSession.set();
+ assertThat(proxy.isUserAuthorized(newProjectDto("abcd"))).isTrue();
+ }
+
+ @Test
+ public void is_authorized_on_component_user_bypass() {
+
+ @NavigationSection(NavigationSection.RESOURCE)
+ @UserRole(UserRole.USER)
+ class MyView extends FakeView {
+ MyView() {
+ super("fake");
+ }
+ }
+
+ ViewProxy proxy = new ViewProxy<View>(new MyView());
+
+ MockUserSession.set();
+ assertThat(proxy.isUserAuthorized(newProjectDto("abcd"))).isTrue();
+ }
}
class FakeView implements View {
@Test
public void with_extensions() throws Exception {
- final String language = "xoo";
ComponentDto project = dbClient.componentDao().insert(session, ComponentTesting.newProjectDto("abcd")
- .setKey("polop").setName("Polop").setLanguage(language));
+ .setKey("polop").setName("Polop").setLanguage("xoo"));
dbClient.snapshotDao().insert(session, new SnapshotDto()
.setLast(true).setQualifier(project.qualifier()).setResourceId(project.getId()).setRootProjectId(project.getId()).setScope(project.scope()));
session.commit();
MockUserSession.set().addProjectUuidPermissions(UserRole.USER, "abcd");
+ Views views = createViews();
+
+ wsTester = new WsTester(new NavigationWs(new ComponentNavigationAction(dbClient, activeDashboardDao,
+ views, i18n, resourceTypes)));
+
+ wsTester.newGetRequest("api/navigation", "component").setParam("componentKey", "polop").execute().assertJson(getClass(), "with_extensions.json");
+ }
+
+ @Test
+ public void admin_with_extensions() throws Exception {
+ ComponentDto project = dbClient.componentDao().insert(session, ComponentTesting.newProjectDto("abcd")
+ .setKey("polop").setName("Polop").setLanguage("xoo"));
+ dbClient.snapshotDao().insert(session, new SnapshotDto()
+ .setLast(true).setQualifier(project.qualifier()).setResourceId(project.getId()).setRootProjectId(project.getId()).setScope(project.scope()));
+ session.commit();
+
+ MockUserSession.set()
+ .addProjectUuidPermissions(UserRole.USER, "abcd")
+ .addProjectUuidPermissions(UserRole.ADMIN, "abcd");
+
+ Views views = createViews();
+
+ wsTester = new WsTester(new NavigationWs(new ComponentNavigationAction(dbClient, activeDashboardDao,
+ views, i18n, resourceTypes)));
+
+ wsTester.newGetRequest("api/navigation", "component").setParam("componentKey", "polop").execute().assertJson(getClass(), "admin_with_extensions.json");
+ }
+
+ private Views createViews() {
@NavigationSection(NavigationSection.RESOURCE)
@ResourceScope(Scopes.PROJECT)
@ResourceQualifier(Qualifiers.PROJECT)
- @ResourceLanguage(language)
+ @ResourceLanguage("xoo")
class FirstPage implements Page {
@Override
public String getTitle() {
@NavigationSection(NavigationSection.RESOURCE)
@ResourceScope(Scopes.PROJECT)
@ResourceQualifier(Qualifiers.PROJECT)
- @ResourceLanguage(language)
+ @ResourceLanguage("xoo")
class SecondPage implements Page {
@Override
public String getTitle() {
}
Page page2 = new SecondPage();
- wsTester = new WsTester(new NavigationWs(new ComponentNavigationAction(dbClient, activeDashboardDao,
- new Views(new Page[] {page1, page2}), i18n, resourceTypes)));
+ @NavigationSection(NavigationSection.RESOURCE)
+ @ResourceScope(Scopes.PROJECT)
+ @ResourceQualifier(Qualifiers.PROJECT)
+ @ResourceLanguage("xoo")
+ @UserRole(UserRole.ADMIN)
+ class AdminPage implements Page {
+ @Override
+ public String getTitle() {
+ return "Admin Page";
+ }
- wsTester.newGetRequest("api/navigation", "component").setParam("componentKey", "polop").execute().assertJson(getClass(), "with_extensions.json");
+ @Override
+ public String getId() {
+ return "/admin/page";
+ }
+ }
+ Page adminPage = new AdminPage();
+ Views views = new Views(new Page[] {page1, page2, adminPage});
+ return views;
}
@Test