<commit_before>import pytest

from awx.main.access import CredentialAccess
from awx.main.models.credential import Credential
from awx.main.models.jobs import JobTemplate
from awx.main.models.inventory import InventorySource
from awx.main.migrations import _rbac as rbac
from django.apps import apps
from django.contrib.auth.models import User

@pytest.mark.django_db
def test_credential_migration_user(credential, user, permissions):
    u = user('user', False)
    credential.deprecated_user = u
    credential.save()

    rbac.migrate_credential(apps, None)

    assert u in credential.admin_role

@pytest.mark.django_db
def test_two_teams_same_cred_name(organization_factory):
    objects = organization_factory("test",
                                   teams=["team1", "team2"])

    cred1 = Credential.objects.create(name="test", kind="net", deprecated_team=objects.teams.team1)
    cred2 = Credential.objects.create(name="test", kind="net", deprecated_team=objects.teams.team2)

    rbac.migrate_credential(apps, None)

    assert objects.teams.team1.admin_role in cred1.admin_role.parents.all()
    assert objects.teams.team2.admin_role in cred2.admin_role.parents.all()
    assert objects.teams.team1.member_role in cred1.use_role.parents.all()
    assert objects.teams.team2.member_role in cred2.use_role.parents.all()

@pytest.mark.django_db
def test_credential_use_role(credential, user, permissions):
    u = user('user', False)
    credential.use_role.members.add(u)
    assert u in credential.use_role

@pytest.mark.django_db
def test_credential_migration_team_member(credential, team, user, permissions):
    u = user('user', False)
    team.member_role.members.add(u)
    credential.deprecated_team = team
    credential.save()


    # No permissions pre-migration (this happens automatically so we patch this)
    team.admin_role.children.remove(credential.admin_role)
    team.member_role.children.remove(credential.use_role)
    assert u not in credential.admin_role

    rbac.migrate_credential(apps, None)

    # Admin permissions post migration
    assert u in credential.use_role

@pytest.mark.django_db
def test_credential_migration_team_admin(credential, team, user, permissions):
    u = user('user', False)
    team.member_role.members.add(u)
    credential.deprecated_team = team
    credential.save()

    assert u not in credential.use_role

    # Usage permissions post migration
    rbac.migrate_credential(apps, None)
    assert u in credential.use_role

def test_credential_access_superuser():
    u = User(username='admin', is_superuser=True)
    access = CredentialAccess(u)
    credential = Credential()

    assert access.can_add(None)
    assert access.can_change(credential, None)
    assert access.can_delete(credential)<commit_msg>Update team admin credential migration test to current state-of-knowledge<commit_after>import pytest

from awx.main.access import CredentialAccess
from awx.main.models.credential import Credential
from awx.main.models.jobs import JobTemplate
from awx.main.models.inventory import InventorySource
from awx.main.migrations import _rbac as rbac
from django.apps import apps
from django.contrib.auth.models import User

@pytest.mark.django_db
def test_credential_migration_user(credential, user, permissions):
    u = user('user', False)
    credential.deprecated_user = u
    credential.save()

    rbac.migrate_credential(apps, None)

    assert u in credential.admin_role

@pytest.mark.django_db
def test_two_teams_same_cred_name(organization_factory):
    objects = organization_factory("test",
                                   teams=["team1", "team2"])

    cred1 = Credential.objects.create(name="test", kind="net", deprecated_team=objects.teams.team1)
    cred2 = Credential.objects.create(name="test", kind="net", deprecated_team=objects.teams.team2)

    rbac.migrate_credential(apps, None)

    assert objects.teams.team1.admin_role in cred1.admin_role.parents.all()
    assert objects.teams.team2.admin_role in cred2.admin_role.parents.all()
    assert objects.teams.team1.member_role in cred1.use_role.parents.all()
    assert objects.teams.team2.member_role in cred2.use_role.parents.all()

@pytest.mark.django_db
def test_credential_use_role(credential, user, permissions):
    u = user('user', False)
    credential.use_role.members.add(u)
    assert u in credential.use_role

@pytest.mark.django_db
def test_credential_migration_team_member(credential, team, user, permissions):
    u = user('user', False)
    team.member_role.members.add(u)
    credential.deprecated_team = team
    credential.save()


    # No permissions pre-migration (this happens automatically so we patch this)
    team.admin_role.children.remove(credential.admin_role)
    team.member_role.children.remove(credential.use_role)
    assert u not in credential.admin_role

    rbac.migrate_credential(apps, None)

    # Admin permissions post migration
    assert u in credential.use_role
    assert u not in credential.admin_role

@pytest.mark.django_db
def test_credential_migration_team_admin(credential, team, user, permissions):
    u = user('user', False)
    team.admin_role.members.add(u)
    credential.deprecated_team = team
    credential.save()

    assert u not in credential.use_role

    # Usage permissions post migration
    rbac.migrate_credential(apps, None)
    assert u in credential.admin_role

def test_credential_access_superuser():
    u = User(username='admin', is_superuser=True)
    access = CredentialAccess(u)
    credential = Credential()

    assert access.can_add(None)
    assert access.can_change(credential, None)
    assert access.can_delete(credential)
{'fraction_non_alphanumeric': 0.09219607149177136, 'fraction_numerical': 0.004954875243319766, 'mean_word_length': 4.450337512054002, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4036281', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1782, 'n_words': 370}