diff --git a/CHANGELOG.md b/CHANGELOG.md index 68979b4..ab0e156 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,17 @@ Changelog of Pull Request Notifier for Bitbucket. ## Unreleased +### GitHub [#193](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/193) Enable project level admins to configure on project level + Enable project level admins to configure on project level + + [64f670125111045](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/64f670125111045) Tomas Bjerre *2017-02-05 09:00:55* + +### No issue + doc + + [e6717f40011194e](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/e6717f40011194e) Tomas Bjerre *2017-02-03 13:15:33* + +## 2.52 ### GitHub [#192](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/192) Include status for participants Variables to display names of approved, unapproved and needs work diff --git a/src/main/java/se/bjurr/prnfb/presentation/ButtonServlet.java b/src/main/java/se/bjurr/prnfb/presentation/ButtonServlet.java index 3105175..21a5702 100644 --- a/src/main/java/se/bjurr/prnfb/presentation/ButtonServlet.java +++ b/src/main/java/se/bjurr/prnfb/presentation/ButtonServlet.java @@ -4,6 +4,7 @@ import static javax.ws.rs.core.MediaType.APPLICATION_JSON; import static javax.ws.rs.core.Response.ok; import static javax.ws.rs.core.Response.status; +import static javax.ws.rs.core.Response.Status.NOT_FOUND; import static javax.ws.rs.core.Response.Status.OK; import static javax.ws.rs.core.Response.Status.UNAUTHORIZED; import static se.bjurr.prnfb.transformer.ButtonTransformer.toButtonDto; @@ -27,6 +28,8 @@ import javax.ws.rs.core.Response; import com.atlassian.annotations.security.XsrfProtectionExcluded; +import com.google.common.base.Optional; +import com.google.common.collect.Iterables; import se.bjurr.prnfb.http.NotificationResponse; import se.bjurr.prnfb.presentation.dto.ButtonDTO; @@ -60,9 +63,7 @@ public ButtonServlet( @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) public Response create(ButtonDTO buttonDto) { - if (!userCheckService.isAdminAllowed( // - buttonDto.getProjectKey().orNull(), // - buttonDto.getRepositorySlug().orNull())) { + if (!userCheckService.isAdminAllowed(buttonDto)) { return status(UNAUTHORIZED) // .build(); } @@ -82,10 +83,7 @@ public Response create(ButtonDTO buttonDto) { @Produces(APPLICATION_JSON) public Response delete(@PathParam("uuid") UUID prnfbButtonUuid) { PrnfbButton prnfbButton = settingsService.getButton(prnfbButtonUuid); - if (!userCheckService.isAdminAllowed( // - prnfbButton.getProjectKey().orNull() // - , - prnfbButton.getRepositorySlug().orNull())) { + if (!userCheckService.isAdminAllowed(prnfbButton)) { return status(UNAUTHORIZED) // .build(); } @@ -97,40 +95,18 @@ public Response delete(@PathParam("uuid") UUID prnfbButtonUuid) { @Produces(APPLICATION_JSON) public Response get() { List buttons = settingsService.getButtons(); - Iterable allowedButtons = userCheckService.filterAllowed(buttons); + Iterable allowedButtons = userCheckService.filterAdminAllowed(buttons); List dtos = toButtonDtoList(allowedButtons); Collections.sort(dtos); return ok(dtos, APPLICATION_JSON).build(); } - @GET - @Path("/repository/{repositoryId}/pullrequest/{pullRequestId}") - @Produces(APPLICATION_JSON) - public Response get( - @PathParam("repositoryId") Integer repositoryId, - @PathParam("pullRequestId") Long pullRequestId) { - if (!userCheckService.isViewAllowed()) { - return status(UNAUTHORIZED).build(); - } - List buttons = buttonsService.getButtons(repositoryId, pullRequestId); - Iterable allowedButtons = userCheckService.filterAllowed(buttons); - List dtos = toButtonDtoList(allowedButtons); - Collections.sort(dtos); - - populateButtonFormDtoList(repositoryId, pullRequestId, dtos); - - return ok(dtos, APPLICATION_JSON).build(); - } - @GET @Path("/projectKey/{projectKey}") @Produces(APPLICATION_JSON) public Response get(@PathParam("projectKey") String projectKey) { - if (!userCheckService.isViewAllowed()) { - return status(UNAUTHORIZED).build(); - } List buttons = settingsService.getButtons(projectKey); - Iterable allowedButtons = userCheckService.filterAllowed(buttons); + Iterable allowedButtons = userCheckService.filterAdminAllowed(buttons); List dtos = toButtonDtoList(allowedButtons); Collections.sort(dtos); return ok(dtos, APPLICATION_JSON).build(); @@ -142,11 +118,8 @@ public Response get(@PathParam("projectKey") String projectKey) { public Response get( @PathParam("projectKey") String projectKey, @PathParam("repositorySlug") String repositorySlug) { - if (!userCheckService.isViewAllowed()) { - return status(UNAUTHORIZED).build(); - } List buttons = settingsService.getButtons(projectKey, repositorySlug); - Iterable allowedButtons = userCheckService.filterAllowed(buttons); + Iterable allowedButtons = userCheckService.filterAdminAllowed(buttons); List dtos = toButtonDtoList(allowedButtons); Collections.sort(dtos); return ok(dtos, APPLICATION_JSON).build(); @@ -157,30 +130,26 @@ public Response get( @Produces(APPLICATION_JSON) public Response get(@PathParam("uuid") UUID uuid) { PrnfbButton button = settingsService.getButton(uuid); - if (!userCheckService.isAllowedUseButton(button)) { + if (!userCheckService.isAdminAllowed(button)) { return status(UNAUTHORIZED).build(); } ButtonDTO dto = toButtonDto(button); return ok(dto, APPLICATION_JSON).build(); } - private void populateButtonFormDtoList( - Integer repositoryId, Long pullRequestId, List dtos) { - for (ButtonDTO dto : dtos) { - PrnfbRendererWrapper renderer = - buttonsService.getRenderer(repositoryId, pullRequestId, dto.getUuid()); - List buttonFormDtoList = dto.getButtonFormList(); - if (buttonFormDtoList != null) { - for (ButtonFormElementDTO buttonFormElementDto : buttonFormDtoList) { - String defaultValue = buttonFormElementDto.getDefaultValue(); - if (!isNullOrEmpty(defaultValue)) { - String defaultValueRendered = renderer.render(defaultValue, ENCODE_FOR.NONE); - buttonFormElementDto.setDefaultValue(defaultValueRendered); - } - } - dto.setButtonFormList(buttonFormDtoList); - } - } + @GET + @Path("/repository/{repositoryId}/pullrequest/{pullRequestId}") + @Produces(APPLICATION_JSON) + public Response get( + @PathParam("repositoryId") Integer repositoryId, + @PathParam("pullRequestId") Long pullRequestId) { + List buttons = buttonsService.getButtons(repositoryId, pullRequestId); + List dtos = toButtonDtoList(buttons); + Collections.sort(dtos); + + populateButtonFormDtoList(repositoryId, pullRequestId, dtos); + + return ok(dtos, APPLICATION_JSON).build(); } @POST @@ -192,15 +161,36 @@ public Response press( @PathParam("repositoryId") Integer repositoryId, @PathParam("pullRequestId") Long pullRequestId, @PathParam("uuid") final UUID buttionUuid) { - PrnfbButton button = settingsService.getButton(buttionUuid); - if (!userCheckService.isAllowedUseButton(button)) { - return status(UNAUTHORIZED).build(); + List buttons = buttonsService.getButtons(repositoryId, pullRequestId); + Optional button = + Iterables.tryFind(buttons, (b) -> b.getUuid().equals(buttionUuid)); + if (!button.isPresent()) { + return status(NOT_FOUND).build(); } String formData = request.getParameter("form"); List results = buttonsService.handlePressed(repositoryId, pullRequestId, buttionUuid, formData); - ButtonPressDTO dto = toTriggerResultDto(button, results); + ButtonPressDTO dto = toTriggerResultDto(button.get(), results); return ok(dto, APPLICATION_JSON).build(); } + + private void populateButtonFormDtoList( + Integer repositoryId, Long pullRequestId, List dtos) { + for (ButtonDTO dto : dtos) { + PrnfbRendererWrapper renderer = + buttonsService.getRenderer(repositoryId, pullRequestId, dto.getUuid()); + List buttonFormDtoList = dto.getButtonFormList(); + if (buttonFormDtoList != null) { + for (ButtonFormElementDTO buttonFormElementDto : buttonFormDtoList) { + String defaultValue = buttonFormElementDto.getDefaultValue(); + if (!isNullOrEmpty(defaultValue)) { + String defaultValueRendered = renderer.render(defaultValue, ENCODE_FOR.NONE); + buttonFormElementDto.setDefaultValue(defaultValueRendered); + } + } + dto.setButtonFormList(buttonFormDtoList); + } + } + } } diff --git a/src/main/java/se/bjurr/prnfb/presentation/GlobalAdminServlet.java b/src/main/java/se/bjurr/prnfb/presentation/GlobalAdminServlet.java index 7ef3584..6fb96b8 100644 --- a/src/main/java/se/bjurr/prnfb/presentation/GlobalAdminServlet.java +++ b/src/main/java/se/bjurr/prnfb/presentation/GlobalAdminServlet.java @@ -13,8 +13,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import se.bjurr.prnfb.service.UserCheckService; - +import com.atlassian.bitbucket.project.Project; +import com.atlassian.bitbucket.project.ProjectService; import com.atlassian.bitbucket.repository.Repository; import com.atlassian.bitbucket.repository.RepositoryService; import com.atlassian.sal.api.auth.LoginUriProvider; @@ -24,11 +24,14 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; +import se.bjurr.prnfb.service.UserCheckService; + public class GlobalAdminServlet extends HttpServlet { private static final long serialVersionUID = 3846987953228399693L; private final LoginUriProvider loginUriProvider; private final TemplateRenderer renderer; private final RepositoryService repositoryService; + private final ProjectService projectService; private final UserCheckService userCheckService; private final UserManager userManager; @@ -37,12 +40,14 @@ public GlobalAdminServlet( LoginUriProvider loginUriProvider, TemplateRenderer renderer, RepositoryService repositoryService, - UserCheckService userCheckService) { + UserCheckService userCheckService, + ProjectService projectService) { this.userManager = userManager; this.loginUriProvider = loginUriProvider; this.renderer = renderer; this.repositoryService = repositoryService; this.userCheckService = userCheckService; + this.projectService = projectService; } @Override @@ -54,24 +59,44 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) { return; } - final Optional repository = getRepository(request.getPathInfo()); - boolean isSystemAdmin = this.userCheckService.isSystemAdmin(user.getUserKey()); String projectKey = null; String repositorySlug = null; + + final Optional repository = getRepository(request.getPathInfo()); if (repository.isPresent()) { projectKey = repository.get().getProject().getKey(); repositorySlug = repository.get().getSlug(); } + + final Optional project = getProject(request.getPathInfo()); + if (project.isPresent()) { + projectKey = project.get().getKey(); + repositorySlug = null; + } + boolean isAdmin = this.userCheckService.isAdmin(user.getUserKey(), projectKey, repositorySlug); + boolean isSystemAdmin = this.userCheckService.isSystemAdmin(user.getUserKey()); Map context = newHashMap(); if (repository.isPresent()) { context = of( // - "repository", repository.orNull(), // - "isAdmin", isAdmin, // - "isSystemAdmin", isSystemAdmin); + "repository", + repository.get(), // + "isAdmin", + isAdmin, // + "isSystemAdmin", + isSystemAdmin); + } else if (project.isPresent()) { + context = + of( // + "project", + project.get(), // + "isAdmin", + isAdmin, // + "isSystemAdmin", + isSystemAdmin); } else { context = of( // @@ -99,23 +124,46 @@ private URI getUri(HttpServletRequest request) { } @VisibleForTesting - Optional getRepository(String pathInfo) { - if (pathInfo == null - || !pathInfo.contains("/") - || pathInfo.endsWith("prnfb/admin") - || pathInfo.endsWith("prnfb/admin/")) { + Optional getProject(String pathInfo) { + Optional componentsOpt = getComponents(pathInfo); + if (!componentsOpt.isPresent() || componentsOpt.get().length != 1) { return absent(); } - String[] components = pathInfo.split("/"); - if (components.length == 0) { + String[] components = componentsOpt.get(); + String projectKey = components[0]; + Project project = projectService.getByKey(projectKey); + return Optional.of(project); + } + + @VisibleForTesting + Optional getRepository(String pathInfo) { + Optional componentsOpt = getComponents(pathInfo); + if (!componentsOpt.isPresent() || componentsOpt.get().length != 2) { return absent(); } - String project = components[components.length - 2]; - String repoSlug = components[components.length - 1]; + String[] components = componentsOpt.get(); + String project = components[0]; + String repoSlug = components[1]; final Repository repository = checkNotNull( this.repositoryService.getBySlug(project, repoSlug), // "Did not find " + project + " " + repoSlug); return Optional.of(repository); } + + private Optional getComponents(String pathInfo) { + if (pathInfo == null || pathInfo.isEmpty()) { + return absent(); + } + int indexOf = pathInfo.indexOf("prnfb/admin/"); + if (indexOf == -1) { + return absent(); + } + String root = pathInfo.substring(indexOf + "prnfb/admin/".length()); + if (root.isEmpty()) { + return absent(); + } + String[] split = root.split("/"); + return Optional.of(split); + } } diff --git a/src/main/java/se/bjurr/prnfb/presentation/NotificationServlet.java b/src/main/java/se/bjurr/prnfb/presentation/NotificationServlet.java index 7a774cc..263d261 100644 --- a/src/main/java/se/bjurr/prnfb/presentation/NotificationServlet.java +++ b/src/main/java/se/bjurr/prnfb/presentation/NotificationServlet.java @@ -23,13 +23,13 @@ import javax.ws.rs.Produces; import javax.ws.rs.core.Response; +import com.atlassian.annotations.security.XsrfProtectionExcluded; + import se.bjurr.prnfb.presentation.dto.NotificationDTO; import se.bjurr.prnfb.service.SettingsService; import se.bjurr.prnfb.service.UserCheckService; import se.bjurr.prnfb.settings.PrnfbNotification; -import com.atlassian.annotations.security.XsrfProtectionExcluded; - @Path("/settings/notifications") public class NotificationServlet { private final SettingsService settingsService; @@ -45,8 +45,7 @@ public NotificationServlet(SettingsService settingsService, UserCheckService use @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) public Response create(NotificationDTO notificationDto) { - if (!this.userCheckService.isAdminAllowed( - notificationDto.getProjectKey(), notificationDto.getRepositorySlug())) { + if (!this.userCheckService.isAdminAllowed(notificationDto)) { return status(UNAUTHORIZED).build(); } try { @@ -67,9 +66,7 @@ public Response create(NotificationDTO notificationDto) { @Produces(APPLICATION_JSON) public Response delete(@PathParam("uuid") UUID notification) { PrnfbNotification notificationDto = this.settingsService.getNotification(notification); - if (!this.userCheckService.isAdminAllowed( // - notificationDto.getProjectKey().orNull(), // - notificationDto.getRepositorySlug().orNull())) { + if (!this.userCheckService.isAdminAllowed(notificationDto)) { return status(UNAUTHORIZED).build(); } this.settingsService.deleteNotification(notification); @@ -79,11 +76,10 @@ public Response delete(@PathParam("uuid") UUID notification) { @GET @Produces(APPLICATION_JSON) public Response get() { - if (!this.userCheckService.isViewAllowed()) { - return status(UNAUTHORIZED).build(); - } List notifications = this.settingsService.getNotifications(); - List dtos = toNotificationDtoList(notifications); + Iterable notificationsFiltered = + userCheckService.filterAdminAllowed(notifications); + List dtos = toNotificationDtoList(notificationsFiltered); Collections.sort(dtos); return ok(dtos).build(); } @@ -92,11 +88,10 @@ public Response get() { @Path("/projectKey/{projectKey}") @Produces(APPLICATION_JSON) public Response get(@PathParam("projectKey") String projectKey) { - if (!this.userCheckService.isViewAllowed()) { - return status(UNAUTHORIZED).build(); - } List notifications = this.settingsService.getNotifications(projectKey); - List dtos = toNotificationDtoList(notifications); + Iterable notificationsFiltered = + userCheckService.filterAdminAllowed(notifications); + List dtos = toNotificationDtoList(notificationsFiltered); Collections.sort(dtos); return ok(dtos).build(); } @@ -107,12 +102,11 @@ public Response get(@PathParam("projectKey") String projectKey) { public Response get( @PathParam("projectKey") String projectKey, @PathParam("repositorySlug") String repositorySlug) { - if (!this.userCheckService.isViewAllowed()) { - return status(UNAUTHORIZED).build(); - } List notifications = this.settingsService.getNotifications(projectKey, repositorySlug); - List dtos = toNotificationDtoList(notifications); + Iterable notificationsFiltered = + userCheckService.filterAdminAllowed(notifications); + List dtos = toNotificationDtoList(notificationsFiltered); Collections.sort(dtos); return ok(dtos).build(); } @@ -121,10 +115,10 @@ public Response get( @Path("{uuid}") @Produces(APPLICATION_JSON) public Response get(@PathParam("uuid") UUID notificationUuid) { - if (!this.userCheckService.isViewAllowed()) { + PrnfbNotification notification = this.settingsService.getNotification(notificationUuid); + if (!this.userCheckService.isAdminAllowed(notification)) { return status(UNAUTHORIZED).build(); } - PrnfbNotification notification = this.settingsService.getNotification(notificationUuid); NotificationDTO dto = toNotificationDto(notification); return ok(dto).build(); } diff --git a/src/main/java/se/bjurr/prnfb/presentation/SettingsDataServlet.java b/src/main/java/se/bjurr/prnfb/presentation/SettingsDataServlet.java index b3ae43b..4b80aea 100644 --- a/src/main/java/se/bjurr/prnfb/presentation/SettingsDataServlet.java +++ b/src/main/java/se/bjurr/prnfb/presentation/SettingsDataServlet.java @@ -15,12 +15,14 @@ import javax.ws.rs.Produces; import javax.ws.rs.core.Response; +import com.atlassian.annotations.security.XsrfProtectionExcluded; +import com.google.common.base.Optional; + import se.bjurr.prnfb.presentation.dto.SettingsDataDTO; import se.bjurr.prnfb.service.SettingsService; import se.bjurr.prnfb.service.UserCheckService; import se.bjurr.prnfb.settings.PrnfbSettingsData; - -import com.atlassian.annotations.security.XsrfProtectionExcluded; +import se.bjurr.prnfb.settings.Restricted; @Path("/settings") public class SettingsDataServlet { @@ -50,7 +52,18 @@ public Response get() { @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) public Response post(SettingsDataDTO settingsDataDto) { - if (!this.userCheckService.isAdminAllowed(null, null)) { + if (!this.userCheckService.isAdminAllowed( + new Restricted() { + @Override + public Optional getRepositorySlug() { + return null; + } + + @Override + public Optional getProjectKey() { + return null; + } + })) { return status(UNAUTHORIZED).build(); } diff --git a/src/main/java/se/bjurr/prnfb/presentation/dto/ButtonDTO.java b/src/main/java/se/bjurr/prnfb/presentation/dto/ButtonDTO.java index 77e7bd4..886254b 100644 --- a/src/main/java/se/bjurr/prnfb/presentation/dto/ButtonDTO.java +++ b/src/main/java/se/bjurr/prnfb/presentation/dto/ButtonDTO.java @@ -13,11 +13,12 @@ import com.google.common.base.Optional; import com.google.gson.reflect.TypeToken; +import se.bjurr.prnfb.settings.Restricted; import se.bjurr.prnfb.settings.USER_LEVEL; @XmlRootElement @XmlAccessorType(FIELD) -public class ButtonDTO implements Comparable { +public class ButtonDTO implements Comparable, Restricted { public static Type BUTTON_FORM_LIST_DTO_TYPE = new TypeToken>() {}.getType(); @@ -132,10 +133,12 @@ public String getName() { return this.name; } + @Override public Optional getProjectKey() { return Optional.fromNullable(this.projectKey); } + @Override public Optional getRepositorySlug() { return Optional.fromNullable(this.repositorySlug); } diff --git a/src/main/java/se/bjurr/prnfb/presentation/dto/NotificationDTO.java b/src/main/java/se/bjurr/prnfb/presentation/dto/NotificationDTO.java index fc3bf9e..37fbd13 100644 --- a/src/main/java/se/bjurr/prnfb/presentation/dto/NotificationDTO.java +++ b/src/main/java/se/bjurr/prnfb/presentation/dto/NotificationDTO.java @@ -8,13 +8,16 @@ import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; +import com.google.common.base.Optional; + import se.bjurr.prnfb.http.UrlInvoker.HTTP_METHOD; import se.bjurr.prnfb.service.PrnfbRenderer.ENCODE_FOR; +import se.bjurr.prnfb.settings.Restricted; import se.bjurr.prnfb.settings.TRIGGER_IF_MERGE; @XmlRootElement @XmlAccessorType(FIELD) -public class NotificationDTO implements Comparable { +public class NotificationDTO implements Comparable, Restricted { private String filterRegexp; private String filterString; private List headers; @@ -237,8 +240,14 @@ public String getPostContent() { return this.postContent; } - public String getProjectKey() { - return this.projectKey; + @Override + public Optional getProjectKey() { + return Optional.fromNullable(this.projectKey); + } + + @Override + public Optional getRepositorySlug() { + return Optional.fromNullable(this.repositorySlug); } public String getProxyPassword() { @@ -257,10 +266,6 @@ public String getProxyUser() { return this.proxyUser; } - public String getRepositorySlug() { - return this.repositorySlug; - } - public TRIGGER_IF_MERGE getTriggerIfCanMerge() { return this.triggerIfCanMerge; } diff --git a/src/main/java/se/bjurr/prnfb/service/ButtonsService.java b/src/main/java/se/bjurr/prnfb/service/ButtonsService.java index 19eb0c1..03cba0e 100644 --- a/src/main/java/se/bjurr/prnfb/service/ButtonsService.java +++ b/src/main/java/se/bjurr/prnfb/service/ButtonsService.java @@ -55,6 +55,9 @@ List doGetButtons( ClientKeyStore clientKeyStore, final PullRequest pullRequest, boolean shouldAcceptAnyCertificate) { + + String projectKey = pullRequest.getToRef().getRepository().getProject().getKey(); + String repositoryKey = pullRequest.getToRef().getRepository().getSlug(); List allFoundButtons = newArrayList(); for (PrnfbButton candidate : settingsService.getButtons()) { Map> variables = new HashMap<>(); @@ -62,7 +65,7 @@ List doGetButtons( variables.put(BUTTON_TRIGGER_TITLE, Suppliers.ofInstance(button.getName())); PrnfbPullRequestAction pullRequestAction = BUTTON_TRIGGER; - if (userCheckService.isAllowedUseButton(candidate) // + if (userCheckService.isAllowed(candidate.getUserLevel(), projectKey, repositoryKey) // && isTriggeredByAction( clientKeyStore, notifications, diff --git a/src/main/java/se/bjurr/prnfb/service/UserCheckService.java b/src/main/java/se/bjurr/prnfb/service/UserCheckService.java index 26d3018..abc8a66 100644 --- a/src/main/java/se/bjurr/prnfb/service/UserCheckService.java +++ b/src/main/java/se/bjurr/prnfb/service/UserCheckService.java @@ -27,9 +27,8 @@ import com.atlassian.sal.api.user.UserManager; import com.atlassian.sal.api.user.UserProfile; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Predicate; -import se.bjurr.prnfb.settings.PrnfbButton; +import se.bjurr.prnfb.settings.Restricted; import se.bjurr.prnfb.settings.USER_LEVEL; public class UserCheckService { @@ -56,17 +55,18 @@ public UserCheckService( this.securityService = securityService; } - public Iterable filterAllowed(List buttons) { - Iterable allowedButtons = - filter( - buttons, - new Predicate() { - @Override - public boolean apply(PrnfbButton input) { - return isAllowedUseButton(input); - } - }); - return allowedButtons; + public Iterable filterAllowed(USER_LEVEL adminRestriction, List r) { + return filter( + r, + (c) -> + isAllowed( // + adminRestriction, // + c.getProjectKey().orNull(), // + c.getRepositorySlug().orNull())); + } + + public Iterable filterAdminAllowed(List list) { + return filter(list, (r) -> isAdminAllowed(r)); } @VisibleForTesting @@ -140,37 +140,27 @@ public boolean isAdmin(UserKey userKey, String projectKey, String repositorySlug return false; } - /** null if global. */ - public boolean isAdminAllowed(@Nullable String projectKey, @Nullable String repositorySlug) { - final UserProfile user = userManager.getRemoteUser(); - if (user == null) { - return false; - } + public boolean isAdminAllowed(Restricted restricted) { USER_LEVEL adminRestriction = settingsService.getPrnfbSettingsData().getAdminRestriction(); - return isAdminAllowed(adminRestriction, projectKey, repositorySlug); + String projectKey = restricted.getProjectKey().orNull(); + String repositorySlug = restricted.getRepositorySlug().orNull(); + return isAllowed(adminRestriction, projectKey, repositorySlug); } - private boolean isAdminAllowed( - USER_LEVEL adminRestriction, @Nullable String projectKey, @Nullable String repositorySlug) { + public boolean isAllowed( + USER_LEVEL userLevel, @Nullable String projectKey, @Nullable String repositorySlug) { UserKey userKey = userManager.getRemoteUser().getUserKey(); boolean isAdmin = isAdmin(userKey, projectKey, repositorySlug); boolean isSystemAdmin = isSystemAdmin(userKey); - return isAdminAllowedCheck(adminRestriction, isAdmin, isSystemAdmin); + return isAllowed(userLevel, isAdmin, isSystemAdmin); } - boolean isAdminAllowedCheck(USER_LEVEL userLevel, boolean isAdmin, boolean isSystemAdmin) { + boolean isAllowed(USER_LEVEL userLevel, boolean isAdmin, boolean isSystemAdmin) { return userLevel == EVERYONE // || isSystemAdmin // || isAdmin && userLevel == ADMIN; } - public boolean isAllowedUseButton(PrnfbButton candidate) { - return isAdminAllowed( // - candidate.getUserLevel(), // - candidate.getProjectKey().orNull(), // - candidate.getRepositorySlug().orNull()); - } - public boolean isSystemAdmin(UserKey userKey) { return userManager.isSystemAdmin(userKey); } diff --git a/src/main/java/se/bjurr/prnfb/settings/PrnfbButton.java b/src/main/java/se/bjurr/prnfb/settings/PrnfbButton.java index 5408aef..3296f7c 100644 --- a/src/main/java/se/bjurr/prnfb/settings/PrnfbButton.java +++ b/src/main/java/se/bjurr/prnfb/settings/PrnfbButton.java @@ -13,7 +13,7 @@ import se.bjurr.prnfb.presentation.dto.ON_OR_OFF; -public class PrnfbButton implements HasUuid { +public class PrnfbButton implements HasUuid, Restricted { private final ON_OR_OFF confirmation; private final String name; @@ -60,10 +60,12 @@ public List getButtonFormElementList() { return buttonFormElementList; } + @Override public Optional getProjectKey() { return fromNullable(this.projectKey); } + @Override public Optional getRepositorySlug() { return fromNullable(this.repositorySlug); } diff --git a/src/main/java/se/bjurr/prnfb/settings/PrnfbNotification.java b/src/main/java/se/bjurr/prnfb/settings/PrnfbNotification.java index f77c12a..a023fb0 100644 --- a/src/main/java/se/bjurr/prnfb/settings/PrnfbNotification.java +++ b/src/main/java/se/bjurr/prnfb/settings/PrnfbNotification.java @@ -22,7 +22,7 @@ import se.bjurr.prnfb.listener.PrnfbPullRequestAction; import se.bjurr.prnfb.service.PrnfbRenderer.ENCODE_FOR; -public class PrnfbNotification implements HasUuid { +public class PrnfbNotification implements HasUuid, Restricted { private static final String DEFAULT_NAME = "Notification"; private final String filterRegexp; @@ -286,6 +286,7 @@ public Optional getPostContent() { return fromNullable(this.postContent); } + @Override public Optional getProjectKey() { return fromNullable(this.projectKey); } @@ -306,6 +307,7 @@ public Optional getProxyUser() { return fromNullable(this.proxyUser); } + @Override public Optional getRepositorySlug() { return fromNullable(this.repositorySlug); } diff --git a/src/main/java/se/bjurr/prnfb/settings/Restricted.java b/src/main/java/se/bjurr/prnfb/settings/Restricted.java new file mode 100644 index 0000000..aa0bce2 --- /dev/null +++ b/src/main/java/se/bjurr/prnfb/settings/Restricted.java @@ -0,0 +1,10 @@ +package se.bjurr.prnfb.settings; + +import com.google.common.base.Optional; + +public interface Restricted { + + Optional getRepositorySlug(); + + Optional getProjectKey(); +} diff --git a/src/main/java/se/bjurr/prnfb/transformer/NotificationTransformer.java b/src/main/java/se/bjurr/prnfb/transformer/NotificationTransformer.java index 28e04d9..42bdc3d 100644 --- a/src/main/java/se/bjurr/prnfb/transformer/NotificationTransformer.java +++ b/src/main/java/se/bjurr/prnfb/transformer/NotificationTransformer.java @@ -44,7 +44,7 @@ public static NotificationDTO toNotificationDto(PrnfbNotification from) { return to; } - public static List toNotificationDtoList(List from) { + public static List toNotificationDtoList(Iterable from) { List to = newArrayList(); if (from != null) { for (PrnfbNotification n : from) { @@ -77,8 +77,8 @@ public static PrnfbNotification toPrnfbNotification(NotificationDTO from) .withUrl(from.getUrl()) // .withUser(from.getUser()) // .withUuid(from.getUuid()) // - .withRepositorySlug(from.getRepositorySlug()) // - .withProjectKey(from.getProjectKey()) // + .withRepositorySlug(from.getRepositorySlug().orNull()) // + .withProjectKey(from.getProjectKey().orNull()) // .build(); } diff --git a/src/main/resources/admin.vm b/src/main/resources/admin.vm index 86b608a..8582808 100644 --- a/src/main/resources/admin.vm +++ b/src/main/resources/admin.vm @@ -201,14 +201,23 @@ #else -
- - -
If project selected, this notification will only trigger for pull requests to that project.
-
+ + #if($project) + + + + #else + +
+ + +
If project selected, this notification will only trigger for pull requests to that project.
+
+ + #end
@@ -312,6 +321,13 @@ #else + + #if($project) + + + + #else +
@@ -320,6 +336,8 @@
If project selected, this notification will only trigger for pull requests to that project.
+ + #end
diff --git a/src/main/resources/atlassian-plugin.xml b/src/main/resources/atlassian-plugin.xml index 355c734..854ad71 100644 --- a/src/main/resources/atlassian-plugin.xml +++ b/src/main/resources/atlassian-plugin.xml @@ -22,6 +22,7 @@ + @@ -48,6 +49,12 @@ + + + /plugins/servlet/prnfb/admin/${project.key} + + + triggerManualNotification diff --git a/src/test/java/se/bjurr/prnfb/presentation/ButtonServletTest.java b/src/test/java/se/bjurr/prnfb/presentation/ButtonServletTest.java index ce20987..df829f0 100644 --- a/src/test/java/se/bjurr/prnfb/presentation/ButtonServletTest.java +++ b/src/test/java/se/bjurr/prnfb/presentation/ButtonServletTest.java @@ -3,11 +3,11 @@ import static com.google.common.collect.Lists.newArrayList; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.anyListOf; -import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; +import static org.mockito.internal.verification.VerificationModeFactory.times; import static se.bjurr.prnfb.settings.USER_LEVEL.EVERYONE; import static se.bjurr.prnfb.test.Podam.populatedInstanceOf; import static se.bjurr.prnfb.transformer.ButtonTransformer.toPrnfbButton; @@ -23,6 +23,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -47,7 +48,7 @@ public class ButtonServletTest { private PrnfbRendererWrapper rendererWrapper; private void allowAll() { - when(this.userCheckService.filterAllowed(anyListOf(PrnfbButton.class))) // + when(this.userCheckService.filterAdminAllowed(anyListOf(PrnfbButton.class))) // .thenAnswer( new Answer>() { @SuppressWarnings("unchecked") @@ -63,7 +64,7 @@ public void before() throws Exception { initMocks(this); when(this.userCheckService.isViewAllowed()) // .thenReturn(true); - when(this.userCheckService.isAdminAllowed(anyString(), anyString())) // + when(this.userCheckService.isAdminAllowed(Mockito.any())) // .thenReturn(true); this.sut = new ButtonServlet(this.buttonsService, this.settingsService, this.userCheckService); @@ -257,14 +258,29 @@ public void testThatButtonCanBeListedPerProjectAndRepo() throws Exception { @Test public void testThatButtonCanBePressed() throws Exception { + Integer repositoryId = 1; + Long pullRequestId = 2L; + UUID buttonUuid = button1.getUuid(); + when(buttonsService.getButtons(repositoryId, pullRequestId)) + .thenReturn(newArrayList(this.button1)); + + HttpServletRequest mockRequest = mock(HttpServletRequest.class); + String formDataFromUserInPrView = "{}"; + when(mockRequest.getParameter("form")).thenReturn(formDataFromUserInPrView); + + this.sut.press(mockRequest, repositoryId, pullRequestId, buttonUuid); + + verify(this.buttonsService, times(1)) // + .handlePressed(repositoryId, pullRequestId, buttonUuid, formDataFromUserInPrView); + } + + @Test + public void testThatButtonCanNotBePressed() throws Exception { Integer repositoryId = 1; Long pullRequestId = 2L; UUID buttonUuid = UUID.randomUUID(); - PrnfbButton button = createPrnfbButton(createButton()); - when(this.settingsService.getButton(buttonUuid)) // - .thenReturn(button); - when(this.userCheckService.isAllowedUseButton(button)) // - .thenReturn(true); + when(buttonsService.getButtons(repositoryId, pullRequestId)) + .thenReturn(newArrayList(this.button1)); HttpServletRequest mockRequest = mock(HttpServletRequest.class); String formDataFromUserInPrView = "{}"; @@ -272,7 +288,7 @@ public void testThatButtonCanBePressed() throws Exception { this.sut.press(mockRequest, repositoryId, pullRequestId, buttonUuid); - verify(this.buttonsService) // + verify(this.buttonsService, times(0)) // .handlePressed(repositoryId, pullRequestId, buttonUuid, formDataFromUserInPrView); } diff --git a/src/test/java/se/bjurr/prnfb/presentation/GlobalAdminServletTest.java b/src/test/java/se/bjurr/prnfb/presentation/GlobalAdminServletTest.java index de2ee88..d68807a 100644 --- a/src/test/java/se/bjurr/prnfb/presentation/GlobalAdminServletTest.java +++ b/src/test/java/se/bjurr/prnfb/presentation/GlobalAdminServletTest.java @@ -9,14 +9,16 @@ import org.junit.Test; import org.mockito.Mock; -import se.bjurr.prnfb.service.UserCheckService; - +import com.atlassian.bitbucket.project.Project; +import com.atlassian.bitbucket.project.ProjectService; import com.atlassian.bitbucket.repository.Repository; import com.atlassian.bitbucket.repository.RepositoryService; import com.atlassian.sal.api.auth.LoginUriProvider; import com.atlassian.sal.api.user.UserManager; import com.atlassian.templaterenderer.TemplateRenderer; +import se.bjurr.prnfb.service.UserCheckService; + public class GlobalAdminServletTest { @Mock private LoginUriProvider loginUriProvider; @@ -25,6 +27,7 @@ public class GlobalAdminServletTest { private GlobalAdminServlet sut; @Mock private UserCheckService userCheckService; @Mock private UserManager userManager; + @Mock private ProjectService projectService; @Before public void before() { @@ -35,7 +38,8 @@ public void before() { this.loginUriProvider, this.renderer, this.repositoryService, - this.userCheckService); + this.userCheckService, + this.projectService); } @Test @@ -51,10 +55,31 @@ public void testGetRepository() { when(this.repositoryService.getBySlug("p", "r")) // .thenReturn(repository); - assertThat(this.sut.getRepository("p/r").orNull()) // + assertThat(this.sut.getRepository("prnfb/admin/p/r").orNull()) // .isSameAs(repository); assertThat(this.sut.getRepository("some/path/prnfb/admin").orNull()) // .isNull(); } + + @Test + public void testGetProject() { + assertThat(this.sut.getProject(null).orNull()) // + .isNull(); + assertThat(this.sut.getProject("").orNull()) // + .isNull(); + assertThat(this.sut.getProject("/").orNull()) // + .isNull(); + + Project project = mock(Project.class); + when(this.projectService.getByKey("p")) // + .thenReturn(project); + + assertThat(this.sut.getProject("/prnfb/admin/p").orNull()) // + .isSameAs(project); + assertThat(this.sut.getProject("asd/asd/prnfb/admin/p").orNull()) // + .isSameAs(project); + assertThat(this.sut.getProject("some/path/prnfb/admin").orNull()) // + .isNull(); + } } diff --git a/src/test/java/se/bjurr/prnfb/presentation/NotificationServletTest.java b/src/test/java/se/bjurr/prnfb/presentation/NotificationServletTest.java index bf173e2..dd28e97 100644 --- a/src/test/java/se/bjurr/prnfb/presentation/NotificationServletTest.java +++ b/src/test/java/se/bjurr/prnfb/presentation/NotificationServletTest.java @@ -3,7 +3,6 @@ import static com.atlassian.bitbucket.pull.PullRequestState.DECLINED; import static com.google.common.collect.Lists.newArrayList; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -19,14 +18,15 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import org.mockito.Mockito; + +import com.google.common.collect.Lists; import se.bjurr.prnfb.presentation.dto.NotificationDTO; import se.bjurr.prnfb.service.SettingsService; import se.bjurr.prnfb.service.UserCheckService; import se.bjurr.prnfb.settings.PrnfbNotification; -import com.google.common.collect.Lists; - public class NotificationServletTest { private PrnfbNotification notification1; private PrnfbNotification notification2; @@ -41,7 +41,7 @@ public void before() throws Exception { initMocks(this); when(this.userCheckService.isViewAllowed()) // .thenReturn(true); - when(this.userCheckService.isAdminAllowed(anyString(), anyString())) // + when(this.userCheckService.isAdminAllowed(Mockito.any())) // .thenReturn(true); this.sut = new NotificationServlet(this.settingsService, this.userCheckService); this.notificationDto1 = populatedInstanceOf(NotificationDTO.class); @@ -89,6 +89,8 @@ public void testNotificationCanBeRead() throws Exception { List storedSettings = newArrayList(this.notification1, this.notification2); when(this.settingsService.getNotifications()) // .thenReturn(storedSettings); + when(userCheckService.filterAdminAllowed(storedSettings)) // + .thenReturn(storedSettings); List actual = (List) this.sut.get().getEntity(); @@ -98,10 +100,13 @@ public void testNotificationCanBeRead() throws Exception { @Test public void testThatNotificationCanBeListedPerProject() throws Exception { - when(this.settingsService.getNotifications(this.notificationDto1.getProjectKey())) // - .thenReturn(newArrayList(this.notification1)); + List notifications = newArrayList(this.notification1); + when(this.settingsService.getNotifications(this.notificationDto1.getProjectKey().orNull())) // + .thenReturn(notifications); + when(userCheckService.filterAdminAllowed(notifications)) // + .thenReturn(notifications); - Response actual = this.sut.get(this.notificationDto1.getProjectKey()); + Response actual = this.sut.get(this.notificationDto1.getProjectKey().orNull()); @SuppressWarnings("unchecked") Iterable actualList = (Iterable) actual.getEntity(); @@ -111,13 +116,18 @@ public void testThatNotificationCanBeListedPerProject() throws Exception { @Test public void testThatNotificationCanBeListedPerProjectAndRepo() throws Exception { + List notifications = newArrayList(this.notification1); when(this.settingsService.getNotifications( - this.notificationDto1.getProjectKey(), this.notificationDto1.getRepositorySlug())) // - .thenReturn(newArrayList(this.notification1)); + this.notificationDto1.getProjectKey().orNull(), + this.notificationDto1.getRepositorySlug().orNull())) // + .thenReturn(notifications); + when(userCheckService.filterAdminAllowed(notifications)) // + .thenReturn(notifications); Response actual = this.sut.get( - this.notificationDto1.getProjectKey(), this.notificationDto1.getRepositorySlug()); + this.notificationDto1.getProjectKey().orNull(), + this.notificationDto1.getRepositorySlug().orNull()); @SuppressWarnings("unchecked") Iterable actualList = (Iterable) actual.getEntity(); diff --git a/src/test/java/se/bjurr/prnfb/presentation/SettingsDataServletTest.java b/src/test/java/se/bjurr/prnfb/presentation/SettingsDataServletTest.java index 605d1cb..b6027a0 100644 --- a/src/test/java/se/bjurr/prnfb/presentation/SettingsDataServletTest.java +++ b/src/test/java/se/bjurr/prnfb/presentation/SettingsDataServletTest.java @@ -1,7 +1,6 @@ package se.bjurr.prnfb.presentation; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -12,6 +11,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import org.mockito.Mockito; import se.bjurr.prnfb.presentation.dto.SettingsDataDTO; import se.bjurr.prnfb.service.SettingsService; @@ -29,7 +29,7 @@ public void before() { initMocks(this); when(this.userCheckService.isViewAllowed()) // .thenReturn(true); - when(this.userCheckService.isAdminAllowed(anyString(), anyString())) // + when(this.userCheckService.isAdminAllowed(Mockito.any())) // .thenReturn(true); this.sut = new SettingsDataServlet(this.userCheckService, this.settingsService); } diff --git a/src/test/java/se/bjurr/prnfb/service/ButtonsServiceTest.java b/src/test/java/se/bjurr/prnfb/service/ButtonsServiceTest.java index 8fed06d..bf8d5fc 100644 --- a/src/test/java/se/bjurr/prnfb/service/ButtonsServiceTest.java +++ b/src/test/java/se/bjurr/prnfb/service/ButtonsServiceTest.java @@ -100,12 +100,15 @@ public void before() throws ValidationException { this.buttonDto1 = populatedInstanceOf(ButtonDTO.class); this.buttonDto1.setProjectKey(null); this.buttonDto1.setRepositorySlug(null); + this.buttonDto1.setUserLevel(null); this.button1 = toPrnfbButton(this.buttonDto1); this.buttonDto2 = populatedInstanceOf(ButtonDTO.class); this.buttonDto2.setProjectKey(null); this.buttonDto2.setRepositorySlug(null); + this.buttonDto2.setUserLevel(null); this.button2 = toPrnfbButton(this.buttonDto2); this.buttonDto3 = populatedInstanceOf(ButtonDTO.class); + this.buttonDto3.setUserLevel(null); this.button3 = toPrnfbButton(this.buttonDto3); when(this.settingsService.getButton(this.button1.getUuid())) // @@ -130,6 +133,12 @@ public void before() throws ValidationException { this.notifications = newArrayList(this.notification1, this.notification2); when(this.settingsService.getNotifications()) // .thenReturn(this.notifications); + + when(this.pullRequest.getToRef()).thenReturn(this.prRef); + when(this.prRef.getRepository()).thenReturn(this.repository); + when(this.repository.getSlug()).thenReturn(this.button3.getRepositorySlug().get()); + when(this.repository.getProject()).thenReturn(this.project); + when(this.project.getKey()).thenReturn(this.button3.getProjectKey().get()); } @Test @@ -137,12 +146,25 @@ public void testThatButtonsCanBeRetrievedWhenAllAllowed() { List candidates = newArrayList(this.button1, this.button2, this.button3); when(this.settingsService.getButtons()) // .thenReturn(candidates); - when(this.userCheckService.isAllowedUseButton(this.button1)) // + String projectKey = prRef.getRepository().getProject().getKey(); + String repoSlug = prRef.getRepository().getSlug(); + when(this.userCheckService.isAllowed(this.button1.getUserLevel(), projectKey, repoSlug)) // + .thenReturn(true); + when(this.userCheckService.isAllowed(this.button2.getUserLevel(), projectKey, repoSlug)) // + .thenReturn(true); + when(this.userCheckService.isAllowed(this.button3.getUserLevel(), projectKey, repoSlug)) // .thenReturn(true); - when(this.userCheckService.isAllowedUseButton(this.button2)) // + + when(this.userCheckService.isAllowed( + this.button1.getUserLevel(), projectKey, "otherrepository")) // + .thenReturn(true); + when(this.userCheckService.isAllowed( + this.button2.getUserLevel(), projectKey, "otherrepository")) // .thenReturn(true); - when(this.userCheckService.isAllowedUseButton(this.button3)) // + when(this.userCheckService.isAllowed( + this.button3.getUserLevel(), projectKey, "otherrepository")) // .thenReturn(true); + when(this.prnfbPullRequestEventListener.isNotificationTriggeredByAction( this.notification1, this.pullRequestAction, @@ -159,11 +181,6 @@ public void testThatButtonsCanBeRetrievedWhenAllAllowed() { this.clientKeyStore, this.shouldAcceptAnyCertificate)) // .thenReturn(true); - when(this.pullRequest.getToRef()).thenReturn(this.prRef); - when(this.prRef.getRepository()).thenReturn(this.repository); - when(this.repository.getSlug()).thenReturn(this.button3.getRepositorySlug().get()); - when(this.repository.getProject()).thenReturn(this.project); - when(this.project.getKey()).thenReturn(this.button3.getProjectKey().get()); List actual = this.sut.doGetButtons( @@ -205,9 +222,11 @@ public void testThatButtonsCanBeRetrievedWhenNoneAllowed() { List candidates = newArrayList(this.button1, this.button2); when(this.settingsService.getButtons()) // .thenReturn(candidates); - when(this.userCheckService.isAllowedUseButton(this.button1)) // + String projectKey = prRef.getRepository().getProject().getKey(); + String repoSlug = prRef.getRepository().getSlug(); + when(this.userCheckService.isAllowed(this.button1.getUserLevel(), projectKey, repoSlug)) // .thenReturn(false); - when(this.userCheckService.isAllowedUseButton(this.button2)) // + when(this.userCheckService.isAllowed(this.button2.getUserLevel(), projectKey, repoSlug)) // .thenReturn(false); List actual = diff --git a/src/test/java/se/bjurr/prnfb/service/UserCheckServiceTest.java b/src/test/java/se/bjurr/prnfb/service/UserCheckServiceTest.java index 8ac89dc..2522b1b 100644 --- a/src/test/java/se/bjurr/prnfb/service/UserCheckServiceTest.java +++ b/src/test/java/se/bjurr/prnfb/service/UserCheckServiceTest.java @@ -1,14 +1,14 @@ package se.bjurr.prnfb.service; -import static com.google.common.collect.Lists.newArrayList; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; +import static se.bjurr.prnfb.settings.PrnfbSettingsDataBuilder.prnfbSettingsDataBuilder; import static se.bjurr.prnfb.settings.USER_LEVEL.ADMIN; import static se.bjurr.prnfb.settings.USER_LEVEL.EVERYONE; import static se.bjurr.prnfb.settings.USER_LEVEL.SYSTEM_ADMIN; -import java.util.List; import java.util.Set; import org.junit.Before; @@ -26,9 +26,10 @@ import com.atlassian.sal.api.user.UserKey; import com.atlassian.sal.api.user.UserManager; import com.atlassian.sal.api.user.UserProfile; +import com.google.common.base.Optional; -import se.bjurr.prnfb.presentation.dto.ON_OR_OFF; -import se.bjurr.prnfb.settings.PrnfbButton; +import se.bjurr.prnfb.settings.PrnfbSettingsData; +import se.bjurr.prnfb.settings.Restricted; public class UserCheckServiceTest { private final EscalatedSecurityContext escalatedSecurityContext = @@ -59,10 +60,8 @@ public EscalatedSecurityContext withPermissions(Set arg0) { }; @Mock private PermissionService permissionService; - private String projectKey; @Mock private ProjectService projectService; @Mock private RepositoryService repositoryService; - private String repositorySlug; @Mock private SecurityService securityService; @Mock private SettingsService settingsService; private UserCheckService sut; @@ -88,63 +87,38 @@ public void before() throws Exception { @Test public void testThatAdminAllowedCanBeChecked() { - this.projectKey = null; - this.repositorySlug = null; - this.sut.isAdminAllowed(this.projectKey, this.repositorySlug); - } - - @Test - public void testThatAllowedButtonsCanBeFiltered() { - this.projectKey = "p1"; - this.repositorySlug = "r1"; - - when(this.userManager.getRemoteUser()) // - .thenReturn(this.user); - when(this.userManager.getRemoteUser().getUserKey()) // - .thenReturn(this.userKey); - when(this.userManager.isSystemAdmin(this.userKey)) // - .thenReturn(false); - when(this.userManager.isAdmin(this.userKey)) // - .thenReturn(false); - - PrnfbButton button1 = - new PrnfbButton(null, "title1", ADMIN, ON_OR_OFF.off, "p1", "r1", "confirmationText", null); - PrnfbButton button2 = - new PrnfbButton( - null, "title2", EVERYONE, ON_OR_OFF.off, "p1", "r1", "confirmationText", null); - PrnfbButton button3 = - new PrnfbButton( - null, "title3", SYSTEM_ADMIN, ON_OR_OFF.off, "p1", "r1", "confirmationText", null); - List buttons = newArrayList(button1, button2, button3); - - Iterable onlyAllowed = this.sut.filterAllowed(buttons); - - assertThat(onlyAllowed) // - .containsOnly(button2); + PrnfbSettingsData prnfbSettingsData = + prnfbSettingsDataBuilder().setAdminRestriction(SYSTEM_ADMIN).build(); + when(settingsService.getPrnfbSettingsData()).thenReturn(prnfbSettingsData); + UserProfile remoteUser = mock(UserProfile.class); + when(remoteUser.getUserKey()).thenReturn(userKey); + when(userManager.getRemoteUser()).thenReturn(remoteUser); + when(userManager.isAdmin(userKey)).thenReturn(false); + when(userManager.isSystemAdmin(userKey)).thenReturn(false); + boolean actual = + this.sut.isAdminAllowed( + new Restricted() { + @Override + public Optional getRepositorySlug() { + return Optional.absent(); + } + + @Override + public Optional getProjectKey() { + return Optional.absent(); + } + }); + + assertThat(actual).isFalse(); } @Test public void testThatAllowedCanBeChecked() { - this.projectKey = "p1"; - this.repositorySlug = "r1"; - - when(this.userManager.getRemoteUser()) // - .thenReturn(this.user); - when(this.userManager.getRemoteUser().getUserKey()) // - .thenReturn(this.userKey); - when(this.userManager.isSystemAdmin(this.userKey)) // - .thenReturn(true); - - PrnfbButton candidate = - new PrnfbButton(null, "title", ADMIN, ON_OR_OFF.off, "p1", "r1", "confirmationText", null); - assertThat(this.sut.isAllowedUseButton(candidate)) // - .isTrue(); - - assertThat(this.sut.isAdminAllowedCheck(ADMIN, true, false)) // + assertThat(this.sut.isAllowed(ADMIN, true, false)) // .isTrue(); - assertThat(this.sut.isAdminAllowedCheck(EVERYONE, false, false)) // + assertThat(this.sut.isAllowed(EVERYONE, false, false)) // .isTrue(); - assertThat(this.sut.isAdminAllowedCheck(SYSTEM_ADMIN, false, true)) // + assertThat(this.sut.isAllowed(SYSTEM_ADMIN, false, true)) // .isTrue(); }