public void AddAdoptionCatWithInvalidVolunteerCredentialsShouldReturnFalse() { // Arrange var db = Tests.GetDatabase(); var volunteer = new User { Email = "*****@*****.**", UserName = "******", Name = "NonVolunteer", Gender = Gender.Male, Birthdate = DateTime.UtcNow, Location = "Sofia", ProfilePhoto = null }; var cat = new AdoptionCat { Name = "Anton", Age = 1, Image = null, Description = "Test Description", Gender = Gender.Male, OwnerId = volunteer.Id, Owner = volunteer, Location = "Sofia" }; db.Add(volunteer); db.Add(cat); db.SaveChanges(); var service = new AdoptionCatService(db); // Act var result = service.Add("Test Cat", null, 1, "Sofia", "Test Description", Gender.Male); // Assert result .Should() .BeFalse(); }
// by default the owner is set to be the volunteer profile public bool Add(string name, IFormFile image, int age, string location, string description, Gender gender) { if (this.db.AdoptionCats.Any(c => c.Name == name)) { return(false); } if (!this.db.Users.Any(u => u.UserName == WebConstants.VolunteerUsername)) { return(false); } var volunteerId = this.db .Users .Where(u => u.UserName == WebConstants.VolunteerUsername) .Select(u => u.Id) .FirstOrDefault(); var cat = new AdoptionCat { Name = name, Age = age, Description = description, Gender = gender, OwnerId = volunteerId, Owner = this.db.Users.FirstOrDefault(u => u.Id == volunteerId), Location = location }; using (var memoryStream = new MemoryStream()) { image.CopyToAsync(memoryStream); cat.Image = memoryStream.ToArray(); } this.db.AdoptionCats.Add(cat); this.db.SaveChanges(); return(true); }