public void GetPostAuthor_ShouldWorkFine() { var author = new SimpleSocialUser { Id = "test", UserName = "******", }; var userManager = (UserManager <SimpleSocialUser>) this.Provider.GetService(typeof(UserManager <SimpleSocialUser>)); userManager.CreateAsync(author).GetAwaiter(); var post = new MyProfileViewModel { CreatePost = new CreatePostInputModel() { UserId = author.Id, Content = "Post", WallId = "wallId", } }; this.PostServices.CreatePost(post); var postId = this.PostsRepository.All().FirstOrDefault(x => x.UserId == author.Id).Id; var postAuthor = this.PostServices.GetPostAuthor(postId); postAuthor.Id.ShouldBe(author.Id); }
public void GetUserFollowings_ShouldWorkFine() { var user = new SimpleSocialUser { Id = "test", UserName = "******", ProfilePictureURL = "test" }; var following = new SimpleSocialUser { Id = "follower", UserName = "******", ProfilePictureURL = "test" }; this.UserManager.CreateAsync(user).GetAwaiter(); this.UserManager.CreateAsync(following).GetAwaiter(); this.Context.UserFollowers.Add(new UserFollower() { User = following, UserId = following.Id, Follower = user, FollowerId = "test" }); this.Context.SaveChanges(); var claims = this.SingInManager.CreateUserPrincipalAsync(user).GetAwaiter().GetResult(); var followers = this.FollowersServices.GetFollowings(claims).ToList(); followers.Count.ShouldBe(1); }
public void UserShoudFollowOtherUserSuccessfull() { var user = new SimpleSocialUser { Id = "test", UserName = "******", ProfilePictureURL = "test" }; var userToFollow = new SimpleSocialUser { Id = "toFollow", UserName = "******", ProfilePictureURL = "test" }; //test must follow userToFollow this.UserManager.CreateAsync(user).GetAwaiter(); this.UserManager.CreateAsync(userToFollow).GetAwaiter(); var claims = this.SingInManager.CreateUserPrincipalAsync(user).GetAwaiter().GetResult(); this.FollowersServices.Follow(userToFollow.Id, claims.Identity.Name); var currentUserFollowing = this.Context.UserFollowers.ToArray()[0]; currentUserFollowing.FollowerId.ShouldBe(user.Id); }
public void GetPostById_ShuldWorkFine() { var user = new SimpleSocialUser { Id = "test", UserName = "******", ProfilePictureId = "test" }; var userManager = (UserManager <SimpleSocialUser>) this.Provider.GetService(typeof(UserManager <SimpleSocialUser>)); userManager.CreateAsync(user).GetAwaiter(); var post = new Post() { Content = "test", UserId = user.Id, WallId = "zaebi", }; this.Context.Posts.Add(post); this.Context.SaveChanges(); var postFromService = this.PostServices.GetPostById(post.Id); postFromService.Id.ShouldBe(post.Id); }
public void GetSinglePostViewModel_ShouldWorkFine() { var user = new SimpleSocialUser { Id = "test", UserName = "******", ProfilePictureId = "test" }; var userManager = (UserManager <SimpleSocialUser>) this.Provider.GetService(typeof(UserManager <SimpleSocialUser>)); userManager.CreateAsync(user).GetAwaiter(); var post = new Post() { Content = "test", UserId = user.Id, WallId = "zaebi", }; this.Context.Posts.Add(post); this.Context.SaveChanges(); var viewModel = this.PostServices.GetSinglePostViewComponentModel(post.Id, user.Id); viewModel.PostAuthorId.ShouldBe("test"); }
public void CreatePost_ShouldWorkFine() { var user = new SimpleSocialUser { Id = "test", UserName = "******" }; var wall = new Wall() { Id = "Test" }; var createPostViewModel = new CreatePostInputModel { Content = "test, test, test", UserId = user.Id, WallId = wall.Id, }; this.PostServices.CreatePost(new MyProfileViewModel { CreatePost = createPostViewModel }); var postsCount = PostsRepository.All().Count(); postsCount.ShouldBe(1); }
public ICollection <SimpleUserViewModel> GetNonAdminUsers(ClaimsPrincipal currentUser, List <SimpleUserViewModel> users = null) { var usersFound = new List <SimpleSocialUser>(); if (users == null) { usersFound = this.userRepository.All().Include(x => x.ProfilePicture).Where(x => x.UserName != currentUser.Identity.Name).Take(20).ToList(); } else { foreach (var user in users) { var userToAdd = new SimpleSocialUser() { Id = user.Id, ProfilePicture = user.ProfilePicture, UserName = user.UserName, }; usersFound.Add(userToAdd); } } var nonAdmins = new List <SimpleSocialUser>(); foreach (var user in usersFound) { if (!userManager.IsInRoleAsync(user, "Admin").GetAwaiter().GetResult()) { nonAdmins.Add(user); } } var result = nonAdmins.Select(Mapper.Map <SimpleUserViewModel>).ToArray(); return(result); }
private async Task SeedAdminUserAsync(UserManager <SimpleSocialUser> userManager) { var user = new SimpleSocialUser() { UserName = AdminConstants.AdminUsername, Email = AdminConstants.AdminEmail, }; var result = await userManager.CreateAsync(user, AdminConstants.AdminPassword); if (result.Succeeded) { await userManager.AddToRoleAsync(user, AdminConstants.AdminRoleName); } }
public void GetUserPosts_ShouldReturnAll() { var user = new SimpleSocialUser { Id = "test", UserName = "******", }; var user2 = new SimpleSocialUser { Id = "secondUser", UserName = "******" }; var userManager = (UserManager <SimpleSocialUser>) this.Provider.GetService(typeof(UserManager <SimpleSocialUser>)); userManager.CreateAsync(user).GetAwaiter(); userManager.CreateAsync(user2).GetAwaiter(); var post = new MyProfileViewModel { CreatePost = new CreatePostInputModel() { UserId = user.Id, Content = "Post", WallId = "wallId", } }; var postForSecondUser = new MyProfileViewModel { CreatePost = new CreatePostInputModel() { UserId = user2.Id, Content = "Post2", WallId = "wallId2", } }; this.PostServices.CreatePost(postForSecondUser); this.PostServices.CreatePost(post); this.PostServices.CreatePost(post); this.PostServices.CreatePost(post); var userPostsCount = this.PostServices.GetUserPosts(user.Id, "1", 0).Count; userPostsCount.ShouldBe(3); }
public void GetNewsFeedPosts_ShouldWorkFine() { var user = new SimpleSocialUser { Id = "test", UserName = "******", ProfilePictureId = "test" }; var userManager = (UserManager <SimpleSocialUser>) this.Provider.GetService(typeof(UserManager <SimpleSocialUser>)); userManager.CreateAsync(user).GetAwaiter(); var user2 = new SimpleSocialUser { Id = "test2", UserName = "******", ProfilePictureId = "test" }; userManager.CreateAsync(user2).GetAwaiter(); var post = new Post() { Content = "test", UserId = user.Id, WallId = "zaebi", }; this.Context.Posts.Add(post); this.Context.SaveChanges(); //test2 follova test var userFollower = new UserFollower() { UserId = user.Id, FollowerId = user2.Id, }; this.Context.UserFollowers.Add(userFollower); this.Context.SaveChanges(); var posts = this.PostServices.GetNewsFeedPosts(user2.Id, 0).ToArray(); var firstPostNewsFeed = posts[0]; firstPostNewsFeed.Id.ShouldBe(post.Id); }
private async Task LoadSharedKeyAndQrCodeUriAsync(SimpleSocialUser user) { // Load the authenticator key & QR code URI to display on the form var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); if (string.IsNullOrEmpty(unformattedKey)) { await _userManager.ResetAuthenticatorKeyAsync(user); unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); } SharedKey = FormatKey(unformattedKey); var email = await _userManager.GetEmailAsync(user); AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new SimpleSocialUser() { UserName = Input.Username, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); user.ProfilePictureURL = "https://res.cloudinary.com/svetlinmld/image/upload/v1546050240/default.jpg"; await dbContext.SaveChangesAsync(); if (result.Succeeded) { await this._userManager.AddToRoleAsync(user, GlobalConstants.NormalUserRoleName); _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect("/Identity/Account/Manage")); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information during confirmation."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } if (ModelState.IsValid) { var user = new SimpleSocialUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } LoginProvider = info.LoginProvider; ReturnUrl = returnUrl; return(Page()); }
public void GetUserProfilePic_ShouldWorkFine() { var user = new SimpleSocialUser { Id = "test", UserName = "******", ProfilePictureId = "test" }; this.UserManager.CreateAsync(user).GetAwaiter(); this.Context.ProfilePictures.Add(new ProfilePicture() { UserId = user.Id, FileName = "test" }); this.Context.SaveChanges(); var profPic = this.MyProfileServices.GetUserProfilePicture(user.Id); profPic.FileName.ShouldBe("test"); }
public void DeletePostWhereUserIsntTheCreator_ShoudWorkFine() { var user = new SimpleSocialUser { Id = "test", UserName = "******", }; var wall = new Wall() { Id = "Test" }; var post = new Post() { Id = "Test", UserId = "otherTest", WallId = wall.Id }; //add the new post this.PostsRepository.AddAsync(post).GetAwaiter().GetResult(); this.PostsRepository.SaveChangesAsync().GetAwaiter().GetResult(); //create the test user var userManager = (UserManager <SimpleSocialUser>) this.Provider.GetService(typeof(UserManager <SimpleSocialUser>)); userManager.CreateAsync(user).GetAwaiter(); //create the claims principal user thats needed in the test. var signInManager = (SignInManager <SimpleSocialUser>) this.Provider.GetService(typeof(SignInManager <SimpleSocialUser>)); var claims = signInManager.CreateUserPrincipalAsync(user).GetAwaiter().GetResult(); //delete the post this.PostServices.DeletePost(post.Id, claims); //check if its deleted this.PostsRepository.All().Count().ShouldBe(1); }
public ICollection <SimpleUserViewModel> GetAdminUsers(ClaimsPrincipal currentUser, List <SimpleUserViewModel> users = null) { var usersFound = new List <SimpleSocialUser>(); if (users == null) { usersFound = this.dbContext.Users.Where(x => x.UserName != currentUser.Identity.Name).Take(10).ToList(); } else { foreach (var user in users) { var userToAdd = new SimpleSocialUser() { Id = user.Id, ProfilePictureURL = user.ProfilePictureURL, UserName = user.UserName, }; usersFound.Add(userToAdd); } } var admins = new List <SimpleSocialUser>(); foreach (var user in usersFound) { if (userManager.IsInRoleAsync(user, "Admin").GetAwaiter().GetResult()) { admins.Add(user); } } var result = mapper.Map <List <SimpleUserViewModel> >(admins); return(result); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new SimpleSocialUser() { UserName = Input.Username, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); var wall = new Wall() { UserId = dbContext.Users.FirstOrDefault(x => x.UserName == user.UserName)?.Id }; dbContext.Walls.Add(wall); dbContext.SaveChanges(); user.WallId = wall.Id; dbContext.SaveChanges(); user.ProfilePictureURL = "https://res.cloudinary.com/svetlinmld/image/upload/v1546050240/default.jpg"; dbContext.SaveChanges(); if (result.Succeeded) { if (this.dbContext.Users.Count() == 1) { await this._userManager.AddToRoleAsync(user, "Admin"); } else { await this._userManager.AddToRoleAsync(user, "User"); } _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect("/Identity/Account/Manage")); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }