Skip to content

Commit

Permalink
Sync account when creating in Okta
Browse files Browse the repository at this point in the history
This just syncs a bunch of other fields for accounts created during the Okta
settings creation process.
  • Loading branch information
michelletran-codecov committed Aug 6, 2024
1 parent 24c8d91 commit 28562be
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
41 changes: 35 additions & 6 deletions codecov_auth/commands/owner/interactors/save_okta_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from dataclasses import dataclass
from typing import Optional

from django.contrib.auth.models import User
from shared.django_apps.codecov_auth.models import AccountsUsers

from codecov.commands.base import BaseInteractor
from codecov.commands.exceptions import Unauthenticated, Unauthorized, ValidationError
from codecov.db import sync_to_async
Expand Down Expand Up @@ -42,14 +45,40 @@ def execute(self, input: dict):
).first()
self.validate(owner=owner)

account_id = owner.account_id
if not account_id:
account = Account.objects.create()
account_id = account.id
owner.account_id = account_id
account = owner.account
if not account:
account = Account.objects.create(
name=owner.username,
plan=owner.plan,
plan_seat_count=owner.plan_user_count,
free_seat_count=owner.free,
plan_auto_activate=owner.plan_auto_activate,
)
owner.account = account
owner.save()

okta_config, created = OktaSettings.objects.get_or_create(account_id=account_id)
# Update the activated users to be added to the account
plan_activated_user_owners: list[int] = owner.plan_activated_users
activated_connections: list[AccountsUsers] = []
for activated_user_owner in plan_activated_user_owners:
user_owner: Owner = Owner.objects.get(pk=activated_user_owner)
user = user_owner.user
if user is None:
user = User(name=user_owner.name, email=user_owner.email)
user_owner.userid = user.id
user_owner.save()

Check warning on line 69 in codecov_auth/commands/owner/interactors/save_okta_config.py

View check run for this annotation

Codecov Notifications / codecov/patch

codecov_auth/commands/owner/interactors/save_okta_config.py#L67-L69

Added lines #L67 - L69 were not covered by tests

activated_connections.append(AccountsUsers(account=account, user=user))

# Batch the user creation in batches of 50 users
if len(activated_connections) > 50:
AccountsUsers.objects.bulk_create(activated_connections)
activated_connections = []

if activated_connections:
AccountsUsers.objects.bulk_create(activated_connections)

okta_config, created = OktaSettings.objects.get_or_create(account=account)

for field in ["client_id", "client_secret", "url", "enabled", "enforced"]:
value = getattr(typed_input, field)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from asgiref.sync import async_to_sync
from django.contrib.auth.models import AnonymousUser
from django.test import TransactionTestCase
from shared.django_apps.codecov_auth.models import Account

from codecov.commands.exceptions import Unauthenticated, Unauthorized, ValidationError
from codecov_auth.models import OktaSettings
Expand All @@ -18,16 +19,20 @@ class SaveOktaConfigInteractorTest(TransactionTestCase):
def setUp(self):
self.current_user = OwnerFactory(username="codecov-user")
self.service = "github"
user1 = OwnerFactory()
user2 = OwnerFactory()
self.owner = OwnerFactory(
username=self.current_user.username,
service=self.service,
account=AccountFactory(),
)

self.owner_with_admins = OwnerFactory(
username=self.current_user.username,
service=self.service,
admins=[self.current_user.ownerid],
account=AccountFactory(),
plan_activated_users=[user1.ownerid, user2.ownerid],
account=None,
)

self.interactor = SaveOktaConfigInteractor(
Expand Down Expand Up @@ -88,21 +93,43 @@ def test_unauthorized_error_when_user_is_not_admin(self):
)

def test_create_okta_settings_when_account_does_not_exist(self):
plan_activated_users = []
for _ in range(100):
user_owner = OwnerFactory()
plan_activated_users.append(user_owner.ownerid)

org_with_lots_of_users = OwnerFactory(
service=self.service,
admins=[self.current_user.ownerid],
plan_activated_users=plan_activated_users,
)

input_data = {
"client_id": "some-client-id",
"client_secret": "some-client-secret",
"url": "https://okta.example.com",
"enabled": True,
"enforced": True,
"org_username": self.owner_with_admins.username,
"org_username": org_with_lots_of_users.username,
}

interactor = SaveOktaConfigInteractor(
current_owner=self.current_user, service=self.service
)
self.execute(interactor=interactor, input=input_data)

okta_config = OktaSettings.objects.get(account=self.owner_with_admins.account)
org_with_lots_of_users.refresh_from_db()
account = org_with_lots_of_users.account

assert account.name == org_with_lots_of_users.username
assert account.plan == org_with_lots_of_users.plan
assert account.plan_seat_count == org_with_lots_of_users.plan_user_count
assert account.free_seat_count == org_with_lots_of_users.free

assert account.users.count() == 100
assert account.users.count() == len(org_with_lots_of_users.plan_activated_users)

okta_config = OktaSettings.objects.get(account=org_with_lots_of_users.account)

assert okta_config.client_id == input_data["client_id"]
assert okta_config.client_secret == input_data["client_secret"]
Expand Down

0 comments on commit 28562be

Please sign in to comment.