-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2917 from cisagov/nl/2859-multiple-portfolio-flag…
…s-bug #2859 multiple portfolio flags bug - [KO]
- Loading branch information
Showing
2 changed files
with
50 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,6 +299,7 @@ class TestUserPortfolioPermission(TestCase): | |
@less_console_noise_decorator | ||
def setUp(self): | ||
self.user, _ = User.objects.get_or_create(email="[email protected]") | ||
self.user2, _ = User.objects.get_or_create(email="[email protected]", username="user2") | ||
super().setUp() | ||
|
||
def tearDown(self): | ||
|
@@ -336,24 +337,53 @@ def test_clean_on_multiple_portfolios_when_flag_active(self): | |
@override_flag("multiple_portfolios", active=False) | ||
def test_clean_on_creates_multiple_portfolios(self): | ||
"""Ensures that a user cannot create multiple portfolio permission objects when the flag is disabled""" | ||
# Create an instance of User with a portfolio but no roles or additional permissions | ||
# Create an instance of User with a single portfolio | ||
portfolio, _ = Portfolio.objects.get_or_create(creator=self.user, organization_name="Hotel California") | ||
portfolio_2, _ = Portfolio.objects.get_or_create(creator=self.user, organization_name="Motel California") | ||
portfolio_permission, _ = UserPortfolioPermission.objects.get_or_create( | ||
portfolio=portfolio, user=self.user, roles=[UserPortfolioRoleChoices.ORGANIZATION_ADMIN] | ||
) | ||
portfolio_2, _ = Portfolio.objects.get_or_create(creator=self.user, organization_name="Motel California") | ||
portfolio_permission_2 = UserPortfolioPermission( | ||
portfolio=portfolio_2, user=self.user, roles=[UserPortfolioRoleChoices.ORGANIZATION_ADMIN] | ||
) | ||
|
||
# This should work as intended | ||
portfolio_permission.clean() | ||
|
||
# Test if the ValidationError is raised with the correct message | ||
with self.assertRaises(ValidationError) as cm: | ||
portfolio_permission_2.clean() | ||
|
||
portfolio_permission_2, _ = UserPortfolioPermission.objects.get_or_create(portfolio=portfolio, user=self.user) | ||
self.assertEqual( | ||
cm.exception.message, | ||
( | ||
"This user is already assigned to a portfolio. " | ||
"Based on current waffle flag settings, users cannot be assigned to multiple portfolios." | ||
), | ||
) | ||
|
||
@less_console_noise_decorator | ||
@override_flag("multiple_portfolios", active=False) | ||
def test_multiple_portfolio_reassignment(self): | ||
"""Ensures that a user cannot be assigned to multiple portfolios based on reassignment""" | ||
# Create an instance of two users with separate portfolios | ||
portfolio, _ = Portfolio.objects.get_or_create(creator=self.user, organization_name="Hotel California") | ||
portfolio_permission, _ = UserPortfolioPermission.objects.get_or_create( | ||
portfolio=portfolio, user=self.user, roles=[UserPortfolioRoleChoices.ORGANIZATION_ADMIN] | ||
) | ||
portfolio_2, _ = Portfolio.objects.get_or_create(creator=self.user2, organization_name="Motel California") | ||
portfolio_permission_2 = UserPortfolioPermission( | ||
portfolio=portfolio_2, user=self.user2, roles=[UserPortfolioRoleChoices.ORGANIZATION_ADMIN] | ||
) | ||
|
||
# This should work as intended | ||
portfolio_permission.clean() | ||
portfolio_permission_2.clean() | ||
|
||
# Reassign the portfolio of "user2" to "user" (this should throw an error | ||
# preventing "user" from having multiple portfolios) | ||
with self.assertRaises(ValidationError) as cm: | ||
portfolio_permission_2.user = self.user | ||
portfolio_permission_2.clean() | ||
|
||
self.assertEqual( | ||
cm.exception.message, | ||
|