comparison Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs @ 269:17694f70cfb6

Test de PersonaController.
author juanjose.montesdeocaarbos
date Tue, 25 Oct 2011 23:24:57 -0300
parents 3339d1da6a63
children 3bb779434ae8
comparison
equal deleted inserted replaced
268:d255b0621503 269:17694f70cfb6
1 using System; 1 using System.Linq;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc; 2 using System.Web.Mvc;
6 using Agendas.NHibernate; 3 using Agendas.NHibernate;
7 using Agendas.Repositories.Tests.Infraestructure; 4 using Agendas.Repositories.Tests.Infraestructure;
8 using AltNetHispano.Agendas.Domain; 5 using AltNetHispano.Agendas.Domain;
9 using AltNetHispano.Agendas.Repositories.NHibernate; 6 using AltNetHispano.Agendas.Repositories.NHibernate;
22 var seguridad = new Mock<ISeguridad>(); 19 var seguridad = new Mock<ISeguridad>();
23 seguridad.Setup(s => s.GetUserName()).Returns("neluz"); 20 seguridad.Setup(s => s.GetUserName()).Returns("neluz");
24 IdentityContext.Init(seguridad.Object, new PersonaRepository(NhHelper.GetSessionFactory())); 21 IdentityContext.Init(seguridad.Object, new PersonaRepository(NhHelper.GetSessionFactory()));
25 } 22 }
26 23
24 private void PersonaNueva(PersonaController controller)
25 {
26 var personaNew = new PersonaNewModel
27 {
28 Blog = "http://juanjose.montesdeocaarbos.com.ar/blog/",
29 EMail = "juanjose.montesdeocaarbos@gmail.com",
30 Nombre = "Juan José Montes de Oca Arbós",
31 Roles = new string[] { "Administrador", "Usuario" },
32 Twitter = "jjmoa"
33 };
34
35 using (new RequestEmulator(NhHelper.GetSessionFactory()))
36 {
37 controller.Nueva(personaNew);
38 }
39
40 personaNew = new PersonaNewModel
41 {
42 Blog = "http://nelopauselli.blogspot.com/",
43 EMail = "nelo@gmail.com",
44 Nombre = "Nelo Mariano Pauselli",
45 Roles = new string[] { "Administrador", "Usuario" },
46 Twitter = "npauselli"
47 };
48
49 using (new RequestEmulator(NhHelper.GetSessionFactory()))
50 {
51 controller.Nueva(personaNew);
52 }
53 }
54
27 [SetUp] 55 [SetUp]
28 public void Setup() 56 public void Setup()
29 { 57 {
30 NhHelperTest.CleanDb(); 58 NhHelperTest.CleanDb();
31 SetearUsuario(); 59 SetearUsuario();
32 } 60 }
33 61
34 [Test] 62 [Test]
35 public void PersonaNueva_ErrorAlGuardarTwitterConArroba() 63 public void PersonaNuevaGET()
64 {
65 var personaController = new PersonaController { ControllerContext = ControllerCtx };
66 using (new RequestEmulator(NhHelper.GetSessionFactory()))
67 {
68 var resultNuevo = personaController.Nueva();
69 Assert.IsInstanceOf(typeof(ViewResult), resultNuevo);
70 Assert.AreEqual("Defaulteditor", ((ViewResult)resultNuevo).ViewName);
71 }
72 }
73
74 [Test]
75 public void PersonaModificarGET()
76 {
77 var personaController = new PersonaController { ControllerContext = ControllerCtx };
78 PersonaNueva(personaController);
79
80 using (new RequestEmulator(NhHelper.GetSessionFactory()))
81 {
82 var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory());
83
84 var resultModificar = personaController.Modificar(personaRepository.GetAll().FirstOrDefault().Id.ToString());
85 Assert.IsInstanceOf(typeof(ViewResult), resultModificar);
86 Assert.AreEqual("Defaulteditor", ((ViewResult)resultModificar).ViewName);
87 }
88 }
89
90 [Test]
91 public void PersonaModificarGET_PersonaInexistente()
92 {
93 var personaController = new PersonaController { ControllerContext = ControllerCtx };
94
95 using (new RequestEmulator(NhHelper.GetSessionFactory()))
96 {
97 var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory());
98
99 var resultModificar = personaController.Modificar("11111111-1111-1111-1111-111111111111");
100 Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultModificar);
101 Assert.AreEqual("Index", ((RedirectToRouteResult)resultModificar).RouteValues["action"]);
102 Assert.AreEqual("No se encontró la persona que intenta modificar", personaController.TempData["error"]);
103 }
104 }
105
106 [Test]
107 public void PersonaNuevaPOST_RequiredFields()
36 { 108 {
37 var personaController = new PersonaController { ControllerContext = ControllerCtx }; 109 var personaController = new PersonaController { ControllerContext = ControllerCtx };
38 110
39 var form = new FormCollection 111 var form = new FormCollection
40 { 112 {
41 {"Blog", "http://juanjose.montesdeocaarbos.com.ar/blog/"}, 113 {"Blog", ""},
114 {"EMail", ""},
115 {"Nombre", ""},
116 {"Roles", "Administrador"},
117 {"Roles", "Usuario"},
118 {"Twitter", ""}
119 };
120 var personaNew = BindModel<PersonaNewModel>(personaController, form);
121
122 using (new RequestEmulator(NhHelper.GetSessionFactory()))
123 {
124 var resultNuevo = personaController.Nueva(personaNew);
125 Assert.IsInstanceOf(typeof(ViewResult), resultNuevo);
126 var viewResultNuevo = (ViewResult) resultNuevo;
127 Assert.AreEqual("Defaulteditor", viewResultNuevo.ViewName);
128 Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["EMail"].Errors.Count);
129 Assert.AreEqual("El campo EMail es obligatorio.", viewResultNuevo.ViewData.ModelState["EMail"].Errors[0].ErrorMessage);
130 Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Nombre"].Errors.Count);
131 Assert.AreEqual("El campo Nombre es obligatorio.", viewResultNuevo.ViewData.ModelState["Nombre"].Errors[0].ErrorMessage);
132 Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Twitter"].Errors.Count);
133 Assert.AreEqual("El campo Twitter es obligatorio.", viewResultNuevo.ViewData.ModelState["Twitter"].Errors[0].ErrorMessage);
134 }
135 }
136
137 [Test]
138 public void PersonaNuevaPOST_FormatFields()
139 {
140 var personaController = new PersonaController { ControllerContext = ControllerCtx };
141
142 var form = new FormCollection
143 {
144 {"Blog", "montesdeocaarbos.com.ar/blog/"},
42 {"EMail", "juanjose.montesdeocaarbos gmail.com"}, 145 {"EMail", "juanjose.montesdeocaarbos gmail.com"},
43 {"Nombre", "Juan José Montes de Oca Arbós"}, 146 {"Nombre", "Juan José Montes de Oca Arbós"},
44 {"Roles", "Administrador"}, 147 {"Roles", "Administrador"},
45 {"Roles", "Usuario"}, 148 {"Roles", "Usuario"},
46 {"Twitter", "@jjmoa"} 149 {"Twitter", "@jjmoa"}
48 var personaNew = BindModel<PersonaNewModel>(personaController, form); 151 var personaNew = BindModel<PersonaNewModel>(personaController, form);
49 152
50 using (new RequestEmulator(NhHelper.GetSessionFactory())) 153 using (new RequestEmulator(NhHelper.GetSessionFactory()))
51 { 154 {
52 var resultNuevo = personaController.Nueva(personaNew); 155 var resultNuevo = personaController.Nueva(personaNew);
53 Assert.IsInstanceOf(typeof (ViewResult), resultNuevo); 156 Assert.IsInstanceOf(typeof(ViewResult), resultNuevo);
54 Assert.AreEqual("Defaulteditor", ((ViewResult) resultNuevo).ViewName); 157 var viewResultNuevo = (ViewResult)resultNuevo;
55 Assert.IsFalse(personaController.TempData.ContainsKey("error")); 158 Assert.AreEqual("Defaulteditor", viewResultNuevo.ViewName);
56 } 159 Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Twitter"].Errors.Count);
57 } 160 Assert.AreEqual("No debe ingresar el arroba al escribir la cuenta de twitter.", viewResultNuevo.ViewData.ModelState["Twitter"].Errors[0].ErrorMessage);
58 161 Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Blog"].Errors.Count);
59 [Test] 162 Assert.AreEqual("The Blog field is not a valid fully-qualified http, https, or ftp URL.", viewResultNuevo.ViewData.ModelState["Blog"].Errors[0].ErrorMessage);
60 public void PersonaModificar_ErrorAlGuardarTwitterConArroba() 163 Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["EMail"].Errors.Count);
61 { 164 Assert.AreEqual("The EMail field is not a valid e-mail address.", viewResultNuevo.ViewData.ModelState["EMail"].Errors[0].ErrorMessage);
62 var personaController = new PersonaController {ControllerContext = ControllerCtx}; 165 }
63 166 }
64 #region PersonaNew 167
65 var personaNew = new PersonaNewModel 168 [Test]
66 { 169 public void PersonaNuevaPOST_PersonaExistente()
67 Blog = "http://juanjose.montesdeocaarbos.com.ar/blog/", 170 {
68 EMail = "juanjose.montesdeocaarbos@gmail.com", 171 var personaController = new PersonaController { ControllerContext = ControllerCtx };
69 Nombre = "Juan José Montes de Oca Arbós", 172 PersonaNueva(personaController);
70 Roles = new string[] {"Administrador", "Usuario"}, 173
71 Twitter = "jjmoa" 174 var form = new FormCollection
72 }; 175 {
73 176 {"Blog", "http://montesdeocaarbos.com.ar/blog/"},
74 using (new RequestEmulator(NhHelper.GetSessionFactory())) 177 {"EMail", "juanjose.montesdeocaarbos@gmail.com"},
75 { 178 {"Nombre", "Juan José Montes de Oca Arbós"},
76 personaController.Nueva(personaNew); 179 {"Roles", "Administrador"},
77 } 180 {"Roles", "Usuario"},
78 #endregion 181 {"Twitter", "jjmoa"}
182 };
183 var personaNew = BindModel<PersonaNewModel>(personaController, form);
184
185 using (new RequestEmulator(NhHelper.GetSessionFactory()))
186 {
187 var resultNuevo = personaController.Nueva(personaNew);
188 Assert.IsInstanceOf(typeof(ViewResult), resultNuevo);
189 var viewResultNuevo = (ViewResult)resultNuevo;
190 Assert.AreEqual("Defaulteditor", viewResultNuevo.ViewName);
191 Assert.AreEqual("Ya existe una persona con el nombre " + personaNew.Nombre, personaController.TempData["error"]);
192 }
193 }
194
195 [Test]
196 public void PersonaModificarPOST_RequiredFields()
197 {
198 var personaController = new PersonaController { ControllerContext = ControllerCtx };
199 PersonaNueva(personaController);
200
201 PersonaEditModel personaEdit = null;
202 using (new RequestEmulator(NhHelper.GetSessionFactory()))
203 {
204 var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory());
205
206 var form = new FormCollection
207 {
208 {"Id", ""},
209 {"Blog", ""},
210 {"EMail", ""},
211 {"Nombre", ""},
212 {"Roles", "Administrador"},
213 {"Roles", "Usuario"},
214 {"Twitter", ""}
215 };
216 personaEdit = BindModel<PersonaEditModel>(personaController, form);
217 }
218
219 using (new RequestEmulator(NhHelper.GetSessionFactory()))
220 {
221 var resultNuevo = personaController.Modificar(personaEdit);
222 Assert.IsInstanceOf(typeof(ViewResult), resultNuevo);
223 var viewResultNuevo = (ViewResult)resultNuevo;
224 Assert.AreEqual("Defaulteditor", viewResultNuevo.ViewName);
225 Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Id"].Errors.Count);
226 Assert.AreEqual("El campo Id es obligatorio.", viewResultNuevo.ViewData.ModelState["Id"].Errors[0].ErrorMessage);
227 Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["EMail"].Errors.Count);
228 Assert.AreEqual("El campo EMail es obligatorio.", viewResultNuevo.ViewData.ModelState["EMail"].Errors[0].ErrorMessage);
229 Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Nombre"].Errors.Count);
230 Assert.AreEqual("El campo Nombre es obligatorio.", viewResultNuevo.ViewData.ModelState["Nombre"].Errors[0].ErrorMessage);
231 Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Twitter"].Errors.Count);
232 Assert.AreEqual("El campo Twitter es obligatorio.", viewResultNuevo.ViewData.ModelState["Twitter"].Errors[0].ErrorMessage);
233 }
234 }
235
236 [Test]
237 public void PersonaModificarPOST_FormatFields()
238 {
239 var personaController = new PersonaController { ControllerContext = ControllerCtx };
240 PersonaNueva(personaController);
79 241
80 PersonaEditModel personaEdit = null; 242 PersonaEditModel personaEdit = null;
81 using (new RequestEmulator(NhHelper.GetSessionFactory())) 243 using (new RequestEmulator(NhHelper.GetSessionFactory()))
82 { 244 {
83 var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory()); 245 var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory());
84 246
85 var form = new FormCollection 247 var form = new FormCollection
86 { 248 {
87 {"Id", personaRepository.GetAll().FirstOrDefault().Id.ToString()}, 249 {"Id", personaRepository.GetAll().FirstOrDefault().Id.ToString()},
88 {"Blog", "http://juanjose.montesdeocaarbos.com.ar/blog/"}, 250 {"Blog", "montesdeocaarbos.com.ar/blog/"},
89 {"EMail", "juanjose.montesdeocaarbos gmail.com"}, 251 {"EMail", "juanjose.montesdeocaarbos gmail.com"},
90 {"Nombre", "Juan José Montes de Oca Arbós"}, 252 {"Nombre", "Juan José Montes de Oca Arbós"},
91 {"Roles", "Administrador"}, 253 {"Roles", "Administrador"},
92 {"Roles", "Usuario"}, 254 {"Roles", "Usuario"},
93 {"Twitter", "@jjmoa"} 255 {"Twitter", "@jjmoa"}
94 }; 256 };
95 personaEdit = BindModel<PersonaEditModel>(personaController, form); 257 personaEdit = BindModel<PersonaEditModel>(personaController, form);
96 } 258 }
97 259
98 using (new RequestEmulator(NhHelper.GetSessionFactory())) 260 using (new RequestEmulator(NhHelper.GetSessionFactory()))
99 { 261 {
100 var resultModificar = personaController.Modificar(personaEdit); 262 var resultModificar = personaController.Modificar(personaEdit);
101 Assert.IsInstanceOf(typeof (ViewResult), resultModificar); 263 Assert.IsInstanceOf(typeof(ViewResult), resultModificar);
102 Assert.AreEqual("Defaulteditor", ((ViewResult) resultModificar).ViewName); 264 var viewResultModificar = (ViewResult)resultModificar;
103 Assert.IsFalse(personaController.TempData.ContainsKey("error")); 265 Assert.AreEqual("Defaulteditor", viewResultModificar.ViewName);
266 Assert.AreEqual(1, viewResultModificar.ViewData.ModelState["Twitter"].Errors.Count);
267 Assert.AreEqual("No debe ingresar el arroba al escribir la cuenta de twitter.", viewResultModificar.ViewData.ModelState["Twitter"].Errors[0].ErrorMessage);
268 Assert.AreEqual(1, viewResultModificar.ViewData.ModelState["Blog"].Errors.Count);
269 Assert.AreEqual("The Blog field is not a valid fully-qualified http, https, or ftp URL.", viewResultModificar.ViewData.ModelState["Blog"].Errors[0].ErrorMessage);
270 Assert.AreEqual(1, viewResultModificar.ViewData.ModelState["EMail"].Errors.Count);
271 Assert.AreEqual("The EMail field is not a valid e-mail address.", viewResultModificar.ViewData.ModelState["EMail"].Errors[0].ErrorMessage);
272 }
273 }
274
275 [Test]
276 public void PersonaModificarPOST_PersonaExistente()
277 {
278 var personaController = new PersonaController { ControllerContext = ControllerCtx };
279 PersonaNueva(personaController);
280
281 PersonaEditModel personaEdit = null;
282 using (new RequestEmulator(NhHelper.GetSessionFactory()))
283 {
284 var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory());
285
286 var form = new FormCollection
287 {
288 {"Id", personaRepository.GetAll().FirstOrDefault().Id.ToString()},
289 {"Blog", "http://nelopauselli.blogspot.com/"},
290 {"EMail", "nelo@gmail.com"},
291 {"Nombre", "Nelo Mariano Pauselli"},
292 {"Roles", "Administrador"},
293 {"Roles", "Usuario"},
294 {"Twitter", "npauselli"}
295 };
296 personaEdit = BindModel<PersonaEditModel>(personaController, form);
297 }
298
299 using (new RequestEmulator(NhHelper.GetSessionFactory()))
300 {
301 var resultEdit = personaController.Modificar(personaEdit);
302 Assert.IsInstanceOf(typeof(ViewResult), resultEdit);
303 var viewResultNuevo = (ViewResult)resultEdit;
304 Assert.AreEqual("Defaulteditor", viewResultNuevo.ViewName);
305 Assert.AreEqual("Ya existe una persona con el twitter " + personaEdit.Twitter, personaController.TempData["error"]);
306 }
307 }
308
309 [Test]
310 public void PersonaModificarPOST_Successful()
311 {
312 var personaController = new PersonaController { ControllerContext = ControllerCtx };
313 PersonaNueva(personaController);
314
315 PersonaEditModel personaEdit = null;
316 using (new RequestEmulator(NhHelper.GetSessionFactory()))
317 {
318 var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory());
319
320 var form = new FormCollection
321 {
322 {"Id", personaRepository.GetAll().FirstOrDefault().Id.ToString()},
323 {"Blog", "http://montesdeocaarbos.com.ar/blog/"},
324 {"EMail", "juanjose.montesdeocaarbos@gmail.com"},
325 {"Nombre", "Juan José Montes de Oca Arbós"},
326 {"Roles", "Administrador"},
327 {"Roles", "Usuario"},
328 {"Twitter", "jjmoa"}
329 };
330 personaEdit = BindModel<PersonaEditModel>(personaController, form);
331 }
332
333 using (new RequestEmulator(NhHelper.GetSessionFactory()))
334 {
335 var resultModificar = personaController.Modificar(personaEdit);
336 Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultModificar);
337 Assert.AreEqual("Index", ((RedirectToRouteResult)resultModificar).RouteValues["action"]);
104 } 338 }
105 } 339 }
106 } 340 }
107 } 341 }