public void RateByID_ShouldFailUnregistredToken() { var context = new MockNewsFeedAPIContext(); var controller = new UserRatingController(context); context.NewsInstances.Add(GetNewsInstance()); var controllerContext = new HttpControllerContext(); var request = new HttpRequestMessage(); string token = "123dhjksad"; request.Headers.Add("Token", token); controllerContext.Request = request; controller.ControllerContext = controllerContext; var result = controller.PostUserRating(1, 1); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(BadRequestErrorMessageResult)); Assert.AreEqual($"Token {token} is not registred!", (result as BadRequestErrorMessageResult).Message); }
public void CancelRateByID_ShouldFailNotRatedNews() { var context = new MockNewsFeedAPIContext(); var newsController = new UserRatingController(context); var userContoller = new UserController(context); context.NewsInstances.Add(GetNewsInstance()); var controllerContext = new HttpControllerContext(); var request = new HttpRequestMessage(); string token = (userContoller.GetToken() as OkNegotiatedContentResult <string>).Content; request.Headers.Add("Token", token); controllerContext.Request = request; newsController.ControllerContext = controllerContext; var result = newsController.DeleteUserRating(1); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(BadRequestErrorMessageResult)); Assert.AreEqual("User with given token has not rated the news with given id", (result as BadRequestErrorMessageResult).Message); }
public void CancelRateByID_ShouldSuccess() { var context = new MockNewsFeedAPIContext(); var newsController = new UserRatingController(context); var userContoller = new UserController(context); context.NewsInstances.Add(GetNewsInstance()); var controllerContext = new HttpControllerContext(); var request = new HttpRequestMessage(); string token = (userContoller.GetToken() as OkNegotiatedContentResult <string>).Content; request.Headers.Add("Token", token); controllerContext.Request = request; newsController.ControllerContext = controllerContext; newsController.PostUserRating(1, 1); var result = newsController.DeleteUserRating(1) as OkNegotiatedContentResult <NewsInstanceViewModel>; Assert.IsNotNull(result); Assert.AreEqual(10 / 3d, result.Content.Rating); }
public void RateByID_ShouldFailRatingOutOfBounds() { var context = new MockNewsFeedAPIContext(); var newsController = new UserRatingController(context); var userContoller = new UserController(context); context.NewsInstances.Add(GetNewsInstance()); var controllerContext = new HttpControllerContext(); var request = new HttpRequestMessage(); string token = (userContoller.GetToken() as OkNegotiatedContentResult <string>).Content; request.Headers.Add("Token", token); controllerContext.Request = request; newsController.ControllerContext = controllerContext; var result = newsController.PostUserRating(1, 10); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(BadRequestErrorMessageResult)); Assert.AreEqual($"Given rating was out of bounds. The range of rating is from 1 to 5", (result as BadRequestErrorMessageResult).Message); }
public void RateByID_ShouldFailRepeatedRate() { var context = new MockNewsFeedAPIContext(); var newsController = new UserRatingController(context); var userContoller = new UserController(context); context.NewsInstances.Add(GetNewsInstance()); var controllerContext = new HttpControllerContext(); var request = new HttpRequestMessage(); string token = (userContoller.GetToken() as OkNegotiatedContentResult <string>).Content; request.Headers.Add("Token", token); controllerContext.Request = request; newsController.ControllerContext = controllerContext; newsController.PostUserRating(1, 1); var result = newsController.PostUserRating(1, 1); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(BadRequestErrorMessageResult)); Assert.AreEqual($"User with this token has already rated that piece of news", (result as BadRequestErrorMessageResult).Message); }