Skip to content

Organizations: Handle subscription statuses in upload and admin #18035

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

Merged
merged 4 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
193 changes: 193 additions & 0 deletions tests/unit/forklift/test_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
from ...common.db.accounts import EmailFactory, UserFactory
from ...common.db.classifiers import ClassifierFactory
from ...common.db.oidc import GitHubPublisherFactory
from ...common.db.organizations import (
OrganizationFactory,
OrganizationProjectFactory,
OrganizationRoleFactory,
OrganizationStripeSubscriptionFactory,
)
from ...common.db.packaging import (
FileFactory,
ProjectFactory,
Expand Down Expand Up @@ -5403,6 +5409,193 @@ def test_upload_fails_when_license_and_license_expression_are_present(
"for more information."
)

def test_upload_for_organization_owned_project_succeeds(
self, pyramid_config, db_request, monkeypatch
):
organization = OrganizationFactory.create(orgtype="Community")
user = UserFactory.create(with_verified_primary_email=True)
OrganizationRoleFactory.create(organization=organization, user=user)
project = OrganizationProjectFactory.create(organization=organization).project
version = "1.0.0"

filename = (
f"{project.normalized_name.replace('-', '_')}-{version}-py3-none-any.whl"
)
filebody = _get_whl_testdata(
name=project.normalized_name.replace("-", "_"), version=version
)

@pretend.call_recorder
def storage_service_store(path, file_path, *, meta):
with open(file_path, "rb") as fp:
if file_path.endswith(".metadata"):
assert fp.read() == b"Fake metadata"
else:
assert fp.read() == filebody

storage_service = pretend.stub(store=storage_service_store)

db_request.find_service = pretend.call_recorder(
lambda svc, name=None, context=None: {
IFileStorage: storage_service,
}.get(svc)
)

monkeypatch.setattr(
legacy, "_is_valid_dist_file", lambda *a, **kw: (True, None)
)

pyramid_config.testing_securitypolicy(identity=user)
db_request.user = user
db_request.user_agent = "warehouse-tests/6.6.6"
db_request.POST = MultiDict(
{
"metadata_version": "1.2",
"name": project.name,
"version": "1.0.0",
"filetype": "bdist_wheel",
"pyversion": "py3",
"md5_digest": hashlib.md5(filebody).hexdigest(),
"content": pretend.stub(
filename=filename,
file=io.BytesIO(filebody),
type="application/zip",
),
}
)

resp = legacy.file_upload(db_request)

assert resp.status_code == 200

def test_upload_for_company_organization_owned_project_fails_without_subscription(
self, pyramid_config, db_request, monkeypatch
):
organization = OrganizationFactory.create(orgtype="Company")
user = UserFactory.create(with_verified_primary_email=True)
OrganizationRoleFactory.create(organization=organization, user=user)
project = OrganizationProjectFactory.create(organization=organization).project
version = "1.0.0"

filename = (
f"{project.normalized_name.replace('-', '_')}-{version}-py3-none-any.whl"
)
filebody = _get_whl_testdata(
name=project.normalized_name.replace("-", "_"), version=version
)

@pretend.call_recorder
def storage_service_store(path, file_path, *, meta):
with open(file_path, "rb") as fp:
if file_path.endswith(".metadata"):
assert fp.read() == b"Fake metadata"
else:
assert fp.read() == filebody

storage_service = pretend.stub(store=storage_service_store)

db_request.find_service = pretend.call_recorder(
lambda svc, name=None, context=None: {
IFileStorage: storage_service,
}.get(svc)
)

monkeypatch.setattr(
legacy, "_is_valid_dist_file", lambda *a, **kw: (True, None)
)

pyramid_config.testing_securitypolicy(identity=user)
db_request.user = user
db_request.user_agent = "warehouse-tests/6.6.6"
db_request.POST = MultiDict(
{
"metadata_version": "1.2",
"name": project.name,
"version": "1.0.0",
"filetype": "bdist_wheel",
"pyversion": "py3",
"md5_digest": hashlib.md5(filebody).hexdigest(),
"content": pretend.stub(
filename=filename,
file=io.BytesIO(filebody),
type="application/zip",
),
}
)

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

resp = excinfo.value

assert resp.status_code == 400
assert resp.status == (
"400 Organization account owning this project is inactive. "
"This may be due to inactive billing for Company Organizations, "
"or administrator intervention for Community Organizations. "
"Please contact [email protected]."
)

def test_upload_for_company_organization_owned_project_suceeds_with_subscription(
self, pyramid_config, db_request, monkeypatch
):
organization = OrganizationFactory.create(orgtype="Company")
user = UserFactory.create(with_verified_primary_email=True)
OrganizationRoleFactory.create(organization=organization, user=user)
OrganizationStripeSubscriptionFactory.create(organization=organization)
project = OrganizationProjectFactory.create(organization=organization).project
version = "1.0.0"

filename = (
f"{project.normalized_name.replace('-', '_')}-{version}-py3-none-any.whl"
)
filebody = _get_whl_testdata(
name=project.normalized_name.replace("-", "_"), version=version
)

@pretend.call_recorder
def storage_service_store(path, file_path, *, meta):
with open(file_path, "rb") as fp:
if file_path.endswith(".metadata"):
assert fp.read() == b"Fake metadata"
else:
assert fp.read() == filebody

storage_service = pretend.stub(store=storage_service_store)

db_request.find_service = pretend.call_recorder(
lambda svc, name=None, context=None: {
IFileStorage: storage_service,
}.get(svc)
)

monkeypatch.setattr(
legacy, "_is_valid_dist_file", lambda *a, **kw: (True, None)
)

pyramid_config.testing_securitypolicy(identity=user)
db_request.user = user
db_request.user_agent = "warehouse-tests/6.6.6"
db_request.POST = MultiDict(
{
"metadata_version": "1.2",
"name": project.name,
"version": "1.0.0",
"filetype": "bdist_wheel",
"pyversion": "py3",
"md5_digest": hashlib.md5(filebody).hexdigest(),
"content": pretend.stub(
filename=filename,
file=io.BytesIO(filebody),
type="application/zip",
),
}
)

resp = legacy.file_upload(db_request)

assert resp.status_code == 200


def test_submit(pyramid_request):
resp = legacy.submit(pyramid_request)
Expand Down
37 changes: 34 additions & 3 deletions tests/unit/organizations/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
update_organziation_subscription_usage_record,
)
from warehouse.subscriptions.interfaces import IBillingService
from warehouse.subscriptions.models import StripeSubscriptionStatus

from ...common.db.organizations import (
OrganizationApplicationFactory,
Expand Down Expand Up @@ -156,9 +157,8 @@ def test_delete_declined_organization_applications(self, db_request):

class TestUpdateOrganizationSubscriptionUsage:
def test_update_organization_subscription_usage_record(self, db_request):
# Create an organization with a subscription and members
# Setup an organization with an active subscription
organization = OrganizationFactory.create()
# Add a couple members
owner_user = UserFactory.create()
OrganizationRoleFactory(
organization=organization,
Expand All @@ -171,7 +171,6 @@ def test_update_organization_subscription_usage_record(self, db_request):
user=member_user,
role_name=OrganizationRoleType.Member,
)
# Wire up the customer, subscripton, organization, and subscription item
stripe_customer = StripeCustomerFactory.create()
OrganizationStripeCustomerFactory.create(
organization=organization, customer=stripe_customer
Expand All @@ -189,6 +188,38 @@ def test_update_organization_subscription_usage_record(self, db_request):
)
StripeSubscriptionItemFactory.create(subscription=subscription)

# Setup an organization with a cancelled subscription
organization = OrganizationFactory.create()
owner_user = UserFactory.create()
OrganizationRoleFactory(
organization=organization,
user=owner_user,
role_name=OrganizationRoleType.Owner,
)
member_user = UserFactory.create()
OrganizationRoleFactory(
organization=organization,
user=member_user,
role_name=OrganizationRoleType.Member,
)
stripe_customer = StripeCustomerFactory.create()
OrganizationStripeCustomerFactory.create(
organization=organization, customer=stripe_customer
)
subscription_product = StripeSubscriptionProductFactory.create()
subscription_price = StripeSubscriptionPriceFactory.create(
subscription_product=subscription_product
)
subscription = StripeSubscriptionFactory.create(
customer=stripe_customer,
subscription_price=subscription_price,
status=StripeSubscriptionStatus.Canceled,
)
OrganizationStripeSubscriptionFactory.create(
organization=organization, subscription=subscription
)
StripeSubscriptionItemFactory.create(subscription=subscription)

create_or_update_usage_record = pretend.call_recorder(
lambda *a, **kw: {
"subscription_item_id": "si_1234",
Expand Down
9 changes: 8 additions & 1 deletion warehouse/admin/templates/admin/organizations/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@
<th>Organization</th>
<th>Description</th>
<th>Type</th>
<th><i class="fa fa-fw"></i> Status</th>
<th>Status</th>
<th>Subscription</th>
<th>Good Standing</th>
</tr>
</thead>

Expand All @@ -81,6 +83,11 @@
{% else %}
<td><i class="fa fa-fw fa-times text-red"></i> Inactive</td>
{% endif %}
{% if organization.good_standing %}
<td><i class="fa fa-fw fa-check text-green"></i> Yes</td>
{% else %}
<td><i class="fa fa-fw fa-times text-red"></i> No</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
Expand Down
67 changes: 49 additions & 18 deletions warehouse/admin/templates/admin/projects/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,34 @@ <h3 class="card-title">
</div>
</div> <!-- .card #attributes -->

{% if project.organization %}
<div class="card card-secondary" id="maintainers">
<div class="card-header">Organization</div>
<div class="card-body">
<div class="table-responsive p-0">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>Name</th>
<th>Account Name</th>
<th>Active</th>
<th>Good Standing</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="{{ request.route_path('admin.organization.detail', organization_id=project.organization.id) }}">{{ project.organization.display_name }}</a></td>
<td>{{ project.organization.name }}</td>
<td>{{ project.organization.is_active }}</td>
<td>{{ project.organization.good_standing }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
{% endif %}

<div class="card card-secondary" id="maintainers">
<div class="card-header">Maintainers</div>
<div class="card-body">
Expand Down Expand Up @@ -274,28 +302,31 @@ <h4 class="modal-title" id="exampleModalLabel">Remove role for {{ role.user.user
</table>
</div>
</div>
</div>

<div class="card card-secondary" id="invitations">
<div class="card-header">Invitations</div>
<div class="card-body">
<div class="table-responsive p-0">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>Username</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{% for invitation in project.invitations %}
<tr>
<td><a href="{{ request.route_path('admin.user.detail', username=invitation.user.username) }}">{{ invitation.user.username }}</a></td>
<td>{{ invitation.invite_status.value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="table-responsive p-0">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>Username</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{% for invitation in project.invitations %}
<tr>
<td><a href="{{ request.route_path('admin.user.detail', username=invitation.user.username) }}">{{ invitation.user.username }}</a></td>
<td>{{ invitation.invite_status.value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div> <!-- .card #maintainers -->
</div>

<div class="card card-info" id="releases">
<div class="card-header">Releases</div>
Expand Down
6 changes: 4 additions & 2 deletions warehouse/admin/views/organizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ def organization_list(request):
except ValueError:
raise HTTPBadRequest("'page' must be an integer.") from None

organizations_query = request.db.query(Organization).order_by(
Organization.normalized_name
organizations_query = (
request.db.query(Organization)
.options(joinedload(Organization.subscriptions))
.order_by(Organization.normalized_name)
)

if q:
Expand Down
Loading