public void TestGetChatroomInformation() { //Necessary to create the chatroom before requesting its information var USER = _helper.testUsers[1]; ChatroomController chatControllerTest = new ChatroomController(); ChatRequestModel model = new ChatRequestModel() { RawChatroomIdValue = "123400", UserHandle = USER.DefaultHandle, ChatroomName = "TestGetChatroomInformation", User = new UserModel() { Id = USER.Id, Username = USER.Username } }; ChatResponseTestModel chatRoom = _helper.createChatroomAndAddUser(model);//for now don't do anything with the result //Create test data model GetChatroomInformationRequestModel model2 = new GetChatroomInformationRequestModel() { UserId = chatRoom.ChatroomModel.UserId, ChatroomId = chatRoom.ChatroomModel.ChatroomId, ParentChatroomId = chatRoom.ChatroomModel.ParentChatroomId }; //Test getting the information from the chatroom success var result = chatControllerTest.GetChatroomInformation(model2) as JsonResult; Assert.AreEqual(1, ((GetChatroomInformationResponseModel)result.Data).UsersInformation.Count); Assert.AreEqual(USER.Id, ((GetChatroomInformationResponseModel)result.Data).UsersInformation[0].Id); Assert.AreEqual(USER.Username, ((GetChatroomInformationResponseModel)result.Data).UsersInformation[0].Username); }
public void TestJoinChatroom() { //Necessary to create the child chatroom before requesting its information var USER = _helper.testUsers[1]; var JOINING_USER = _helper.testUsers[0]; ChatroomController chatControllerTest = new ChatroomController(); ChatRequestModel model = new ChatRequestModel() { RawChatroomIdValue = "123402", UserHandle = USER.DefaultHandle, ChatroomName = "TestJoinChatroom", User = new UserModel() { Id = USER.Id, Username = USER.Username } }; ChatResponseTestModel chatRoom = _helper.createChatroomAndAddUser(model);//for now don't do anything with the result //Pre-sanity check amke sure only 1 user in the room before we try ad join //Create test data model GetChatroomInformationRequestModel modelInfo = new GetChatroomInformationRequestModel() { UserId = chatRoom.ChatroomModel.UserId, ChatroomId = chatRoom.ChatroomModel.ChatroomId, ParentChatroomId = chatRoom.ChatroomModel.ParentChatroomId }; //Test getting the information from the chatroom success var result = chatControllerTest.GetChatroomInformation(modelInfo) as JsonResult; Assert.AreEqual(1, ((GetChatroomInformationResponseModel)result.Data).UsersInformation.Count); Assert.AreEqual(USER.Id, ((GetChatroomInformationResponseModel)result.Data).UsersInformation[0].Id); Assert.AreEqual(USER.Username, ((GetChatroomInformationResponseModel)result.Data).UsersInformation[0].Username); //Create test data model - with all options possible since without them is easier. Do hard mode xD JoinChatroomRequestModel model2 = new JoinChatroomRequestModel() { UserId = JOINING_USER.Id, ChatroomId = chatRoom.ChatroomModel.ChatroomId, ParentChatroomId = chatRoom.ChatroomModel.ParentChatroomId, CurrentChatroomId = -1, UserHandle = JOINING_USER.DefaultHandle, Password = "", User = new UserModel() { Id = JOINING_USER.Id } }; //Test joining a chatroom success result = chatControllerTest.JoinChatroom(model2) as JsonResult; //Post-sanity check NOW there will be two users in the chatroom xD result = chatControllerTest.GetChatroomInformation(modelInfo) as JsonResult; Assert.AreEqual(2, ((GetChatroomInformationResponseModel)result.Data).UsersInformation.Count); Assert.AreEqual(USER.Id, ((GetChatroomInformationResponseModel)result.Data).UsersInformation[0].Id); Assert.AreEqual(USER.Username, ((GetChatroomInformationResponseModel)result.Data).UsersInformation[0].Username); Assert.AreEqual(JOINING_USER.Id, ((GetChatroomInformationResponseModel)result.Data).UsersInformation[1].Id); Assert.AreEqual(JOINING_USER.Username, ((GetChatroomInformationResponseModel)result.Data).UsersInformation[1].Username); }
public void TestLeaveChatroom() { //Necessary to create the child chatroom before requesting its information var USER = _helper.testUsers[1]; ChatroomController chatControllerTest = new ChatroomController(); ChatRequestModel model = new ChatRequestModel() { RawChatroomIdValue = "123403", UserHandle = USER.DefaultHandle, ChatroomName = "TestLeaveChatroom", User = new UserModel() { Id = USER.Id, Username = USER.Username } }; ChatResponseTestModel chatRoom = _helper.createChatroomAndAddUser(model);//for now don't do anything with the result //Pre-sanity check amke sure only 1 user in the room before we try ad join //Create test data model GetChatroomInformationRequestModel modelInfo = new GetChatroomInformationRequestModel() { UserId = chatRoom.ChatroomModel.UserId, ChatroomId = chatRoom.ChatroomModel.ChatroomId, ParentChatroomId = chatRoom.ChatroomModel.ParentChatroomId }; //Test getting the information from the chatroom success var result = chatControllerTest.GetChatroomInformation(modelInfo) as JsonResult; Assert.AreEqual(1, ((GetChatroomInformationResponseModel)result.Data).UsersInformation.Count); Assert.AreEqual(USER.Id, ((GetChatroomInformationResponseModel)result.Data).UsersInformation[0].Id); Assert.AreEqual(USER.Username, ((GetChatroomInformationResponseModel)result.Data).UsersInformation[0].Username); //Create test data model LeaveChatroomRequestModel model2 = new LeaveChatroomRequestModel() { ChatroomId = chatRoom.ChatroomModel.ChatroomId, ParentId = chatRoom.ChatroomModel.ParentChatroomId, UserId = USER.Id, User = new UserModel() { Id = USER.Id } }; var result2 = chatControllerTest.LeaveChatroom(model2) as EmptyResult; //nothing to assert since an empty object is returned but check below that there are no users in the room //Post-sanity check NOW there will be two users in the chatroom xD result = chatControllerTest.GetChatroomInformation(modelInfo) as JsonResult; Assert.AreEqual(0, ((GetChatroomInformationResponseModel)result.Data).UsersInformation.Count); }
public void TestComposeAndGetNewMessages() { //Necessary to create the child chatroom before requesting its information var USER = _helper.testUsers[1]; ChatroomController chatControllerTest = new ChatroomController(); ChatRequestModel model = new ChatRequestModel() { RawChatroomIdValue = "123404", UserHandle = USER.DefaultHandle, ChatroomName = "TestComposeAndGetNewMessages", User = new UserModel() { Id = USER.Id, Username = USER.Username } }; ChatResponseTestModel chatRoom = _helper.createChatroomAndAddUser(model);//for now don't do anything with the result //Right now every time we run the test another message gets added. ComposeMessageRequestModel messageModel = new ComposeMessageRequestModel() { Message = "This is a test message", ChatroomId = chatRoom.ChatroomModel.ChatroomId, UserId = USER.Id, ParentChatroomId = chatRoom.ChatroomModel.ParentChatroomId, UserHandle = USER.DefaultHandle }; var msgResult = chatControllerTest.ComposeMessage(messageModel); //Create test data model GetNewMessagesRequestModel model2 = new GetNewMessagesRequestModel() { ChatroomId = chatRoom.ChatroomModel.ChatroomId, UserId = USER.Id, ExistingMessageIds = new List <int>(), ParentChatroomId = chatRoom.ChatroomModel.ParentChatroomId, User = new UserModel() { Id = USER.Id } }; //Test that there are > 0 message results returned. var result = chatControllerTest.GetNewMessages(model2) as JsonResult; Assert.AreNotEqual(0, ((GetNewMessagesResponseModel)result.Data).MessagesInformation.Count); }
public void TestCreateChatroom() { //Necessary to create the child chatroom before requesting its information var USER = _helper.testUsers[1]; ChatroomController chatControllerTest = new ChatroomController(); ChatRequestModel model = new ChatRequestModel() { RawChatroomIdValue = "123401", UserHandle = USER.DefaultHandle, ChatroomName = "TestCreateChatroom", User = new UserModel() { Id = USER.Id, Username = USER.Username } }; ChatResponseTestModel chatRoom = _helper.createChatroomAndAddUser(model);//for now don't do anything with the result //Create test data model - with all options possible since without them is easier. Do hard mode xD CreateChatroomRequestModel model2 = new CreateChatroomRequestModel() { ChatroomName = ("PRIVATE" + chatRoom.ChatroomModel.ChatroomName), ParentChatroomId = chatRoom.ChatroomModel.ChatroomId, Blacklist = "test1,test2", Password = "******", Capacity = 30, User = new UserModel() { Id = USER.Id } }; //Test creating a private chatroom success var result = chatControllerTest.CreateChatroom(model2) as JsonResult; Assert.AreEqual(0, ((CreateChatroomResponseModel)result.Data).Errors.Count); Assert.AreEqual(("PRIVATE" + chatRoom.ChatroomModel.ChatroomName), ((CreateChatroomResponseModel)result.Data).ChatroomName); Assert.AreEqual(chatRoom.ChatroomModel.ChatroomId, ((CreateChatroomResponseModel)result.Data).ParentChatroomId); Assert.AreEqual(USER.Id, ((CreateChatroomResponseModel)result.Data).UserId); //Test trying to create duplicate named private chatroom fail result = chatControllerTest.CreateChatroom(model2) as JsonResult; Assert.AreNotEqual(0, ((CreateChatroomResponseModel)result.Data).Errors.Count); Assert.AreEqual("A private chatroom with this name already exists.", ((CreateChatroomResponseModel)result.Data).Errors[0].ErrorMessage); }
//Helper method for creating a chatroom since MVC uses controller contexts to route the user to the chat partial view page/ //We don't need that routing here, all we need is the logic or 99% above that line so just replicate it here and //This will create a chatroom and add the user to it //The chatrrom only exists in memory while the tests are run. Each new build refreshes public ChatResponseTestModel createChatroomAndAddUser(ChatRequestModel request) { var response = new ChatResponseTestModel(); int chatroomId = 0; if (request.RawChatroomIdValue != null) { chatroomId = request.RawChatroomIdValue.GetHashCode(); } int parentChatroomId = chatroomId; //temporary during initial testing string chatroomName = request.ChatroomName; if (!ChatroomService.DoesChatroomExist(chatroomId)) { ChatroomService.CreateChatroom(chatroomId, chatroomName); } var joinErrors = SecurityService.CanUserJoinChatroom(request); response.Errors.AddRange(joinErrors); if (joinErrors.Count == 0) { if (!ChatroomService.AddUserToChatroom(chatroomId, parentChatroomId, request.User.Id, request.UserHandle)) { response.AddError("Error adding user into chatroom."); } } var chatroomModel = new ChatroomModel() { ChatroomId = chatroomId, ChatroomName = chatroomName, ParentChatroomId = parentChatroomId, UserHandle = request.UserHandle, UserId = request.User.Id }; response.ChatroomModel = chatroomModel; return(response); }