public void EditExistingChannel_ShouldReturn200OK_Modify() { // Arrange -> create a new channel TestingEngine.CleanDatabase(); var channelName = "channel" + DateTime.Now.Ticks; var httpPostResponse = this.CreateChannelHttpPost(channelName); Assert.AreEqual(HttpStatusCode.Created, httpPostResponse.StatusCode); var postedChannel = httpPostResponse.Content.ReadAsAsync <ChannelModel>().Result; // Act -> edit the above created channel var channelNewName = "Edited " + channelName; var httpPutResponse = this.EditChannelHttpPut(postedChannel.Id, channelNewName); // Assert -> the PUT result is 200 OK Assert.AreEqual(HttpStatusCode.OK, httpPutResponse.StatusCode); // Assert the service holds the modified channel var httpGetResponse = TestingEngine.HttpClient.GetAsync("/api/channels").Result; var channelsFromService = httpGetResponse.Content.ReadAsAsync <List <ChannelModel> >().Result; Assert.AreEqual(HttpStatusCode.OK, httpGetResponse.StatusCode); Assert.AreEqual(1, channelsFromService.Count); Assert.AreEqual(postedChannel.Id, channelsFromService.First().Id); Assert.AreEqual(channelNewName, channelsFromService.First().Name); }
public void RegisterUser_InvalidUserData_ShouldReturn400_BadRequest() { // Arrange TestingEngine.CleanDatabase(); // Act -> empty username var responseEmptyUsername = TestingEngine.RegisterUserHttpPost("", "#paSSw@rd12345"); // Assert -> 400 (Bad Request) Assert.AreEqual(HttpStatusCode.BadRequest, responseEmptyUsername.StatusCode); // Act -> empty password var responseEmptyPassword = TestingEngine.RegisterUserHttpPost("maria", ""); // Assert -> 400 (Bad Request) Assert.AreEqual(HttpStatusCode.BadRequest, responseEmptyPassword.StatusCode); // Act -> null username var responseNullUsername = TestingEngine.RegisterUserHttpPost(null, "#paSSw@rd12345"); // Assert -> 400 (Bad Request) Assert.AreEqual(HttpStatusCode.BadRequest, responseNullUsername.StatusCode); // Act -> null password var responseNullPassword = TestingEngine.RegisterUserHttpPost("maria", null); // Assert -> 400 (Bad Request) Assert.AreEqual(HttpStatusCode.BadRequest, responseNullPassword.StatusCode); // Act -> no data (empty HTTP body) var httpResponse = TestingEngine.HttpClient.PostAsync("/api/user/register", null).Result; // Assert -> 400 (Bad Request) Assert.AreEqual(HttpStatusCode.BadRequest, httpResponse.StatusCode); }
public void ListChannelMessages_ExistingChannel_ShouldReturn200OK_SortedMessagesByDate() { // Arrange -> create a chennel and send a few messages to it TestingEngine.CleanDatabase(); // Create a channel var channelName = "channel" + DateTime.Now.Ticks; var httpResponseCreateChannel = TestingEngine.CreateChannelHttpPost(channelName); Assert.AreEqual(HttpStatusCode.Created, httpResponseCreateChannel.StatusCode); // Send a few messages to the channel string firstMsg = "First message"; var httpResponseFirstMsg = TestingEngine.SendChannelMessageHttpPost(channelName, firstMsg); Assert.AreEqual(HttpStatusCode.OK, httpResponseFirstMsg.StatusCode); Thread.Sleep(2); string secondMsg = "Second message"; var httpResponseSecondMsg = TestingEngine.SendChannelMessageHttpPost(channelName, secondMsg); Assert.AreEqual(HttpStatusCode.OK, httpResponseSecondMsg.StatusCode); Thread.Sleep(2); string thirdMsg = "Third message"; var httpResponseThirdMsg = TestingEngine.SendChannelMessageHttpPost(channelName, thirdMsg); Assert.AreEqual(HttpStatusCode.OK, httpResponseThirdMsg.StatusCode); // Act -> list the channel messages var urlMessages = "/api/channel-messages/" + WebUtility.UrlEncode(channelName); var httpResponseMessages = TestingEngine.HttpClient.GetAsync(urlMessages).Result; // Assert -> messages are returned correcty, ordered from the last to the first Assert.AreEqual(HttpStatusCode.OK, httpResponseMessages.StatusCode); var messages = httpResponseMessages.Content.ReadAsAsync <List <MessageModel> >().Result; Assert.AreEqual(3, messages.Count); // Check the first message Assert.IsTrue(messages[2].Id > 0); Assert.AreEqual(firstMsg, messages[2].Text); Assert.IsTrue((DateTime.Now - messages[2].DateSent) < TimeSpan.FromMinutes(1)); Assert.IsNull(messages[2].Sender); // Check the second message Assert.IsTrue(messages[1].Id > 0); Assert.AreEqual(secondMsg, messages[1].Text); Assert.IsTrue((DateTime.Now - messages[1].DateSent) < TimeSpan.FromMinutes(1)); Assert.IsNull(messages[1].Sender); // Check the third message Assert.IsTrue(messages[0].Id > 0); Assert.AreEqual(thirdMsg, messages[0].Text); Assert.IsTrue((DateTime.Now - messages[0].DateSent) < TimeSpan.FromMinutes(1)); Assert.IsNull(messages[0].Sender); }
public void RegisterUser_DuplicatedUsername_ShouldReturn400_BadRequest() { // Arrange TestingEngine.CleanDatabase(); // Act var responseFirstRegistration = TestingEngine.RegisterUserHttpPost("maria", "#paSSw@rd12345"); var responseSecondRegistration = TestingEngine.RegisterUserHttpPost("maria", "0th3RPassw@rd"); // Assert Assert.AreEqual(HttpStatusCode.OK, responseFirstRegistration.StatusCode); Assert.AreEqual(HttpStatusCode.BadRequest, responseSecondRegistration.StatusCode); }
public void UserLogin_InvalidUser_ShouldReturn400_BadRequest() { // Arrange TestingEngine.CleanDatabase(); var username = "******"; var password = "******"; // Act var loginResponse = TestingEngine.LoginUserHttpPost(username, password); // Assert Assert.AreEqual(HttpStatusCode.BadRequest, loginResponse.StatusCode); }
public void UserLogin_ValidUser_ShouldReturn200Ok_AccessToken() { // Arrange TestingEngine.CleanDatabase(); var username = "******"; var password = "******"; // Act var userSessionRegister = TestingEngine.RegisterUser(username, password); var userSessionLogin = TestingEngine.LoginUser(username, password); // Assert Assert.AreEqual(username, userSessionRegister.UserName); Assert.AreEqual(username, userSessionLogin.UserName); Assert.AreEqual(userSessionLogin.UserName, userSessionRegister.UserName); Assert.AreNotEqual(userSessionLogin.Access_Token, userSessionRegister.Access_Token); }
public void SendPersonalMessage_ListPersonalMessages_ShouldReturn200Ok_MessagesList() { // Arrange -> register sender and recipient users TestingEngine.CleanDatabase(); string senderUsername = "******"; var senderUserSession = TestingEngine.RegisterUser(senderUsername, "P@ssW01345"); string recipientUsername = "******"; var recipientSession = TestingEngine.RegisterUser(recipientUsername, "#testAZx$27"); var messages = new string[] { "Hello Peter " + DateTime.Now.Ticks, "Hello Peter (again) " + DateTime.Now.Ticks + 1, "Hello Peter (one more time) " + DateTime.Now.Ticks + 2 }; // Act -> send several messages to the user foreach (var message in messages) { var httpResponse = TestingEngine.SendPersonalMessageHttpPost( senderUserSession.Access_Token, recipientUsername, message); Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode); } // Assert -> expect to get the messages correctly var httpResponseMessages = TestingEngine.GetPersonalMessagesForUserHttpGet(recipientSession.Access_Token); var messagesFromService = httpResponseMessages.Content.ReadAsAsync <List <MessageModel> >().Result; Assert.AreEqual(messages.Count(), messagesFromService.Count); // Expect the messages to arrive ordered by date (from the last to the first) messagesFromService.Reverse(); for (int i = 0; i < messages.Length; i++) { Assert.IsTrue(messagesFromService[i].Id > 0); Assert.AreEqual(senderUsername, messagesFromService[i].Sender); Assert.IsNotNull(messagesFromService[i].DateSent); Assert.IsTrue((DateTime.Now - messagesFromService[0].DateSent) < TimeSpan.FromMinutes(1)); Assert.AreEqual(messages[i], messagesFromService[i].Text); } }
public void DeleteExistingChannel_ShouldReturn200OK() { // Arrange -> create a channel TestingEngine.CleanDatabase(); var channelName = "channel" + DateTime.Now.Ticks; var httpPostResponse = this.CreateChannelHttpPost(channelName); Assert.AreEqual(HttpStatusCode.Created, httpPostResponse.StatusCode); var channel = httpPostResponse.Content.ReadAsAsync <ChannelModel>().Result; Assert.AreEqual(1, TestingEngine.GetChannelsCountFromDb()); // Act -> delete the channel var httpDeleteResponse = TestingEngine.HttpClient.DeleteAsync( "/api/channels/" + channel.Id).Result; // Assert -> HTTP status code is 200 (OK) Assert.AreEqual(HttpStatusCode.OK, httpDeleteResponse.StatusCode); Assert.AreEqual(0, TestingEngine.GetChannelsCountFromDb()); }
public void RegisterUser_EmptyDb_ShouldReturn200Ok_AccessToken() { // Arrange TestingEngine.CleanDatabase(); var username = "******"; // Act var postContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("username", username), new KeyValuePair <string, string>("password", "pAssW@rd#123456") }); var httpResponse = TestingEngine.HttpClient.PostAsync("/api/user/register", postContent).Result; Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode); var userSession = httpResponse.Content.ReadAsAsync <UserSessionModel>().Result; // Assert Assert.AreEqual(userSession.UserName, username); Assert.IsNotNull(userSession.Access_Token); }
public void CreateNewChannel_ShouldCreateChannel_Return201Created() { // Arrange TestingEngine.CleanDatabase(); var channelName = "channel" + DateTime.Now.Ticks; // Act var httpResponse = this.CreateChannelHttpPost(channelName); // Assert Assert.AreEqual(HttpStatusCode.Created, httpResponse.StatusCode); Assert.IsNotNull(httpResponse.Headers.Location); var newChannel = httpResponse.Content.ReadAsAsync <ChannelModel>().Result; Assert.IsTrue(newChannel.Id != 0); Assert.AreEqual(newChannel.Name, channelName); var channelsCountInDb = TestingEngine.GetChannelsCountFromDb(); Assert.AreEqual(1, channelsCountInDb); }
public void CreateChannels_ListChannels_ShouldListCreatedChannelsAlphabetically() { // Arrange -> prepare a few channels TestingEngine.CleanDatabase(); var channelsToAdds = new string[] { "Channel Omega" + DateTime.Now.Ticks, "Channel Alpha" + DateTime.Now.Ticks, "Channel Zeta" + DateTime.Now.Ticks, "Channel X" + DateTime.Now.Ticks, "Channel Psy" + DateTime.Now.Ticks }; // Act -> create a few channels foreach (var channelName in channelsToAdds) { var httpPostResponse = this.CreateChannelHttpPost(channelName); // Assert -> ensure each channel is successfully created Assert.AreEqual(HttpStatusCode.Created, httpPostResponse.StatusCode); } // Assert -> list the channels and assert their count, order and content are correct var httpResponse = TestingEngine.HttpClient.GetAsync("/api/channels").Result; Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode); var channelsFromService = httpResponse.Content.ReadAsAsync <List <ChannelModel> >().Result; Assert.AreEqual(channelsToAdds.Count(), channelsFromService.Count); var sortedChannels = channelsToAdds.OrderBy(c => c).ToList(); for (int i = 0; i < sortedChannels.Count; i++) { Assert.IsTrue(channelsFromService[i].Id != 0); Assert.AreEqual(sortedChannels[i], channelsFromService[i].Name); } }
public void SendChannelMessage_FromExisitingUser_ShouldListMessagesCorectly() { // Arrange -> create a channel TestingEngine.CleanDatabase(); var channelName = "channel" + DateTime.Now.Ticks; var httpResponseCreateChannel = TestingEngine.CreateChannelHttpPost(channelName); Assert.AreEqual(HttpStatusCode.Created, httpResponseCreateChannel.StatusCode); // Arrange -> register two users var userSessionPeter = TestingEngine.RegisterUser("peter", "pAssW@rd#123456"); var userSessionMaria = TestingEngine.RegisterUser("maria", "SECret#76^%asf!"); // Act -> send a few messages to the channel (from the registered users and anonymous) string firstMsg = "A message from Peter"; var httpResponseFirstMsg = TestingEngine.SendChannelMessageFromUserHttpPost( channelName, firstMsg, userSessionPeter.Access_Token); Assert.AreEqual(HttpStatusCode.OK, httpResponseFirstMsg.StatusCode); Thread.Sleep(2); string secondMsg = "Anonymous message"; var httpResponseThirdMsg = TestingEngine.SendChannelMessageHttpPost(channelName, secondMsg); Assert.AreEqual(HttpStatusCode.OK, httpResponseThirdMsg.StatusCode); Thread.Sleep(2); string thirdMsg = "A message from Maria"; var httpResponseSecondMsg = TestingEngine.SendChannelMessageFromUserHttpPost( channelName, thirdMsg, userSessionMaria.Access_Token); Assert.AreEqual(HttpStatusCode.OK, httpResponseSecondMsg.StatusCode); // Act -> list the channel messages var urlMessages = "/api/channel-messages/" + WebUtility.UrlEncode(channelName); var httpResponseMessages = TestingEngine.HttpClient.GetAsync(urlMessages).Result; // Assert -> messages are returned correcty, ordered from the last to the first Assert.AreEqual(HttpStatusCode.OK, httpResponseMessages.StatusCode); var messages = httpResponseMessages.Content.ReadAsAsync <List <MessageModel> >().Result; Assert.AreEqual(3, messages.Count); // Check the first message Assert.IsTrue(messages[2].Id > 0); Assert.AreEqual(firstMsg, messages[2].Text); Assert.IsTrue((DateTime.Now - messages[2].DateSent) < TimeSpan.FromMinutes(1)); Assert.AreEqual("peter", messages[2].Sender); // Check the second message Assert.IsTrue(messages[1].Id > 0); Assert.AreEqual(secondMsg, messages[1].Text); Assert.IsTrue((DateTime.Now - messages[1].DateSent) < TimeSpan.FromMinutes(1)); Assert.IsNull(messages[1].Sender); // Check the third message Assert.IsTrue(messages[0].Id > 0); Assert.AreEqual(thirdMsg, messages[0].Text); Assert.IsTrue((DateTime.Now - messages[0].DateSent) < TimeSpan.FromMinutes(1)); Assert.AreEqual("maria", messages[0].Sender); }