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 postitions can have gangs from seperate organizations #1636

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def seed():
RecruitmentPosition.objects.all().delete()
yield 0, 'Deleted old recruitmentpositions'

gangs = Gang.objects.all()
recruitments = Recruitment.objects.all()
created_count = 0
for recruitment_index, recruitment in enumerate(recruitments):
gangs = Gang.objects.filter(organization=recruitment.organization)
for gang_index, gang in enumerate(sample(list(gangs), 6)):
for i in range(2): # Create 2 instances for each gang and recruitment
position_data = POSITION_DATA.copy()
Expand Down
26 changes: 19 additions & 7 deletions backend/samfundet/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,26 +237,38 @@ def fixture_organization2() -> Iterator[Organization]:

@pytest.fixture
def fixture_gang(fixture_organization: Organization) -> Iterator[Gang]:
organization = Gang.objects.create(
gang = Gang.objects.create(
name_nb='Gang',
name_en='Gang',
abbreviation='G',
organization=fixture_organization,
)
yield organization
organization.delete()
yield gang
gang.delete()


@pytest.fixture
def fixture_gang2(fixture_organization2: Organization) -> Iterator[Gang]:
organization = Gang.objects.create(
def fixture_gang2(fixture_organization: Organization) -> Iterator[Gang]:
gang = Gang.objects.create(
name_nb='Gang 2',
name_en='Gang 2',
abbreviation='G2',
organization=fixture_organization,
)
yield gang
gang.delete()


@pytest.fixture
def fixture_gang_org2(fixture_organization2: Organization) -> Iterator[Gang]:
gang = Gang.objects.create(
name_nb='Gang 3',
name_en='Gang 3',
abbreviation='G3',
organization=fixture_organization2,
)
yield organization
organization.delete()
yield gang
gang.delete()


@pytest.fixture
Expand Down
17 changes: 15 additions & 2 deletions backend/samfundet/models/recruitment.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,24 @@ def resolve_org(self, *, return_id: bool = False) -> Organization | int:
def __str__(self) -> str:
return f'Position: {self.name_en} in {self.recruitment}'

# Error messages
ONLY_ONE_OWNER_ERROR = 'Position must be owned by either gang or section, not both'
NO_OWNER_ERROR = 'Position must have an owner, either a gang or a gang section'
POSITION_NOT_IN_RECRUITMENTORGANIZATION_ERROR = 'Position must be of the organization which hosts the recruitment'

def clean(self) -> None:
super().clean()
errors: dict[str, list[ValidationError]] = defaultdict(list)

if (self.gang and self.section) or not (self.gang or self.section):
raise ValidationError('Position must be owned by either gang or section, not both')
if self.gang and self.gang.organization != self.recruitment.organization:
errors['gang'].append(self.POSITION_NOT_IN_RECRUITMENTORGANIZATION_ERROR)
if self.gang and self.section:
errors['gang'].append(self.ONLY_ONE_OWNER_ERROR)
errors['section'].append(self.ONLY_ONE_OWNER_ERROR)
elif not (self.gang or self.section):
errors['gang'].append(self.NO_OWNER_ERROR)
errors['section'].append(self.NO_OWNER_ERROR)
raise ValidationError(errors)
Snorre98 marked this conversation as resolved.
Show resolved Hide resolved

def save(self, *args: tuple, **kwargs: dict) -> None:
if self.norwegian_applicants_only:
Expand Down
36 changes: 36 additions & 0 deletions backend/samfundet/models/tests/test_recruitment.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,42 @@ def test_actual_deadline_before_shown_deadline(self, fixture_org):
assert Recruitment.SHOWN_AFTER_ACTUAL_ERROR in e['shown_application_deadline']


class TestRecruitmentPosition:
default_data = {
'name_en': 'Name_en',
'name_nb': 'Name_nb',
'short_description_nb': 'short_description_nb',
'short_description_en': 'short_description_en',
'long_description_nb': 'long_description_nb',
'long_description_en': 'long_description_en',
'is_funksjonaer_position': False,
'default_application_letter_nb': 'default_application_letter_nb',
'default_application_letter_en': 'default_application_letter_en',
'norwegian_applicants_only': False,
'tags': 'tag1, tag2, tag3',
}

def test_create_recruitmentposition_gang(self, fixture_recruitment: Recruitment, fixture_gang: Gang):
test_position = RecruitmentPosition.objects.create(**self.default_data, recruitment=fixture_recruitment, gang=fixture_gang)
assert test_position.id

def test_edit_recruitmentposition_gang_from_other_org(self, fixture_recruitment: Recruitment, fixture_gang: Gang, fixture_gang_org2: Gang):
test_position = RecruitmentPosition.objects.create(**self.default_data, recruitment=fixture_recruitment, gang=fixture_gang)
assert test_position.id

with pytest.raises(ValidationError) as error:
test_position.gang = fixture_gang_org2
test_position.save()
e = dict(error.value)
assert RecruitmentPosition.POSITION_NOT_IN_RECRUITMENTORGANIZATION_ERROR in e['gang']

def test_create_recruitmentposition_gang_from_other_org(self, fixture_recruitment: Recruitment, fixture_gang_org2: Gang):
with pytest.raises(ValidationError) as error:
RecruitmentPosition.objects.create(**self.default_data, recruitment=fixture_recruitment, gang=fixture_gang_org2)
e = dict(error.value)
assert RecruitmentPosition.POSITION_NOT_IN_RECRUITMENTORGANIZATION_ERROR in e['gang']


class TestRecruitmentStats:
def test_recruitment_has_stats(self, fixture_recruitment: Recruitment):
"""Check if fixture_recruitment has the related object"""
Expand Down
8 changes: 4 additions & 4 deletions backend/samfundet/tests/test_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_has_perm_different_orgs(
def test_has_perm_different_gangs(
fixture_user: User,
fixture_gang: Gang,
fixture_gang2: Gang,
fixture_gang_org2: Gang,
fixture_gang_permission: Permission,
fixture_role: Role,
):
Expand All @@ -132,7 +132,7 @@ def test_has_perm_different_gangs(
UserGangRole.objects.create(user=fixture_user, role=fixture_role, obj=fixture_gang)

assert backend.has_perm(fixture_user, fixture_gang_permission.codename, fixture_gang)
assert not backend.has_perm(fixture_user, fixture_gang_permission.codename, fixture_gang2)
assert not backend.has_perm(fixture_user, fixture_gang_permission.codename, fixture_gang_org2)


def test_has_perm_different_gang_sections(
Expand Down Expand Up @@ -185,7 +185,7 @@ def test_has_perm_org_downward(
fixture_organization: Organization,
fixture_organization2: Organization,
fixture_gang: Gang,
fixture_gang2: Gang,
fixture_gang_org2: Gang,
fixture_gang_section: GangSection,
fixture_role: Role,
fixture_org_permission: Permission,
Expand Down Expand Up @@ -230,7 +230,7 @@ def test_has_perm_org_downward(

assert backend.has_perm(fixture_user, fixture_gang_section_permission.codename, fixture_gang_section)

fixture_gang_section.gang = fixture_gang2
fixture_gang_section.gang = fixture_gang_org2

assert not backend.has_perm(fixture_user, fixture_gang_section_permission.codename, fixture_gang_section)

Expand Down
Loading