Exemplo n.º 1
0
 /// <summary>
 /// Поиск существующего и создание нового пользователя
 /// </summary>
 /// <param name="cookieUserData"></param>
 /// <param name="newUser"></param>
 /// <returns></returns>
 private User FindOrCreateUser(CookieUserData cookieUserData, out bool newUser)
 {
     using (IUnitOfWork unitOfWork = _unitOfWorkFactory.Create())
     {
         OAuthMembership oAuthMembership =
             _oAuthMembershipRepository.GetWithUser()
             .FirstOrDefault(
                 m =>
                 m.OAuthUserId == cookieUserData.OAuthUserId &&
                 m.ProviderName == cookieUserData.OAuthProviderName);
         if (oAuthMembership != null)
         {
             newUser = false;
             return(oAuthMembership.User);
         }
         User user = new User();
         user.Name = cookieUserData.UserName;
         OAuthMembership membership = new OAuthMembership();
         membership.User         = user;
         membership.ProviderName = cookieUserData.OAuthProviderName;
         membership.OAuthUserId  = cookieUserData.OAuthUserId;
         _userRepository.Create(user);
         _oAuthMembershipRepository.Create(membership);
         unitOfWork.Commit();
         newUser = true;
         return(user);
     }
 }
        public void OAuthMembershipRepository_GetByUserIdTest()
        {
            //Arange
            Guid            testUserId = _testOAuthMembership.User.Id;
            OAuthMembership resultMembership;

            //Act
            using (_unitOfWorkFactory.Create())
            {
                resultMembership = _oAtuhMembershipRepository.GetWithUser().FirstOrDefault(m => m.User.Id == testUserId);
            }
            //Assert
            Assert.IsNotNull(resultMembership);
            Assert.IsNotNull(resultMembership.User);
            Assert.AreEqual(resultMembership.ProviderName, _testOAuthMembership.ProviderName);
            Assert.AreEqual(resultMembership.OAuthUserId, _testOAuthMembership.OAuthUserId);
            Assert.AreEqual(resultMembership.User.Id, _testOAuthMembership.User.Id);
        }