Exemple #1
0
        public void UploadVideo_ShouldCreateVideo()
        {
            var videoRepositoryMock   = new Mock <IRepository <Video> >();
            var unitOfWorkMock        = new Mock <IUnitOfWork>();
            var commentRepositoryMock = new Mock <IRepository <Comment> >();

            Video video = null;

            videoRepositoryMock.Setup(x => x.Add(It.IsAny <Video>()))
            .Callback <Video>(r => video = r);;

            Guid id = Guid.NewGuid();

            VideoServices videoService = new VideoServices(videoRepositoryMock.Object, unitOfWorkMock.Object, commentRepositoryMock.Object);

            VidconfileUser user = new VidconfileUser();

            byte[] videoData    = new byte[2];
            string thumbnailUrl = "far";
            string description  = "hi";
            string title        = "fasd";

            videoService.UploadVideo(user, videoData, description, thumbnailUrl, title);

            Assert.Same(user, video.Uploader);
            Assert.Same(videoData, video.VideoData);
            Assert.Same(thumbnailUrl, video.ThumbnailUrl);
            Assert.Same(description, video.Description);
            Assert.Same(title, video.Title);

            videoRepositoryMock.Verify(x => x.Add(It.IsAny <Video>()), Times.Once());
            unitOfWorkMock.Verify(x => x.Commit(), Times.Once());
        }
Exemple #2
0
        public IActionResult AddComment(AddCommentApiModel commentModel)
        {
            Guid cl = Guid.Parse(this.User.Claims
                                 .FirstOrDefault(x => !x.Properties.FirstOrDefault(v => v.Value == "nameid").Equals(default(KeyValuePair <string, string>)))
                                 .Value);

            if (string.IsNullOrEmpty(commentModel.CommentText))
            {
                return(NotFound("Comment can't be empty"));
            }

            VidconfileUser user = this.userServices.GetUserById(cl);

            if (user == null)
            {
                return(Unauthorized());
            }

            Video video = this.videoServices.GetVideoById(commentModel.VideoId);

            if (video == null)
            {
                return(NotFound("Video is not found"));
            }

            var commentRes = this.commentServices.AddComment(video, user, commentModel.CommentText);

            GetAllCommentsApiModel model = this.mapper.Map <GetAllCommentsApiModel>(commentRes);

            return(Ok(model));
        }
        public void UploadVideo(VidconfileUser uploader, byte[] videoData, string description, string thumbnailUrl, string title)
        {
            if (string.IsNullOrEmpty(description))
            {
                throw new NullReferenceException("description cannot be null");
            }

            if (string.IsNullOrEmpty(thumbnailUrl))
            {
                throw new NullReferenceException("thumbnailUrl cannot be null");
            }

            if (string.IsNullOrEmpty(title))
            {
                throw new NullReferenceException("title cannot be null");
            }

            Video video = new Video();

            video.Uploader     = uploader ?? throw new NullReferenceException("uploader cannot be null");
            video.Title        = title;
            video.ThumbnailUrl = thumbnailUrl;
            video.Description  = description;
            video.VideoData    = videoData ?? throw new NullReferenceException("videoData cannot be null");
            video.Created      = DateTime.Now;

            this.videoRepository.Add(video);

            this.unitOfWork.Commit();
        }
Exemple #4
0
        public void GetUserByVideoId_ShouldGetUser()
        {
            var videoRepositoryMock       = new Mock <IRepository <Video> >();
            var unitOfWorkMock            = new Mock <IUnitOfWork>();
            var commentRepositoryMock     = new Mock <IRepository <Comment> >();
            var userRepositoryMock        = new Mock <IRepository <VidconfileUser> >();
            var passwordHasherMock        = new Mock <IPasswordHasher>();
            var subscribeToSubscriberMock = new Mock <IRepository <SubscribeToSubscribers> >();

            Guid id = Guid.NewGuid();

            var video = new Video();

            video.Id = id;

            VidconfileUser user = new VidconfileUser();

            video.Uploader = user;

            var videos = new List <Video>();

            videos.Add(video);

            videoRepositoryMock.Setup(x => x.All(s => s.Uploader))
            .Returns(videos.AsQueryable());

            VidconfileUserServices userService =
                new VidconfileUserServices(userRepositoryMock.Object, unitOfWorkMock.Object, passwordHasherMock.Object,
                                           videoRepositoryMock.Object, subscribeToSubscriberMock.Object);

            var res = userService.GetUserByVideoId(id);

            Assert.Same(user, res);
            videoRepositoryMock.Verify(x => x.All(s => s.Uploader), Times.Once);
        }
Exemple #5
0
        public void Register(string username, string password, string profilePhotoUrl)
        {
            if (string.IsNullOrEmpty(username))
            {
                throw new NullReferenceException("username cannot be null");
            }

            if (string.IsNullOrEmpty(password))
            {
                throw new NullReferenceException("password cannot be null");
            }

            if (string.IsNullOrEmpty(profilePhotoUrl))
            {
                throw new NullReferenceException("profilePhotoUrl cannot be null");
            }

            PasswordHashModel hashModel = this.passwordHasher.CreatePasswordHash(password);

            var user = new VidconfileUser();

            user.PasswordHash = hashModel.PasswordHash;
            user.PasswordSalt = hashModel.PasswordSalt;

            user.Username        = username;
            user.ProfilePhotoUrl = profilePhotoUrl;

            this.userRepository.Add(user);

            this.unitOfWork.Commit();
        }
Exemple #6
0
        public IActionResult Login(LoginUserApiModel loginUser)
        {
            VidconfileUser user = this.userServices.Login(loginUser.Username, loginUser.Password);

            if (user == null)
            {
                return(Unauthorized());
            }

            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
                new Claim(ClaimTypes.Name, user.Username)
            };

            SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(this.config.GetSection(AppSettingsConstants.Token).Value));

            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);

            var tokenDescriptor = new SecurityTokenDescriptor()
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(1),
                SigningCredentials = creds
            };

            var tokenHandler = new JwtSecurityTokenHandler();

            var token = tokenHandler.CreateToken(tokenDescriptor);

            return(Ok(new
            {
                token = tokenHandler.WriteToken(token)
            }));
        }
        public void AddComment_ShouldAdd()
        {
            var videoRepositoryMock   = new Mock <IRepository <Video> >();
            var unitOfWorkMock        = new Mock <IUnitOfWork>();
            var commentRepositoryMock = new Mock <IRepository <Comment> >();
            var userRepositoryMock    = new Mock <IRepository <VidconfileUser> >();

            Comment value = null;

            commentRepositoryMock.Setup(x => x.Add(It.IsAny <Comment>()))
            .Callback <Comment>(x => value = x);

            CommentServices commentService =
                new CommentServices(commentRepositoryMock.Object, unitOfWorkMock.Object, userRepositoryMock.Object, videoRepositoryMock.Object);

            Video          video       = new Video();
            VidconfileUser user        = new VidconfileUser();
            string         commentText = "asdasd";

            commentService.AddComment(video, user, commentText);

            Assert.Same(video, value.Video);
            Assert.Same(user, value.Author);
            Assert.Same(commentText, value.CommentText);

            commentRepositoryMock.Verify(x => x.Add(value), Times.Once);
            unitOfWorkMock.Verify(x => x.Commit(), Times.Once);
        }
Exemple #8
0
        public VidconfileUser Login(string username, string password)
        {
            if (string.IsNullOrEmpty(username))
            {
                throw new NullReferenceException("username cannot be null or empty");
            }

            if (string.IsNullOrEmpty(password))
            {
                throw new NullReferenceException("password cannot be null or empty");
            }

            VidconfileUser user = this.userRepository
                                  .All()
                                  .FirstOrDefault(x => x.Username == username);

            if (user == null)
            {
                return(null);
            }

            if (this.passwordHasher.VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt) == false)
            {
                return(null);
            }

            return(user);
        }
Exemple #9
0
        public void SubscribeFromTo(VidconfileUser from, VidconfileUser to)
        {
            if (from == null)
            {
                throw new NullReferenceException("from cannot be null");
            }

            if (to == null)
            {
                throw new NullReferenceException("to cannot be null");
            }

            if (this.IsSubscribed(from, to))
            {
                throw new ArgumentException("the user is already subscribed");
            }

            var subscriberToSubscribers = new SubscribeToSubscribers();

            subscriberToSubscribers.SubscriberId = from.Id;

            subscriberToSubscribers.SubscribedToId = to.Id;

            this.subscribeToSubscribersRepository.Add(subscriberToSubscribers);

            this.unitOfWork.Commit();
        }
Exemple #10
0
        public int GetSubscriberCount(VidconfileUser user)
        {
            if (user == null)
            {
                throw new NullReferenceException("user cannot be null");
            }

            var count = this.subscribeToSubscribersRepository.All()
                        .Where(x => x.SubscribedToId == user.Id).Count();

            return(count);
        }
Exemple #11
0
        public VidconfileUser GetUserByVideoId(Guid videoId)
        {
            Video video = this.videoRepository.All(x => x.Uploader)
                          .FirstOrDefault(x => x.Id == videoId);

            if (video == null)
            {
                return(null);
            }

            VidconfileUser user = video.Uploader;

            return(user);
        }
        public IActionResult GetUser(Guid id)
        {
            VidconfileUser user = this.userServices.GetUserByIdWithVideos(id);

            if (user == null)
            {
                return(NotFound("User not found"));
            }

            GetUserApiModel model = this.mapper.Map <GetUserApiModel>(user);


            model.SubscriberCount = this.userServices.GetSubscriberCount(user);

            return(Ok(model));
        }
Exemple #13
0
        public void SubscribeFromTo_ShouldSubscribe()
        {
            var videoRepositoryMock       = new Mock <IRepository <Video> >();
            var unitOfWorkMock            = new Mock <IUnitOfWork>();
            var commentRepositoryMock     = new Mock <IRepository <Comment> >();
            var userRepositoryMock        = new Mock <IRepository <VidconfileUser> >();
            var passwordHasherMock        = new Mock <IPasswordHasher>();
            var subscribeToSubscriberMock = new Mock <IRepository <SubscribeToSubscribers> >();

            Guid fromId = Guid.NewGuid();
            Guid toId   = Guid.NewGuid();

            VidconfileUser from = new VidconfileUser();
            VidconfileUser to   = new VidconfileUser();

            from.Id = fromId;
            to.Id   = toId;

            var sb = new SubscribeToSubscribers();

            sb.SubscribedToId = toId;
            sb.SubscriberId   = Guid.Empty;

            SubscribeToSubscribers res = null;

            subscribeToSubscriberMock.Setup(x => x.All())
            .Returns(new List <SubscribeToSubscribers>()
            {
                sb
            }.AsQueryable());

            subscribeToSubscriberMock.Setup(x => x.Add(It.IsAny <SubscribeToSubscribers>()))
            .Callback <SubscribeToSubscribers>(x => res = x)
            .Verifiable();

            VidconfileUserServices userService =
                new VidconfileUserServices(userRepositoryMock.Object, unitOfWorkMock.Object, passwordHasherMock.Object,
                                           videoRepositoryMock.Object, subscribeToSubscriberMock.Object);

            userService.SubscribeFromTo(from, to);

            subscribeToSubscriberMock.Verify();
            unitOfWorkMock.Verify(x => x.Commit(), Times.Once);

            Assert.Equal(toId, res.SubscribedToId);
            Assert.Equal(fromId, res.SubscriberId);
        }
        public IActionResult EditProfile(EditProfileApiModel model)
        {
            Guid cl = Guid.Parse(this.User.Claims
                                 .FirstOrDefault(x => !x.Properties.FirstOrDefault(v => v.Value == "nameid").Equals(default(KeyValuePair <string, string>)))
                                 .Value);

            VidconfileUser user = this.userServices.GetUserById(cl);

            if (user == null)
            {
                return(BadRequest("User does not exist"));
            }

            this.userServices.EditProfile(user, model.AuthorProfilePhotoUrl);

            return(Ok());
        }
Exemple #15
0
        public bool IsSubscribed(VidconfileUser from, VidconfileUser to)
        {
            if (from == null)
            {
                throw new NullReferenceException("from cannot be null");
            }

            if (to == null)
            {
                throw new NullReferenceException("to cannot be null");
            }

            bool isSubscribed = this.subscribeToSubscribersRepository.All()
                                .FirstOrDefault(x => x.SubscribedToId == to.Id && x.SubscriberId == from.Id) != null;

            return(isSubscribed);
        }
Exemple #16
0
        public void EditProfile(VidconfileUser user, string newProfilePhotoUrl)
        {
            if (user == null)
            {
                throw new NullReferenceException("user cannot be null");
            }

            if (string.IsNullOrEmpty(newProfilePhotoUrl))
            {
                throw new NullReferenceException("newProfilePhotoUrl cannot be null");
            }

            user.ProfilePhotoUrl = newProfilePhotoUrl;

            this.userRepository.Update(user);

            this.unitOfWork.Commit();
        }
Exemple #17
0
        public void GetSubscriberCount_NullUser_ShouldThrow()
        {
            var videoRepositoryMock       = new Mock <IRepository <Video> >();
            var unitOfWorkMock            = new Mock <IUnitOfWork>();
            var commentRepositoryMock     = new Mock <IRepository <Comment> >();
            var userRepositoryMock        = new Mock <IRepository <VidconfileUser> >();
            var passwordHasherMock        = new Mock <IPasswordHasher>();
            var subscribeToSubscriberMock = new Mock <IRepository <SubscribeToSubscribers> >();

            VidconfileUser user = new VidconfileUser();

            VidconfileUserServices userService =
                new VidconfileUserServices(userRepositoryMock.Object, unitOfWorkMock.Object, passwordHasherMock.Object,
                                           videoRepositoryMock.Object, subscribeToSubscriberMock.Object);

            string result = Assert.Throws <NullReferenceException>(() => userService.GetSubscriberCount(null))
                            .Message;

            Assert.Equal("user cannot be null", result);
        }
        public void AddComment_NullVideo_ShouldThrow()
        {
            var videoRepositoryMock   = new Mock <IRepository <Video> >();
            var unitOfWorkMock        = new Mock <IUnitOfWork>();
            var commentRepositoryMock = new Mock <IRepository <Comment> >();
            var userRepositoryMock    = new Mock <IRepository <VidconfileUser> >();

            CommentServices commentService =
                new CommentServices(commentRepositoryMock.Object, unitOfWorkMock.Object, userRepositoryMock.Object, videoRepositoryMock.Object);

            Video          video       = new Video();
            VidconfileUser user        = new VidconfileUser();
            string         commentText = "asdasd";

            string message = Assert
                             .Throws <NullReferenceException>(() => commentService.AddComment(null, user, commentText))
                             .Message;

            Assert.Equal("video cannot be null", message);
        }
Exemple #19
0
        public Comment AddComment(Video video, VidconfileUser author, string commentText)
        {
            if (string.IsNullOrEmpty(commentText))
            {
                throw new NullReferenceException("commentText cannot be null or empty");
            }

            Comment comment = new Comment();

            comment.Video       = video ?? throw new NullReferenceException("video cannot be null");
            comment.Author      = author ?? throw new NullReferenceException("author cannot be null");
            comment.Created     = DateTime.Now;
            comment.CommentText = commentText;

            this.commentRepository.Add(comment);

            this.unitOfWork.Commit();

            return(comment);
        }
Exemple #20
0
        public void EditProfile_ShouldUpdate()
        {
            var videoRepositoryMock       = new Mock <IRepository <Video> >();
            var unitOfWorkMock            = new Mock <IUnitOfWork>();
            var commentRepositoryMock     = new Mock <IRepository <Comment> >();
            var userRepositoryMock        = new Mock <IRepository <VidconfileUser> >();
            var passwordHasherMock        = new Mock <IPasswordHasher>();
            var subscribeToSubscriberMock = new Mock <IRepository <SubscribeToSubscribers> >();

            Guid userId = Guid.NewGuid();

            VidconfileUser user = new VidconfileUser();

            string photoUrl = "asd";

            subscribeToSubscriberMock.Setup(x => x.All())
            .Returns(new List <SubscribeToSubscribers>()
            {
                new SubscribeToSubscribers()
                {
                    SubscribedToId = userId
                },
                new SubscribeToSubscribers()
                {
                    SubscribedToId = userId
                }, new SubscribeToSubscribers()
                {
                    SubscribedToId = userId
                }
            }.AsQueryable());

            VidconfileUserServices userService =
                new VidconfileUserServices(userRepositoryMock.Object, unitOfWorkMock.Object, passwordHasherMock.Object,
                                           videoRepositoryMock.Object, subscribeToSubscriberMock.Object);

            userService.EditProfile(user, photoUrl);

            Assert.Equal(photoUrl, user.ProfilePhotoUrl);
            userRepositoryMock.Verify(x => x.Update(user), Times.Once);
            unitOfWorkMock.Verify(x => x.Commit(), Times.Once);
        }
Exemple #21
0
        public void UnsubscribeFromTo_NullTo_ShouldThrow()
        {
            var videoRepositoryMock       = new Mock <IRepository <Video> >();
            var unitOfWorkMock            = new Mock <IUnitOfWork>();
            var commentRepositoryMock     = new Mock <IRepository <Comment> >();
            var userRepositoryMock        = new Mock <IRepository <VidconfileUser> >();
            var passwordHasherMock        = new Mock <IPasswordHasher>();
            var subscribeToSubscriberMock = new Mock <IRepository <SubscribeToSubscribers> >();

            VidconfileUser from = new VidconfileUser();
            VidconfileUser to   = new VidconfileUser();

            VidconfileUserServices userService =
                new VidconfileUserServices(userRepositoryMock.Object, unitOfWorkMock.Object, passwordHasherMock.Object,
                                           videoRepositoryMock.Object, subscribeToSubscriberMock.Object);

            string msg = Assert.Throws <NullReferenceException>(() => userService.UnsubscribeFromTo(from, null))
                         .Message;

            Assert.Equal("to cannot be null", msg);
        }
Exemple #22
0
        public void EditProfile_PhotoNull_ShouldThrow()
        {
            var videoRepositoryMock       = new Mock <IRepository <Video> >();
            var unitOfWorkMock            = new Mock <IUnitOfWork>();
            var commentRepositoryMock     = new Mock <IRepository <Comment> >();
            var userRepositoryMock        = new Mock <IRepository <VidconfileUser> >();
            var passwordHasherMock        = new Mock <IPasswordHasher>();
            var subscribeToSubscriberMock = new Mock <IRepository <SubscribeToSubscribers> >();

            Guid userId = Guid.NewGuid();

            VidconfileUser user = new VidconfileUser();

            string photoUrl = "asd";

            subscribeToSubscriberMock.Setup(x => x.All())
            .Returns(new List <SubscribeToSubscribers>()
            {
                new SubscribeToSubscribers()
                {
                    SubscribedToId = userId
                },
                new SubscribeToSubscribers()
                {
                    SubscribedToId = userId
                }, new SubscribeToSubscribers()
                {
                    SubscribedToId = userId
                }
            }.AsQueryable());

            VidconfileUserServices userService =
                new VidconfileUserServices(userRepositoryMock.Object, unitOfWorkMock.Object, passwordHasherMock.Object,
                                           videoRepositoryMock.Object, subscribeToSubscriberMock.Object);

            string msg = Assert.Throws <NullReferenceException>(() => userService.EditProfile(user, null))
                         .Message;

            Assert.Equal("newProfilePhotoUrl cannot be null", msg);
        }
        public IActionResult IsSubscribed(Guid isSubscribedUser)
        {
            Guid cl = Guid.Parse(this.User.Claims
                                 .FirstOrDefault(x => !x.Properties.FirstOrDefault(v => v.Value == "nameid").Equals(default(KeyValuePair <string, string>)))
                                 .Value);

            VidconfileUser userFrom = this.userServices.GetUserById(cl);
            VidconfileUser userTo   = this.userServices.GetUserById(isSubscribedUser);

            if (userFrom == null || userTo == null)
            {
                return(BadRequest("User to unsubscribe to does not exist"));
            }


            bool isSubscribed = this.userServices.IsSubscribed(userFrom, userTo);

            return(Ok(new
            {
                isSubscribed
            }));
        }
Exemple #24
0
        public void GetSubscriberCount_ShouldGet()
        {
            var videoRepositoryMock       = new Mock <IRepository <Video> >();
            var unitOfWorkMock            = new Mock <IUnitOfWork>();
            var commentRepositoryMock     = new Mock <IRepository <Comment> >();
            var userRepositoryMock        = new Mock <IRepository <VidconfileUser> >();
            var passwordHasherMock        = new Mock <IPasswordHasher>();
            var subscribeToSubscriberMock = new Mock <IRepository <SubscribeToSubscribers> >();

            VidconfileUser user = new VidconfileUser();

            Guid userId = Guid.NewGuid();

            user.Id = userId;

            subscribeToSubscriberMock.Setup(x => x.All())
            .Returns(new List <SubscribeToSubscribers>()
            {
                new SubscribeToSubscribers()
                {
                    SubscribedToId = userId
                },
                new SubscribeToSubscribers()
                {
                    SubscribedToId = userId
                }, new SubscribeToSubscribers()
                {
                    SubscribedToId = userId
                }
            }.AsQueryable());

            VidconfileUserServices userService =
                new VidconfileUserServices(userRepositoryMock.Object, unitOfWorkMock.Object, passwordHasherMock.Object,
                                           videoRepositoryMock.Object, subscribeToSubscriberMock.Object);

            int subs = userService.GetSubscriberCount(user);

            Assert.Equal(3, subs);
        }
Exemple #25
0
        public void UnsubscribeFromTo_AlreadySubscribed_ShouldThrow()
        {
            var videoRepositoryMock       = new Mock <IRepository <Video> >();
            var unitOfWorkMock            = new Mock <IUnitOfWork>();
            var commentRepositoryMock     = new Mock <IRepository <Comment> >();
            var userRepositoryMock        = new Mock <IRepository <VidconfileUser> >();
            var passwordHasherMock        = new Mock <IPasswordHasher>();
            var subscribeToSubscriberMock = new Mock <IRepository <SubscribeToSubscribers> >();

            Guid fromId = Guid.NewGuid();
            Guid toId   = Guid.NewGuid();

            VidconfileUser from = new VidconfileUser();
            VidconfileUser to   = new VidconfileUser();

            from.Id = fromId;
            to.Id   = toId;

            var sb = new SubscribeToSubscribers();

            sb.SubscribedToId = toId;
            sb.SubscriberId   = Guid.Empty;

            subscribeToSubscriberMock.Setup(x => x.All())
            .Returns(new List <SubscribeToSubscribers>()
            {
                sb
            }.AsQueryable());

            VidconfileUserServices userService =
                new VidconfileUserServices(userRepositoryMock.Object, unitOfWorkMock.Object, passwordHasherMock.Object,
                                           videoRepositoryMock.Object, subscribeToSubscriberMock.Object);

            string msg = Assert.Throws <ArgumentException>(() => userService.UnsubscribeFromTo(from, to))
                         .Message;

            Assert.Equal("the user is not subscribed", msg);
        }
        public IActionResult Unsubscribe(Guid userToUnsubscribeTo)
        {
            Guid cl = Guid.Parse(this.User.Claims
                                 .FirstOrDefault(x => !x.Properties.FirstOrDefault(v => v.Value == "nameid").Equals(default(KeyValuePair <string, string>)))
                                 .Value);

            VidconfileUser userFrom = this.userServices.GetUserById(cl);
            VidconfileUser userTo   = this.userServices.GetUserById(userToUnsubscribeTo);

            if (userFrom == null || userTo == null)
            {
                return(BadRequest("User to unsubscribe to does not exist"));
            }

            if (!this.userServices.IsSubscribed(userFrom, userTo))
            {
                return(BadRequest("You are not subscribed to this user"));
            }

            this.userServices.UnsubscribeFromTo(userFrom, userTo);

            return(Ok());
        }
Exemple #27
0
        public void UploadVideo_NullDescription_ShouldThrow()
        {
            var videoRepositoryMock   = new Mock <IRepository <Video> >();
            var unitOfWorkMock        = new Mock <IUnitOfWork>();
            var commentRepositoryMock = new Mock <IRepository <Comment> >();

            var videos = new List <Video>();

            Guid id = Guid.NewGuid();

            VideoServices videoService = new VideoServices(videoRepositoryMock.Object, unitOfWorkMock.Object, commentRepositoryMock.Object);

            VidconfileUser user = new VidconfileUser();

            byte[] videoData    = new byte[4];
            string thumbnailUrl = "asd";
            string title        = "test";

            string message = Assert.Throws <NullReferenceException>(() => videoService.UploadVideo(user, videoData, null, thumbnailUrl, title))
                             .Message;

            Assert.Same("description cannot be null", message);
        }
Exemple #28
0
        public void Register_ShouldAddUser()
        {
            var videoRepositoryMock       = new Mock <IRepository <Video> >();
            var unitOfWorkMock            = new Mock <IUnitOfWork>();
            var commentRepositoryMock     = new Mock <IRepository <Comment> >();
            var userRepositoryMock        = new Mock <IRepository <VidconfileUser> >();
            var passwordHasherMock        = new Mock <IPasswordHasher>();
            var subscribeToSubscriberMock = new Mock <IRepository <SubscribeToSubscribers> >();

            VidconfileUser user = null;

            userRepositoryMock.Setup(x => x.Add(It.IsAny <VidconfileUser>()))
            .Callback <VidconfileUser>(x => user = x);

            VidconfileUserServices userService =
                new VidconfileUserServices(userRepositoryMock.Object, unitOfWorkMock.Object, passwordHasherMock.Object,
                                           videoRepositoryMock.Object, subscribeToSubscriberMock.Object);

            string username        = "******";
            string password        = "******";
            string profilePhotoUrl = "nubfas";

            PasswordHashModel hashModel = new PasswordHashModel(new byte[1], new byte[2]);

            passwordHasherMock.Setup(x => x.CreatePasswordHash(password))
            .Returns(hashModel)
            .Verifiable();

            userService.Register(username, password, profilePhotoUrl);

            passwordHasherMock.Verify();
            unitOfWorkMock.Verify(x => x.Commit(), Times.Once);
            userRepositoryMock.Verify(x => x.Add(It.IsAny <VidconfileUser>()), Times.Once);

            Assert.Equal(username, user.Username);
            Assert.Equal(profilePhotoUrl, user.ProfilePhotoUrl);
        }
Exemple #29
0
        public void Login_NotWrongPassword_ShouldReturnUser()
        {
            var videoRepositoryMock       = new Mock <IRepository <Video> >();
            var unitOfWorkMock            = new Mock <IUnitOfWork>();
            var commentRepositoryMock     = new Mock <IRepository <Comment> >();
            var userRepositoryMock        = new Mock <IRepository <VidconfileUser> >();
            var passwordHasherMock        = new Mock <IPasswordHasher>();
            var subscribeToSubscriberMock = new Mock <IRepository <SubscribeToSubscribers> >();


            VidconfileUser user = new VidconfileUser();

            string username = "******";
            string password = "******";

            user.Username = username;

            userRepositoryMock.Setup(x => x.All())
            .Returns(new List <VidconfileUser>()
            {
                user
            }.AsQueryable());

            passwordHasherMock.Setup(x => x.VerifyPasswordHash(It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <byte[]>()))
            .Returns(true);

            VidconfileUserServices userService =
                new VidconfileUserServices(userRepositoryMock.Object, unitOfWorkMock.Object, passwordHasherMock.Object,
                                           videoRepositoryMock.Object, subscribeToSubscriberMock.Object);

            var userRet = userService.Login(username, password);

            passwordHasherMock.Verify(x => x.VerifyPasswordHash(It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <byte[]>()), Times.Once);

            Assert.Same(user, userRet);
        }
Exemple #30
0
        public void UnsubscribeFromTo(VidconfileUser from, VidconfileUser to)
        {
            if (from == null)
            {
                throw new NullReferenceException("from cannot be null");
            }

            if (to == null)
            {
                throw new NullReferenceException("to cannot be null");
            }

            if (!this.IsSubscribed(from, to))
            {
                throw new ArgumentException("the user is not subscribed");
            }

            var subToDelete = this.subscribeToSubscribersRepository.All()
                              .FirstOrDefault(x => x.SubscribedToId == to.Id && x.SubscriberId == from.Id);

            this.subscribeToSubscribersRepository.Delete(subToDelete);

            this.unitOfWork.Commit();
        }