add some test for user user_management
authorymh <ymh.work@gmail.com>
Wed, 11 Dec 2013 11:36:08 +0100
branchuser_management
changeset 236 73150fdbcafc
parent 235 f8746a482459
child 237 0e6c826a046e
add some test for user
server/src/test/java/org/iri_research/renkan/test/controller/UsersAdminControllerTest.java
--- a/server/src/test/java/org/iri_research/renkan/test/controller/UsersAdminControllerTest.java	Wed Dec 11 10:23:05 2013 +0100
+++ b/server/src/test/java/org/iri_research/renkan/test/controller/UsersAdminControllerTest.java	Wed Dec 11 11:36:08 2013 +0100
@@ -1,5 +1,6 @@
 package org.iri_research.renkan.test.controller;
 
+import java.security.SecureRandom;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -7,6 +8,7 @@
 import java.util.TimeZone;
 import java.util.UUID;
 
+import org.apache.commons.codec.binary.Hex;
 import org.iri_research.renkan.models.Project;
 import org.iri_research.renkan.models.Space;
 import org.iri_research.renkan.models.User;
@@ -23,16 +25,20 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
 import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import org.springframework.test.context.web.WebAppConfiguration;
 import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.MvcResult;
 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
 import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.client.HttpClientErrorException;
 import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.util.NestedServletException;
 
 
 @RunWith(SpringJUnit4ClassRunner.class)
@@ -291,292 +297,198 @@
 
     }
 
-//    @Test
-//    public void testSpacePostCreateBadJson() throws Exception {
-//
-//        MockHttpServletRequestBuilder post = MockMvcRequestBuilders
-//                .post("/admin/spaces/save");
-//        post = post.param("title", "New Title");
-//        post = post.param("description", "New description");
-//        post = post.param("uri", "http://ldt.iri.centrepompidou.fr/new/uri");
-//        post = post.param("color", "#ffffff");
-//        post = post.param("binConfig", "}");
-//
-//        this.mvc.perform(post)
-//                .andExpect(MockMvcResultMatchers.status().isOk())
-//                .andExpect(MockMvcResultMatchers.view().name("admin/spaceEdit"))
-//                .andExpect(MockMvcResultMatchers.model().hasErrors())
-//                .andExpect(MockMvcResultMatchers.model().errorCount(1))
-//                .andExpect(
-//                        MockMvcResultMatchers.model().attributeHasErrors(
-//                                "space"))
-//                .andExpect(
-//                        MockMvcResultMatchers.model().attributeHasFieldErrors(
-//                                "space", "binConfig"));
-//
-//        Assert.assertEquals("Must not have one more space", SPACE_NB,
-//                this.spacesRepository.count());
-//
-//    }
-//
-//    @Test
-//    public void testSpacePostUpdateAllErrors() throws Exception {
-//
-//        MockHttpServletRequestBuilder post = MockMvcRequestBuilders
-//                .post("/admin/spaces/save");
-//        post = post.param("id", this.spacesUuids.get(0));
-//        post = post.param("title", "");
-//        post = post.param("description", "New description");
-//        post = post.param("uri", "http://ldt.iri.centrepompidou.fr/new/uri");
-//        post = post.param("color", "#ffffff");
-//        post = post.param("binConfig", "{");
-//
-//        this.mvc.perform(post)
-//                .andExpect(MockMvcResultMatchers.status().isOk())
-//                .andExpect(MockMvcResultMatchers.view().name("admin/spaceEdit"))
-//                .andExpect(MockMvcResultMatchers.model().hasErrors())
-//                .andExpect(MockMvcResultMatchers.model().errorCount(2))
-//                .andExpect(
-//                        MockMvcResultMatchers.model().attributeHasErrors(
-//                                "space"))
-//                .andExpect(
-//                        MockMvcResultMatchers.model().attributeHasFieldErrors(
-//                                "space", "title"))
-//                .andExpect(
-//                        MockMvcResultMatchers.model().attributeHasFieldErrors(
-//                                "space", "binConfig"));
-//
-//        Space sp = this.spacesRepository.findOne(this.spacesUuids.get(0));
-//
-//        Assert.assertNotNull("Should find space", sp);
-//        Assert.assertEquals("Bin config equals", "{}", sp.getBinConfig());
-//        Assert.assertEquals("title equals", "test 0", sp.getTitle());
-//
-//    }
-//
-//    @Test
-//    public void testSpacePostCreateAllErrors() throws Exception {
-//
-//        MockHttpServletRequestBuilder post = MockMvcRequestBuilders
-//                .post("/admin/spaces/save");
-//        post = post.param("title", "");
-//        post = post.param("description", "New description");
-//        post = post.param("uri", "http://ldt.iri.centrepompidou.fr/new/uri");
-//        post = post.param("color", "#ffffff");
-//        post = post.param("binConfig", "}");
-//
-//        this.mvc.perform(post)
-//                .andExpect(MockMvcResultMatchers.status().isOk())
-//                .andExpect(MockMvcResultMatchers.view().name("admin/spaceEdit"))
-//                .andExpect(MockMvcResultMatchers.model().hasErrors())
-//                .andExpect(MockMvcResultMatchers.model().errorCount(2))
-//                .andExpect(
-//                        MockMvcResultMatchers.model().attributeHasErrors(
-//                                "space"))
-//                .andExpect(
-//                        MockMvcResultMatchers.model().attributeHasFieldErrors(
-//                                "space", "title"))
-//                .andExpect(
-//                        MockMvcResultMatchers.model().attributeHasFieldErrors(
-//                                "space", "binConfig"));
-//
-//        Assert.assertEquals("Must not have one more space", SPACE_NB,
-//                this.spacesRepository.count());
-//
-//    }
-//
-//    @Test
-//    public void testDeleteSpace() throws Exception {
-//
-//        MockHttpServletRequestBuilder get = MockMvcRequestBuilders
-//                .get("/admin/spaces/delete/"
-//                        + this.spacesUuids.get(SPACE_NB - 1));
-//
-//        MvcResult res = this.mvc
-//                .perform(get)
-//                .andExpect(MockMvcResultMatchers.status().isOk())
-//                .andExpect(
-//                        MockMvcResultMatchers.view().name(
-//                                "admin/spaceDeleteConfirm"))
-//                .andExpect(
-//                        MockMvcResultMatchers.model().attributeExists(
-//                                "spaceObj", "key", "salt")).andReturn();
-//
-//        Map<String, Object> model = res.getModelAndView().getModel();
-//
-//        Space space = (Space) model.get("spaceObj");
-//        Assert.assertNotNull("Space is not null", space);
-//        Assert.assertEquals("Must be first space id",
-//                this.spacesUuids.get(SPACE_NB - 1), space.getId());
-//
-//        String key = (String) model.get("key");
-//        Assert.assertNotNull("key is not null", key);
-//
-//        String salt = (String) model.get("salt");
-//        Assert.assertNotNull("salt is not null", salt);
-//
-//        Assert.assertTrue("Key must be checked", space.checkKey(key, salt));
-//
-//    }
-//
-//    @Test
-//    public void testDeleteFakeSpace() throws Exception {
-//
-//        MockHttpServletRequestBuilder get = MockMvcRequestBuilders
-//                .get("/admin/spaces/delete/" + UUID.randomUUID().toString());
-//
-//        try {
-//            this.mvc.perform(get).andExpect(
-//                    MockMvcResultMatchers.status().isNotFound());
-//        } catch (NestedServletException e) {
-//            Assert.assertNotNull("Nested exception must not be null",
-//                    e.getCause());
-//            Assert.assertEquals(
-//                    "Inner exception must be a HttpClientErrorException",
-//                    HttpClientErrorException.class, e.getCause().getClass());
-//            Assert.assertEquals("Exception error status must be not found",
-//                    HttpStatus.NOT_FOUND,
-//                    ((HttpClientErrorException) e.getCause()).getStatusCode());
-//        }
-//
-//    }
-//
-//    @Test
-//    public void testDeleteSpaceProject() throws Exception {
-//
-//        MockHttpServletRequestBuilder get = MockMvcRequestBuilders
-//                .get("/admin/spaces/delete/" + this.spacesUuids.get(0));
-//
-//        try {
-//            this.mvc.perform(get).andExpect(
-//                    MockMvcResultMatchers.status().isBadRequest());
-//        } catch (NestedServletException e) {
-//            Assert.assertNotNull("Nested exception must not be null",
-//                    e.getCause());
-//            Assert.assertEquals(
-//                    "Inner exception must be a HttpClientErrorException",
-//                    HttpClientErrorException.class, e.getCause().getClass());
-//            Assert.assertEquals("Exception error status must be not found",
-//                    HttpStatus.BAD_REQUEST,
-//                    ((HttpClientErrorException) e.getCause()).getStatusCode());
-//        }
-//
-//    }
-//
-//    @Test
-//    public void testDoDeleteSpaceNoKey() throws Exception {
-//        MockHttpServletRequestBuilder post = MockMvcRequestBuilders
-//                .post("/admin/spaces/delete/"
-//                        + this.spacesUuids.get(SPACE_NB - 1));
-//
-//        try {
-//            this.mvc.perform(post).andExpect(
-//                    MockMvcResultMatchers.status().isBadRequest());
-//        } catch (NestedServletException e) {
-//            Assert.assertNotNull("Nested exception must not be null",
-//                    e.getCause());
-//            Assert.assertEquals(
-//                    "Inner exception must be a HttpClientErrorException",
-//                    HttpClientErrorException.class, e.getCause().getClass());
-//            Assert.assertEquals("Exception error status must be not found",
-//                    HttpStatus.BAD_REQUEST,
-//                    ((HttpClientErrorException) e.getCause()).getStatusCode());
-//        }
-//
-//        Assert.assertEquals("Must have same nb of space", SPACE_NB,
-//                this.spacesRepository.count());
-//
-//    }
-//
-//    @Test
-//    public void testDoDeleteSpace() throws Exception {
-//
-//        Space space = this.spacesList.get(this.spacesUuids.get(SPACE_NB - 1));
-//
-//        SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
-//        rand.setSeed(System.currentTimeMillis());
-//        byte[] rawSalt = new byte[50];
-//        rand.nextBytes(rawSalt);
-//        String salt = Hex.encodeHexString(rawSalt);
-//        String key = space.getKey(salt);
-//
-//        MockHttpServletRequestBuilder post = MockMvcRequestBuilders.post(String
-//                .format("/admin/spaces/delete/%s?key=%s&salt=%s",
-//                        this.spacesUuids.get(SPACE_NB - 1), key, salt));
-//
-//        this.mvc.perform(post)
-//                .andExpect(MockMvcResultMatchers.status().isSeeOther())
-//                .andExpect(MockMvcResultMatchers.redirectedUrl("/admin/spaces"));
-//
-//        Assert.assertEquals("Must have one less space", SPACE_NB - 1,
-//                this.spacesRepository.count());
-//
-//        space = this.spacesRepository.findOne(this.spacesUuids
-//                .get(SPACE_NB - 1));
-//
-//        Assert.assertNull("Space " + this.spacesUuids.get(SPACE_NB - 1)
-//                + " deleted", space);
-//
-//    }
-//
-//    @Test
-//    public void testDoDeleteSpaceFake() throws Exception {
-//
-//        Space space = this.spacesList.get(this.spacesUuids.get(SPACE_NB - 1));
-//
-//        SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
-//        rand.setSeed(System.currentTimeMillis());
-//        byte[] rawSalt = new byte[50];
-//        rand.nextBytes(rawSalt);
-//        String salt = Hex.encodeHexString(rawSalt);
-//        String key = space.getKey(salt);
-//
-//        MockHttpServletRequestBuilder post = MockMvcRequestBuilders.post(String
-//                .format("/admin/spaces/delete/%s?key=%s&salt=%s",
-//                        UUID.randomUUID(), key, salt));
-//
-//        this.mvc.perform(post)
-//                .andExpect(MockMvcResultMatchers.status().isSeeOther())
-//                .andExpect(MockMvcResultMatchers.redirectedUrl("/admin/spaces"));
-//
-//        Assert.assertEquals("Must have the same nb of space", SPACE_NB,
-//                this.spacesRepository.count());
-//
-//    }
-//
-//    @Test
-//    public void testDoDeleteSpaceProject() throws Exception {
-//
-//        Space space = this.spacesList.get(this.spacesUuids.get(0));
-//
-//        SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
-//        rand.setSeed(System.currentTimeMillis());
-//        byte[] rawSalt = new byte[50];
-//        rand.nextBytes(rawSalt);
-//        String salt = Hex.encodeHexString(rawSalt);
-//        String key = space.getKey(salt);
-//
-//        MockHttpServletRequestBuilder post = MockMvcRequestBuilders.post(String
-//                .format("/admin/spaces/delete/%s?key=%s&salt=%s",
-//                        this.spacesUuids.get(0), key, salt));
-//
-//        try {
-//            this.mvc.perform(post).andExpect(
-//                    MockMvcResultMatchers.status().isBadRequest());
-//        } catch (NestedServletException e) {
-//            Assert.assertNotNull("Nested exception must not be null",
-//                    e.getCause());
-//            Assert.assertEquals(
-//                    "Inner exception must be a HttpClientErrorException",
-//                    HttpClientErrorException.class, e.getCause().getClass());
-//            Assert.assertEquals("Exception error status must be not found",
-//                    HttpStatus.BAD_REQUEST,
-//                    ((HttpClientErrorException) e.getCause()).getStatusCode());
-//        }
-//
-//        Assert.assertEquals("Must have the same nb of space", SPACE_NB,
-//                this.spacesRepository.count());
-//
-//    }
+    @Test
+    public void testDeleteUser() throws Exception {
+
+        MockHttpServletRequestBuilder get = MockMvcRequestBuilders
+                .get("/admin/users/delete/"
+                        + this.usersUuids.get(USER_NB - 1));
+
+        MvcResult res = this.mvc
+                .perform(get)
+                .andExpect(MockMvcResultMatchers.status().isOk())
+                .andExpect(
+                        MockMvcResultMatchers.view().name(
+                                "admin/userDeleteConfirm"))
+                .andExpect(
+                        MockMvcResultMatchers.model().attributeExists(
+                                "userObj", "key", "salt")).andReturn();
+
+        Map<String, Object> model = res.getModelAndView().getModel();
+
+        User user = (User) model.get("userObj");
+        Assert.assertNotNull("User is not null", user);
+        Assert.assertEquals("Must be first user id",
+                this.usersUuids.get(USER_NB - 1), user.getId());
+
+        String key = (String) model.get("key");
+        Assert.assertNotNull("key is not null", key);
+
+        String salt = (String) model.get("salt");
+        Assert.assertNotNull("salt is not null", salt);
+
+        Assert.assertTrue("Key must be checked", user.checkKey(key, salt));
+
+    }
+
+    @Test
+    public void testDeleteFakeUser() throws Exception {
+
+        MockHttpServletRequestBuilder get = MockMvcRequestBuilders
+                .get("/admin/users/delete/" + UUID.randomUUID().toString());
+
+        try {
+            this.mvc.perform(get).andExpect(
+                    MockMvcResultMatchers.status().isNotFound());
+        } catch (NestedServletException e) {
+            Assert.assertNotNull("Nested exception must not be null",
+                    e.getCause());
+            Assert.assertEquals(
+                    "Inner exception must be a HttpClientErrorException",
+                    HttpClientErrorException.class, e.getCause().getClass());
+            Assert.assertEquals("Exception error status must be not found",
+                    HttpStatus.NOT_FOUND,
+                    ((HttpClientErrorException) e.getCause()).getStatusCode());
+        }
+
+    }
+
+    @Test
+    public void testDeleteUserProject() throws Exception {
+
+        MockHttpServletRequestBuilder get = MockMvcRequestBuilders
+                .get("/admin/users/delete/" + this.usersUuids.get(0));
+
+        try {
+            this.mvc.perform(get).andExpect(
+                    MockMvcResultMatchers.status().isBadRequest());
+        } catch (NestedServletException e) {
+            Assert.assertNotNull("Nested exception must not be null",
+                    e.getCause());
+            Assert.assertEquals(
+                    "Inner exception must be a HttpClientErrorException",
+                    HttpClientErrorException.class, e.getCause().getClass());
+            Assert.assertEquals("Exception error status must be not found",
+                    HttpStatus.BAD_REQUEST,
+                    ((HttpClientErrorException) e.getCause()).getStatusCode());
+        }
+
+    }
+
+    @Test
+    public void testDoDeleteUserNoKey() throws Exception {
+        MockHttpServletRequestBuilder post = MockMvcRequestBuilders
+                .post("/admin/users/delete/"
+                        + this.usersUuids.get(USER_NB - 1));
+
+        try {
+            this.mvc.perform(post).andExpect(
+                    MockMvcResultMatchers.status().isBadRequest());
+        } catch (NestedServletException e) {
+            Assert.assertNotNull("Nested exception must not be null",
+                    e.getCause());
+            Assert.assertEquals(
+                    "Inner exception must be a HttpClientErrorException",
+                    HttpClientErrorException.class, e.getCause().getClass());
+            Assert.assertEquals("Exception error status must be not found",
+                    HttpStatus.BAD_REQUEST,
+                    ((HttpClientErrorException) e.getCause()).getStatusCode());
+        }
+
+        Assert.assertEquals("Must have same nb of user", USER_NB,
+                this.usersRepository.count());
+
+    }
+
+    @Test
+    public void testDoDeleteUser() throws Exception {
+
+        User user = this.usersList.get(this.usersUuids.get(USER_NB - 1));
+
+        SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
+        rand.setSeed(System.currentTimeMillis());
+        byte[] rawSalt = new byte[50];
+        rand.nextBytes(rawSalt);
+        String salt = Hex.encodeHexString(rawSalt);
+        String key = user.getKey(salt);
+
+        MockHttpServletRequestBuilder post = MockMvcRequestBuilders.post(String
+                .format("/admin/users/delete/%s?key=%s&salt=%s",
+                        this.usersUuids.get(USER_NB - 1), key, salt));
+
+        this.mvc.perform(post)
+                .andExpect(MockMvcResultMatchers.status().isSeeOther())
+                .andExpect(MockMvcResultMatchers.redirectedUrl("/admin/users"));
+
+        Assert.assertEquals("Must have one less user", USER_NB - 1,
+                this.usersRepository.count());
+
+        user = this.usersRepository.findOne(this.usersUuids
+                .get(USER_NB - 1));
+
+        Assert.assertNull("User " + this.usersUuids.get(USER_NB - 1)
+                + " deleted", user);
+
+    }
+
+    @Test
+    public void testDoDeleteSpaceFake() throws Exception {
+
+        User user = this.usersList.get(this.usersUuids.get(USER_NB - 1));
+
+        SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
+        rand.setSeed(System.currentTimeMillis());
+        byte[] rawSalt = new byte[50];
+        rand.nextBytes(rawSalt);
+        String salt = Hex.encodeHexString(rawSalt);
+        String key = user.getKey(salt);
+
+        MockHttpServletRequestBuilder post = MockMvcRequestBuilders.post(String
+                .format("/admin/users/delete/%s?key=%s&salt=%s",
+                        UUID.randomUUID(), key, salt));
+
+        this.mvc.perform(post)
+                .andExpect(MockMvcResultMatchers.status().isSeeOther())
+                .andExpect(MockMvcResultMatchers.redirectedUrl("/admin/users"));
+
+        Assert.assertEquals("Must have the same nb of user", USER_NB,
+                this.usersRepository.count());
+
+    }
+
+    @Test
+    public void testDoDeleteUserProject() throws Exception {
+
+        User user = this.usersList.get(this.usersUuids.get(0));
+
+        SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
+        rand.setSeed(System.currentTimeMillis());
+        byte[] rawSalt = new byte[50];
+        rand.nextBytes(rawSalt);
+        String salt = Hex.encodeHexString(rawSalt);
+        String key = user.getKey(salt);
+
+        MockHttpServletRequestBuilder post = MockMvcRequestBuilders.post(String
+                .format("/admin/users/delete/%s?key=%s&salt=%s",
+                        this.usersUuids.get(0), key, salt));
+
+        try {
+            this.mvc.perform(post).andExpect(
+                    MockMvcResultMatchers.status().isBadRequest());
+        } catch (NestedServletException e) {
+            Assert.assertNotNull("Nested exception must not be null",
+                    e.getCause());
+            Assert.assertEquals(
+                    "Inner exception must be a HttpClientErrorException",
+                    HttpClientErrorException.class, e.getCause().getClass());
+            Assert.assertEquals("Exception error status must be not found",
+                    HttpStatus.BAD_REQUEST,
+                    ((HttpClientErrorException) e.getCause()).getStatusCode());
+        }
+
+        Assert.assertEquals("Must have the same nb of user", USER_NB,
+                this.usersRepository.count());
+
+    }
 
 }