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

Fix plan_activated_users count for an installation #132

Merged
merged 4 commits into from
Oct 10, 2023
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
2 changes: 1 addition & 1 deletion services/license.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get_installation_plan_activated_users(db_session) -> list:
query_string = text(
"""
WITH all_plan_activated_users AS (
SELECT
SELECT DISTINCT
UNNEST(o.plan_activated_users) AS activated_owner_id
FROM owners o
) SELECT count(*) as count
Expand Down
54 changes: 51 additions & 3 deletions services/tests/test_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from database.tests.factories import OwnerFactory
from services.activation import activate_user
from services.activation import activate_user, get_installation_plan_activated_users
from services.license import _get_now, is_enterprise


Expand Down Expand Up @@ -62,7 +62,6 @@ def test_activate_user_success_for_users_free(
def test_activate_user_success_for_enterprise_pr_billing(
self, request, dbsession, mocker, mock_configuration, with_sql_functions
):

mocker.patch("services.license.is_enterprise", return_value=True)
mocker.patch("services.license._get_now", return_value=datetime(2020, 4, 2))

Expand All @@ -89,11 +88,60 @@ def test_activate_user_success_for_enterprise_pr_billing(
dbsession.commit()
assert user.ownerid in org.plan_activated_users

def test_activate_user_success_user_org_overlap(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand this test case.

I looks like you are creating 2 orgs with 5 seats taken each. Idk how many total seats this installation (self-hosted) can have. But it seems that all owners of the 2 orgs are distinct (e.g. with different IDs)

So the total count of seats used is 10, and you create a new owner to prove that we can extend that.

Is that the idea?


(assuming I'm correct - which I might very well not be)
What I'd like to see:

  1. Call get_installation_plan_activated_users explicitly to see what users are activated (before and after creating the new user)
  2. Have some overlap on the plan_activated_users across the orgs (cause that was the original problem, right?)

self, request, dbsession, mock_configuration, mocker, with_sql_functions
):
mocker.patch("services.license.is_enterprise", return_value=True)
mocker.patch("services.license._get_now", return_value=datetime(2020, 4, 2))

# Create two orgs to ensure our seat availability checking works across
# multiple organizations.
org = OwnerFactory.create(
service="github",
oauth_token=None,
plan_activated_users=list(range(1, 6)),
plan_auto_activate=True,
)
dbsession.add(org)
dbsession.flush()

org_second = OwnerFactory.create(
service="github",
oauth_token=None,
plan_activated_users=list(range(2, 8)),
plan_auto_activate=True,
)
dbsession.add(org_second)
dbsession.flush()

assert get_installation_plan_activated_users(dbsession)[0][0] == 7

# {'company': 'Test Company', 'expires': '2021-01-01 00:00:00', 'url': 'https://codecov.mysite.com', 'trial': False, 'users': 10, 'repos': None, 'pr_billing': True}
encrypted_license = "wxWEJyYgIcFpi6nBSyKQZQeaQ9Eqpo3SXyUomAqQOzOFjdYB3A8fFM1rm+kOt2ehy9w95AzrQqrqfxi9HJIb2zLOMOB9tSy52OykVCzFtKPBNsXU/y5pQKOfV7iI3w9CHFh3tDwSwgjg8UsMXwQPOhrpvl2GdHpwEhFdaM2O3vY7iElFgZfk5D9E7qEnp+WysQwHKxDeKLI7jWCnBCBJLDjBJRSz0H7AfU55RQDqtTrnR+rsLDHOzJ80/VxwVYhb"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How many seats does this license allows for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a comment to the code

mock_configuration.params["setup"]["enterprise_license"] = encrypted_license
mock_configuration.params["setup"]["codecov_url"] = "https://codecov.mysite.com"

user = OwnerFactory.create_from_test_request(request)
dbsession.add(org_second)
dbsession.add(user)
dbsession.flush()

was_activated = activate_user(dbsession, org_second.ownerid, user.ownerid)
assert was_activated is True
dbsession.commit()

was_activated = activate_user(dbsession, org.ownerid, user.ownerid)
assert was_activated is True
dbsession.commit()

assert get_installation_plan_activated_users(dbsession)[0][0] == 8

def test_activate_user_failure_for_enterprise_pr_billing_no_seats(
self, request, dbsession, mock_configuration, mocker, with_sql_functions
):
mocker.patch("services.license.is_enterprise", return_value=True)
mocker.patch("services.license._get_now", return_value=datetime(2020, 4, 2))

mocker.patch("helpers.environment.is_enterprise", return_value=True)
# Create two orgs to ensure our seat availability checking works across
# multiple organizations.
org = OwnerFactory.create(
Expand Down
Loading