예제 #1
0
        private async Task <object> DeleteGroupAsync(dynamic input, CancellationToken cancellationToken)
        {
            Guid groupId = input.id;

            await RequiresAccess()
            .WithTenantIdExpansion(async(ctx, ct) => (await _groupsController.GetTenantIdForGroupIdAsync(groupId, TenantId, ct)).GetValueOrDefault())
            .ExecuteAsync(cancellationToken);

            try
            {
                await _groupsController.DeleteGroupAsync(groupId, TenantId, PrincipalId, cancellationToken);

                return(Response.NoContent("Resource has been deleted"));
            }
            catch (ValidationFailedException ex)
            {
                Logger.Info($"Validation failed while deleting group '{groupId}' from tenant '{TenantId}' as principal '{PrincipalId}'", ex);
                return(Response.BadRequestValidationFailed(ex.Errors));
            }
            catch (NotFoundException ex)
            {
                Logger.Info("Group is either deleted or doesn't exist", ex);
                return(new Response
                {
                    StatusCode = HttpStatusCode.NoContent,
                    ReasonPhrase = "Group is either deleted or doesn't exist"
                });
            }
            catch (Exception ex)
            {
                Logger.Error("Failed to create group resource due to an error", ex);
                return(Response.InternalServerError(ResponseReasons.InternalServerErrorCreateUser));
            }
        }
        public async Task DeleteGroupAsyncReturnsTrueIfSuccessful()
        {
            var group = Group.Example();

            _groupRepositoryMock.Setup(m => m.GetItemAsync(It.IsAny <Guid>(), It.IsAny <QueryOptions>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(group);

            _groupRepositoryMock.Setup(m => m.DeleteItemAsync(It.IsAny <Guid>(), It.IsAny <QueryOptions>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(0));

            _userRepositoryMock.SetupCreateItemQuery();

            var result = await _controller.DeleteGroupAsync(group.Id.GetValueOrDefault(), Guid.NewGuid(), Guid.NewGuid());

            Assert.True(result);
        }