Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebApplication/MVC2/plugins/HLWebHelloWorldPlugin.Tests/Controllers/AccountControllerTest.cs @ 4606

Last change on this file since 4606 was 4605, checked in by dkahn, 14 years ago

#1198 Added Hello World demo plugin

File size: 13.8 KB
Line 
1using System;
2using System.Security.Principal;
3using System.Web;
4using System.Web.Mvc;
5using System.Web.Routing;
6using System.Web.Security;
7using Microsoft.VisualStudio.TestTools.UnitTesting;
8using HLWebHelloWorldPlugin;
9using HLWebHelloWorldPlugin.Controllers;
10using HLWebHelloWorldPlugin.Models;
11
12namespace HLWebHelloWorldPlugin.Tests.Controllers {
13
14    [TestClass]
15    public class AccountControllerTest {
16
17        [TestMethod]
18        public void ChangePassword_Get_ReturnsView() {
19            // Arrange
20            AccountController controller = GetAccountController();
21
22            // Act
23            ActionResult result = controller.ChangePassword();
24
25            // Assert
26            Assert.IsInstanceOfType(result, typeof(ViewResult));
27            Assert.AreEqual(10, ((ViewResult)result).ViewData["PasswordLength"]);
28        }
29
30        [TestMethod]
31        public void ChangePassword_Post_ReturnsRedirectOnSuccess() {
32            // Arrange
33            AccountController controller = GetAccountController();
34            ChangePasswordModel model = new ChangePasswordModel() {
35                OldPassword = "goodOldPassword",
36                NewPassword = "goodNewPassword",
37                ConfirmPassword = "goodNewPassword"
38            };
39
40            // Act
41            ActionResult result = controller.ChangePassword(model);
42
43            // Assert
44            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
45            RedirectToRouteResult redirectResult = (RedirectToRouteResult)result;
46            Assert.AreEqual("ChangePasswordSuccess", redirectResult.RouteValues["action"]);
47        }
48
49        [TestMethod]
50        public void ChangePassword_Post_ReturnsViewIfChangePasswordFails() {
51            // Arrange
52            AccountController controller = GetAccountController();
53            ChangePasswordModel model = new ChangePasswordModel() {
54                OldPassword = "goodOldPassword",
55                NewPassword = "badNewPassword",
56                ConfirmPassword = "badNewPassword"
57            };
58
59            // Act
60            ActionResult result = controller.ChangePassword(model);
61
62            // Assert
63            Assert.IsInstanceOfType(result, typeof(ViewResult));
64            ViewResult viewResult = (ViewResult)result;
65            Assert.AreEqual(model, viewResult.ViewData.Model);
66            Assert.AreEqual("The current password is incorrect or the new password is invalid.", controller.ModelState[""].Errors[0].ErrorMessage);
67            Assert.AreEqual(10, viewResult.ViewData["PasswordLength"]);
68        }
69
70        [TestMethod]
71        public void ChangePassword_Post_ReturnsViewIfModelStateIsInvalid() {
72            // Arrange
73            AccountController controller = GetAccountController();
74            ChangePasswordModel model = new ChangePasswordModel() {
75                OldPassword = "goodOldPassword",
76                NewPassword = "goodNewPassword",
77                ConfirmPassword = "goodNewPassword"
78            };
79            controller.ModelState.AddModelError("", "Dummy error message.");
80
81            // Act
82            ActionResult result = controller.ChangePassword(model);
83
84            // Assert
85            Assert.IsInstanceOfType(result, typeof(ViewResult));
86            ViewResult viewResult = (ViewResult)result;
87            Assert.AreEqual(model, viewResult.ViewData.Model);
88            Assert.AreEqual(10, viewResult.ViewData["PasswordLength"]);
89        }
90
91        [TestMethod]
92        public void ChangePasswordSuccess_ReturnsView() {
93            // Arrange
94            AccountController controller = GetAccountController();
95
96            // Act
97            ActionResult result = controller.ChangePasswordSuccess();
98
99            // Assert
100            Assert.IsInstanceOfType(result, typeof(ViewResult));
101        }
102
103        [TestMethod]
104        public void LogOff_LogsOutAndRedirects() {
105            // Arrange
106            AccountController controller = GetAccountController();
107
108            // Act
109            ActionResult result = controller.LogOff();
110
111            // Assert
112            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
113            RedirectToRouteResult redirectResult = (RedirectToRouteResult)result;
114            Assert.AreEqual("Home", redirectResult.RouteValues["controller"]);
115            Assert.AreEqual("Index", redirectResult.RouteValues["action"]);
116            Assert.IsTrue(((MockFormsAuthenticationService)controller.FormsService).SignOut_WasCalled);
117        }
118
119        [TestMethod]
120        public void LogOn_Get_ReturnsView() {
121            // Arrange
122            AccountController controller = GetAccountController();
123
124            // Act
125            ActionResult result = controller.LogOn();
126
127            // Assert
128            Assert.IsInstanceOfType(result, typeof(ViewResult));
129        }
130
131        [TestMethod]
132        public void LogOn_Post_ReturnsRedirectOnSuccess_WithoutReturnUrl() {
133            // Arrange
134            AccountController controller = GetAccountController();
135            LogOnModel model = new LogOnModel() {
136                UserName = "someUser",
137                Password = "goodPassword",
138                RememberMe = false
139            };
140
141            // Act
142            ActionResult result = controller.LogOn(model, null);
143
144            // Assert
145            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
146            RedirectToRouteResult redirectResult = (RedirectToRouteResult)result;
147            Assert.AreEqual("Home", redirectResult.RouteValues["controller"]);
148            Assert.AreEqual("Index", redirectResult.RouteValues["action"]);
149            Assert.IsTrue(((MockFormsAuthenticationService)controller.FormsService).SignIn_WasCalled);
150        }
151
152        [TestMethod]
153        public void LogOn_Post_ReturnsRedirectOnSuccess_WithReturnUrl() {
154            // Arrange
155            AccountController controller = GetAccountController();
156            LogOnModel model = new LogOnModel() {
157                UserName = "someUser",
158                Password = "goodPassword",
159                RememberMe = false
160            };
161
162            // Act
163            ActionResult result = controller.LogOn(model, "/someUrl");
164
165            // Assert
166            Assert.IsInstanceOfType(result, typeof(RedirectResult));
167            RedirectResult redirectResult = (RedirectResult)result;
168            Assert.AreEqual("/someUrl", redirectResult.Url);
169            Assert.IsTrue(((MockFormsAuthenticationService)controller.FormsService).SignIn_WasCalled);
170        }
171
172        [TestMethod]
173        public void LogOn_Post_ReturnsViewIfModelStateIsInvalid() {
174            // Arrange
175            AccountController controller = GetAccountController();
176            LogOnModel model = new LogOnModel() {
177                UserName = "someUser",
178                Password = "goodPassword",
179                RememberMe = false
180            };
181            controller.ModelState.AddModelError("", "Dummy error message.");
182
183            // Act
184            ActionResult result = controller.LogOn(model, null);
185
186            // Assert
187            Assert.IsInstanceOfType(result, typeof(ViewResult));
188            ViewResult viewResult = (ViewResult)result;
189            Assert.AreEqual(model, viewResult.ViewData.Model);
190        }
191
192        [TestMethod]
193        public void LogOn_Post_ReturnsViewIfValidateUserFails() {
194            // Arrange
195            AccountController controller = GetAccountController();
196            LogOnModel model = new LogOnModel() {
197                UserName = "someUser",
198                Password = "badPassword",
199                RememberMe = false
200            };
201
202            // Act
203            ActionResult result = controller.LogOn(model, null);
204
205            // Assert
206            Assert.IsInstanceOfType(result, typeof(ViewResult));
207            ViewResult viewResult = (ViewResult)result;
208            Assert.AreEqual(model, viewResult.ViewData.Model);
209            Assert.AreEqual("The user name or password provided is incorrect.", controller.ModelState[""].Errors[0].ErrorMessage);
210        }
211
212        [TestMethod]
213        public void Register_Get_ReturnsView() {
214            // Arrange
215            AccountController controller = GetAccountController();
216
217            // Act
218            ActionResult result = controller.Register();
219
220            // Assert
221            Assert.IsInstanceOfType(result, typeof(ViewResult));
222            Assert.AreEqual(10, ((ViewResult)result).ViewData["PasswordLength"]);
223        }
224
225        [TestMethod]
226        public void Register_Post_ReturnsRedirectOnSuccess() {
227            // Arrange
228            AccountController controller = GetAccountController();
229            RegisterModel model = new RegisterModel() {
230                UserName = "someUser",
231                Email = "goodEmail",
232                Password = "goodPassword",
233                ConfirmPassword = "goodPassword"
234            };
235
236            // Act
237            ActionResult result = controller.Register(model);
238
239            // Assert
240            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
241            RedirectToRouteResult redirectResult = (RedirectToRouteResult)result;
242            Assert.AreEqual("Home", redirectResult.RouteValues["controller"]);
243            Assert.AreEqual("Index", redirectResult.RouteValues["action"]);
244        }
245
246        [TestMethod]
247        public void Register_Post_ReturnsViewIfRegistrationFails() {
248            // Arrange
249            AccountController controller = GetAccountController();
250            RegisterModel model = new RegisterModel() {
251                UserName = "duplicateUser",
252                Email = "goodEmail",
253                Password = "goodPassword",
254                ConfirmPassword = "goodPassword"
255            };
256
257            // Act
258            ActionResult result = controller.Register(model);
259
260            // Assert
261            Assert.IsInstanceOfType(result, typeof(ViewResult));
262            ViewResult viewResult = (ViewResult)result;
263            Assert.AreEqual(model, viewResult.ViewData.Model);
264            Assert.AreEqual("Username already exists. Please enter a different user name.", controller.ModelState[""].Errors[0].ErrorMessage);
265            Assert.AreEqual(10, viewResult.ViewData["PasswordLength"]);
266        }
267
268        [TestMethod]
269        public void Register_Post_ReturnsViewIfModelStateIsInvalid() {
270            // Arrange
271            AccountController controller = GetAccountController();
272            RegisterModel model = new RegisterModel() {
273                UserName = "someUser",
274                Email = "goodEmail",
275                Password = "goodPassword",
276                ConfirmPassword = "goodPassword"
277            };
278            controller.ModelState.AddModelError("", "Dummy error message.");
279
280            // Act
281            ActionResult result = controller.Register(model);
282
283            // Assert
284            Assert.IsInstanceOfType(result, typeof(ViewResult));
285            ViewResult viewResult = (ViewResult)result;
286            Assert.AreEqual(model, viewResult.ViewData.Model);
287            Assert.AreEqual(10, viewResult.ViewData["PasswordLength"]);
288        }
289
290        private static AccountController GetAccountController() {
291            AccountController controller = new AccountController() {
292                FormsService = new MockFormsAuthenticationService(),
293                MembershipService = new MockMembershipService()
294            };
295            controller.ControllerContext = new ControllerContext() {
296                Controller = controller,
297                RequestContext = new RequestContext(new MockHttpContext(), new RouteData())
298            };
299            return controller;
300        }
301
302        private class MockFormsAuthenticationService : IFormsAuthenticationService {
303            public bool SignIn_WasCalled;
304            public bool SignOut_WasCalled;
305
306            public void SignIn(string userName, bool createPersistentCookie) {
307                // verify that the arguments are what we expected
308                Assert.AreEqual("someUser", userName);
309                Assert.IsFalse(createPersistentCookie);
310
311                SignIn_WasCalled = true;
312            }
313
314            public void SignOut() {
315                SignOut_WasCalled = true;
316            }
317        }
318
319        private class MockHttpContext : HttpContextBase {
320            private readonly IPrincipal _user = new GenericPrincipal(new GenericIdentity("someUser"), null /* roles */);
321
322            public override IPrincipal User {
323                get {
324                    return _user;
325                }
326                set {
327                    base.User = value;
328                }
329            }
330        }
331
332        private class MockMembershipService : IMembershipService {
333            public int MinPasswordLength {
334                get { return 10; }
335            }
336
337            public bool ValidateUser(string userName, string password) {
338                return (userName == "someUser" && password == "goodPassword");
339            }
340
341            public MembershipCreateStatus CreateUser(string userName, string password, string email) {
342                if (userName == "duplicateUser") {
343                    return MembershipCreateStatus.DuplicateUserName;
344                }
345
346                // verify that values are what we expected
347                Assert.AreEqual("goodPassword", password);
348                Assert.AreEqual("goodEmail", email);
349
350                return MembershipCreateStatus.Success;
351            }
352
353            public bool ChangePassword(string userName, string oldPassword, string newPassword) {
354                return (userName == "someUser" && oldPassword == "goodOldPassword" && newPassword == "goodNewPassword");
355            }
356        }
357
358    }
359}
Note: See TracBrowser for help on using the repository browser.