Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Deprecated by" feature #4950

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 92 additions & 2 deletions tests/unit/admin/views/test_classifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import pretend
import pytest

from webob.multidict import MultiDict

from warehouse.admin.views import classifiers as views
from warehouse.classifiers.models import Classifier

Expand Down Expand Up @@ -83,13 +85,101 @@ def test_add_parent_classifier(self, db_request):

class TestDeprecateClassifier:
def test_deprecate_classifier(self, db_request):
classifier = ClassifierFactory(deprecated=False)
classifier = ClassifierFactory(
classifier="Classifier :: For Testing", deprecated=False
)

db_request.params = {"classifier_id": classifier.id}
db_request.params = MultiDict({"classifier_id": classifier.id})
db_request.session.flash = pretend.call_recorder(lambda *a, **kw: None)
db_request.route_path = lambda *a: "/the/path"

views.deprecate_classifier(db_request)
db_request.db.flush()

assert classifier.deprecated
assert db_request.session.flash.calls == [
pretend.call(
"Deprecated classifier 'Classifier :: For Testing'", queue="success"
)
]

def test_deprecation_with_alternative(self, db_request):
classifier = ClassifierFactory(
classifier="Classifier :: For tSeting", deprecated=False
)
alternative_one = ClassifierFactory(
classifier="Classifier :: For Testing", deprecated=False
)
alternative_two = ClassifierFactory(
classifier="Classifier :: For QA", deprecated=False
)
db_request.params = MultiDict(
[
("classifier_id", classifier.id),
("deprecated_by", alternative_one.id),
("deprecated_by", alternative_two.id),
]
)

db_request.session.flash = pretend.call_recorder(lambda *a, **kw: None)
db_request.route_path = lambda *a: "/the/path"

views.deprecate_classifier(db_request)
db_request.db.flush()

assert classifier.deprecated
assert db_request.session.flash.calls == [
pretend.call(
(
"Deprecated classifier 'Classifier :: For tSeting' "
"in favor of 'Classifier :: For Testing', 'Classifier :: For QA'"
),
queue="success",
)
]

def test_self_deprecation(self, db_request):
classifier = ClassifierFactory(deprecated=False)
db_request.params = MultiDict(
[("classifier_id", classifier.id), ("deprecated_by", classifier.id)]
)

db_request.session.flash = pretend.call_recorder(lambda *a, **kw: None)
db_request.route_path = lambda *a: "/the/path"

views.deprecate_classifier(db_request)
db_request.db.flush()

assert not classifier.deprecated
assert db_request.session.flash.calls == [
pretend.call(
"You can not deprecate the classifier in favor of itself", queue="error"
)
]

def test_deprecation_in_favor_of_deprecated(self, db_request):
classifier = ClassifierFactory(classifier="Classifier :: OK", deprecated=False)
alternative = ClassifierFactory(
classifier="Classifier :: Deprecated", deprecated=True
)
db_request.params = MultiDict(
[("classifier_id", classifier.id), ("deprecated_by", alternative.id)]
)

db_request.session.flash = pretend.call_recorder(lambda *a, **kw: None)
db_request.route_path = lambda *a: "/the/path"

views.deprecate_classifier(db_request)
db_request.db.flush()

assert not classifier.deprecated
assert db_request.session.flash.calls == [
pretend.call(
(
"You can not deprecate the classifier 'Classifier :: OK' "
"in favor of already deprecated classifier "
"'Classifier :: Deprecated'"
),
queue="error",
)
]
129 changes: 128 additions & 1 deletion tests/unit/forklift/test_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,134 @@ def test_upload_fails_with_deprecated_classifier(self, pyramid_config, db_reques
assert resp.status_code == 400
assert resp.status == (
"400 Invalid value for classifiers. "
"Error: Classifier 'AA :: BB' has been deprecated, see /url "
"Error: Classifier 'AA :: BB' has been deprecated. See /url "
"for a list of valid classifiers."
)

@pytest.mark.parametrize(
"is_alternative_deprecated, expected_message",
[
(
False,
(
"400 Invalid value for classifiers. "
"Error: Classifier 'AA :: BB' has been deprecated, "
"and replaced with the following classifier: 'AA :: Alternative'. "
"See /url for a list of valid classifiers."
),
),
(
True,
(
"400 Invalid value for classifiers. "
"Error: Classifier 'AA :: BB' has been deprecated. "
"See /url for a list of valid classifiers."
),
),
],
)
def test_upload_fails_with_deprecated_classifier_with_alternative(
self, pyramid_config, db_request, is_alternative_deprecated, expected_message
):
pyramid_config.testing_securitypolicy(userid=1)

user = UserFactory.create()
db_request.user = user
EmailFactory.create(user=user)
project = ProjectFactory.create()
release = ReleaseFactory.create(project=project, version="1.0")
RoleFactory.create(user=user, project=project)
classifier = ClassifierFactory(classifier="AA :: BB", deprecated=True)

alternative = ClassifierFactory(
classifier="AA :: Alternative", deprecated=is_alternative_deprecated
)
classifier.alternatives.append(alternative)

filename = "{}-{}.tar.gz".format(project.name, release.version)

db_request.POST = MultiDict(
{
"metadata_version": "1.2",
"name": project.name,
"version": release.version,
"filetype": "sdist",
"md5_digest": "335c476dc930b959dda9ec82bd65ef19",
"content": pretend.stub(
filename=filename,
file=io.BytesIO(b"A fake file."),
type="application/tar",
),
}
)
db_request.POST.extend([("classifiers", classifier.classifier)])
db_request.route_url = pretend.call_recorder(lambda *a, **kw: "/url")

with pytest.raises(HTTPBadRequest) as excinfo:
legacy.file_upload(db_request)

resp = excinfo.value

assert resp.status_code == 400
assert resp.status == expected_message

def test_upload_fails_with_deprecated_classifier_with_alternatives(
self, pyramid_config, db_request
):
pyramid_config.testing_securitypolicy(userid=1)

user = UserFactory.create()
db_request.user = user
EmailFactory.create(user=user)
project = ProjectFactory.create()
release = ReleaseFactory.create(project=project, version="1.0")
RoleFactory.create(user=user, project=project)
classifier = ClassifierFactory(classifier="AA :: BB", deprecated=True)

alternative_one = ClassifierFactory(
classifier="AA :: FirstLevel :: Deprecated Alternative", deprecated=True
)
alternative_two = ClassifierFactory(
classifier="AA :: FirstLevel :: OK", deprecated=False
)
alternative_three = ClassifierFactory(
classifier="AA :: SecondLevel :: OK", deprecated=False
)
classifier.alternatives.append(alternative_one)
classifier.alternatives.append(alternative_two)
alternative_one.alternatives.append(classifier)
alternative_one.alternatives.append(alternative_three)

filename = "{}-{}.tar.gz".format(project.name, release.version)

db_request.POST = MultiDict(
{
"metadata_version": "1.2",
"name": project.name,
"version": release.version,
"filetype": "sdist",
"md5_digest": "335c476dc930b959dda9ec82bd65ef19",
"content": pretend.stub(
filename=filename,
file=io.BytesIO(b"A fake file."),
type="application/tar",
),
}
)
db_request.POST.extend([("classifiers", classifier.classifier)])
db_request.route_url = pretend.call_recorder(lambda *a, **kw: "/url")

with pytest.raises(HTTPBadRequest) as excinfo:
legacy.file_upload(db_request)

resp = excinfo.value

assert resp.status_code == 400
assert resp.status == (
"400 Invalid value for classifiers. "
"Error: Classifier 'AA :: BB' has been deprecated, "
"and replaced with the following classifiers: "
"'AA :: FirstLevel :: OK', 'AA :: SecondLevel :: OK'. See /url "
"for a list of valid classifiers."
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


import { Controller } from "stimulus";

export default class extends Controller {
static targets = [
"deprecatedClassifier", "alternativeClassifier"
]

update() {
const deprecatedClassifierId = this.deprecatedClassifierTarget.options[this.deprecatedClassifierTarget.selectedIndex].value;
this.alternativeClassifierTargets.forEach(target => {
const selectedAlternativeId = target.options[target.selectedIndex].value;

// Reset the value to prevent self-selection.
if (deprecatedClassifierId === selectedAlternativeId) {
target.selectedIndex = 0;
}

// Disable deprecated classifier selection.
for (let optionIndex = 0; optionIndex < target.options.length; ++optionIndex) {
const option = target.options[optionIndex];
option.disabled = option.value === deprecatedClassifierId || option.dataset.deprecated;
}
});
}
}
15 changes: 13 additions & 2 deletions warehouse/admin/templates/admin/classifiers/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ <h3 class="box-title">Add Sub-Classifier</h3>
</form>
</div>

<div class="box box-primary">
<div class="box box-primary" data-controller="deprecate-classifier">
<div class="box-header with-border">
<h3 class="box-title">Deprecate Classifier</h3>
</div>
Expand All @@ -87,14 +87,25 @@ <h3 class="box-title">Deprecate Classifier</h3>
<div class="box-body">
<div class="form-group col-sm-4">
<label for="classifier_id">Classifier:</label>
<select name="classifier_id" id="classifier_id">
<select name="classifier_id" id="classifier_id" data-target="deprecate-classifier.deprecatedClassifier" data-action="deprecate-classifier#update">
<option disabled selected>Select one</option>
{% for classifier in classifiers %}
<option value="{{ classifier.id }}" {{'disabled' if classifier.deprecated else ''}}>
{{ classifier.classifier }}{{ ' (Deprecated)' if classifier.deprecated else ''}}
</option>
{% endfor %}
</select>
<label>Deprecated in favor of (optional):</label>
{% for deprecation_alternative_index in range(5) %}
<select name="deprecated_by" data-target="deprecate-classifier.alternativeClassifier">
<option selected value="">(Empty)</option>
{% for classifier in classifiers %}
<option value="{{ classifier.id }}" {{'disabled data-deprecated="true"' if classifier.deprecated else ''}}>
{{ classifier.classifier }}{{ ' (Deprecated)' if classifier.deprecated else ''}}
</option>
{% endfor %}
</select>
{% endfor %}
</div>
</div>
<div class="box-footer">
Expand Down
Loading