Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
fxprunayre committed Jul 12, 2016
2 parents 632c7c4 + 6ae2f6e commit a00cde1
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 4 deletions.
13 changes: 11 additions & 2 deletions services/src/main/java/org/fao/geonet/api/users/UsersApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.fao.geonet.domain.UserGroup;
import org.fao.geonet.domain.UserGroupId;
import org.fao.geonet.domain.UserGroupId_;
import org.fao.geonet.exceptions.UserNotFoundEx;
import org.fao.geonet.kernel.DataManager;
import org.fao.geonet.repository.GroupRepository;
import org.fao.geonet.repository.SortUtils;
Expand All @@ -51,7 +52,6 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.method.P;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -169,6 +169,10 @@ public User getUser(

User user = userRepository.findOne(userIdentifier);

if (user == null) {
throw new UserNotFoundEx(Integer.toString(userIdentifier));
}

if (!(myUserId.equals(userIdentifier)) && myProfile == Profile.UserAdmin) {

//--- retrieve session user groups and check to see whether this user is
Expand Down Expand Up @@ -250,7 +254,12 @@ public ResponseEntity<String> deleteUser(

userGroupRepository.deleteAllByIdAttribute(UserGroupId_.userId,
Arrays.asList(userIdentifier));
userRepository.delete(userIdentifier);

try {
userRepository.delete(userIdentifier);
} catch (org.springframework.dao.EmptyResultDataAccessException ex) {
throw new UserNotFoundEx(Integer.toString(userIdentifier));
}

return new ResponseEntity(HttpStatus.NO_CONTENT);
}
Expand Down
149 changes: 149 additions & 0 deletions services/src/test/java/org/fao/geonet/api/users/UsersApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (C) 2001-2016 Food and Agriculture Organization of the
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
* and United Nations Environment Programme (UNEP)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
* Rome - Italy. email: [email protected]
*/
package org.fao.geonet.api.users;


import jeeves.constants.Jeeves;
import jeeves.server.UserSession;
import org.fao.geonet.domain.Profile;
import org.fao.geonet.domain.User;
import org.fao.geonet.repository.UserRepository;
import org.fao.geonet.services.AbstractServiceIntegrationTest;
import org.json.JSONArray;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
* Created by jose on 11/07/16.
*/
public class UsersApiTest extends AbstractServiceIntegrationTest {
@Autowired
private WebApplicationContext wac;

@Autowired
private UserRepository userRepository;

private MockMvc mockMvc;

private MockHttpSession mockHttpSession;


@Before
public void setUp() {
// Setup session
mockHttpSession = new MockHttpSession();

final User admin = _userRepo.findAllByProfile(Profile.Administrator).get(0);

UserSession userSession = (UserSession) mockHttpSession.getAttribute(Jeeves.Elem.SESSION);
if (userSession == null) {
userSession = new UserSession();
userSession.loginAs(admin);

mockHttpSession.setAttribute(Jeeves.Elem.SESSION, userSession);
userSession.setsHttpSession(mockHttpSession);
}
}


@Test
public void getUsers() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();

MvcResult result =this.mockMvc.perform(get("/api/users")
.session(this.mockHttpSession)
.accept(MediaType.parseMediaType("application/json")))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json")).andReturn();

String content = result.getResponse().getContentAsString();
JSONArray json = new JSONArray(content);
Assert.assertTrue(json.length() >= 1);
}

@Test
public void getNonExistingUser() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();

this.mockMvc.perform(get("/api/users/22")
.session(this.mockHttpSession)
.accept(MediaType.parseMediaType("application/json")))
.andExpect(status().is(404))
.andExpect(content().contentType("application/json"));
}

@Test
public void getExistingUser() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();

this.mockMvc.perform(get("/api/users/1")
.session(this.mockHttpSession)
.accept(MediaType.parseMediaType("application/json")))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"));
}


@Test
public void deleteExistingUser() throws Exception {
User user = new User();
user.setUsername("editor");
user.setProfile(Profile.Editor);
user.setEnabled(true);
user.getEmailAddresses().add("[email protected]");
_userRepo.save(user);

user = _userRepo.findOneByUsername("editor");

this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();

this.mockMvc.perform(delete("/api/users/" + user.getId())
.session(this.mockHttpSession)
.accept(MediaType.parseMediaType("application/json")))
.andExpect(status().is(204));
}

@Test
public void deleteNonExistingUser() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();

this.mockMvc.perform(delete("/api/users/22")
.session(this.mockHttpSession)
.accept(MediaType.parseMediaType("application/json")))
.andExpect(status().is(404))
.andExpect(content().contentType("application/json"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@

function loadGroups() {
$scope.isLoadingGroups = true;
$http.get('../api/groups').
// If not send profile, all groups are returned
var profile = ($scope.user.profile)?
"?profile=" +$scope.user.profile:"";

$http.get('../api/groups' + profile).
success(function(data) {
$scope.groups = data;
$scope.isLoadingGroups = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<span data-ng-hide="groupSelected.id != ''" data-translate="">newGroup</span>
<span><strong>{{groupSelected.name}}</strong></span>
<div class="btn-toolbar"
data-ng-show="user.isAdministratorOrMore() || groupSelected['@editable'] == 'true'">
data-ng-show="user.isUserAdminOrMore()">
<button type="button" class="btn btn-primary pull-right"
data-ng-disabled="!gnGroupEdit.$valid && !gnGroupEdit.dirty"
data-ng-click="saveGroup('#gn-group-edit', '#group-logo-upload')"
Expand Down

0 comments on commit a00cde1

Please sign in to comment.