Esempio n. 1
0
        public static async Task CreateBuyerAsync_CreateBuyerService_CreateAsync_CalledOnce()
        {
            var context = UsersControllerTestContext.Setup();

            var controller = context.Controller;

            var organisationId             = Guid.NewGuid();
            var createUserRequestViewModel = new CreateBuyerRequestModel
            {
                FirstName    = "Bob",
                LastName     = "Smith",
                PhoneNumber  = "98654321",
                EmailAddress = "*****@*****.**",
            };

            await controller.CreateBuyerAsync(organisationId, createUserRequestViewModel);

            var expected = CreateBuyerRequestBuilder
                           .Create()
                           .WithFirstName(createUserRequestViewModel.FirstName)
                           .WithLastName(createUserRequestViewModel.LastName)
                           .WithPhoneNumber(createUserRequestViewModel.PhoneNumber)
                           .WithEmailAddress(createUserRequestViewModel.EmailAddress)
                           .WithPrimaryOrganisationId(organisationId)
                           .Build();

            context.CreateBuyerServiceMock.Verify(s => s.CreateAsync(expected));
        }
Esempio n. 2
0
        public static async Task CreateBuyerAsync_CreateBuyerFailureResult_ReturnsBadRequest()
        {
            var errors = new List <ErrorDetails> {
                new("TestErrorId", "TestField")
            };

            var context = UsersControllerTestContext.Setup();

            context.CreateBuyerResult = Result.Failure <string>(errors);

            var organisationId             = Guid.NewGuid();
            var createUserRequestViewModel = new CreateBuyerRequestModel();

            var response = await context.Controller.CreateBuyerAsync(organisationId, createUserRequestViewModel);

            response.Should().BeOfType <ActionResult <CreateBuyerResponseModel> >();
            var actual = response.Result;

            var expectedErrors = new List <ErrorMessageViewModel> {
                new("TestErrorId", "TestField")
            };
            var expected = new BadRequestObjectResult(new CreateBuyerResponseModel {
                Errors = expectedErrors
            });

            actual.Should().BeEquivalentTo(expected);
        }
Esempio n. 3
0
        public static async Task CreateBuyerAsync_CreateBuyerSuccessfulResult_ReturnsUserId()
        {
            const string newUserId = "New Test User Id";

            var context = UsersControllerTestContext.Setup();

            context.CreateBuyerResult = Result.Success(newUserId);

            var controller = context.Controller;

            var response = await controller.CreateBuyerAsync(Guid.Empty, new CreateBuyerRequestModel());

            response.Should().BeOfType <ActionResult <CreateBuyerResponseModel> >();
            var actual = response.Result;

            var expectation = new CreatedAtActionResult(
                nameof(controller.GetUserByIdAsync).TrimAsync(),
                null,
                new { userId = newUserId },
                new CreateBuyerResponseModel {
                UserId = newUserId
            });

            actual.Should().BeEquivalentTo(expectation);
        }
Esempio n. 4
0
        public static async Task GetUserById_NoExistingUserId_ReturnsNotFound()
        {
            var context = UsersControllerTestContext.Setup();

            var controller = context.Controller;

            var result = await controller.GetUserByIdAsync(string.Empty);

            result.Result.Should().BeOfType <NotFoundResult>();
        }
Esempio n. 5
0
        public static async Task GetUsersByOrganisationId_UserRepository_GetUsersByOrganisationIdAsync_CalledOnce()
        {
            var context = UsersControllerTestContext.Setup();

            var controller = context.Controller;

            await controller.GetUsersByOrganisationId(Guid.Empty);

            context.UsersRepositoryMock.Verify(r => r.FindByOrganisationIdAsync(Guid.Empty));
        }
Esempio n. 6
0
        public static async Task GetUserById_UserRepository_GetUserByIdAsync_CalledOnce()
        {
            var context = UsersControllerTestContext.Setup();

            var controller = context.Controller;

            await controller.GetUserByIdAsync(string.Empty);

            context.UsersRepositoryMock.Verify(r => r.GetByIdAsync(string.Empty));
        }
Esempio n. 7
0
        public static async Task DisableUserAsync_UserIsNull_ReturnsNotFound()
        {
            var context = UsersControllerTestContext.Setup();

            var controller = context.Controller;

            var result = await controller.DisableUserAsync("unknown");

            result.Should().BeOfType <NotFoundResult>();
        }
Esempio n. 8
0
        public async Task GetUserById_UserRepository_GetUserByIdAsync_CalledOnce()
        {
            var context = UsersControllerTestContext.Setup();

            using var controller = context.Controller;

            await controller.GetUserByIdAsync(string.Empty);

            context.UsersRepositoryMock.Verify(x => x.GetByIdAsync(String.Empty), Times.Once);
        }
Esempio n. 9
0
        public static async Task GetUsersByOrganisationId_ReturnsOkObjectResult()
        {
            var context = UsersControllerTestContext.Setup();

            var controller = context.Controller;

            var result = await controller.GetUsersByOrganisationId(Guid.Empty);

            result.Should().BeOfType <OkObjectResult>();
            result.As <OkObjectResult>().Value.Should().BeOfType <GetAllOrganisationUsersModel>();
        }
Esempio n. 10
0
        public void CreateBuyerAsync_NullApplicationUser_ThrowsException()
        {
            var context = UsersControllerTestContext.Setup();

            async Task <ActionResult <CreateBuyerResponseModel> > CreateUser()
            {
                using var controller = context.Controller;
                return(await controller.CreateBuyerAsync(Guid.Empty, null));
            }

            Assert.ThrowsAsync <ArgumentNullException>(CreateUser);
        }
Esempio n. 11
0
        public static async Task DisableUser_UserRepository_UpdateAsync_CalledOnce()
        {
            var context    = UsersControllerTestContext.Setup();
            var controller = context.Controller;

            context.User = ApplicationUserBuilder.Create().WithDisabled(false).Build();

            await controller.DisableUserAsync(context.User.Id);

            context.UsersRepositoryMock.Verify(r => r.GetByIdAsync(context.User.Id));
            context.UsersRepositoryMock.Verify(r => r.UpdateAsync(context.User));
        }
Esempio n. 12
0
        public async Task GetUsersByOrganisationId_NoUsers_ReturnsEmptyList()
        {
            var context = UsersControllerTestContext.Setup();

            using var controller = context.Controller;

            var result = await controller.GetUsersByOrganisationId(Guid.Empty) as OkObjectResult;

            var users = result.Value as GetAllOrganisationUsersModel;

            users.Users.Should().BeEmpty();
        }
Esempio n. 13
0
        public async Task EnableUserAsync_UserRepository_UpdateAsync_CalledOnce()
        {
            var context = UsersControllerTestContext.Setup();

            using var controller = context.Controller;

            context.User = ApplicationUserBuilder.Create().WithDisabled(true).Build();

            await controller.EnableUserAsync(context.User.Id);

            context.UsersRepositoryMock.Verify(x => x.GetByIdAsync(context.User.Id), Times.Once);
            context.UsersRepositoryMock.Verify(x => x.UpdateAsync(context.User), Times.Once);
        }
Esempio n. 14
0
        public static async Task DisableUserAsync_GetUserByIdAndDisableThem_ReturnsOk()
        {
            var context = UsersControllerTestContext.Setup();

            var controller = context.Controller;

            context.User = ApplicationUserBuilder.Create().WithDisabled(false).Build();

            var result = await controller.DisableUserAsync(context.User.Id);

            result.Should().BeOfType <NoContentResult>();

            context.User.Disabled.Should().BeTrue();
        }
Esempio n. 15
0
        public async Task GetUsersByOrganisationId_ValidId_ReturnsUsers()
        {
            var users = new List <(ApplicationUser RepoUser, OrganisationUserModel Expected)>
            {
                CreateApplicationUserTestData(false),
                CreateApplicationUserTestData(true),
                CreateApplicationUserTestData(false)
            };

            var context = UsersControllerTestContext.Setup();

            context.Users = users.Select(x => x.RepoUser);

            using var controller = context.Controller;

            var result = await controller.GetUsersByOrganisationId(Guid.Empty) as OkObjectResult;

            var viewModel = result.Value as GetAllOrganisationUsersModel;

            viewModel.Users.Should().BeEquivalentTo(users.Select(x => x.Expected));
        }
Esempio n. 16
0
        public static async Task GetUserById_WithExistingUserId_ReturnsTheUser()
        {
            var context = UsersControllerTestContext.Setup();

            context.User = ApplicationUserBuilder.Create().Build();

            var expected = new GetUserModel
            {
                Name                  = context.User.FirstName + " " + context.User.LastName,
                PhoneNumber           = context.User.PhoneNumber,
                EmailAddress          = context.User.Email,
                Disabled              = context.User.Disabled,
                PrimaryOrganisationId = context.User.PrimaryOrganisationId,
            };

            var controller = context.Controller;

            var result = await controller.GetUserByIdAsync(context.User.Id);

            result.Result.Should().BeOfType <OkObjectResult>();
            result.Result.As <OkObjectResult>().Value.Should().BeOfType <GetUserModel>();
            result.Result.As <OkObjectResult>().Value.Should().BeEquivalentTo(expected);
        }
Esempio n. 17
0
        public static async Task GetUsersByOrganisationId_ValidId_ReturnsUsers()
        {
            var users = new List <(ApplicationUser RepoUser, OrganisationUserModel Expected)>
            {
                CreateApplicationUserTestData(false),
                CreateApplicationUserTestData(true),
                CreateApplicationUserTestData(false),
            };

            var context = UsersControllerTestContext.Setup();

            context.Users = users.Select(t => t.RepoUser);

            var controller = context.Controller;

            var result = await controller.GetUsersByOrganisationId(Guid.Empty);

            result.Should().BeOfType <OkObjectResult>();
            result.As <OkObjectResult>().Value.Should().BeOfType <GetAllOrganisationUsersModel>();
            result.As <OkObjectResult>()
            .Value.As <GetAllOrganisationUsersModel>()
            .Users.Should()
            .BeEquivalentTo(users.Select(t => t.Expected));
        }