コード例 #1
0
        /// <summary>
        /// Validates whether current user has a specified route role.
        /// </summary>
        /// <param name="currentUserService">Instance of service class that provides access
        /// to properties of the current user.</param>
        /// <param name="tripFlipDbContext">Instance of database context.</param>
        /// <param name="routeId">Route id.</param>
        /// <param name="routeRoleToValidate">Route role to validate.</param>
        /// <param name="errorMessage">Error message to show,
        /// if validation is not successful.</param>
        public static async Task ValidateCurrentUserRouteRoleAsync(
            ICurrentUserService currentUserService,
            TripFlipDbContext tripFlipDbContext,
            int routeId,
            RouteRoles routeRoleToValidate,
            string errorMessage)
        {
            var currentUserId = currentUserService.UserId;

            var routeSubscriberEntity = await tripFlipDbContext
                                        .RouteSubscribers
                                        .AsNoTracking()
                                        .Include(routeSubscriber => routeSubscriber.RouteRoles)
                                        .Include(routeSubscriber => routeSubscriber.TripSubscriber)
                                        .SingleOrDefaultAsync(routeSubscriber =>
                                                              routeSubscriber.TripSubscriber.UserId == currentUserId &&
                                                              routeSubscriber.RouteId == routeId);

            ValidateEntityNotNull(routeSubscriberEntity,
                                  ErrorConstants.NotRouteSubscriber);

            var routeSubscriberHasSpecifiedRole = routeSubscriberEntity
                                                  .RouteRoles
                                                  .Any(routeRole =>
                                                       routeRole.RouteRoleId == (int)routeRoleToValidate);

            if (!routeSubscriberHasSpecifiedRole)
            {
                throw new ArgumentException(errorMessage);
            }
        }
コード例 #2
0
        public async Task ValidateCurrentUserRouteRoleAsync_GivenValidCurrentUserAndRole_ExceptionThrown()
        {
            // Arrange.
            Seed(TripFlipDbContext, ValidUser);
            Seed(TripFlipDbContext, TripSubscriberEntitiesToSeed);
            Seed(TripFlipDbContext, RouteRolesToSeed);
            Seed(TripFlipDbContext, RouteSubscriberEntityToSeed);
            Seed(TripFlipDbContext, RouteSubscriberRoleEntityToSeed);

            int existingRouteId = 1;

            CurrentUserService = CreateCurrentUserService(ValidUser.Id, ValidUser.Email);

            RouteRoles roleThatIsExpectedFromCurrentUser = RouteRoles.Admin;

            // Act.
            await EntityValidationHelper.ValidateCurrentUserRouteRoleAsync(
                currentUserService : CurrentUserService,
                tripFlipDbContext : TripFlipDbContext,
                routeId : existingRouteId,
                routeRoleToValidate : roleThatIsExpectedFromCurrentUser,
                errorMessage : string.Empty);
        }
コード例 #3
0
        public async Task ValidateCurrentUserRouteRoleAsync_GivenNotValidCurrentUser_ExceptionThrown(
            string displayName, ICurrentUserService currentUserService)
        {
            // Arrange.
            Seed(TripFlipDbContext, NonExistentUser);
            Seed(TripFlipDbContext, NotRouteSubscriberUser);
            Seed(TripFlipDbContext, TripSubscriberEntitiesToSeed);
            Seed(TripFlipDbContext, RouteSubscriberEntityToSeed);

            CurrentUserService = currentUserService;

            int existentRouteId = 1;

            RouteRoles validRouteRole = RouteRoles.Editor;

            // Act + Assert.
            await Assert.ThrowsExceptionAsync <NotFoundException>(async() =>
                                                                  await EntityValidationHelper.ValidateCurrentUserRouteRoleAsync(
                                                                      currentUserService: CurrentUserService,
                                                                      tripFlipDbContext: TripFlipDbContext,
                                                                      routeId: existentRouteId,
                                                                      routeRoleToValidate: validRouteRole,
                                                                      errorMessage: string.Empty), displayName);
        }