public void WhenGettingUserWithNullToken_ThenThrowsArgumentNullException()
        {
            var services = new UserServices(new Mock<IUserRepository>().Object);

            var ex = Assert.Throws<BusinessServicesException>(() => services.GetUserByClaimedIdentifier(null));
            Assert.IsType<ArgumentNullException>(ex.InnerException);
        }
        public void WhenCreatingUser_ThenDelegatesToUserRepository()
        {
            var userRepositoryMock = new Mock<IUserRepository>();

            var services = new UserServices(userRepositoryMock.Object);

            services.CreateUser(new User() {AuthorizationId = "authId"});

            userRepositoryMock.Verify(r => r.Create(It.IsAny<MileageStats.Model.User>()), Times.Once());
        }
        public void WhenGettingUser_ThenReturnsUser()
        {
            var userRepositoryMock = new Mock<IUserRepository>();
            var dataUser = new Model.User {UserId = 1, DisplayName = "test"};

            userRepositoryMock
                .Setup(ur => ur.GetByAuthenticatedId(It.IsAny<string>()))
                .Returns(dataUser);

            var services = new UserServices(userRepositoryMock.Object);

            var returnedUser = services.GetUserByClaimedIdentifier("something");

            Assert.NotNull(returnedUser);
            Assert.Equal("test", returnedUser.DisplayName);
        }
        public void WhenCallingGetOrCreateUserWithNoMatchingUser_ThenUserIsCreated()
        {
            var userRepositoryMock = new Mock<IUserRepository>();
            userRepositoryMock.Setup(u => u.Create(It.IsAny<MileageStats.Model.User>()))
                .Callback<MileageStats.Model.User>(user => user.UserId = 12);

            Model.User userToReturn = null;
            userRepositoryMock.Setup(u => u.GetByAuthenticatedId(It.IsAny<string>())).Returns(userToReturn);
            userRepositoryMock.Setup(u => u.Create(It.IsAny<Model.User>()))
                .Callback<MileageStats.Model.User>(user => user.UserId = 12);

            var services = new UserServices(userRepositoryMock.Object);

            User createdUser = services.GetOrCreateUser("claimedId");

            userRepositoryMock.Verify(u => u.Create(It.IsAny<Model.User>()), Times.Once());
        }
        public void WhenCallingGetOrCreateUserWithMatchingUser_ThenExistingUserIsReturned()
        {
            var userRepositoryMock = new Mock<IUserRepository>();
            userRepositoryMock.Setup(u => u.Create(It.IsAny<MileageStats.Model.User>()))
                .Callback<MileageStats.Model.User>(user => user.UserId = 12);
            string claimedId = "claimedId";

            Model.User userToReturn = new Model.User() {AuthorizationId = claimedId};
            userRepositoryMock.Setup(u => u.GetByAuthenticatedId(It.IsAny<string>())).Returns(userToReturn);

            var services = new UserServices(userRepositoryMock.Object);

            User createdUser = services.GetOrCreateUser(claimedId);

            userRepositoryMock.Verify(u => u.Create(It.IsAny<Model.User>()), Times.Never());
            Assert.Equal(claimedId, createdUser.AuthorizationId);
        }
        public void WhenUpdatingUser_ThenDelegatesToUserRepository()
        {
            var userRepositoryMock = new Mock<IUserRepository>();
            userRepositoryMock.Setup(u => u.Create(It.IsAny<MileageStats.Model.User>()))
                .Callback<MileageStats.Model.User>(u => u.UserId = 12);

            var services = new UserServices(userRepositoryMock.Object);

            User user = new User() {AuthorizationId = "authId"};
            services.CreateUser(user);
            user.AuthorizationId = "newAuthId";
            services.UpdateUser(user);

            userRepositoryMock.Verify(r => r.Update(It.IsAny<MileageStats.Model.User>()), Times.Once());
        }
        public void WhenUpdatingUserAndRepositoryThrows_ThenWrapsException()
        {
            var userRepositoryMock = new Mock<IUserRepository>();
            userRepositoryMock.Setup(u => u.Update(It.IsAny<MileageStats.Model.User>()))
                .Throws<InvalidOperationException>();

            var services = new UserServices(userRepositoryMock.Object);

            var ex = Assert.Throws<BusinessServicesException>(
                () => services.UpdateUser(new User() {AuthorizationId = "authId"}));
            Assert.IsType<InvalidOperationException>(ex.InnerException);
        }
        public void WhenGettingUserAndRepositoryThrows_ThenWrapsException()
        {
            var userRepositoryMock = new Mock<IUserRepository>();
            userRepositoryMock.Setup(u => u.GetByAuthenticatedId(It.IsAny<string>()))
                .Throws<InvalidOperationException>();

            var services = new UserServices(userRepositoryMock.Object);

            var ex = Assert.Throws<BusinessServicesException>(() => services.GetUserByClaimedIdentifier("test"));
            Assert.IsType<InvalidOperationException>(ex.InnerException);
        }
        public void WhenCreatingUser_ThenReturnsUpdatedUserWithUserId()
        {
            var userRepositoryMock = new Mock<IUserRepository>();
            userRepositoryMock.Setup(u => u.Create(It.IsAny<MileageStats.Model.User>()))
                .Callback<MileageStats.Model.User>(user => user.UserId = 12);
            var services = new UserServices(userRepositoryMock.Object);

            User servicesUser = new User() {AuthorizationId = "authId"};
            Assert.Equal(0, servicesUser.UserId);
            User createdUser = services.CreateUser(servicesUser);

            Assert.Equal(12, createdUser.UserId);
        }