public ProfileControllerFixture() { currentUser = new User {UserId = 1}; formsAuthenticationMock = new Mock<IFormsAuthentication>(); userServicesMock = new Mock<IUserServices>(); userServicesMock .Setup(r => r.GetUserByClaimedIdentifier(It.IsAny<string>())) .Returns(currentUser); userServicesMock.Setup(r => r.UpdateUser(It.IsAny<User>())) .Callback((User u) => { modifiedUser = u; }).Verifiable(); countryServicesMock = new Mock<ICountryServices>(); countryServicesMock .Setup(r => r.GetCountriesAndRegionsList()) .Returns(() => new ReadOnlyCollection<KeyValuePair<string, string>>(new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("a", "a"), new KeyValuePair<string, string>("b", "b") })); profileController = new ProfileController( userServicesMock.Object, countryServicesMock.Object, formsAuthenticationMock.Object); profileController.SetFakeControllerContext(); profileController.SetUserIdentity(new MileageStatsIdentity("CleverClaimsId", "Test display name", 1)); }
public ProfileControllerFixture() { currentUser = new User {UserId = 1, Country = "argentina", DisplayName = "a display name"}; formsAuthenticationMock = new Mock<IFormsAuthentication>(); userServicesMock = new Mock<GetUserByClaimId>(null); updateUserMock = new Mock<UpdateUser>(null); userServicesMock .Setup(r => r.Execute(It.IsAny<string>())) .Returns(currentUser); updateUserMock.Setup(r => r.Execute(It.IsAny<User>())) .Callback((User u) => { modifiedUser = u; }).Verifiable(); countryServicesMock = new Mock<GetCountries>(null); countryServicesMock .Setup(r => r.Execute()) .Returns(() => new ReadOnlyCollection<string>(new List<string> {"a", "b"})); profileController = new ProfileController( updateUserMock.Object, userServicesMock.Object, countryServicesMock.Object, formsAuthenticationMock.Object); profileController.SetFakeControllerContext(); profileController.SetUserIdentity(new MileageStatsIdentity("CleverClaimsId", "Test display name", 1)); }
public void WhenCompletingRegistrationWithDifferentUser_ThenThrows() { var user = new User {UserId = 2}; var actualUser = new User {UserId = 1}; userServicesMock.Setup(r => r.GetUserByClaimedIdentifier(It.IsAny<string>())).Returns(actualUser); var controller = new ProfileController( userServicesMock.Object, countryServicesMock.Object, formsAuthenticationMock.Object); controller.SetFakeControllerContext(); controller.SetUserIdentity(new MileageStatsIdentity("TestName", "TestDisplayName", 1)); Assert.Throws<SecurityException>(() => { controller.CompleteRegistration(user); }); }
public void WhenCompletingRegistration_ThenUserUpdated() { var testUser = new User { UserId = 1, DisplayName = "DisplayName", PostalCode = "12345", TwoLetterCountryCode = "US" }; userServicesMock .Setup(r => r.GetUserByClaimedIdentifier(It.IsAny<string>())) .Returns(testUser); var controller = new ProfileController( userServicesMock.Object, countryServicesMock.Object, formsAuthenticationMock.Object); controller.SetFakeControllerContext(); controller.SetUserIdentity(new MileageStatsIdentity("TestName", "TestDisplayName", 1)); ActionResult response = controller.CompleteRegistration(testUser); userServicesMock .Verify(x => x.UpdateUser(testUser), Times.Once()); formsAuthenticationMock .Verify(x => x.SetAuthCookie(It.IsAny<HttpContextBase>(), It.IsAny<FormsAuthenticationTicket>()), Times.Once()); var result = (RedirectToRouteResult) response; Assert.Equal("Dashboard", result.RouteName); }
public void WhenCompletingRegistrationWithInvalidUser_ThenViewReturned() { var user = new User {UserId = 1, DisplayName = new string('X', 200)}; userServicesMock .Setup(r => r.GetUserByClaimedIdentifier(It.IsAny<string>())) .Returns(user); var controller = new ProfileController( userServicesMock.Object, countryServicesMock.Object, formsAuthenticationMock.Object); controller.SetFakeControllerContext(); controller.ModelState.AddModelError("DisplayName", "Too long"); controller.SetUserIdentity(new MileageStatsIdentity("TestName", "TestDisplayName", 1)); ActionResult actual = controller.CompleteRegistration(user); Assert.IsType<ViewResult>(actual); var viewResult = (ViewResult) actual; Assert.NotNull(viewResult.ViewBag.CountryList); var model = (User) viewResult.Model; Assert.NotNull(model); Assert.Same(user, model); }