Beispiel #1
0
        public virtual async Task <JsonResult> Deprecate(
            DeprecatePackageRequest request)
        {
            var error = await _deprecationManagementService.UpdateDeprecation(
                GetCurrentUser(),
                request.Id,
                request.Versions.ToList(),
                request.IsLegacy,
                request.HasCriticalBugs,
                request.IsOther,
                request.AlternatePackageId,
                request.AlternatePackageVersion,
                request.CustomMessage);

            if (error != null)
            {
                return(Json(error.Status, new { error = error.Message }));
            }

            var packagePluralString = request.Versions.Count() > 1 ? "packages have" : "package has";
            var deprecatedString    = request.IsLegacy || request.HasCriticalBugs || request.IsOther
                ? "deprecated" : "undeprecated";

            TempData["Message"] =
                $"Your {packagePluralString} been {deprecatedString}. " +
                $"It may take several hours for this change to propagate through our system.";

            return(Json(HttpStatusCode.OK));
        }
        public virtual async Task <JsonResult> Deprecate(
            DeprecatePackageRequest request)
        {
            var error = await _deprecationManagementService.UpdateDeprecation(
                GetCurrentUser(),
                request.Id,
                request.Versions.ToList(),
                request.IsLegacy,
                request.HasCriticalBugs,
                request.IsOther,
                request.AlternatePackageId,
                request.AlternatePackageVersion,
                request.CustomMessage);

            if (error != null)
            {
                return(Json(error.Status, new { error = error.Message }));
            }

            return(Json(HttpStatusCode.OK));
        }
Beispiel #3
0
        public virtual async Task <JsonResult> Deprecate(
            DeprecatePackageRequest request)
        {
            var status = PackageDeprecationStatus.NotDeprecated;

            if (request.IsLegacy)
            {
                status |= PackageDeprecationStatus.Legacy;
            }

            if (request.HasCriticalBugs)
            {
                status |= PackageDeprecationStatus.CriticalBugs;
            }

            var customMessage = request.CustomMessage;

            if (request.IsOther)
            {
                if (string.IsNullOrWhiteSpace(customMessage))
                {
                    return(DeprecateErrorResponse(HttpStatusCode.BadRequest, Strings.DeprecatePackage_CustomMessageRequired));
                }

                status |= PackageDeprecationStatus.Other;
            }

            if (customMessage != null)
            {
                if (customMessage.Length > MaxCustomMessageLength)
                {
                    return(DeprecateErrorResponse(
                               HttpStatusCode.BadRequest,
                               string.Format(Strings.DeprecatePackage_CustomMessageTooLong, MaxCustomMessageLength)));
                }
            }

            if (request.Versions == null || !request.Versions.Any())
            {
                return(DeprecateErrorResponse(HttpStatusCode.BadRequest, Strings.DeprecatePackage_NoVersions));
            }

            var packages     = _packageService.FindPackagesById(request.Id, PackageDeprecationFieldsToInclude.DeprecationAndRelationships);
            var registration = packages.FirstOrDefault()?.PackageRegistration;

            if (registration == null)
            {
                // This should only happen if someone hacks the form or if the package is deleted while the user is filling out the form.
                return(DeprecateErrorResponse(
                           HttpStatusCode.NotFound,
                           string.Format(Strings.DeprecatePackage_MissingRegistration, request.Id)));
            }

            var currentUser = GetCurrentUser();

            if (!_featureFlagService.IsManageDeprecationEnabled(GetCurrentUser(), registration))
            {
                return(DeprecateErrorResponse(HttpStatusCode.Forbidden, Strings.DeprecatePackage_Forbidden));
            }

            if (ActionsRequiringPermissions.DeprecatePackage.CheckPermissionsOnBehalfOfAnyAccount(currentUser, registration) != PermissionsCheckResult.Allowed)
            {
                return(DeprecateErrorResponse(HttpStatusCode.Forbidden, Strings.DeprecatePackage_Forbidden));
            }

            if (registration.IsLocked)
            {
                return(DeprecateErrorResponse(
                           HttpStatusCode.Forbidden,
                           string.Format(Strings.DeprecatePackage_Locked, request.Id)));
            }

            PackageRegistration alternatePackageRegistration = null;
            Package             alternatePackage             = null;

            if (!string.IsNullOrWhiteSpace(request.AlternatePackageId))
            {
                if (!string.IsNullOrWhiteSpace(request.AlternatePackageVersion))
                {
                    alternatePackage = _packageService.FindPackageByIdAndVersionStrict(request.AlternatePackageId, request.AlternatePackageVersion);
                    if (alternatePackage == null)
                    {
                        return(DeprecateErrorResponse(
                                   HttpStatusCode.NotFound,
                                   string.Format(Strings.DeprecatePackage_NoAlternatePackage, request.AlternatePackageId, request.AlternatePackageVersion)));
                    }
                }
                else
                {
                    alternatePackageRegistration = _packageService.FindPackageRegistrationById(request.AlternatePackageId);
                    if (alternatePackageRegistration == null)
                    {
                        return(DeprecateErrorResponse(
                                   HttpStatusCode.NotFound,
                                   string.Format(Strings.DeprecatePackage_NoAlternatePackageRegistration, request.AlternatePackageId)));
                    }
                }
            }

            var packagesToUpdate = new List <Package>();

            foreach (var version in request.Versions)
            {
                var normalizedVersion = NuGetVersionFormatter.Normalize(version);
                var package           = packages.SingleOrDefault(v => v.NormalizedVersion == normalizedVersion);
                if (package == null)
                {
                    // This should only happen if someone hacks the form or if a version of the package is deleted while the user is filling out the form.
                    return(DeprecateErrorResponse(
                               HttpStatusCode.NotFound,
                               string.Format(Strings.DeprecatePackage_MissingVersion, request.Id)));
                }
                else
                {
                    packagesToUpdate.Add(package);
                }
            }

            if (alternatePackage != null && packagesToUpdate.Any(p => p == alternatePackage))
            {
                return(DeprecateErrorResponse(
                           HttpStatusCode.BadRequest,
                           Strings.DeprecatePackage_AlternateOfSelf));
            }

            await _deprecationService.UpdateDeprecation(
                packagesToUpdate,
                status,
                alternatePackageRegistration,
                alternatePackage,
                customMessage,
                currentUser);

            return(Json(HttpStatusCode.OK));
        }
            public async Task ReturnsProperJsonResult(
                bool isLegacy,
                bool hasCriticalBugs,
                bool isOther,
                bool multipleVersions,
                bool success)
            {
                // Arrange
                var id               = "Crested.Gecko";
                var versions         = multipleVersions ? new[] { "1.0.0", "2.0.0" } : new[] { "1.0.0" };
                var alternateId      = "alt.Id";
                var alternateVersion = "3.0.0";
                var customMessage    = "custom";

                var currentUser = Get <Fakes>().User;

                var errorStatus        = HttpStatusCode.InternalServerError;
                var errorMessage       = "woops";
                var deprecationService = GetMock <IPackageDeprecationManagementService>();

                deprecationService
                .Setup(x => x.UpdateDeprecation(
                           currentUser,
                           id,
                           versions,
                           isLegacy,
                           hasCriticalBugs,
                           isOther,
                           alternateId,
                           alternateVersion,
                           customMessage))
                .ReturnsAsync(success ? null : new UpdateDeprecationError(errorStatus, errorMessage))
                .Verifiable();

                var controller = GetController <ManageDeprecationJsonApiController>();

                controller.SetCurrentUser(currentUser);

                var request = new DeprecatePackageRequest
                {
                    Id                      = id,
                    Versions                = versions,
                    IsLegacy                = isLegacy,
                    HasCriticalBugs         = hasCriticalBugs,
                    IsOther                 = isOther,
                    AlternatePackageId      = alternateId,
                    AlternatePackageVersion = alternateVersion,
                    CustomMessage           = customMessage
                };

                // Act
                var result = await controller.Deprecate(request);

                // Assert
                if (success)
                {
                    AssertResponseStatusCode(controller, HttpStatusCode.OK);
                    string expectedString;
                    if (isLegacy || hasCriticalBugs || isOther)
                    {
                        if (multipleVersions)
                        {
                            expectedString = "Your packages have been deprecated.";
                        }
                        else
                        {
                            expectedString = "Your package has been deprecated.";
                        }
                    }
                    else
                    {
                        if (multipleVersions)
                        {
                            expectedString = "Your packages have been undeprecated.";
                        }
                        else
                        {
                            expectedString = "Your package has been undeprecated.";
                        }
                    }

                    Assert.StartsWith(expectedString, controller.TempData["Message"] as string);
                }
                else
                {
                    AssertResponseStatusCode(controller, errorStatus);

                    // Using JObject to get the property from the result easily.
                    // Alternatively we could use reflection, but this is easier, and makes sense as the response is intended to be JSON anyway.
                    var jObject = JObject.FromObject(result.Data);
                    Assert.Equal(errorMessage, jObject["error"].Value <string>());
                }

                deprecationService.Verify();
            }
            public async Task ReturnsProperJsonResult(
                bool isLegacy,
                bool hasCriticalBugs,
                bool isOther,
                bool success)
            {
                // Arrange
                var id               = "Crested.Gecko";
                var versions         = new[] { "1.0.0", "2.0.0" };
                var alternateId      = "alt.Id";
                var alternateVersion = "3.0.0";
                var customMessage    = "custom";

                var currentUser = Get <Fakes>().User;

                var errorStatus        = HttpStatusCode.InternalServerError;
                var errorMessage       = "woops";
                var deprecationService = GetMock <IPackageDeprecationManagementService>();

                deprecationService
                .Setup(x => x.UpdateDeprecation(
                           currentUser,
                           id,
                           versions,
                           isLegacy,
                           hasCriticalBugs,
                           isOther,
                           alternateId,
                           alternateVersion,
                           customMessage))
                .ReturnsAsync(success ? null : new UpdateDeprecationError(errorStatus, errorMessage))
                .Verifiable();

                var controller = GetController <ManageDeprecationJsonApiController>();

                controller.SetCurrentUser(currentUser);

                var request = new DeprecatePackageRequest
                {
                    Id                      = id,
                    Versions                = versions,
                    IsLegacy                = isLegacy,
                    HasCriticalBugs         = hasCriticalBugs,
                    IsOther                 = isOther,
                    AlternatePackageId      = alternateId,
                    AlternatePackageVersion = alternateVersion,
                    CustomMessage           = customMessage
                };

                // Act
                var result = await controller.Deprecate(request);

                // Assert
                if (success)
                {
                    AssertSuccessResponse(controller);
                }
                else
                {
                    AssertErrorResponse(controller, result, errorStatus, errorMessage);
                }

                deprecationService.Verify();
            }