public async Task CreateAsync_Comment_CreateNewComment() { // Arrange var commentController = this.CreateCommentController(true); Comments comment = new Comments() { Text = "TestComment" }; //create new listing for comment Listings listing = new Listings() { Name = "Test Nice Car", Description = "free car :)", Userid = fakeUser.Id }; mockadvert_siteContext.Listings.Add(listing); mockadvert_siteContext.SaveChanges(); //get id int id = listing.Id; // Act var result = (RedirectToActionResult)await commentController.CreateAsync(id, comment); var addedComment = await mockadvert_siteContext.Comments.FirstAsync(c => c.Userid == fakeUser.Id); // Assert Assert.Equal(addedComment.Text, comment.Text); Assert.Equal(addedComment.Userid, fakeUser.Id); }
public static (ApplicationUser fakeUser, ControllerContext fakeControllerContext) FakeUserAndControllerContext(advert_siteContext mockadvert_siteContext) { //setup fake user var fakeUser = new ApplicationUser() { Email = "*****@*****.**", UserName = "******" }; mockadvert_siteContext.Users.Add(fakeUser); mockadvert_siteContext.SaveChanges(); var identity = new GenericIdentity(fakeUser.Id, ClaimTypes.NameIdentifier); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, fakeUser.Id)); var fakeUserIdent = new GenericPrincipal(identity, new string[] { "User" }); //fakeUserIdent.AddIdentity(identity); var fakeControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = fakeUserIdent } }; //set fake user return(fakeUser, fakeControllerContext); }
public async Task GetPicture_Picture_ShouldReturnPictureFileResult() { // Arrange var listingPicturesController = this.CreateListingPicturesController(); var pic = new ListingPictures() { PictureId = 1, FileName = "TEST_PICTURE.png", ContentType = "image/png" }; mockadvert_siteContext.ListingPictures.Add(pic); mockadvert_siteContext.SaveChanges(); // Act var result = await listingPicturesController.GetPicture(pic.PictureId); // Assert Assert.IsType <PhysicalFileResult>(result); var fileResult = (PhysicalFileResult)result; Assert.Equal(pic.FileName, Path.GetFileName(fileResult.FileName)); Assert.Equal(pic.ContentType, fileResult.ContentType); }
public void ListingsExists_ListingId_ReturnBool(int id, bool expectedResponse) { var listingsController = this.CreateListingsController(true); List <Listings> list = new List <Listings>() { GenerateListing(id: 5555), GenerateListing(id: 463768), GenerateListing(id: 456772), GenerateListing(id: 4501788), GenerateListing(id: 4567871), GenerateListing(id: 54728767), }; foreach (var item in list) { context.Listings.Add(item); } context.SaveChanges(); bool result = listingsController.ListingsExists(id); Assert.Equal(expectedResponse, result); }
public async Task <IActionResult> CreatePost([Bind("Subcategoryid,Name,Description,Price,GoogleLatitude,GoogleLongitude,GoogleRadius,ListingPictures")] ListingNewModel newListing) { if (ModelState.IsValid) { Listings listings = new Listings() { Subcategoryid = newListing.Subcategoryid, Name = newListing.Name, Description = newListing.Description, Price = newListing.Price, GoogleLatitude = newListing.GoogleLatitude, GoogleLongitude = newListing.GoogleLongitude, GoogleRadius = newListing.GoogleRadius * 1000 }; listings.Userid = this.User.FindFirstValue(ClaimTypes.NameIdentifier); listings.Date = DateTime.Now; listings.Display = 1; listings.Verified = 0; /* * listings.GoogleLongitude = 0;// newListing.GoogleLongitude; * listings.GoogleLatitude = 0;// newListing.GoogleLatitude; * listings.GoogleRadius = 10000;// newListing.GoogleRadius; */ _context.Add(listings); _context.SaveChanges(); if (newListing.ListingPictures != null && newListing.ListingPictures.Count() > 4) //jei nuotrauku daugiau nei 4 atmetam { TempData["PictureError"] = "Nuotraukų negali būti daugiau nei 4!"; return(RedirectToAction(nameof(Create), newListing)); } if (newListing.ListingPictures != null) { foreach (var picture in newListing.ListingPictures) { if (picture.Length > ImageMaximumBytes) //jei dydid didesnis uz 10MB atmeta { TempData["PictureError"] = "Nuotraukos dydis negali būti didesnis nei 10Mb!"; return(RedirectToAction(nameof(Create), newListing)); } if (!IsImage(picture)) { TempData["PictureError"] = "Failas nėra nuotrauka!"; return(RedirectToAction(nameof(Create), newListing)); } } foreach (var picture in newListing.ListingPictures) { if (picture.Length > 0) { var pic = new ListingPictures { ListingId = listings.Id, ContentType = picture.ContentType }; _context.Add(pic); await _context.SaveChangesAsync(); string[] filenameAndExtension = picture.FileName.Split('.'); filenameAndExtension[0] = pic.PictureId.ToString(); string fileName = filenameAndExtension[0] + "." + filenameAndExtension[1]; string path = "UserPictures" + "\\" + fileName; path = Path.GetFullPath(path); pic.FileName = fileName; _context.ListingPictures.Update(pic); using (var stream = new FileStream(path, FileMode.Create)) { await picture.CopyToAsync(stream); } } } } await _context.SaveChangesAsync(); //TempData["Success"] = "Jūsų skelbimas bus patalpintas, kai administratorius jį patikrins"; return(RedirectToAction(nameof(Index))); } ViewData["Subcategoryid"] = new SelectList(_context.Subcategory, "Id", "Id"); ViewData["Userid"] = new SelectList(_context.Users, "Id", "UserName"); return(View(newListing)); }
public async Task Inbox_Messages_ReturnUserMessagesView(int msgCount) { // Arrange var messagesController = this.CreateMessagesController(true); //create sender var user = new ApplicationUser() { UserName = "******", Email = "*****@*****.**" }; mockadvert_siteContext.Users.Add(user); mockadvert_siteContext.SaveChanges(); //create messages for (int i = 0; i < msgCount; i++) { var msg = new Messages() { DateSent = DateTime.Now, Subject = "TestMsg" + i, Text = "yo" + i }; mockadvert_siteContext.Messages.Add(msg); mockadvert_siteContext.SaveChanges(); var userHasMsg = new UsersHasMessages() { MessagesId = msg.Id, RecipientId = fakeUser.Id, SenderId = user.Id, IsDeleted = 0 }; mockadvert_siteContext.UsersHasMessages.Add(userHasMsg); mockadvert_siteContext.SaveChanges(); } // Act var result = await messagesController.Inbox(); var viewResult = (ViewResult)result; var messages = (IEnumerable <UsersHasMessages>)viewResult.Model; // Assert Assert.IsType <ViewResult>(result); Assert.Equal(msgCount, messages.Count()); }