public IHttpActionResult AddNewBug([FromBody]AddBugBindingModel model) { if (model == null) { return this.BadRequest("Model cannot be null."); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } string loggedUserId = this.User.Identity.GetUserId(); var user = this.Data.Users.Find(loggedUserId); var bug = new Bug() { Title = model.Title, Description = model.Description, Status = Status.Open, SubmitDate = DateTime.Now, BugAuthor = user }; this.Data.Bugs.Add(bug); this.Data.SaveChanges(); if (user == null) { return this.CreatedAtRoute("DefaultApi", new { id = bug.Id }, new { id = bug.Id, message = "Annonymous bug submitted." }); } else { return this.CreatedAtRoute("DefaultApi", new { id = bug.Id }, new { id = bug.Id, author = user.UserName, message = "User bug submitted." }); } }
public IHttpActionResult SubmitBug(SubmitBugInputModel model) { if (model == null || !this.ModelState.IsValid) { return this.BadRequest("You must submit bug title."); } var newBug = new Bug { Title = model.Title, Description = model.Description, DateCreated = DateTime.Now, Status = BugStatus.Open, UserId = this.User.Identity.GetUserId() }; this.Data.Bugs.Add(newBug); this.Data.SaveChanges(); object infoToReturn = new { newBug.Id, Message = "Anonymous bug submitted." }; if (newBug.UserId != null) { infoToReturn = new { newBug.Id, Author = this.User.Identity.GetUserName(), Message = "User bug submitted." }; } return this.CreatedAtRoute( "DefaultApi", new { controller = "bugs", id = newBug.Id }, infoToReturn); }
public IHttpActionResult Submit([FromBody]SubmitBugBindingModel model) { if (model == null) { this.BadRequest(); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } string userId = this.User.Identity.GetUserId(); var user = this.Data.Users .FirstOrDefault(u => u.Id == userId); var newBug = new Bug() { Title = model.Title, Description = model.Description, Status = Status.Open, SubmitDate = DateTime.Now, Author = user }; this.Data.Bugs.Add(newBug); this.Data.SaveChanges(); if (newBug.Author == null) { return this.CreatedAtRoute("DefaultApi", new { Id = newBug.Id }, new { Id = newBug.Id, Message = "Anonymous bug submitted." }); } else { return this.CreatedAtRoute("DefaultApi", new { Id = newBug.Id }, new { Id = newBug.Id, Author = newBug.Author.UserName, Message = "User bug submitted." }); } }
public void EditExistingBug_NoDataSend_ShouldReturn400BadRequest() { // Arrange -> create a new bug var data = new BugTrackerDataMock(); var bug = new Bug() { Id = 1, Title = "test", Status = Status.Open }; data.Bugs.Add(bug); var controller = new BugsController(data); this.SetupControllerForTesting(controller, "bugs"); // Act -> edit the above created bug var httpResponse = controller.PatchEditBug(1, null) .ExecuteAsync(new CancellationToken()).Result; // Assert -> 404 Not found Assert.AreEqual(HttpStatusCode.BadRequest, httpResponse.StatusCode); }
public void EditBugNoInputModel_ShouldReturnBadRequest() { var controller = new BugsController(dataMock.Object); SetupController(controller, "Bugs"); var bugToEdit = new Bug { Title = "Bug4e Tuka", Description = "ne6to ne e nared", Status = BugStatus.InProgress, DateCreated = DateTime.Now }; dataMock.Setup(data => data.Bugs.Find(It.IsAny<object>())).Returns(bugToEdit); var result = controller.EditBug(2, null).ExecuteAsync(new CancellationToken()).Result; Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode); }
public void EditExistingBugTitle_ShouldReturn200OK_Modify() { // Arrange -> create a new bug var data = new BugTrackerDataMock(); var bug = new Bug() { Id = 1, Title = "test", Status = Status.Open }; data.Bugs.Add(bug); var controller = new BugsController(data); this.SetupControllerForTesting(controller, "bugs"); // Act -> edit the above created bug var httpResponse = controller.PatchEditBug(1, new BugBindingEditBugModel() { Title = "test1" }) .ExecuteAsync(new CancellationToken()).Result; // Assert -> 200 OK Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode); // Assert the service holds the modified bug Assert.AreEqual("test1", bug.Title); Assert.AreEqual(Status.Open, bug.Status); }
public IHttpActionResult SubmitBug(BugPostBindingModel model) { if (model == null) { return BadRequest("Missing bug data."); } if (!ModelState.IsValid) { return BadRequest(ModelState); } var currUserId = User.Identity.GetUserId(); var currUser = db.Users.Find(currUserId); var bug = new Bug() { Title = model.Title, Description = model.Description ?? null, Status = Status.Open, Author = null, DateCreated = DateTime.Now, Comments = new List<Comment>() }; if (this.User.Identity.IsAuthenticated) { var loggedUserId = this.User.Identity.GetUserId(); var user = db.Users.Find(loggedUserId); bug.Author = user; } db.Bugs.Add(bug); db.SaveChanges(); if (currUser == null) { return this.Created("http://localhost:5555/api/bugs/" + bug.Id, new { Id = bug.Id, Message = "Anonymous bug submitted." }); } return this.Created("http://localhost:5555/api/bugs/" + bug.Id, new { Id = bug.Id, Author = bug.Author.UserName, Message = "Anonymous bug submitted." }); }
public IHttpActionResult SubmitBug(SubmitBugInputModel bugData) { if (bugData == null) { return BadRequest("Missing bug data."); } if (!ModelState.IsValid) { return BadRequest(ModelState); } var currentUserId = User.Identity.GetUserId(); var bug = new Bug() { Title = bugData.Title, Description = bugData.Description, Status = BugStatus.Open, AuthorId = currentUserId, DateCreated = DateTime.Now }; db.Bugs.Add(bug); db.SaveChanges(); if (currentUserId != null) { return this.CreatedAtRoute( "DefaultApi", new { id = bug.Id }, new { bug.Id, Author = User.Identity.GetUserName(), Message = "User bug submitted." }); } return this.CreatedAtRoute( "DefaultApi", new { id = bug.Id }, new { bug.Id, Message = "Anonymous bug submitted." }); }
public void EditBugStatusOnly() { var controller = new BugsController(dataMock.Object); SetupController(controller, "Bugs"); var bugToEdit = new Bug { Id = 22, Title = "Bug4e Tuka", Description = "ne6to ne e nared", Status = BugStatus.InProgress, DateCreated = DateTime.Now }; dataMock.Setup(data => data.Bugs.Find(It.IsAny<object>())).Returns(bugToEdit); var updatedBug = new EditBugInputModel { Status = "Closed" }; var result = controller.EditBug(2, updatedBug).ExecuteAsync(new CancellationToken()).Result; Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); var resultContent = result.Content.ReadAsStringAsync().Result; Assert.IsTrue(resultContent.Contains("Bug #" + bugToEdit.Id + " patched.")); }