Exemplo n.º 1
0
        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 = 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);
        }
Exemplo n.º 4
0
        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 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);
        }
        public void DeleteChannel_WhenIsExisting_ShoulReturn200()
        {
            // Arrange -> create a channel
            TestingEngine.CleanDatabase();
            var existingChannel = new Channel()
            {
                Name = "Existing Channel"
            };
            MessagesDbContext dbContext = new MessagesDbContext();

            dbContext.Channels.Add(existingChannel);
            dbContext.SaveChanges();

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

            // Assert -> HTTP status code is 200
            Assert.AreEqual(HttpStatusCode.OK, httpDeleteResponse.StatusCode);
            var result = httpDeleteResponse.Content.ReadAsStringAsync().Result;

            Assert.IsNotNull(result);
            Assert.AreEqual(0, dbContext.Channels.Count());
        }
Exemplo n.º 9
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);
        }