Skip to content

Commit

Permalink
refactor: Update feature flag keys (#1192)
Browse files Browse the repository at this point in the history
* refactor: Update feature flag keys and improve error logging in AuthService

* test: Add unit tests for community admin testimonial nudge functionality

* test: Remove debug print statement from CadminTestimonialNudge test

* refactor: Improve community admin authorization check in EventStore

* refactor: Remove unnecessary user_info parameter from send_events_report call in download_data

* fix: Handle case where community may be None in Action model serialization

* refactor: Simplify admin authorization check in EventStore and enhance test coverage for file URL retrieval
  • Loading branch information
abdullai-t authored Dec 4, 2024
1 parent 2f571ee commit 3bddeed
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/_main_/utils/feature_flag_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
REMOVE_DUPLICATE_IMAGE_FF = "remove-duplicate-images-feature-flag"
UPDATE_ACTIONS_CONTENT_FF = "update-actions-content-feature-flag"
REWIRING_AMERICA_MENU_ITEM_FF = "rewiring-america-menu-item-feature-flag"
SHARED_TESTIMONIALS_NUDGE_FF = "shared-testimonials-nudge-feature-flag"
TESTIMONIAL_AUTO_SHARE_SETTINGS_NUDGE_FEATURE_FLAG_KEY = "testimonial-auto-sharing-feature-flag"
4 changes: 2 additions & 2 deletions src/api/services/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ def login(self, context: Context):
else:
return None, None, CustomMassenergizeError("invalid_auth")
except PermissionError:
log.error("not_an_admin", level="error")
log.error("not_an_admin")
return None, None, CustomMassenergizeError('not_an_admin')
except Exception as e:
print(e)
log.error("Authentication Error", level="error")
log.error("Authentication Error")
return None, None, CustomMassenergizeError(e)


Expand Down
5 changes: 3 additions & 2 deletions src/api/store/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,8 +801,9 @@ def delete_event(self, context: Context, event_id) -> Tuple[dict, MassEnergizeAP
events = Event.objects.filter(id=event_id)
if not events:
return None, InvalidResourceError()

if not is_admin_of_community(context, events.first().community.id):

event_community = events.first().community
if event_community and not is_admin_of_community(context, event_community.id):
return None, NotAuthorizedError()

if len(events) > 1:
Expand Down
2 changes: 1 addition & 1 deletion src/api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def download_data(self, args, download_type):
for com in community_list:
events = generate_event_list_for_community(com)
event_list = events.get("events", [])
stat = send_events_report(user.full_name, user.email, event_list, user.user_info)
stat = send_events_report(user.full_name, user.email, event_list)
if not stat:
error_notification(CADMIN_REPORT, email)
return
Expand Down
92 changes: 92 additions & 0 deletions src/api/tests/unit/tasks/test_cadmin_testimonial_nudge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from django.test.testcases import TestCase
from _main_.utils.feature_flag_keys import TESTIMONIAL_AUTO_SHARE_SETTINGS_NUDGE_FEATURE_FLAG_KEY
from _main_.utils.utils import Console
from api.tests.common import make_feature_flag, make_testimonial_auto_share_settings, makeAdmin, makeCommunity, makeTestimonial, makeUser
from database.models import TestimonialSharedCommunity
from database.utils.settings.model_constants.enums import SharingType
from task_queue.nudges.cadmin_testimonial_nudge import get_cadmin_names_and_emails, prepare_testimonials_for_community_admins
from django.utils import timezone

class CadminTestimonialNudgeTestCases(TestCase):

def setUp(self):
self.c1 = makeCommunity(name="Test Community 1")
self.c2 = makeCommunity(name="Test Community 2")
self.c3 = makeCommunity(name="Test Community 3")
self.c4 = makeCommunity(name="Test Community 4")
self.c5 = makeCommunity(name="Test Community 5")

user = makeUser(email="[email protected]", full_name="Test Admin")
user1 = makeUser(email="[email protected]", full_name="Test Admin 1")
user2 = makeUser(email="[email protected]", full_name="Test Admin 2")
user3 = makeUser(email="[email protected]", full_name="Test Admin 3")
user4 = makeUser(email="[email protected]", full_name="Test Admin 4")
makeAdmin(communities=[self.c1],admin=user)
makeAdmin(communities=[self.c2],admin=user1)
makeAdmin(communities=[self.c3],admin=user2)
makeAdmin(communities=[self.c4],admin=user3)
makeAdmin(communities=[self.c1],admin=user4)


make_feature_flag(
key=TESTIMONIAL_AUTO_SHARE_SETTINGS_NUDGE_FEATURE_FLAG_KEY,
communities=[self.c1, self.c2, self.c3, self.c4, self.c5],
audience="EVERYONE",
name="Testimonial Auto Share Settings Nudge"
)

t1 =makeTestimonial(
community=self.c2, user=user, title="Testimonial shared to c2 c3, c5",
sharing_type=SharingType.OPEN_TO.value[0], audience=[self.c2, self.c3, self.c5],
is_published=True,
published_at=timezone.now()
)
t2 = makeTestimonial(
community=self.c3, user=user, title="Testimonial shared to none",
sharing_type=SharingType.OPEN.value[0],
is_published=True,
published_at=timezone.now()
)
t3 = makeTestimonial(
community=self.c1, user=user, title="Testimonial shared to none for c1",
sharing_type=SharingType.OPEN.value[0],
is_published=True,
published_at=timezone.now()
)
t4 = makeTestimonial(
community=self.c3, user=user, title="Testimonial shared to none for c3",
sharing_type=SharingType.OPEN.value[0],
is_published=True,
published_at=timezone.now()
)
TestimonialSharedCommunity.objects.create(community=self.c2, testimonial=t1)
TestimonialSharedCommunity.objects.create(community=self.c3, testimonial=t1)
TestimonialSharedCommunity.objects.create(community=self.c5, testimonial=t1)
TestimonialSharedCommunity.objects.create(community=self.c1, testimonial=t3)
TestimonialSharedCommunity.objects.create(community=self.c3, testimonial=t4)


def tearDown(self):
return super().tearDown()


def test_get_cadmin_names_and_emails_success(self):

Console.header("Test get_cadmin_names_and_emails_success for c1")

data = get_cadmin_names_and_emails(self.c1)
keys = data.keys()
self.assertIn('[email protected]', keys)
self.assertIn('[email protected]', keys)
self.assertIsNotNone(data)



def test_prepare_testimonials_for_community_admins_success(self):
Console.header("Test prepare_testimonials_for_community_admins_success")
task = None
result = prepare_testimonials_for_community_admins(task)
self.assertTrue(result)



2 changes: 1 addition & 1 deletion src/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2068,7 +2068,7 @@ def info(self):
"community":{
"id": self.community.id,
"name": self.community.name,
},
} if self.community else None,
"image": get_json_if_not_none(self.image),
}

Expand Down
4 changes: 2 additions & 2 deletions src/task_queue/nudges/cadmin_testimonial_nudge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from _main_.utils.common import encode_data_for_URL, serialize_all
from _main_.utils.constants import ADMIN_URL_ROOT, COMMUNITY_URL_ROOT
from _main_.utils.emailer.send_email import send_massenergize_email_with_attachments
from _main_.utils.feature_flag_keys import SHARED_TESTIMONIALS_NUDGE_FF
from _main_.utils.feature_flag_keys import TESTIMONIAL_AUTO_SHARE_SETTINGS_NUDGE_FEATURE_FLAG_KEY
from _main_.utils.massenergize_logger import log
from api.utils.api_utils import get_sender_email
from api.utils.constants import CADMIN_TESTIMONIAL_NUDGE_TEMPLATE
Expand Down Expand Up @@ -113,7 +113,7 @@ def prepare_testimonials_for_community_admins(task=None):
"""
try:

flag = FeatureFlag.objects.get(key=SHARED_TESTIMONIALS_NUDGE_FF)
flag = FeatureFlag.objects.get(key=TESTIMONIAL_AUTO_SHARE_SETTINGS_NUDGE_FEATURE_FLAG_KEY)
if not flag or not flag.enabled():
return False

Expand Down
20 changes: 18 additions & 2 deletions src/website/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# from django.test import TestCase
from django.test import TestCase
from unittest.mock import Mock
from api.tests.common import makeMedia
from website.views import _get_file_url

# # Create your tests here.
class GetFileUrlTests(TestCase):

def setUp(self):
self.m1 = makeMedia(name="m1")
self.m2 = makeMedia(name="m2", file=None)

def test_get_file_url_with_no_image(self):
result = _get_file_url(None)
self.assertEqual(result, "")

def test_get_file_url_with_file(self):
result = _get_file_url(self.m1)
self.assertRegex(result, r'^/media/media/.*$')

4 changes: 2 additions & 2 deletions src/website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ def _base_community_query(is_sandbox):

def _get_file_url(image):
if not image:
return None
return image.file.url if image.file else None
return ""
return image.file.url if image.file else ""


def _separate_communities(communities, lat, long):
Expand Down

0 comments on commit 3bddeed

Please sign in to comment.