public void DeleteChannel_WithMessages_ShouldReturn409Conflict()
        {
            // Arrange -> create a channel with a message posted in it
            TestingEngine.CleanDatabase();

            // Create a channel
            var channelName = "channel" + DateTime.Now.Ticks;
            var httpResponseCreateChanel = TestingEngine.CreateChannelHttpPost(channelName);

            Assert.AreEqual(HttpStatusCode.Created, httpResponseCreateChanel.StatusCode);
            var channel = httpResponseCreateChanel.Content.ReadAsAsync <ChannelModel>().Result;

            Assert.AreEqual(1, TestingEngine.GetChannelsCountFromDb());

            // Post an anonymous message in the channel
            var httpResponsePostMsg = TestingEngine.SendChannelMessageHttpPost(channelName, "message");

            Assert.AreEqual(HttpStatusCode.OK, httpResponsePostMsg.StatusCode);

            // Act -> try to delete the channel with the message
            var httpDeleteResponse = TestingEngine.HttpClient.DeleteAsync(
                "/api/channels/" + channel.Id).Result;

            // Assert -> HTTP status code is 409 (Conflict), channel is not empty
            Assert.AreEqual(HttpStatusCode.Conflict, httpDeleteResponse.StatusCode);
            Assert.AreEqual(1, TestingEngine.GetChannelsCountFromDb());
        }
        public void EditExistingChannel_ShouldReturn200OK_Modify()
        {
            // Arrange -> create a new channel
            TestingEngine.CleanDatabase();
            var channelName      = "channel" + DateTime.Now.Ticks;
            var httpPostResponse = TestingEngine.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 = TestingEngine.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);
        }
示例#3
0
        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 CreateNewChannel_NameTooLong_ShouldReturn400BadRequest()
        {
            // Arrange
            TestingEngine.CleanDatabase();
            var tooLongChannelName = new string('a', 101);

            // Act
            var httpResponse = TestingEngine.CreateChannelHttpPost(tooLongChannelName);

            // Assert
            Assert.AreEqual(HttpStatusCode.BadRequest, httpResponse.StatusCode);
            var channelsCountInDb = TestingEngine.GetChannelsCountFromDb();

            Assert.AreEqual(0, channelsCountInDb);
        }
        public void CreateNewChannel_InvalidData_ShouldReturn400BadRequest()
        {
            // Arrange
            TestingEngine.CleanDatabase();
            var invalidChannelName = string.Empty;

            // Act
            var httpResponse = TestingEngine.CreateChannelHttpPost(invalidChannelName);

            // Assert
            Assert.AreEqual(HttpStatusCode.BadRequest, httpResponse.StatusCode);
            var channelsCountInDb = TestingEngine.GetChannelsCountFromDb();

            Assert.AreEqual(0, channelsCountInDb);
        }
        public void CreateDuplicatedChannels_ShouldReturn409Conflict()
        {
            // Arrange
            TestingEngine.CleanDatabase();
            var channelName = "channel" + DateTime.Now.Ticks;

            // Act
            var httpResponseFirst = TestingEngine.CreateChannelHttpPost(channelName);

            Assert.AreEqual(HttpStatusCode.Created, httpResponseFirst.StatusCode);

            var httpResponseSecond = TestingEngine.CreateChannelHttpPost(channelName);

            // Assert
            Assert.AreEqual(HttpStatusCode.Conflict, httpResponseSecond.StatusCode);
        }
        public void EditChannel_LeaveSameName_ShouldReturn200OK()
        {
            // Arrange -> create a channel
            TestingEngine.CleanDatabase();

            var channelName      = "channel" + DateTime.Now.Ticks;
            var httpPostResponse = TestingEngine.CreateChannelHttpPost(channelName);

            Assert.AreEqual(HttpStatusCode.Created, httpPostResponse.StatusCode);
            var channel = httpPostResponse.Content.ReadAsAsync <ChannelModel>().Result;

            // Act -> try to edit the channel and leave its name the same
            var httpPutResponse = TestingEngine.EditChannelHttpPut(channel.Id, channelName);

            // Assert -> HTTP status code is 200 (OK)
            Assert.AreEqual(HttpStatusCode.OK, httpPutResponse.StatusCode);
        }
        public void EditChannel_EmptyBody_ShouldReturn400BadRequest()
        {
            // Arrange -> create a new channel
            TestingEngine.CleanDatabase();
            var channelName      = "channel" + DateTime.Now.Ticks;
            var httpPostResponse = TestingEngine.CreateChannelHttpPost(channelName);

            Assert.AreEqual(HttpStatusCode.Created, httpPostResponse.StatusCode);
            var postedChannel = httpPostResponse.Content.ReadAsAsync <ChannelModel>().Result;

            // Act -> try to edit the above created channel
            var httpPutResponse = TestingEngine.HttpClient.PutAsync(
                "/api/channels/" + postedChannel.Id, null).Result;

            // Assert -> the PUT result is 400 Bad Request
            Assert.AreEqual(HttpStatusCode.BadRequest, httpPutResponse.StatusCode);
        }
        public void ListChannelMessages_EmptyDb_ShouldReturn200Ok_EmptyList()
        {
            // Arrange -> create a new channel
            TestingEngine.CleanDatabase();
            var channelName = "channel" + DateTime.Now.Ticks;
            var httpResponseCreateChannel = TestingEngine.CreateChannelHttpPost(channelName);

            Assert.AreEqual(HttpStatusCode.Created, httpResponseCreateChannel.StatusCode);

            // Act -> list channel messages
            var urlMessages          = "/api/channel-messages/" + WebUtility.UrlEncode(channelName);
            var httpResponseMessages = TestingEngine.HttpClient.GetAsync(urlMessages).Result;

            // Assert -> expect empty list of messages
            Assert.AreEqual(HttpStatusCode.OK, httpResponseMessages.StatusCode);
            var messages = httpResponseMessages.Content.ReadAsAsync <List <MessageModel> >().Result;

            Assert.AreEqual(0, messages.Count);
        }
        public void GetChannelById_ExistingChannel_ShouldReturnTheChannel()
        {
            // Arrange -> create a new channel
            var channelName      = "channel" + DateTime.Now.Ticks;
            var httpPostResponse = TestingEngine.CreateChannelHttpPost(channelName);

            Assert.AreEqual(HttpStatusCode.Created, httpPostResponse.StatusCode);
            var postedChannel = httpPostResponse.Content.ReadAsAsync <ChannelModel>().Result;

            // Act -> find the channel by its ID
            var httpResponse = TestingEngine.HttpClient.GetAsync("/api/channels/" + postedChannel.Id).Result;

            // Assert -> the channel by ID holds correct data
            Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode);
            var channelFromService = httpResponse.Content.ReadAsAsync <ChannelModel>().Result;

            Assert.IsTrue(channelFromService.Id != 0);
            Assert.AreEqual(channelFromService.Name, channelName);
        }
        public void ListChannelMessagesWithInvalidLimit_ShouldReturn400BadRequest()
        {
            // 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);

            // Act -> list the channel messages with limit 1001
            var urlMessages  = "/api/channel-messages/" + WebUtility.UrlEncode(channelName);;
            var httpResponse = TestingEngine.HttpClient.GetAsync(urlMessages + "?limit=1001").Result;

            // Assert -> 400 (Bad Request)
            Assert.AreEqual(HttpStatusCode.BadRequest, httpResponse.StatusCode);

            // Act -> list the channel messages with limit 0
            httpResponse = TestingEngine.HttpClient.GetAsync(urlMessages + "?limit=0").Result;

            // Assert -> 400 (Bad Request)
            Assert.AreEqual(HttpStatusCode.BadRequest, httpResponse.StatusCode);

            // Act -> list the channel messages with limit "invalid"
            httpResponse = TestingEngine.HttpClient.GetAsync(urlMessages + "?limit=invalid").Result;

            // Assert -> 400 (Bad Request)
            Assert.AreEqual(HttpStatusCode.BadRequest, httpResponse.StatusCode);
        }
        public void DeleteExistingChannel_ShouldReturn200OK()
        {
            // Arrange -> create a channel
            TestingEngine.CleanDatabase();

            var channelName      = "channel" + DateTime.Now.Ticks;
            var httpPostResponse = TestingEngine.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 CreateNewChannel_ShouldCreateChannel_Return201Created()
        {
            // Arrange
            TestingEngine.CleanDatabase();
            var channelName = "channel" + DateTime.Now.Ticks;

            // Act
            var httpResponse = TestingEngine.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 EditChannel_DuplicatedName_ShouldReturn409Conflict()
        {
            // Arrange -> create two channels
            TestingEngine.CleanDatabase();

            var channelNameFirst         = "channel" + DateTime.Now.Ticks;
            var firstChannelHttpResponse = TestingEngine.CreateChannelHttpPost(channelNameFirst);

            Assert.AreEqual(HttpStatusCode.Created, firstChannelHttpResponse.StatusCode);
            var firstChannel = firstChannelHttpResponse.Content.ReadAsAsync <ChannelModel>().Result;

            var channelNameSecond         = "channel" + DateTime.Now.Ticks + 1;
            var secondChannelHttpResponse = TestingEngine.CreateChannelHttpPost(channelNameSecond);

            Assert.AreEqual(HttpStatusCode.Created, secondChannelHttpResponse.StatusCode);

            // Act -> try to edit the first channel and duplicate its name with the second channel
            var httpPutResponseFirst = TestingEngine.EditChannelHttpPut(firstChannel.Id, channelNameSecond);

            // Assert -> HTTP status code is 409 (Conflict)
            Assert.AreEqual(HttpStatusCode.Conflict, httpPutResponseFirst.StatusCode);
        }
        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 = TestingEngine.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_InvalidMessageData_ShouldReturn400BadRequest()
        {
            // 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);

            // Act -> try to send a message with empty HTTP POST body
            var sendMsgUrl          = "/api/channel-messages/" + WebUtility.UrlEncode(channelName);
            var httpResponseNullMsg = TestingEngine.HttpClient.PostAsync(sendMsgUrl, null).Result;

            // Assert -> 400 (Bad Request)
            Assert.AreEqual(HttpStatusCode.BadRequest, httpResponseNullMsg.StatusCode);

            // Act -> try to send a message with empty text
            var httpResponseEmptyMsg = TestingEngine.SendChannelMessageHttpPost(channelName, "");

            // Assert -> 400 (Bad Request)
            Assert.AreEqual(HttpStatusCode.BadRequest, httpResponseEmptyMsg.StatusCode);
        }
示例#17
0
        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);
        }