public void ChatsControllerTest_CreateChat_HTTP200() { // Arrange var controller = new ChatsController(ChatService); string uniqueUserScreenName = "unique user screen name" + Guid.NewGuid().ToString(); // act IHttpActionResult result = controller.CreateChat(uniqueUserScreenName); var createdResult = result as CreatedNegotiatedContentResult <ChatDTO>; // Assert Assert.IsNotNull(createdResult); Assert.AreEqual(new Uri($"/api/chats/{createdResult.Content.Id}", UriKind.Relative), createdResult.Location); Assert.AreEqual(uniqueUserScreenName, createdResult.Content.UserScreenName); Assert.IsTrue(DateTime.Now.Subtract(createdResult.Content.CreatedDateTime).TotalMilliseconds <= 5000); // Db test //Test data is persisted into database as expected. var expected = new Chat() { Id = createdResult.Content.Id, UserScreenName = createdResult.Content.UserScreenName, }; Chat actual = null; using (var context = new EEChatDataContext(ConnectionString)) { actual = context.Chats.Find(createdResult.Content.Id); Assert.IsNotNull(actual); Assert.AreEqual(expected, actual); context.Chats.Remove(actual); } }
public void ChatsControllerTest_CreateChat_HTTP409_conflict() { // Arrange var controller = new ChatsController(ChatService); string uniqueUserScreenName = "unique user screen name" + Guid.NewGuid().ToString(); Chat chat = null; using (var context = new EEChatDataContext(ConnectionString)) { // Create a chat chat = new Chat() { UserScreenName = uniqueUserScreenName, }; context.Chats.Add(chat); context.SaveChanges(); // act // We pass controller action the same user screen name we with which created a chat above. IHttpActionResult result = controller.CreateChat(uniqueUserScreenName); var conflictResult = result as NegotiatedContentResult <string>; // Assert Assert.IsNotNull(conflictResult); Assert.AreEqual(HttpStatusCode.Conflict, conflictResult.StatusCode); context.Chats.Remove(chat); } }
public void ChatsControllerTest_SendMessageIntoChat_HTTP200() { // Arrange var controller = new ChatsController(ChatService); string testMessage = "Test message text"; var messageDispatch = new ChatMessageDispatch() { MessageText = testMessage, SentDate = DateTime.Now, }; string uniqueUserScreenName = "unique user screen name" + Guid.NewGuid().ToString(); Chat chat = null; using (var context = new EEChatDataContext(ConnectionString)) { // Create a chat chat = new Chat() { UserScreenName = uniqueUserScreenName, }; context.Chats.Add(chat); context.SaveChanges(); } // act IHttpActionResult result = controller.SendMessageIntoChat(chat.Id, messageDispatch); var okResult = result as OkNegotiatedContentResult <ChatMessageDTO>; // Assert Assert.IsNotNull(okResult); Assert.AreEqual(messageDispatch.MessageText, okResult.Content.MessageText); Assert.AreEqual(messageDispatch.SentDate, okResult.Content.SentDate); Assert.IsTrue(DateTime.Now.Subtract(okResult.Content.CreatedDate).TotalMilliseconds <= 5000); // Db test //Test data is persisted into database as expected. var expected = new ChatMessage() { Id = okResult.Content.Id, MessageText = okResult.Content.MessageText, ChatId = chat.Id, }; using (var context = new EEChatDataContext(ConnectionString)) { var actual = context.ChatMessages.Find(okResult.Content.Id); Assert.IsNotNull(actual); Assert.AreEqual(expected, actual); // Test whether related chat has become active. var chatRelated = context.Chats.Find(actual.ChatId); Assert.IsNotNull(chatRelated); Assert.IsTrue(chatRelated.IsActive); context.Chats.Remove(chatRelated); } }
public ChatsControllerTests() { this.userManager = ChatsControllerExtension.MockUserManager(); this.mockChatService = ChatsControllerExtension.MockChatService(); this.mockUnitOfWork = ChatsControllerExtension.MockUnitOfWork(); this.sut = new ChatsController(userManager.Object, mockChatService.Object, mockUnitOfWork.Object); }
public void ChatsControllerTest_CreateChat_HTTP400_bad_request() { // Arrange var controller = new ChatsController(ChatService); // act // We pass controller action a null as userScreenName parameter to get a HTTP 400 bad request. IHttpActionResult result = controller.CreateChat(null); // Assert Assert.IsInstanceOfType(result, typeof(BadRequestErrorMessageResult)); }
public void ChatsControllerTest_SendMessageIntoChat_HTTP404() { // Arrange var controller = new ChatsController(ChatService); string testMessage = "Test message text"; var messageDispatch = new ChatMessageDispatch() { MessageText = testMessage, SentDate = DateTime.Now, }; // act // We pass controller action a random chat ID, that we suppose not to be found. IHttpActionResult result = controller.SendMessageIntoChat(Guid.NewGuid(), messageDispatch); var notFoundResult = result as NegotiatedContentResult <string>; // Assert Assert.IsNotNull(notFoundResult); Assert.AreEqual(HttpStatusCode.NotFound, notFoundResult.StatusCode); }
public void ChatsControllerTest_SendMessageIntoChat_HTTP409_bad_request() { // Arrange var controller = new ChatsController(ChatService); string testMessage = null; var messageDispatch = new ChatMessageDispatch() { MessageText = testMessage, SentDate = DateTime.Now, }; // act // We pass controller action an invalid model. controller.Request = new HttpRequestMessage(); controller.Configuration = new HttpConfiguration(); controller.Validate(messageDispatch); IHttpActionResult result = controller.SendMessageIntoChat(Guid.NewGuid(), messageDispatch); // Assert Assert.IsInstanceOfType(result, typeof(InvalidModelStateResult)); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { // global cors policy app.UseCors(x => x .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials() ); app.UseExceptionHandlingPolicies(); if (Program.LetsEncryptACME.UseLetsEncrypt) { app.UseFluffySpoonLetsEncryptChallengeApprovalMiddleware(); } app.UseAuthentication(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); app.UseHsts(); } if (Program.AppSettings.UseHttpsRedirection) { app.UseHttpsRedirection(); } app.UseStaticFiles(); app.UseSpaStaticFiles(); app.UseErrorHandlingMiddleware(); app.UseSignalR(routes => { routes.MapHub <ChatHub>("/hubs/chat"); routes.MapHub <PlayerHub>("/hubs/player"); routes.MapHub <BackpackHub>("/hubs/backpack"); routes.MapHub <FactionHub>("/hubs/faction"); routes.MapHub <SysteminfoHub>("/hubs/systeminfo"); routes.MapHub <PlayfieldHub>("/hubs/playfield"); routes.MapHub <ModinfoHub>("/hubs/modinfo"); }); app.UseMvc(routes => { routes.Select().Expand().Filter().OrderBy().MaxTop(1000).Count(); routes.MapODataServiceRoute("player", "odata", PlayersController.GetEdmModel()); routes.MapODataServiceRoute("faction", "odata", FactionsController.GetEdmModel()); routes.MapODataServiceRoute("chat", "odata", ChatsController.GetEdmModel()); routes.EnableDependencyInjection(); routes.MapRoute(name: "default", template: "{controller}/{action=Index}/{id?}"); }); app.UseSpa(spa => { // To learn more about options for serving an Angular SPA from ASP.NET Core, // see https://go.microsoft.com/fwlink/?linkid=864501 spa.Options.SourcePath = "ClientApp"; if (env.IsDevelopment()) { spa.UseAngularCliServer(npmScript: "start"); } }); }