|
61 | 61 | from ...common.db.accounts import EmailFactory, UserFactory
|
62 | 62 | from ...common.db.classifiers import ClassifierFactory
|
63 | 63 | from ...common.db.oidc import GitHubPublisherFactory
|
| 64 | +from ...common.db.organizations import ( |
| 65 | + OrganizationFactory, |
| 66 | + OrganizationProjectFactory, |
| 67 | + OrganizationRoleFactory, |
| 68 | + OrganizationStripeSubscriptionFactory, |
| 69 | +) |
64 | 70 | from ...common.db.packaging import (
|
65 | 71 | FileFactory,
|
66 | 72 | ProjectFactory,
|
@@ -5403,6 +5409,193 @@ def test_upload_fails_when_license_and_license_expression_are_present(
|
5403 | 5409 | "for more information."
|
5404 | 5410 | )
|
5405 | 5411 |
|
| 5412 | + def test_upload_for_organization_owned_project_succeeds( |
| 5413 | + self, pyramid_config, db_request, monkeypatch |
| 5414 | + ): |
| 5415 | + organization = OrganizationFactory.create(orgtype="Community") |
| 5416 | + user = UserFactory.create(with_verified_primary_email=True) |
| 5417 | + OrganizationRoleFactory.create(organization=organization, user=user) |
| 5418 | + project = OrganizationProjectFactory.create(organization=organization).project |
| 5419 | + version = "1.0.0" |
| 5420 | + |
| 5421 | + filename = ( |
| 5422 | + f"{project.normalized_name.replace('-', '_')}-{version}-py3-none-any.whl" |
| 5423 | + ) |
| 5424 | + filebody = _get_whl_testdata( |
| 5425 | + name=project.normalized_name.replace("-", "_"), version=version |
| 5426 | + ) |
| 5427 | + |
| 5428 | + @pretend.call_recorder |
| 5429 | + def storage_service_store(path, file_path, *, meta): |
| 5430 | + with open(file_path, "rb") as fp: |
| 5431 | + if file_path.endswith(".metadata"): |
| 5432 | + assert fp.read() == b"Fake metadata" |
| 5433 | + else: |
| 5434 | + assert fp.read() == filebody |
| 5435 | + |
| 5436 | + storage_service = pretend.stub(store=storage_service_store) |
| 5437 | + |
| 5438 | + db_request.find_service = pretend.call_recorder( |
| 5439 | + lambda svc, name=None, context=None: { |
| 5440 | + IFileStorage: storage_service, |
| 5441 | + }.get(svc) |
| 5442 | + ) |
| 5443 | + |
| 5444 | + monkeypatch.setattr( |
| 5445 | + legacy, "_is_valid_dist_file", lambda *a, **kw: (True, None) |
| 5446 | + ) |
| 5447 | + |
| 5448 | + pyramid_config.testing_securitypolicy(identity=user) |
| 5449 | + db_request.user = user |
| 5450 | + db_request.user_agent = "warehouse-tests/6.6.6" |
| 5451 | + db_request.POST = MultiDict( |
| 5452 | + { |
| 5453 | + "metadata_version": "1.2", |
| 5454 | + "name": project.name, |
| 5455 | + "version": "1.0.0", |
| 5456 | + "filetype": "bdist_wheel", |
| 5457 | + "pyversion": "py3", |
| 5458 | + "md5_digest": hashlib.md5(filebody).hexdigest(), |
| 5459 | + "content": pretend.stub( |
| 5460 | + filename=filename, |
| 5461 | + file=io.BytesIO(filebody), |
| 5462 | + type="application/zip", |
| 5463 | + ), |
| 5464 | + } |
| 5465 | + ) |
| 5466 | + |
| 5467 | + resp = legacy.file_upload(db_request) |
| 5468 | + |
| 5469 | + assert resp.status_code == 200 |
| 5470 | + |
| 5471 | + def test_upload_for_company_organization_owned_project_fails_without_subscription( |
| 5472 | + self, pyramid_config, db_request, monkeypatch |
| 5473 | + ): |
| 5474 | + organization = OrganizationFactory.create(orgtype="Company") |
| 5475 | + user = UserFactory.create(with_verified_primary_email=True) |
| 5476 | + OrganizationRoleFactory.create(organization=organization, user=user) |
| 5477 | + project = OrganizationProjectFactory.create(organization=organization).project |
| 5478 | + version = "1.0.0" |
| 5479 | + |
| 5480 | + filename = ( |
| 5481 | + f"{project.normalized_name.replace('-', '_')}-{version}-py3-none-any.whl" |
| 5482 | + ) |
| 5483 | + filebody = _get_whl_testdata( |
| 5484 | + name=project.normalized_name.replace("-", "_"), version=version |
| 5485 | + ) |
| 5486 | + |
| 5487 | + @pretend.call_recorder |
| 5488 | + def storage_service_store(path, file_path, *, meta): |
| 5489 | + with open(file_path, "rb") as fp: |
| 5490 | + if file_path.endswith(".metadata"): |
| 5491 | + assert fp.read() == b"Fake metadata" |
| 5492 | + else: |
| 5493 | + assert fp.read() == filebody |
| 5494 | + |
| 5495 | + storage_service = pretend.stub(store=storage_service_store) |
| 5496 | + |
| 5497 | + db_request.find_service = pretend.call_recorder( |
| 5498 | + lambda svc, name=None, context=None: { |
| 5499 | + IFileStorage: storage_service, |
| 5500 | + }.get(svc) |
| 5501 | + ) |
| 5502 | + |
| 5503 | + monkeypatch.setattr( |
| 5504 | + legacy, "_is_valid_dist_file", lambda *a, **kw: (True, None) |
| 5505 | + ) |
| 5506 | + |
| 5507 | + pyramid_config.testing_securitypolicy(identity=user) |
| 5508 | + db_request.user = user |
| 5509 | + db_request.user_agent = "warehouse-tests/6.6.6" |
| 5510 | + db_request.POST = MultiDict( |
| 5511 | + { |
| 5512 | + "metadata_version": "1.2", |
| 5513 | + "name": project.name, |
| 5514 | + "version": "1.0.0", |
| 5515 | + "filetype": "bdist_wheel", |
| 5516 | + "pyversion": "py3", |
| 5517 | + "md5_digest": hashlib.md5(filebody).hexdigest(), |
| 5518 | + "content": pretend.stub( |
| 5519 | + filename=filename, |
| 5520 | + file=io.BytesIO(filebody), |
| 5521 | + type="application/zip", |
| 5522 | + ), |
| 5523 | + } |
| 5524 | + ) |
| 5525 | + |
| 5526 | + with pytest.raises(HTTPBadRequest) as excinfo: |
| 5527 | + legacy.file_upload(db_request) |
| 5528 | + |
| 5529 | + resp = excinfo.value |
| 5530 | + |
| 5531 | + assert resp.status_code == 400 |
| 5532 | + assert resp.status == ( |
| 5533 | + "400 Organization account owning this project is inactive. " |
| 5534 | + "This may be due to inactive billing for Company Organizations, " |
| 5535 | + "or administrator intervention for Community Organizations. " |
| 5536 | + "Please contact [email protected]." |
| 5537 | + ) |
| 5538 | + |
| 5539 | + def test_upload_for_company_organization_owned_project_suceeds_with_subscription( |
| 5540 | + self, pyramid_config, db_request, monkeypatch |
| 5541 | + ): |
| 5542 | + organization = OrganizationFactory.create(orgtype="Company") |
| 5543 | + user = UserFactory.create(with_verified_primary_email=True) |
| 5544 | + OrganizationRoleFactory.create(organization=organization, user=user) |
| 5545 | + OrganizationStripeSubscriptionFactory.create(organization=organization) |
| 5546 | + project = OrganizationProjectFactory.create(organization=organization).project |
| 5547 | + version = "1.0.0" |
| 5548 | + |
| 5549 | + filename = ( |
| 5550 | + f"{project.normalized_name.replace('-', '_')}-{version}-py3-none-any.whl" |
| 5551 | + ) |
| 5552 | + filebody = _get_whl_testdata( |
| 5553 | + name=project.normalized_name.replace("-", "_"), version=version |
| 5554 | + ) |
| 5555 | + |
| 5556 | + @pretend.call_recorder |
| 5557 | + def storage_service_store(path, file_path, *, meta): |
| 5558 | + with open(file_path, "rb") as fp: |
| 5559 | + if file_path.endswith(".metadata"): |
| 5560 | + assert fp.read() == b"Fake metadata" |
| 5561 | + else: |
| 5562 | + assert fp.read() == filebody |
| 5563 | + |
| 5564 | + storage_service = pretend.stub(store=storage_service_store) |
| 5565 | + |
| 5566 | + db_request.find_service = pretend.call_recorder( |
| 5567 | + lambda svc, name=None, context=None: { |
| 5568 | + IFileStorage: storage_service, |
| 5569 | + }.get(svc) |
| 5570 | + ) |
| 5571 | + |
| 5572 | + monkeypatch.setattr( |
| 5573 | + legacy, "_is_valid_dist_file", lambda *a, **kw: (True, None) |
| 5574 | + ) |
| 5575 | + |
| 5576 | + pyramid_config.testing_securitypolicy(identity=user) |
| 5577 | + db_request.user = user |
| 5578 | + db_request.user_agent = "warehouse-tests/6.6.6" |
| 5579 | + db_request.POST = MultiDict( |
| 5580 | + { |
| 5581 | + "metadata_version": "1.2", |
| 5582 | + "name": project.name, |
| 5583 | + "version": "1.0.0", |
| 5584 | + "filetype": "bdist_wheel", |
| 5585 | + "pyversion": "py3", |
| 5586 | + "md5_digest": hashlib.md5(filebody).hexdigest(), |
| 5587 | + "content": pretend.stub( |
| 5588 | + filename=filename, |
| 5589 | + file=io.BytesIO(filebody), |
| 5590 | + type="application/zip", |
| 5591 | + ), |
| 5592 | + } |
| 5593 | + ) |
| 5594 | + |
| 5595 | + resp = legacy.file_upload(db_request) |
| 5596 | + |
| 5597 | + assert resp.status_code == 200 |
| 5598 | + |
5406 | 5599 |
|
5407 | 5600 | def test_submit(pyramid_request):
|
5408 | 5601 | resp = legacy.submit(pyramid_request)
|
|
0 commit comments