Skip to content

Commit

Permalink
Add checks and tests for manual OT emails (#4724)
Browse files Browse the repository at this point in the history
* Add checks and tests for manual OT emails

* Update test name

* Add FeatureEntry entity for test

---------

Co-authored-by: DanielRyanSmith <[email protected]>
  • Loading branch information
DanielRyanSmith and DanielRyanSmith authored Jan 24, 2025
1 parent 8197ba9 commit 871b13d
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 2 deletions.
18 changes: 16 additions & 2 deletions internals/maintenance_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,11 +849,19 @@ def get_template_data(self, **kwargs):
stage: Stage|None = Stage.get_by_id(stage_id)
if not stage:
return f'Stage {stage_id} not found'
if stage.stage_type not in ALL_ORIGIN_TRIAL_STAGE_TYPES:
return f'Stage {stage_id} is not an origin trial stage'
if not stage.ot_owner_email and not stage.ot_emails:
return f'Stage {stage_id} has no OT contacts set'
if not stage.ot_display_name:
return f'Stage {stage_id} does not have ot_display_name set'
if stage.ot_activation_date is None:
return f'Stage {stage_id} does not have ot_activation_date set'

cloud_tasks_helpers.enqueue_task(
'/tasks/email-ot-creation-processed',
{'stage': converters.stage_to_json_dict(stage)})
return 'Email task enqueued.'
return 'Email task enqueued'


class SendManualOTActivatedEmail(FlaskHandler):
Expand All @@ -867,8 +875,14 @@ def get_template_data(self, **kwargs):
stage: Stage|None = Stage.get_by_id(stage_id)
if not stage:
return f'Stage {stage_id} not found'
if stage.stage_type not in ALL_ORIGIN_TRIAL_STAGE_TYPES:
return f'Stage {stage_id} is not an origin trial stage'
if not stage.ot_owner_email and not stage.ot_emails:
return f'Stage {stage_id} has no OT contacts set'
if not stage.ot_display_name:
return f'Stage {stage_id} does not have ot_display_name set'

cloud_tasks_helpers.enqueue_task(
'/tasks/email-ot-activated',
{'stage': converters.stage_to_json_dict(stage)})
return 'Email task enqueued.'
return 'Email task enqueued'
135 changes: 135 additions & 0 deletions internals/maintenance_scripts_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,3 +927,138 @@ def test_fetch_webdx_feature_ids__exceptions(self, mock_list_features):
result = self.handler.get_template_data()

self.assertEqual('Running FetchWebdxFeatureId() job failed.', result)


class SendManualOTCreatedEmailTest(testing_config.CustomTestCase):

def setUp(self):
self.handler = maintenance_scripts.SendManualOTCreatedEmail()
self.feature_1 = FeatureEntry(
id=1, name='feature a', summary='sum', category=1)
self.feature_1.put()
self.ot_stage = Stage(id=111,
feature_id=1, stage_type=150, ot_owner_email='[email protected]',
ot_emails=['[email protected]'], ot_display_name='Example trial',
ot_activation_date=date(2020, 1, 1))
self.ot_stage.put()
self.ot_stage_id=self.ot_stage.key.integer_id()
self.non_ot_stage = Stage(id=222, feature_id=1, stage_type=120)
self.non_ot_stage.put()

def tearDown(self):
for kind in [Stage, FeatureEntry]:
for entity in kind.query():
entity.key.delete()

@mock.patch('framework.cloud_tasks_helpers.enqueue_task')
def test_send__valid(self, mock_enqueue):
"""An email is sent if the stage meets all requirements."""
result = self.handler.get_template_data(stage_id=self.ot_stage_id)
self.assertEqual('Email task enqueued', result)
mock_enqueue.assert_called_once()

@mock.patch('framework.cloud_tasks_helpers.enqueue_task')
def test_send__invalid_stage(self, mock_enqueue):
"""No email is sent if the stage does not exist."""
result = self.handler.get_template_data(stage_id=12345)
self.assertEqual('Stage 12345 not found', result)
mock_enqueue.assert_not_called()

@mock.patch('framework.cloud_tasks_helpers.enqueue_task')
def test_send__non_ot_stage(self, mock_enqueue):
"""No email is sent if the stage is not an OT stage."""
result = self.handler.get_template_data(
stage_id=self.non_ot_stage.key.integer_id())
self.assertEqual('Stage 222 is not an origin trial stage', result)
mock_enqueue.assert_not_called()

@mock.patch('framework.cloud_tasks_helpers.enqueue_task')
def test_send__no_contacts(self, mock_enqueue):
"""No email is sent if the stage contains no OT contacts."""
self.ot_stage.ot_owner_email = None
self.ot_stage.ot_emails = []
self.ot_stage.put()
result = self.handler.get_template_data(stage_id=self.ot_stage_id)
self.assertEqual('Stage 111 has no OT contacts set', result)
mock_enqueue.assert_not_called()

@mock.patch('framework.cloud_tasks_helpers.enqueue_task')
def test_send__no_display_name(self, mock_enqueue):
"""No email is sent if the stage does not have a display name."""
self.ot_stage.ot_display_name = None
self.ot_stage.put()
result = self.handler.get_template_data(stage_id=self.ot_stage_id)
self.assertEqual('Stage 111 does not have ot_display_name set', result)
mock_enqueue.assert_not_called()

@mock.patch('framework.cloud_tasks_helpers.enqueue_task')
def test_send__no_activation_date(self, mock_enqueue):
"""No email is sent if the stage does not have a scheduled activation
date."""
self.ot_stage.ot_activation_date = None
self.ot_stage.put()
result = self.handler.get_template_data(stage_id=self.ot_stage_id)
self.assertEqual('Stage 111 does not have ot_activation_date set', result)
mock_enqueue.assert_not_called()


class SendManualOTActivatedEmailTest(testing_config.CustomTestCase):

def setUp(self):
self.handler = maintenance_scripts.SendManualOTActivatedEmail()
self.feature_1 = FeatureEntry(
id=1, name='feature a', summary='sum', category=1)
self.feature_1.put()
self.ot_stage = Stage(id=111,
feature_id=1, stage_type=150, ot_owner_email='[email protected]',
ot_emails=['[email protected]'], ot_display_name='Example trial')
self.ot_stage.put()
self.ot_stage_id=self.ot_stage.key.integer_id()
self.non_ot_stage = Stage(id=222, feature_id=1, stage_type=120)
self.non_ot_stage.put()

def tearDown(self):
for kind in [Stage, FeatureEntry]:
for entity in kind.query():
entity.key.delete()

@mock.patch('framework.cloud_tasks_helpers.enqueue_task')
def test_send__valid(self, mock_enqueue):
"""An email is sent if the stage meets all requirements."""
result = self.handler.get_template_data(stage_id=self.ot_stage_id)
self.assertEqual('Email task enqueued', result)
mock_enqueue.assert_called_once()

@mock.patch('framework.cloud_tasks_helpers.enqueue_task')
def test_send__invalid_stage(self, mock_enqueue):
"""No email is sent if the stage does not exist."""
result = self.handler.get_template_data(stage_id=12345)
self.assertEqual('Stage 12345 not found', result)
mock_enqueue.assert_not_called()

@mock.patch('framework.cloud_tasks_helpers.enqueue_task')
def test_send__non_ot_stage(self, mock_enqueue):
"""No email is sent if the stage is not an OT stage."""
result = self.handler.get_template_data(
stage_id=self.non_ot_stage.key.integer_id())
self.assertEqual('Stage 222 is not an origin trial stage', result)
mock_enqueue.assert_not_called()

@mock.patch('framework.cloud_tasks_helpers.enqueue_task')
def test_send__no_contacts(self, mock_enqueue):
"""No email is sent if the stage contains no OT contacts."""
self.ot_stage.ot_owner_email = None
self.ot_stage.ot_emails = []
self.ot_stage.put()
result = self.handler.get_template_data(stage_id=self.ot_stage_id)
self.assertEqual('Stage 111 has no OT contacts set', result)
mock_enqueue.assert_not_called()

@mock.patch('framework.cloud_tasks_helpers.enqueue_task')
def test_send__no_display_name(self, mock_enqueue):
"""No email is sent if the stage does not have a display name."""
self.ot_stage.ot_display_name = None
self.ot_stage.put()
result = self.handler.get_template_data(stage_id=self.ot_stage_id)
self.assertEqual('Stage 111 does not have ot_display_name set', result)
mock_enqueue.assert_not_called()

0 comments on commit 871b13d

Please sign in to comment.