diff --git a/src/_main_/utils/feature_flag_keys.py b/src/_main_/utils/feature_flag_keys.py index e3628e822..93d8649e7 100644 --- a/src/_main_/utils/feature_flag_keys.py +++ b/src/_main_/utils/feature_flag_keys.py @@ -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" diff --git a/src/api/services/auth.py b/src/api/services/auth.py index 4a5bff363..e241ac45d 100644 --- a/src/api/services/auth.py +++ b/src/api/services/auth.py @@ -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) diff --git a/src/api/store/event.py b/src/api/store/event.py index 34ed462cd..f104ae7a0 100644 --- a/src/api/store/event.py +++ b/src/api/store/event.py @@ -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: diff --git a/src/api/tasks.py b/src/api/tasks.py index 751a30260..ffb867c33 100644 --- a/src/api/tasks.py +++ b/src/api/tasks.py @@ -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 diff --git a/src/api/tests/unit/tasks/test_cadmin_testimonial_nudge.py b/src/api/tests/unit/tasks/test_cadmin_testimonial_nudge.py new file mode 100644 index 000000000..5704ee10a --- /dev/null +++ b/src/api/tests/unit/tasks/test_cadmin_testimonial_nudge.py @@ -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="tamUser+23@me.org", full_name="Test Admin") + user1 = makeUser(email="test+user1@me.com", full_name="Test Admin 1") + user2 = makeUser(email="test+user2@me.com", full_name="Test Admin 2") + user3 = makeUser(email="test+user3@me.com", full_name="Test Admin 3") + user4 = makeUser(email="test+user4@me.com", 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('tamUser+23@me.org', keys) + self.assertIn('test+user4@me.com', 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) + + + diff --git a/src/database/models.py b/src/database/models.py index ee76ded3b..616666b25 100644 --- a/src/database/models.py +++ b/src/database/models.py @@ -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), } diff --git a/src/task_queue/nudges/cadmin_testimonial_nudge.py b/src/task_queue/nudges/cadmin_testimonial_nudge.py index 1b4f1e0ef..a0d206641 100644 --- a/src/task_queue/nudges/cadmin_testimonial_nudge.py +++ b/src/task_queue/nudges/cadmin_testimonial_nudge.py @@ -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 @@ -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 diff --git a/src/website/tests.py b/src/website/tests.py index 1090ffce8..a947b3bd1 100644 --- a/src/website/tests.py +++ b/src/website/tests.py @@ -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/.*$') + diff --git a/src/website/views.py b/src/website/views.py index c38e6817c..48484a876 100644 --- a/src/website/views.py +++ b/src/website/views.py @@ -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):