public async Task TestMethod_Post_Created_Duplicated_NegotiatedContentResult_HttpStatusCodeConflict()
        {
            // Created one
            var    controller = new SignupController();
            Random r          = new Random();

            SignupInput signupInput = new SignupInput()
            {
                Comments   = "",
                Email      = "*****@*****.**" + r.Next(1234, 123456789),
                FirstName  = "Yan",
                LastName   = "Wang",
                FunEventId = 101
            };
            string resourceId = signupInput.Email + "###" + signupInput.FunEventId;

            var actionResult = await controller.Post(signupInput);

            var result = actionResult as CreatedAtRouteNegotiatedContentResult <SignupResult>;

            Assert.IsNotNull(result);
            Assert.AreEqual("DefaultApi", result.RouteName);
            Assert.AreEqual(result.Content.ResourceId, resourceId);
            Assert.IsTrue(result.Content.Status == OperationStatus.Added);

            // Trying to create again
            var actionResultAgain = await controller.Post(signupInput);

            var createdResultAgain = actionResultAgain as NegotiatedContentResult <SignupResult>;

            Assert.IsNotNull(createdResultAgain); // Check Type
            Assert.IsTrue(createdResultAgain.StatusCode == System.Net.HttpStatusCode.Conflict);
            Assert.IsTrue(createdResultAgain.Content.ResourceId == resourceId);
            Assert.IsTrue(createdResultAgain.Content.Status == OperationStatus.DuplicatedWhenAdding);
        }
        public void Post_Empty_Firstname_Test()
        {
            // Arrange
            User userObjAsInput = new User();

            userObjAsInput.firstName = "";
            userObjAsInput.lastName  = "2";
            userObjAsInput.emailId   = "*****@*****.**";
            userObjAsInput.password  = "******";

            var mockRepository = new Mock <IUserRepository>();

            var controller = new SignupController(mockRepository.Object);

            controller.Request       = new HttpRequestMessage();
            controller.Configuration = new HttpConfiguration();

            // Act
            HttpResponseMessage response = controller.Post(userObjAsInput);

            // Assert
            string responseJSONStr = response.Content.ReadAsStringAsync().Result;
            string responseStr     = Helper.Deserialize <string>(responseJSONStr);

            Assert.IsNotNull(response);
            Assert.AreEqual(response.StatusCode, HttpStatusCode.PartialContent);
            Assert.AreEqual(responseStr, "User First Name is empty.");
        }
        public void Post_User_Already_Exists_Test()
        {
            // Arrange
            User userObjAsInput = new User();

            userObjAsInput.firstName = "Team";
            userObjAsInput.lastName  = "2";
            userObjAsInput.emailId   = "*****@*****.**";
            userObjAsInput.password  = "******";

            var mockRepository = new Mock <IUserRepository>();

            mockRepository.Setup(x => x.SaveUser(userObjAsInput))
            .Returns(-1);

            var controller = new SignupController(mockRepository.Object);

            controller.Request       = new HttpRequestMessage();
            controller.Configuration = new HttpConfiguration();

            // Act
            HttpResponseMessage response = controller.Post(userObjAsInput);

            // Assert
            string responseJSONStr = response.Content.ReadAsStringAsync().Result;
            string responseStr     = Helper.Deserialize <string>(responseJSONStr);

            Assert.IsNotNull(response);
            Assert.AreEqual(response.StatusCode, HttpStatusCode.Conflict);
            Assert.AreEqual(responseStr, "User already present.");
        }
        public async Task TestMethod_Post_InvalidModelStateResult_BadRequest()
        {
            var controller = new SignupController();

            SignupInput signupInput = new SignupInput()
            {
                Comments   = "",
                Email      = "",
                FirstName  = "Looooooooooooooong",
                LastName   = "",
                FunEventId = 0
            };

            var actionResult = await controller.Post(signupInput);

            var createdResult = actionResult as InvalidModelStateResult;

            Assert.IsNotNull(createdResult);
            Assert.IsNotNull(createdResult.ModelState);
            ModelStateDictionary info = createdResult.ModelState as ModelStateDictionary;

            Assert.IsTrue(info.Count == 4);

            // Todo: check Details
        }
        public void Post_Null_Input_Test()
        {
            // Arrange
            var mockRepository = new Mock <IUserRepository>();

            var controller = new SignupController(mockRepository.Object);

            controller.Request       = new HttpRequestMessage();
            controller.Configuration = new HttpConfiguration();

            // Act
            HttpResponseMessage response = controller.Post(null);

            // Assert
            string responseJSONStr = response.Content.ReadAsStringAsync().Result;
            string responseStr     = Helper.Deserialize <string>(responseJSONStr);

            Assert.IsNotNull(response);
            Assert.AreEqual(response.StatusCode, HttpStatusCode.NoContent);
            Assert.AreEqual(responseStr, "Invalid JSON Passed.");
        }
Exemple #6
0
        public async Task SignupControllerPostTestExists()
        {
            var data  = new SignupDataHandler();
            var logic = new CoreApiLogicHandler();

            var signup = SignupData.GetSignup();

            data.Result.Setup(m => m.Execute(It.IsAny <UserByEmail>())).Returns(StandardUser);

            var controller = new SignupController(data, Cloud, logic);
            var result     = await controller.Post(signup) as OkObjectResult;

            result.Should().NotBeNull();
            result.Should().BeOfType <OkObjectResult>();

            logic.HasExecuted.Should().BeFalse();

            data.HasExecuted.Should().BeTrue();
            data.HasCommitted.Should().BeFalse();
            data.Result.Verify(s => s.Execute(It.IsAny <UserByEmail>()), Times.Once);
        }
        public async Task TestMethod_Post_InvalidModelStateResult_BadRequest_Not_ExistingEvent()
        {
            var controller = new SignupController();

            SignupInput signupInput = new SignupInput()
            {
                Comments   = "",
                Email      = "*****@*****.**",
                FirstName  = "ABC",
                LastName   = "def",
                FunEventId = 9999
            };

            var actionResult = await controller.Post(signupInput);

            var createdResult = actionResult as InvalidModelStateResult;

            Assert.IsNotNull(createdResult);
            Assert.IsNotNull(createdResult.ModelState);
            ModelStateDictionary info = createdResult.ModelState as ModelStateDictionary;

            Assert.IsTrue(info.Count == 1);
        }
Exemple #8
0
        public async Task SignupControllerPostTest()
        {
            var data  = new SignupDataHandler();
            var logic = new CoreApiLogicHandler();

            var signup = SignupData.GetSignup();
            var auth   = LoginData.GetAuth();

            data.Result.Setup(m => m.Execute(It.IsAny <UserByEmail>())).Returns(null);
            logic.Result.Setup(m => m.Execute(It.IsAny <SignupUser>())).Returns(ActionConfirm.CreateSuccess(auth));

            var controller = new SignupController(data, Cloud, logic);
            var result     = await controller.Post(signup) as OkObjectResult;

            result.Should().NotBeNull();
            result.Should().BeOfType <OkObjectResult>();

            logic.HasExecuted.Should().BeTrue();
            logic.Result.Verify(s => s.Execute(It.IsAny <SignupUser>()), Times.Once);

            data.HasExecuted.Should().BeTrue();
            data.HasCommitted.Should().BeFalse();
            data.Result.Verify(s => s.Execute(It.IsAny <UserByEmail>()), Times.Once);
        }
        public async Task TestMethod_Post_CreatedAtRoute()
        {
            var    controller = new SignupController();
            Random r          = new Random();

            SignupInput signupInput = new SignupInput()
            {
                Comments   = "",
                Email      = "*****@*****.**" + r.Next(1234, 123456789),
                FirstName  = "Yan",
                LastName   = "Wang",
                FunEventId = 101
            };
            string resourceId = signupInput.Email + "###" + signupInput.FunEventId;

            var actionResult = await controller.Post(signupInput);

            var result = actionResult as CreatedAtRouteNegotiatedContentResult <SignupResult>;

            Assert.IsNotNull(result);
            Assert.AreEqual("DefaultApi", result.RouteName);
            Assert.AreEqual(result.Content.ResourceId, resourceId);
            Assert.IsTrue(result.Content.Status == OperationStatus.Added);
        }
        public async Task TestMethod_Get()
        {
            // Prepare data
            var         controller  = new SignupController();
            Random      r           = new Random();
            string      searchKey   = Guid.NewGuid().ToString().Substring(1, 10);
            SignupInput signupInput = new SignupInput()
            {
                Comments  = "",
                Email     = "*****@*****.**" + r.Next(1234, 123456789),
                FirstName = searchKey,
                LastName  = "Wang",
            };

            string emailBackup = signupInput.Email;

            signupInput.FunEventId = 100;
            var task1 = controller.Post(signupInput);
            await Task.WhenAll(task1);

            signupInput.FunEventId = 101;
            var task2 = controller.Post(signupInput);
            await Task.WhenAll(task2);

            signupInput.FunEventId = 102;
            var task3 = controller.Post(signupInput);
            await Task.WhenAll(task3);

            // Noise
            signupInput.FunEventId = 100;
            signupInput.Email      = "ThisisNoise@g" + r.Next(123456789, 223456789);
            signupInput.FirstName  = "NotMatched";
            var task4 = controller.Post(signupInput);
            await Task.WhenAll(task4);

            //
            // Query - By First Name
            //
            QueryInputFromClient queryInputFromClient = new QueryInputFromClient();

            queryInputFromClient.FirstName = searchKey;
            var queryResult = await controller.Get(queryInputFromClient);

            var list = queryResult.ToList();

            Assert.IsTrue(list.Count() == 3);
            // Same person
            Assert.IsTrue(list[0].Email == emailBackup);
            Assert.IsTrue(list[1].Email == emailBackup);
            Assert.IsTrue(list[2].Email == emailBackup);

            Assert.IsTrue(list[0].FirstName.Contains(searchKey));
            Assert.IsTrue(list[1].FirstName.Contains(searchKey));
            Assert.IsTrue(list[2].FirstName.Contains(searchKey));

            // Different events
            Assert.IsTrue(list[0].FunEventId != list[1].FunEventId);
            Assert.IsTrue(list[1].FunEventId != list[2].FunEventId);
            Assert.IsTrue(list[2].FunEventId != list[0].FunEventId);

            //
            // Query - By First Name & Eent
            //
            QueryInputFromClient queryInputFromClient2 = new QueryInputFromClient();

            queryInputFromClient2.FirstName = searchKey;
            queryInputFromClient2.FunEvent  = 101;
            var queryResult2 = await controller.Get(queryInputFromClient2);

            var list2 = queryResult2.ToList();

            Assert.IsTrue(list2.Count() == 1);
            Assert.IsTrue(list2[0].Email == emailBackup);
            Assert.IsTrue(list2[0].FirstName.Contains(searchKey));
            Assert.IsTrue(list2[0].FunEventId == 101);
        }