|
23 | 23 | ) |
24 | 24 | from models_library.products import ProductName |
25 | 25 | from models_library.rest_pagination import Page |
| 26 | +from pytest_simcore.aioresponses_mocker import AioResponsesMock |
26 | 27 | from pytest_simcore.helpers.assert_checks import assert_status |
27 | 28 | from pytest_simcore.helpers.faker_factories import ( |
28 | 29 | DEFAULT_TEST_PASSWORD, |
@@ -457,3 +458,263 @@ async def test_reject_user_account( |
457 | 458 | ) |
458 | 459 | # Should fail as the account is already reviewed |
459 | 460 | assert resp.status == status.HTTP_400_BAD_REQUEST |
| 461 | + |
| 462 | + |
| 463 | +@pytest.mark.parametrize( |
| 464 | + "user_role", |
| 465 | + [ |
| 466 | + UserRole.PRODUCT_OWNER, |
| 467 | + ], |
| 468 | +) |
| 469 | +async def test_approve_user_account_with_full_invitation_details( |
| 470 | + client: TestClient, |
| 471 | + logged_user: UserInfoDict, |
| 472 | + account_request_form: dict[str, Any], |
| 473 | + faker: Faker, |
| 474 | + product_name: ProductName, |
| 475 | + pre_registration_details_db_cleanup: None, |
| 476 | + mock_invitations_service_http_api: AioResponsesMock, |
| 477 | +): |
| 478 | + """Test approving user account with complete invitation details (trial days + credits)""" |
| 479 | + assert client.app |
| 480 | + |
| 481 | + test_email = faker.email() |
| 482 | + |
| 483 | + # 1. Create a pre-registered user |
| 484 | + form_data = account_request_form.copy() |
| 485 | + form_data["firstName"] = faker.first_name() |
| 486 | + form_data["lastName"] = faker.last_name() |
| 487 | + form_data["email"] = test_email |
| 488 | + |
| 489 | + resp = await client.post( |
| 490 | + "/v0/admin/user-accounts:pre-register", |
| 491 | + json=form_data, |
| 492 | + headers={X_PRODUCT_NAME_HEADER: product_name}, |
| 493 | + ) |
| 494 | + await assert_status(resp, status.HTTP_200_OK) |
| 495 | + |
| 496 | + # 2. Approve the user with full invitation details |
| 497 | + approval_payload = { |
| 498 | + "email": test_email, |
| 499 | + "invitation": { |
| 500 | + "trialAccountDays": 30, |
| 501 | + "extraCreditsInUsd": 100.0, |
| 502 | + }, |
| 503 | + } |
| 504 | + |
| 505 | + url = client.app.router["approve_user_account"].url_for() |
| 506 | + resp = await client.post( |
| 507 | + f"{url}", |
| 508 | + headers={X_PRODUCT_NAME_HEADER: product_name}, |
| 509 | + json=approval_payload, |
| 510 | + ) |
| 511 | + await assert_status(resp, status.HTTP_204_NO_CONTENT) |
| 512 | + |
| 513 | + # 3. Verify the user account status and invitation data in extras |
| 514 | + resp = await client.get( |
| 515 | + "/v0/admin/user-accounts:search", |
| 516 | + params={"email": test_email}, |
| 517 | + headers={X_PRODUCT_NAME_HEADER: product_name}, |
| 518 | + ) |
| 519 | + found, _ = await assert_status(resp, status.HTTP_200_OK) |
| 520 | + assert len(found) == 1 |
| 521 | + |
| 522 | + user_data = found[0] |
| 523 | + assert user_data["accountRequestStatus"] == "APPROVED" |
| 524 | + assert user_data["accountRequestReviewedBy"] == logged_user["id"] |
| 525 | + assert user_data["accountRequestReviewedAt"] is not None |
| 526 | + |
| 527 | + # 4. Verify invitation data is stored in extras |
| 528 | + assert "invitation" in user_data["extras"] |
| 529 | + invitation_data = user_data["extras"]["invitation"] |
| 530 | + assert invitation_data["guest"] == test_email |
| 531 | + assert invitation_data["issuer"] == str(logged_user["id"]) |
| 532 | + assert invitation_data["trial_account_days"] == 30 |
| 533 | + assert invitation_data["extra_credits_in_usd"] == 100.0 |
| 534 | + assert "invitation_url" in invitation_data |
| 535 | + |
| 536 | + |
| 537 | +@pytest.mark.parametrize( |
| 538 | + "user_role", |
| 539 | + [UserRole.PRODUCT_OWNER], |
| 540 | +) |
| 541 | +async def test_approve_user_account_with_trial_days_only( |
| 542 | + client: TestClient, |
| 543 | + logged_user: UserInfoDict, |
| 544 | + account_request_form: dict[str, Any], |
| 545 | + faker: Faker, |
| 546 | + product_name: ProductName, |
| 547 | + pre_registration_details_db_cleanup: None, |
| 548 | + mock_invitations_service_http_api: AioResponsesMock, |
| 549 | +): |
| 550 | + """Test approving user account with only trial days""" |
| 551 | + assert client.app |
| 552 | + |
| 553 | + test_email = faker.email() |
| 554 | + |
| 555 | + # 1. Create a pre-registered user |
| 556 | + form_data = account_request_form.copy() |
| 557 | + form_data["firstName"] = faker.first_name() |
| 558 | + form_data["lastName"] = faker.last_name() |
| 559 | + form_data["email"] = test_email |
| 560 | + |
| 561 | + resp = await client.post( |
| 562 | + "/v0/admin/user-accounts:pre-register", |
| 563 | + json=form_data, |
| 564 | + headers={X_PRODUCT_NAME_HEADER: product_name}, |
| 565 | + ) |
| 566 | + await assert_status(resp, status.HTTP_200_OK) |
| 567 | + |
| 568 | + # 2. Approve the user with only trial days |
| 569 | + approval_payload = { |
| 570 | + "email": test_email, |
| 571 | + "invitation": { |
| 572 | + "trialAccountDays": 15, |
| 573 | + # No extra_credits_in_usd |
| 574 | + }, |
| 575 | + } |
| 576 | + |
| 577 | + url = client.app.router["approve_user_account"].url_for() |
| 578 | + resp = await client.post( |
| 579 | + f"{url}", |
| 580 | + headers={X_PRODUCT_NAME_HEADER: product_name}, |
| 581 | + json=approval_payload, |
| 582 | + ) |
| 583 | + await assert_status(resp, status.HTTP_204_NO_CONTENT) |
| 584 | + |
| 585 | + # 3. Verify invitation data in extras |
| 586 | + resp = await client.get( |
| 587 | + "/v0/admin/user-accounts:search", |
| 588 | + params={"email": test_email}, |
| 589 | + headers={X_PRODUCT_NAME_HEADER: product_name}, |
| 590 | + ) |
| 591 | + found, _ = await assert_status(resp, status.HTTP_200_OK) |
| 592 | + user_data = found[0] |
| 593 | + |
| 594 | + assert "invitation" in user_data["extras"] |
| 595 | + invitation_data = user_data["extras"]["invitation"] |
| 596 | + assert invitation_data["trial_account_days"] == 15 |
| 597 | + assert invitation_data["extra_credits_in_usd"] is None |
| 598 | + |
| 599 | + |
| 600 | +@pytest.mark.parametrize( |
| 601 | + "user_role", |
| 602 | + [UserRole.PRODUCT_OWNER], |
| 603 | +) |
| 604 | +async def test_approve_user_account_with_credits_only( |
| 605 | + client: TestClient, |
| 606 | + logged_user: UserInfoDict, |
| 607 | + account_request_form: dict[str, Any], |
| 608 | + faker: Faker, |
| 609 | + product_name: ProductName, |
| 610 | + pre_registration_details_db_cleanup: None, |
| 611 | + mock_invitations_service_http_api: AioResponsesMock, |
| 612 | +): |
| 613 | + """Test approving user account with only extra credits""" |
| 614 | + assert client.app |
| 615 | + |
| 616 | + test_email = faker.email() |
| 617 | + |
| 618 | + # 1. Create a pre-registered user |
| 619 | + form_data = account_request_form.copy() |
| 620 | + form_data["firstName"] = faker.first_name() |
| 621 | + form_data["lastName"] = faker.last_name() |
| 622 | + form_data["email"] = test_email |
| 623 | + |
| 624 | + resp = await client.post( |
| 625 | + "/v0/admin/user-accounts:pre-register", |
| 626 | + json=form_data, |
| 627 | + headers={X_PRODUCT_NAME_HEADER: product_name}, |
| 628 | + ) |
| 629 | + await assert_status(resp, status.HTTP_200_OK) |
| 630 | + |
| 631 | + # 2. Approve the user with only extra credits |
| 632 | + approval_payload = { |
| 633 | + "email": test_email, |
| 634 | + "invitation": { |
| 635 | + # No trial_account_days |
| 636 | + "extra_credits_in_usd": 50.0, |
| 637 | + }, |
| 638 | + } |
| 639 | + |
| 640 | + url = client.app.router["approve_user_account"].url_for() |
| 641 | + resp = await client.post( |
| 642 | + f"{url}", |
| 643 | + headers={X_PRODUCT_NAME_HEADER: product_name}, |
| 644 | + json=approval_payload, |
| 645 | + ) |
| 646 | + await assert_status(resp, status.HTTP_204_NO_CONTENT) |
| 647 | + |
| 648 | + # 3. Verify invitation data in extras |
| 649 | + resp = await client.get( |
| 650 | + "/v0/admin/user-accounts:search", |
| 651 | + params={"email": test_email}, |
| 652 | + headers={X_PRODUCT_NAME_HEADER: product_name}, |
| 653 | + ) |
| 654 | + found, _ = await assert_status(resp, status.HTTP_200_OK) |
| 655 | + user_data = found[0] |
| 656 | + |
| 657 | + assert "invitation" in user_data["extras"] |
| 658 | + invitation_data = user_data["extras"]["invitation"] |
| 659 | + assert invitation_data["trial_account_days"] is None |
| 660 | + assert invitation_data["extra_credits_in_usd"] == 50.0 |
| 661 | + |
| 662 | + |
| 663 | +@pytest.mark.parametrize( |
| 664 | + "user_role", |
| 665 | + [ |
| 666 | + UserRole.PRODUCT_OWNER, |
| 667 | + ], |
| 668 | +) |
| 669 | +async def test_approve_user_account_without_invitation( |
| 670 | + client: TestClient, |
| 671 | + logged_user: UserInfoDict, |
| 672 | + account_request_form: dict[str, Any], |
| 673 | + faker: Faker, |
| 674 | + product_name: ProductName, |
| 675 | + pre_registration_details_db_cleanup: None, |
| 676 | +): |
| 677 | + """Test approving user account without any invitation details""" |
| 678 | + assert client.app |
| 679 | + |
| 680 | + test_email = faker.email() |
| 681 | + |
| 682 | + # 1. Create a pre-registered user |
| 683 | + form_data = account_request_form.copy() |
| 684 | + form_data["firstName"] = faker.first_name() |
| 685 | + form_data["lastName"] = faker.last_name() |
| 686 | + form_data["email"] = test_email |
| 687 | + |
| 688 | + resp = await client.post( |
| 689 | + "/v0/admin/user-accounts:pre-register", |
| 690 | + json=form_data, |
| 691 | + headers={X_PRODUCT_NAME_HEADER: product_name}, |
| 692 | + ) |
| 693 | + await assert_status(resp, status.HTTP_200_OK) |
| 694 | + |
| 695 | + # 2. Approve the user without invitation |
| 696 | + approval_payload = { |
| 697 | + "email": test_email, |
| 698 | + # No invitation field |
| 699 | + } |
| 700 | + |
| 701 | + url = client.app.router["approve_user_account"].url_for() |
| 702 | + resp = await client.post( |
| 703 | + f"{url}", |
| 704 | + headers={X_PRODUCT_NAME_HEADER: product_name}, |
| 705 | + json=approval_payload, |
| 706 | + ) |
| 707 | + await assert_status(resp, status.HTTP_204_NO_CONTENT) |
| 708 | + |
| 709 | + # 3. Verify no invitation data in extras |
| 710 | + resp = await client.get( |
| 711 | + "/v0/admin/user-accounts:search", |
| 712 | + params={"email": test_email}, |
| 713 | + headers={X_PRODUCT_NAME_HEADER: product_name}, |
| 714 | + ) |
| 715 | + found, _ = await assert_status(resp, status.HTTP_200_OK) |
| 716 | + user_data = found[0] |
| 717 | + |
| 718 | + assert user_data["accountRequestStatus"] == "APPROVED" |
| 719 | + # Verify no invitation data stored |
| 720 | + assert "invitation" not in user_data["extras"] |
0 commit comments