コード例 #1
0
        public void AllTest()
        {
            UserRepositorySpy spy_repository = new UserRepositorySpy();
            CredentialsCheckerSpy_AlwaysPass spy_checker = new CredentialsCheckerSpy_AlwaysPass();
            UsersController target = new UsersController(spy_repository, spy_checker);
            List<User> expected = new List<User>();
            expected.Add(new User() { Email = "*****@*****.**", FirstName = "First name", Id = 1, LastName = "Last name", Login = "******", Photo = null });
            expected.Add(new User() { Email = "*****@*****.**", FirstName = "Ostap", Id = 2, LastName = "Bender", Login = "******", Photo = null });
            spy_repository.GetAllUsersResult = expected;

            /*
            var config = new HttpConfiguration();
            var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/1.0/users/all");
            var route = config.Routes.MapHttpRoute("TestApi", "api/1.0/{controller}/{action}");
            var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "users" }, { "action", "all" } });
            target.ControllerContext = new HttpControllerContext(config, routeData, request);
            target.Request = request;
            target.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
            */

            List<User> actual = target.All();

            Assert.AreEqual(expected, actual);
        }
コード例 #2
0
        public void ChangePasswordShouldCallRepositoryTest()
        {
            UserRepositorySpy spy_repository = new UserRepositorySpy();
            CredentialsCheckerSpy_AlwaysPass spy_checker = new CredentialsCheckerSpy_AlwaysPass();
            UsersController target = new UsersController(spy_repository, spy_checker);
            UserChangePasswordData user = new UserChangePasswordData() { NewPassword = "******", OldPassword = "******" };
            spy_repository.GetSingleUserResult = new User() { Id = 1, Login = "******" };

            var config = new HttpConfiguration();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/1.0/users/changepassword");
            var route = config.Routes.MapHttpRoute("TestApi", "api/1.0/{controller}/{action}");
            var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "users" }, { "action", "changepasword" } });
            target.ControllerContext = new HttpControllerContext(config, routeData, request);
            target.Request = request;
            target.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;

            HttpResponseMessage actual = target.ChangePassword(user);

            Assert.IsTrue(spy_repository.ChangePasswordNewPassword == "new_password" && spy_repository.ChangePasswordOldPassword == "old_password" && spy_repository.ChangePasswordUser.Id == 1);
        }
コード例 #3
0
        public void UploadImageShouldCallRepositoryTest()
        {
            UserRepositorySpy spy_repository = new UserRepositorySpy();
            CredentialsCheckerSpy_AlwaysPass spy_checker = new CredentialsCheckerSpy_AlwaysPass();
            UsersController target = new UsersController(spy_repository, spy_checker);
            byte[] fakeImage=new byte[] { 10, 11, 12 };
            UploadImageData arg = new UploadImageData() { FileName = "image1.jpg", ImageType = "image/jpeg", ImageBuffer = fakeImage};

            var config = new HttpConfiguration();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/1.0/users/UploadImage");
            var route = config.Routes.MapHttpRoute("TestApi", "api/1.0/{controller}/{action}");
            var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "users" }, { "action", "UploadImage" } });
            target.ControllerContext = new HttpControllerContext(config, routeData, request);
            target.Request = request;
            target.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;

            var actual = target.UploadImage(arg);

            bool successPassImage = true;
            if ((spy_repository.AddOrUpdatePhotoPhoto != null) && (spy_repository.AddOrUpdatePhotoPhoto.Length == fakeImage.Length))
            {
                for (int ind = 0; ind < fakeImage.Length; ind++)
                {
                    if (spy_repository.AddOrUpdatePhotoPhoto[ind] != fakeImage[ind])
                    {
                        successPassImage = false;
                        break;
                    }
                }
            }
            else
            {
                successPassImage = false;
            }

            Assert.IsTrue(successPassImage && (spy_repository.AddOrUpdatePhotoUserId == 1));
        }
コード例 #4
0
        public void UploadImageShouldReturnOKTest()
        {
            UserRepositorySpy spy_repository = new UserRepositorySpy();
            CredentialsCheckerSpy_AlwaysPass spy_checker = new CredentialsCheckerSpy_AlwaysPass();
            UsersController target = new UsersController(spy_repository, spy_checker);
            byte[] fakeImage = new byte[] { 10, 11, 12 };
            UploadImageData arg = new UploadImageData() { FileName = "image1.jpg", ImageType = "image/jpeg", ImageBuffer = fakeImage };

            var config = new HttpConfiguration();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/1.0/users/UploadImage");
            var route = config.Routes.MapHttpRoute("TestApi", "api/1.0/{controller}/{action}");
            var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "users" }, { "action", "UploadImage" } });
            target.ControllerContext = new HttpControllerContext(config, routeData, request);
            target.Request = request;
            target.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;

            var actual = target.UploadImage(arg);

            Assert.AreEqual(HttpStatusCode.OK, actual.StatusCode);
        }
コード例 #5
0
        public void UnFollowShouldReturnOKTest()
        {
            UserRepositorySpy spy_repository = new UserRepositorySpy();
            CredentialsCheckerSpy_AlwaysPass spy_checker = new CredentialsCheckerSpy_AlwaysPass();
            UsersController target = new UsersController(spy_repository, spy_checker);

            var config = new HttpConfiguration();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/1.0/users/UnFollow");
            var route = config.Routes.MapHttpRoute("TestApi", "api/1.0/{controller}/{action}");
            var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "users" }, { "action", "UnFollow" } });
            target.ControllerContext = new HttpControllerContext(config, routeData, request);
            target.Request = request;
            target.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;

            var actual = target.UnFollow(2);

            Assert.AreEqual(actual.StatusCode, HttpStatusCode.OK);
        }
コード例 #6
0
        public void UpdateShuldCallRepositoryTest()
        {
            UserRepositorySpy spy_repository = new UserRepositorySpy();
            CredentialsCheckerSpy_AlwaysPass spy_checker = new CredentialsCheckerSpy_AlwaysPass();
            UsersController target = new UsersController(spy_repository, spy_checker);
            UserUpdateData userUpdateData = new UserUpdateData() { Email = "*****@*****.**", FirstName = "First name", LastName = "Last name" };
            spy_repository.GetSingleUserResult = new User() { Id = 1, Login = "******" };

            var config = new HttpConfiguration();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/1.0/users/update");
            var route = config.Routes.MapHttpRoute("TestApi", "api/1.0/{controller}/{action}");
            var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "users" }, { "action", "update" } });
            target.ControllerContext = new HttpControllerContext(config, routeData, request);
            target.Request = request;
            target.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;

            var actual = target.Update(userUpdateData);

            Assert.IsTrue
                (spy_repository.UpdateUserUser != null &&
                spy_repository.UpdateUserUser.Email == userUpdateData.Email &&
                spy_repository.UpdateUserUser.FirstName == userUpdateData.FirstName &&
                spy_repository.UpdateUserUser.LastName == userUpdateData.LastName &&
                spy_repository.UpdateUserUser.Id == 1);
        }
コード例 #7
0
        public void StatsShouldCallRepositoryyTest()
        {
            UserRepositorySpy spy_repository = new UserRepositorySpy();
            CredentialsCheckerSpy_AlwaysPass spy_checker = new CredentialsCheckerSpy_AlwaysPass();
            UsersController target = new UsersController(spy_repository, spy_checker);
            spy_repository.GetCountReadersResult = 2;
            spy_repository.GetCountReadUsersResult = 3;
            spy_repository.GetCountTweetsByUserIdResult = 4;

            var config = new HttpConfiguration();
            var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/1.0/users/stats");
            var route = config.Routes.MapHttpRoute("TestApi", "api/1.0/{controller}/{action}");
            var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "users" }, { "action", "stats" } });
            target.ControllerContext = new HttpControllerContext(config, routeData, request);
            target.Request = request;
            target.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;

            var actual = target.Stats(1);

            Assert.IsTrue
                (spy_repository.GetCountReadersUserId == 1 &&
                spy_repository.GetCountReadUsersUserId == 1 &&
                spy_repository.GetCountTweetsByUserIdUserId == 1 &&
                actual != null &&
                actual.CountReaders == 2 &&
                actual.CountReadUsers == 3 &&
                actual.CountTweetsByUser == 4);
        }
コード例 #8
0
        public void SingleShouldRiseNotFoundIfNotFoundTest()
        {
            UserRepositorySpy spy_repository = new UserRepositorySpy();
            CredentialsCheckerSpy_AlwaysPass spy_checker = new CredentialsCheckerSpy_AlwaysPass();
            UsersController target = new UsersController(spy_repository, spy_checker);
            spy_repository.GetSingleUserResult = null;

            var config = new HttpConfiguration();
            var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/1.0/users/single");
            var route = config.Routes.MapHttpRoute("TestApi", "api/1.0/{controller}/{action}");
            var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "users" }, { "action", "single" } });
            target.ControllerContext = new HttpControllerContext(config, routeData, request);
            target.Request = request;
            target.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;

            try
            {
                var actual = target.Single(10);
                Assert.IsTrue(false);
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(HttpResponseException));
            }
        }
コード例 #9
0
        public void SingleShouldCallRepositoryTest()
        {
            UserRepositorySpy spy_repository = new UserRepositorySpy();
            CredentialsCheckerSpy_AlwaysPass spy_checker = new CredentialsCheckerSpy_AlwaysPass();
            UsersController target = new UsersController(spy_repository, spy_checker);
            spy_repository.GetSingleUserResult = new User() { Id = 10, Login = "******" };

            var config = new HttpConfiguration();
            var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/1.0/users/single");
            var route = config.Routes.MapHttpRoute("TestApi", "api/1.0/{controller}/{action}");
            var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "users" }, { "action", "single" } });
            target.ControllerContext = new HttpControllerContext(config, routeData, request);
            target.Request = request;
            target.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;

            var actual = target.Single(10);

            Assert.AreEqual(spy_repository.GetSingleUserId, actual.Id);
        }
コード例 #10
0
        public void CreateShouldRiseExceptionOnEmptyLoginPassowrdTest()
        {
            UserRepositorySpy spy_repository = new UserRepositorySpy();
            CredentialsCheckerSpy_AlwaysPass spy_checker = new CredentialsCheckerSpy_AlwaysPass();
            UsersController target = new UsersController(spy_repository, spy_checker);
            UserCreateData userCreateData = new UserCreateData() { Login = string.Empty, Password = string.Empty };
            spy_repository.CreateUserResult = new User() { Login = string.Empty };

            var config = new HttpConfiguration();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/1.0/users/create");
            var route = config.Routes.MapHttpRoute("TestApi", "api/1.0/{controller}/{action}");
            var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "users" }, { "action", "create" } });
            target.ControllerContext = new HttpControllerContext(config, routeData, request);
            target.Request = request;
            target.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;

            try
            {
                User actual = target.Create(userCreateData);
                Assert.IsTrue(false);
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(HttpResponseException));
            }
        }
コード例 #11
0
        public void CreateShouldReturnUserObjectTest()
        {
            UserRepositorySpy spy_repository = new UserRepositorySpy();
            CredentialsCheckerSpy_AlwaysPass spy_checker = new CredentialsCheckerSpy_AlwaysPass();
            UsersController target = new UsersController(spy_repository, spy_checker);
            UserCreateData userCreateData = new UserCreateData() { Login = "******", Password = "******" };
            spy_repository.CreateUserResult = new User() { Login = "******" };

            var config = new HttpConfiguration();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/1.0/users/create");
            var route = config.Routes.MapHttpRoute("TestApi", "api/1.0/{controller}/{action}");
            var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "users" }, { "action", "create" } });
            target.ControllerContext = new HttpControllerContext(config, routeData, request);
            target.Request = request;
            target.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;

            User actual = target.Create(userCreateData);

            Assert.IsTrue(actual != null && actual.Login == userCreateData.Login);
        }