public async void HandleAsync_WithOrderByRoleAscending_ReturnsSortedResults()
        {
            // Arrage
            var filter = fixture.Create <UserFilter>();
            IFindMatchingUsersDataAccess dataAccess    = CreateFakeDataAccess(filter);
            IWeeeAuthorization           authorization = AuthorizationBuilder.CreateUserWithAllRights();

            FindMatchingUsersHandler handler = new FindMatchingUsersHandler(authorization, dataAccess);

            FindMatchingUsers request = new FindMatchingUsers(1, 1000, FindMatchingUsers.OrderBy.RoleAscending, filter);

            // Act
            UserSearchDataResult response = await handler.HandleAsync(request);

            // Check the first and last results have the correct roles.
            Assert.NotNull(response);
            Assert.Equal(10, response.Results.Count);

            Assert.Collection(response.Results,
                              u => Assert.Equal("User 3", u.Id),
                              u => Assert.Equal("User 5", u.Id),
                              u => Assert.Equal("User 4", u.Id),
                              u => Assert.Equal("User 2", u.Id),
                              u => Assert.Equal("User 1", u.Id),
                              u => Assert.Equal("User 6", u.Id),
                              u => Assert.Equal("User 8", u.Id),
                              u => Assert.Equal("User 10", u.Id),
                              u => Assert.Equal("User 7", u.Id),
                              u => Assert.Equal("User 9", u.Id));
        }
        public async void FindMatchingUsersHandler_WithFullNameAscendingOrdering_ReturnsResultsSortedByFullName()
        {
            // Arrage
            var filter = fixture.Create <UserFilter>();
            IFindMatchingUsersDataAccess dataAccess    = CreateFakeDataAccess(filter);
            IWeeeAuthorization           authorization = AuthorizationBuilder.CreateUserWithAllRights();

            FindMatchingUsersHandler handler = new FindMatchingUsersHandler(authorization, dataAccess);

            FindMatchingUsers request = new FindMatchingUsers(1, 1000, FindMatchingUsers.OrderBy.FullNameAscending, filter);

            // Act
            var response = await handler.HandleAsync(request);

            // Check the first and last results have the correct IDs.
            Assert.NotNull(response);
            Assert.Equal("AGF GUI", response.Results.First().FullName);
            Assert.Equal("YGR FTW", response.Results.Last().FullName);
        }
        public async void FindMatchingUsersHandler_WithInternalUser_DoesntThrowException()
        {
            // Arrage
            IFindMatchingUsersDataAccess dataAccess = CreateFakeDataAccess();

            IWeeeAuthorization authorization = new AuthorizationBuilder()
                .AllowInternalAreaAccess()
                .Build();

            FindMatchingUsersHandler handler = new FindMatchingUsersHandler(authorization, dataAccess);

            FindMatchingUsers request = new FindMatchingUsers(1, 1, FindMatchingUsers.OrderBy.FullNameAscending);

            // Act
            await handler.HandleAsync(request);

            // Assert
            // no exception
        }
        public async void FindMatchingUsersHandler_WithNonInternalUser_ThrowSecurityException(AuthorizationBuilder.UserType userType)
        {
            // Arrage
            IFindMatchingUsersDataAccess dataAccess = A.Fake<IFindMatchingUsersDataAccess>();
            A.CallTo(() => dataAccess.GetOrganisationUsers()).Returns(new UserSearchData[5]);
            A.CallTo(() => dataAccess.GetCompetentAuthorityUsers()).Returns(new UserSearchData[5]);

            IWeeeAuthorization authorization = AuthorizationBuilder.CreateFromUserType(userType);

            FindMatchingUsersHandler handler = new FindMatchingUsersHandler(authorization, dataAccess);
            
            FindMatchingUsers request = new FindMatchingUsers(1, 1, FindMatchingUsers.OrderBy.FullNameAscending);

            // Act
            Func<Task<UserSearchDataResult>> action = () => handler.HandleAsync(request);

            // Assert
            await Assert.ThrowsAsync<SecurityException>(action);
        }
        public async void FindMatchingUsersHandler_WithInternalUser_DoesntThrowException()
        {
            // Arrage
            var filter = fixture.Create <UserFilter>();
            IFindMatchingUsersDataAccess dataAccess = CreateFakeDataAccess(filter);

            IWeeeAuthorization authorization = new AuthorizationBuilder()
                                               .AllowInternalAreaAccess()
                                               .Build();

            FindMatchingUsersHandler handler = new FindMatchingUsersHandler(authorization, dataAccess);

            FindMatchingUsers request = new FindMatchingUsers(1, 1, FindMatchingUsers.OrderBy.FullNameAscending, filter);

            // Act
            await handler.HandleAsync(request);

            // Assert
            // no exception
        }
        public async void FindMatchingUsersHandler_WithNonInternalUser_ThrowSecurityException(AuthorizationBuilder.UserType userType)
        {
            // Arrage
            IFindMatchingUsersDataAccess dataAccess = A.Fake <IFindMatchingUsersDataAccess>();
            var filter = fixture.Create <UserFilter>();

            A.CallTo(() => dataAccess.GetOrganisationUsers(filter)).Returns(new UserSearchData[5]);
            A.CallTo(() => dataAccess.GetCompetentAuthorityUsers(filter)).Returns(new UserSearchData[5]);

            IWeeeAuthorization authorization = AuthorizationBuilder.CreateFromUserType(userType);

            FindMatchingUsersHandler handler = new FindMatchingUsersHandler(authorization, dataAccess);

            FindMatchingUsers request = new FindMatchingUsers(1, 1, FindMatchingUsers.OrderBy.FullNameAscending, filter);

            // Act
            Func <Task <UserSearchDataResult> > action = () => handler.HandleAsync(request);

            // Assert
            await Assert.ThrowsAsync <SecurityException>(action);
        }
        public async void FindMatchingUsersHandler_RequestingSpecificPage_ReturnsCorrectNumberOfResults()
        {
            // Arrage
            var filter = fixture.Create <UserFilter>();
            IFindMatchingUsersDataAccess dataAccess    = CreateFakeDataAccess(filter);
            IWeeeAuthorization           authorization = AuthorizationBuilder.CreateUserWithAllRights();

            FindMatchingUsersHandler handler = new FindMatchingUsersHandler(authorization, dataAccess);

            // Page 2, where each page has 3 results.
            FindMatchingUsers request = new FindMatchingUsers(2, 3, FindMatchingUsers.OrderBy.FullNameAscending, filter);

            // Act
            var response = await handler.HandleAsync(request);

            // Assert - Check that there are 10 users in total.
            Assert.NotNull(response);
            Assert.Equal(10, response.UsersCount);

            // Assert - We asked for page 2 with 3 results, so check that only 3 results are returned.
            Assert.NotNull(response.Results);
            Assert.Equal(3, response.Results.Count);
        }
        public async void FindMatchingUsersHandler_RequestingSpecificPage_ReturnsCorrectNumberOfResults()
        {
            // Arrage

            IFindMatchingUsersDataAccess dataAccess = CreateFakeDataAccess();
            IWeeeAuthorization authorization = AuthorizationBuilder.CreateUserWithAllRights();

            FindMatchingUsersHandler handler = new FindMatchingUsersHandler(authorization, dataAccess);

            // Page 2, where each page has 3 results.
            FindMatchingUsers request = new FindMatchingUsers(2, 3, FindMatchingUsers.OrderBy.FullNameAscending); 

            // Act
            var response = await handler.HandleAsync(request);

            // Assert - Check that there are 10 users in total.
            Assert.NotNull(response);
            Assert.Equal(10, response.UsersCount);

            // Assert - We asked for page 2 with 3 results, so check that only 3 results are returned.
            Assert.NotNull(response.Results);
            Assert.Equal(3, response.Results.Count);
        }
        public async void HandleAsync_WithOrderByRoleAscending_ReturnsSortedResults()
        {
            // Arrage
            IFindMatchingUsersDataAccess dataAccess = CreateFakeDataAccess();
            IWeeeAuthorization authorization = AuthorizationBuilder.CreateUserWithAllRights();

            FindMatchingUsersHandler handler = new FindMatchingUsersHandler(authorization, dataAccess);

            FindMatchingUsers request = new FindMatchingUsers(1, 1000, FindMatchingUsers.OrderBy.RoleAscending);

            // Act
            UserSearchDataResult response = await handler.HandleAsync(request);

            // Check the first and last results have the correct roles.
            Assert.NotNull(response);
            Assert.Equal(10, response.Results.Count);

            Assert.Collection(response.Results,
                u => Assert.Equal("User 3", u.Id),
                u => Assert.Equal("User 5", u.Id),
                u => Assert.Equal("User 4", u.Id),
                u => Assert.Equal("User 2", u.Id),
                u => Assert.Equal("User 1", u.Id),
                u => Assert.Equal("User 6", u.Id),
                u => Assert.Equal("User 8", u.Id),
                u => Assert.Equal("User 10", u.Id),
                u => Assert.Equal("User 7", u.Id),
                u => Assert.Equal("User 9", u.Id));
        }
        public async void FindMatchingUsersHandler_WithFullNameAscendingOrdering_ReturnsResultsSortedByFullName()
        {
            // Arrage
            IFindMatchingUsersDataAccess dataAccess = CreateFakeDataAccess();
            IWeeeAuthorization authorization = AuthorizationBuilder.CreateUserWithAllRights();

            FindMatchingUsersHandler handler = new FindMatchingUsersHandler(authorization, dataAccess);

            FindMatchingUsers request = new FindMatchingUsers(1, 1000, FindMatchingUsers.OrderBy.FullNameAscending);

            // Act
            var response = await handler.HandleAsync(request);

            // Check the first and last results have the correct IDs.
            Assert.NotNull(response);
            Assert.Equal("AGF GUI", response.Results.First().FullName);
            Assert.Equal("YGR FTW", response.Results.Last().FullName);
        }