public async Task<RegisterNewUserResult> RegisterUser(NewUser newUser) { ApplicationUser newApplicationUser = new ApplicationUser() { UserName = newUser.UserName, Email = newUser.EmailAddress, EmailConfirmed = true }; IdentityResult identityResult = await applicationUserManager.CreateAsync(newApplicationUser, newUser.Password); NewlyRegisteredUser newlyRegisteredUser = new NewlyRegisteredUser { UserId = newApplicationUser.Id }; if(identityResult.Succeeded) { newlyRegisteredUser = await this.SignInAndAssociateGamingGroup(newUser, newApplicationUser); } RegisterNewUserResult result = new RegisterNewUserResult { Result = identityResult, NewlyRegisteredUser = newlyRegisteredUser }; return result; }
private async Task<NewlyRegisteredUser> SignInAndAssociateGamingGroup(NewUser newUser, ApplicationUser newApplicationUser) { new Task(() => this.eventTracker.TrackUserRegistration(newUser.Source)).Start(); if (newUser.Source == TransactionSource.WebApplication) { await this.signInManager.SignInAsync(newApplicationUser, false, false); } NewlyRegisteredUser newlyRegisteredUser; if (newUser.GamingGroupInvitationId.HasValue) { newlyRegisteredUser = this.gamingGroupInviteConsumer.AddNewUserToGamingGroup(newApplicationUser.Id, newUser.GamingGroupInvitationId.Value); } else { newlyRegisteredUser = await this.firstTimeUserAuthenticator.CreateGamingGroupAndSendEmailConfirmation(newApplicationUser, newUser.Source); } return newlyRegisteredUser; }
public void SetUp() { firstTimeUserAuthenticatorMock = MockRepository.GenerateMock<IFirstTimeAuthenticator>(); userStoreMock = MockRepository.GenerateMock<IUserStore<ApplicationUser>>(); var dataProtector = MockRepository.GenerateMock<IDataProtector>(); dataProtectionProviderMock = MockRepository.GenerateMock<IDataProtectionProvider>(); dataProtectionProviderMock.Expect(mock => mock.Create(Arg<string>.Is.Anything)).Return(dataProtector); applicationUserManagerMock = MockRepository.GenerateMock<ApplicationUserManager>(userStoreMock, dataProtectionProviderMock); authenticationManagerMock = MockRepository.GenerateMock<IAuthenticationManager>(); signInManagerMock = MockRepository.GenerateMock<ApplicationSignInManager>(applicationUserManagerMock, authenticationManagerMock); dataContextMock = MockRepository.GenerateMock<IDataContext>(); eventTrackerMock = MockRepository.GenerateMock<INemeStatsEventTracker>(); gamingGroupInviteConsumerMock = MockRepository.GenerateMock<IGamingGroupInviteConsumer>(); userRegisterer = new UserRegisterer( applicationUserManagerMock, firstTimeUserAuthenticatorMock, dataContextMock, signInManagerMock, eventTrackerMock, gamingGroupInviteConsumerMock); Guid invitationId = Guid.NewGuid(); const int PLAYER_ID = 1938; GamingGroupInvitation invitation = new GamingGroupInvitation { Id = invitationId, PlayerId = PLAYER_ID, GamingGroupId = 10 }; newUser = new NewUser() { UserName = "******", EmailAddress = "the email", GamingGroupInvitationId = invitationId, Source = TransactionSource.WebApplication }; expectedNewlyRegisteredUser = new NewlyRegisteredUser { GamingGroupId = invitation.GamingGroupId, GamingGroupName = "some awesome gaming group name", PlayerId = PLAYER_ID, PlayerName = "some awesome player name", UserId = applicationUserIdAfterSaving }; IdentityResult result = IdentityResult.Success; dataContextMock.Expect(mock => mock.FindById<GamingGroupInvitation>(invitationId)) .Return(invitation); applicationUserManagerMock.Expect(mock => mock.CreateAsync(Arg<ApplicationUser>.Is.Anything, Arg<string>.Is.Anything)) .Return(Task.FromResult(result)) .WhenCalled(invocation => ((ApplicationUser)invocation.Arguments[0]).Id = applicationUserIdAfterSaving); signInManagerMock.Expect(mock => mock.SignInAsync( Arg<ApplicationUser>.Is.Anything, Arg<bool>.Is.Anything, Arg<bool>.Is.Anything)) .Return(Task.FromResult(-1)); gamingGroupInviteConsumerMock.Expect(mock => mock.AddNewUserToGamingGroup(Arg<string>.Is.Anything, Arg<Guid>.Is.Anything)) .Return(expectedNewlyRegisteredUser); firstTimeUserAuthenticatorMock.Expect(mock => mock.CreateGamingGroupAndSendEmailConfirmation( Arg<ApplicationUser>.Is.Anything, Arg<TransactionSource>.Is.Anything)) .Return(Task.FromResult(expectedNewlyRegisteredUser)); }
public async Task ItDoesntSignInIfTheUserIsntCreatedSuccessfully() { newUser = new NewUser(); IdentityResult result = new IdentityResult("an error"); applicationUserManagerMock = MockRepository.GenerateMock<ApplicationUserManager>(userStoreMock, dataProtectionProviderMock); applicationUserManagerMock.Expect(mock => mock.CreateAsync(Arg<ApplicationUser>.Is.Anything, Arg<string>.Is.Anything)) .Return(Task.FromResult<IdentityResult>(result)); userRegisterer = new UserRegisterer( applicationUserManagerMock, firstTimeUserAuthenticatorMock, dataContextMock, signInManagerMock, eventTrackerMock, gamingGroupInviteConsumerMock); await userRegisterer.RegisterUser(newUser); firstTimeUserAuthenticatorMock.AssertWasNotCalled(mock => mock.CreateGamingGroupAndSendEmailConfirmation( Arg<ApplicationUser>.Is.Anything, Arg<TransactionSource>.Is.Anything)); }
public async Task ItCreatesANewGamingGroupIfNotConsumingAnInvitationToAnExistingGamingGroup() { newUser = new NewUser() { UserName = "******", EmailAddress = "the email", GamingGroupInvitationId = null }; await userRegisterer.RegisterUser(newUser); firstTimeUserAuthenticatorMock.AssertWasCalled(mock => mock.CreateGamingGroupAndSendEmailConfirmation( Arg<ApplicationUser>.Matches(user => user.UserName == newUser.UserName && user.Email == newUser.EmailAddress), Arg<TransactionSource>.Is.Anything)); }
public virtual async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { Guid? gamingGroupInvitation = null; if (!string.IsNullOrWhiteSpace(model.GamingGroupInvitationId)) { gamingGroupInvitation = new Guid(model.GamingGroupInvitationId); } NewUser newUser = new NewUser { EmailAddress = model.EmailAddress.Trim(), UserName = model.UserName.Trim(), Password = model.Password, GamingGroupInvitationId = gamingGroupInvitation }; RegisterNewUserResult registerNewUserResult = await this.userRegisterer.RegisterUser(newUser); if (registerNewUserResult.Result.Succeeded) { return RedirectToAction(MVC.GamingGroup.ActionNames.Index, MVC.GamingGroup.Name); } this.AddErrors(registerNewUserResult.Result); } // If we got this far, something failed, redisplay form return View(MVC.Account.Views.Register, model); }