Add ui plugin and self links

This commit is contained in:
René Pfeuffer
2018-10-01 12:59:45 +02:00
parent aed4c60296
commit 2c5fdd6b41
5 changed files with 61 additions and 10 deletions

View File

@@ -1,6 +1,5 @@
package sonia.scm.api.v2.resources; package sonia.scm.api.v2.resources;
import de.otto.edison.hal.Link;
import de.otto.edison.hal.Links; import de.otto.edison.hal.Links;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.SecurityUtils;
import sonia.scm.config.ConfigurationPermissions; import sonia.scm.config.ConfigurationPermissions;
@@ -9,6 +8,8 @@ import sonia.scm.user.UserPermissions;
import javax.inject.Inject; import javax.inject.Inject;
import static de.otto.edison.hal.Link.link;
public class IndexDtoGenerator { public class IndexDtoGenerator {
private final ResourceLinks resourceLinks; private final ResourceLinks resourceLinks;
@@ -20,25 +21,27 @@ public class IndexDtoGenerator {
public IndexDto generate() { public IndexDto generate() {
Links.Builder builder = Links.linkingTo(); Links.Builder builder = Links.linkingTo();
builder.self(resourceLinks.index().self());
builder.single(link("uiPlugins", resourceLinks.uiPluginCollection().self()));
if (SecurityUtils.getSubject().isAuthenticated()) { if (SecurityUtils.getSubject().isAuthenticated()) {
builder.single( builder.single(
Link.link("me", resourceLinks.me().self()), link("me", resourceLinks.me().self()),
Link.link("logout", resourceLinks.authentication().logout()) link("logout", resourceLinks.authentication().logout())
); );
if (UserPermissions.list().isPermitted()) { if (UserPermissions.list().isPermitted()) {
builder.single(Link.link("users", resourceLinks.userCollection().self())); builder.single(link("users", resourceLinks.userCollection().self()));
} }
if (GroupPermissions.list().isPermitted()) { if (GroupPermissions.list().isPermitted()) {
builder.single(Link.link("groups", resourceLinks.groupCollection().self())); builder.single(link("groups", resourceLinks.groupCollection().self()));
} }
if (ConfigurationPermissions.list().isPermitted()) { if (ConfigurationPermissions.list().isPermitted()) {
builder.single(Link.link("config", resourceLinks.config().self())); builder.single(link("config", resourceLinks.config().self()));
} }
builder.single(Link.link("repositories", resourceLinks.repositoryCollection().self())); builder.single(link("repositories", resourceLinks.repositoryCollection().self()));
} else { } else {
builder.single( builder.single(
Link.link("formLogin", resourceLinks.authentication().formLogin()), link("formLogin", resourceLinks.authentication().formLogin()),
Link.link("jsonLogin", resourceLinks.authentication().jsonLogin()) link("jsonLogin", resourceLinks.authentication().jsonLogin())
); );
} }

View File

@@ -20,6 +20,7 @@ public class IndexResource {
} }
@GET @GET
@Path("")
@Produces(VndMediaType.INDEX) @Produces(VndMediaType.INDEX)
@TypeHint(IndexDto.class) @TypeHint(IndexDto.class)
public IndexDto getIndex() { public IndexDto getIndex() {

View File

@@ -440,7 +440,6 @@ class ResourceLinks {
} }
} }
public UIPluginLinks uiPlugin() { public UIPluginLinks uiPlugin() {
return new UIPluginLinks(scmPathInfoStore.get()); return new UIPluginLinks(scmPathInfoStore.get());
} }
@@ -496,4 +495,21 @@ class ResourceLinks {
return loginLinkBuilder.method("logout").parameters().href(); return loginLinkBuilder.method("logout").parameters().href();
} }
} }
public IndexLinks index() {
return new IndexLinks(scmPathInfoStore.get());
}
static class IndexLinks {
private final LinkBuilder indexLinkBuilder;
IndexLinks(ScmPathInfo pathInfo) {
indexLinkBuilder = new LinkBuilder(pathInfo, IndexResource.class);
}
String self() {
return indexLinkBuilder.method("getIndex").parameters().href();
}
}
} }

View File

@@ -26,6 +26,36 @@ public class IndexResourceTest {
Assertions.assertThat(index.getLinks().getLinkBy("jsonLogin")).matches(Optional::isPresent); Assertions.assertThat(index.getLinks().getLinkBy("jsonLogin")).matches(Optional::isPresent);
} }
@Test
public void shouldRenderSelfLinkForUnauthenticatedRequest() {
IndexDto index = indexResource.getIndex();
Assertions.assertThat(index.getLinks().getLinkBy("self")).matches(Optional::isPresent);
}
@Test
public void shouldRenderUiPluginsLinkForUnauthenticatedRequest() {
IndexDto index = indexResource.getIndex();
Assertions.assertThat(index.getLinks().getLinkBy("uiPlugins")).matches(Optional::isPresent);
}
@Test
@SubjectAware(username = "trillian", password = "secret")
public void shouldRenderSelfLinkForAuthenticatedRequest() {
IndexDto index = indexResource.getIndex();
Assertions.assertThat(index.getLinks().getLinkBy("self")).matches(Optional::isPresent);
}
@Test
@SubjectAware(username = "trillian", password = "secret")
public void shouldRenderUiPluginsLinkForAuthenticatedRequest() {
IndexDto index = indexResource.getIndex();
Assertions.assertThat(index.getLinks().getLinkBy("uiPlugins")).matches(Optional::isPresent);
}
@Test @Test
@SubjectAware(username = "trillian", password = "secret") @SubjectAware(username = "trillian", password = "secret")
public void shouldRenderMeUrlForAuthenticatedRequest() { public void shouldRenderMeUrlForAuthenticatedRequest() {

View File

@@ -35,6 +35,7 @@ public class ResourceLinksMock {
when(resourceLinks.uiPluginCollection()).thenReturn(new ResourceLinks.UIPluginCollectionLinks(uriInfo)); when(resourceLinks.uiPluginCollection()).thenReturn(new ResourceLinks.UIPluginCollectionLinks(uriInfo));
when(resourceLinks.uiPlugin()).thenReturn(new ResourceLinks.UIPluginLinks(uriInfo)); when(resourceLinks.uiPlugin()).thenReturn(new ResourceLinks.UIPluginLinks(uriInfo));
when(resourceLinks.authentication()).thenReturn(new ResourceLinks.AuthenticationLinks(uriInfo)); when(resourceLinks.authentication()).thenReturn(new ResourceLinks.AuthenticationLinks(uriInfo));
when(resourceLinks.index()).thenReturn(new ResourceLinks.IndexLinks(uriInfo));
return resourceLinks; return resourceLinks;
} }