Skip to content

Commit

Permalink
Pull request update/240926
Browse files Browse the repository at this point in the history
cbd2765 OS-7852. Update dependencies
0b2b4c8 OS-7813. Optional template_params parameter
  • Loading branch information
stanfra authored Sep 26, 2024
2 parents 1a1f2cd + cbd2765 commit 82f7757
Show file tree
Hide file tree
Showing 6 changed files with 577 additions and 621 deletions.
15 changes: 11 additions & 4 deletions herald/herald_server/controllers/email.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import json

from herald.herald_server.controllers.base import BaseController
from herald.herald_server.controllers.base_async import BaseAsyncControllerWrapper
from herald.herald_server.controllers.base_async import (
BaseAsyncControllerWrapper
)
from herald.herald_server.utils import is_hystax_email


Expand All @@ -11,12 +13,17 @@ def _get_controller_class(self):


class EmailController(BaseController):
def skip_email_send(self, data):

@staticmethod
def skip_email_send(data):
template_type = data.get('template_type')
if template_type in ['new_employee', 'new_cloud_account',
'cloud_account_deleted']:
auth_user_email = data.get('template_params', {}).get(
'texts', {}).get('user_email')
auth_user_email = None
template_params = data.get('template_params')
if template_params:
auth_user_email = template_params.get('texts', {}).get(
'user_email')
return auth_user_email is not None and is_hystax_email(
auth_user_email)
return False
Expand Down
9 changes: 5 additions & 4 deletions herald/herald_server/handlers/v2/emails.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os.path
from herald.modules.email_generator.generator import get_templates_path
from herald.herald_server.handlers.v1.base_async import BaseAsyncCollectionHandler
from herald.herald_server.handlers.v1.base_async import (
BaseAsyncCollectionHandler
)
from herald.herald_server.handlers.v1.base import BaseAuthHandler
from herald.herald_server.controllers.email import EmailAsyncController
from herald.herald_server.utils import (
Expand Down Expand Up @@ -35,9 +37,8 @@ def _validate_params(self, **kwargs):
'%s.html' % template_type)
if not os.path.exists(template_path):
raise_invalid_argument_exception('template_type')
if not template_params:
raise_not_provided_error('template_params')
if not isinstance(template_params, dict):
if template_params is not None and not isinstance(
template_params, dict):
raise_invalid_argument_exception('template_params')

async def post(self):
Expand Down
112 changes: 78 additions & 34 deletions herald/herald_server/tests/unittests/test_email_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from herald.herald_server.tests.unittests.test_herald_base import TestHeraldBase
from herald.herald_server.tests.unittests.test_herald_base import (
TestHeraldBase
)
from unittest.mock import patch


Expand All @@ -9,65 +11,76 @@ def setUp(self, *args):
def test_email_send(self):
email = ["[email protected]"]
template_type = 'invite'
template_params = {'message': "Email_message",
'customer_id': "b2ed3073-8c48-417f-9275-eb0d6cc5c7f1",
'object_name': "Object_name", 'object_type': "INFO",
'level': "INFO"}
template_params = {
"message": "Email_message",
"customer_id": "b2ed3073-8c48-417f-9275-eb0d6cc5c7f1",
"object_name": "Object_name",
"object_type": "INFO",
"level": "INFO"
}
reply_to_email = '[email protected]'
code, response = self.client_v2.email_send(
email, "Email_subject", template_type, template_params, reply_to_email)
email, "Email_subject", template_type, template_params,
reply_to_email)
result = {
'email': email,
'subject': 'Email_subject',
'template_type': template_type,
'template_params': template_params,
'reply_to_email': reply_to_email
"email": email,
"subject": "Email_subject",
"template_type": template_type,
"template_params": template_params,
"reply_to_email": reply_to_email
}
self.assertEqual(201, code)
self.assertDictEqual(result, response)

def test_email_hystax_registration_send(self):
email = ["[email protected]"]
template_type = 'new_employee'
template_params = {'message': "Email_message",
'customer_id': "b2ed3073-8c48-417f-9275-eb0d6cc5c7f1",
'object_name': "Object_name", 'object_type': "INFO",
'level': "INFO", 'texts': {'user_email': '[email protected]'}}
template_params = {
"message": "Email_message",
"customer_id": "b2ed3073-8c48-417f-9275-eb0d6cc5c7f1",
"object_name": "Object_name",
"object_type": "INFO",
"level": "INFO",
"texts": {"user_email": "[email protected]"}
}
publish_message_p_configured = patch(
'herald.herald_server.controllers.email.EmailController.publish_message'
).start()
"herald.herald_server.controllers.email."
"EmailController.publish_message").start()
code, response = self.client_v2.email_send(
email, "Email_subject", template_type, template_params)
self.assertEqual(publish_message_p_configured.called, True)
result = {
'email': ["[email protected]"],
'subject': 'Email_subject',
'template_type': template_type,
'template_params': template_params,
'reply_to_email': None
"email": ["[email protected]"],
"subject": "Email_subject",
"template_type": template_type,
"template_params": template_params,
"reply_to_email": None
}
self.assertEqual(201, code)
self.assertDictEqual(result, response)

def test_email_hystax_registration_not_publish(self):
email = ["[email protected]"]
template_type = 'new_employee'
template_params = {'message': "Email_message",
'customer_id': "b2ed3073-8c48-417f-9275-eb0d6cc5c7f1",
'object_name': "Object_name", 'object_type': "INFO",
'level': "INFO", 'texts': {'user_email': '[email protected]'}}
template_params = {
"message": "Email_message",
"customer_id": "b2ed3073-8c48-417f-9275-eb0d6cc5c7f1",
"object_name": "Object_name",
"object_type": "INFO",
"level": "INFO",
"texts": {"user_email": "[email protected]"}}
publish_message_p_configured = patch(
'herald.herald_server.controllers.email.EmailController.publish_message'
).start()
"herald.herald_server.controllers.email."
"EmailController.publish_message").start()
code, response = self.client_v2.email_send(
email, "Email_subject", template_type, template_params)
self.assertEqual(publish_message_p_configured.called, False)
result = {
'email': ["[email protected]"],
'subject': 'Email_subject',
'template_type': template_type,
'template_params': template_params,
'reply_to_email': None
"email": ["[email protected]"],
"subject": "Email_subject",
"template_type": template_type,
"template_params": template_params,
"reply_to_email": None
}
self.assertEqual(201, code)
self.assertDictEqual(result, response)
Expand Down Expand Up @@ -106,4 +119,35 @@ def test_validation_email_payload(self):
email=[valid_email], subject='subj',
template_params=invalid_template_params)
self.assertEqual(400, code)
self.assertEqual('invalid template_params', response['error']['reason'])
self.assertEqual('invalid template_params',
response['error']['reason'])

def test_email_template_params(self):
valid_email = '[email protected]'
template_type = 'new_employee'
subject = 'subj'
code, response = self.client_v2.email_send(
email=[valid_email], subject=subject,
template_type=template_type, template_params=None)
self.assertEqual(201, code)
result = {
"email": [valid_email],
"subject": subject,
"template_type": template_type,
"template_params": None,
"reply_to_email": None
}
self.assertDictEqual(result, response)

code, response = self.client_v2.email_send(
email=[valid_email], subject=subject,
template_type=template_type, template_params={})
self.assertEqual(201, code)
result = {
"email": [valid_email],
"subject": subject,
"template_type": template_type,
"template_params": {},
"reply_to_email": None
}
self.assertDictEqual(result, response)
7 changes: 5 additions & 2 deletions herald/modules/email_generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ def generate_control_panel_parameters(organization_map):
list_params = []
for input_key, output_key in control_panel_keys_map.items():
if organization_map.get(input_key):
list_params.append('%s=%s' % (output_key, organization_map[input_key]))
list_params.append('%s=%s' % (output_key,
organization_map[input_key]))
return "?" + '&'.join(list_params) if list_params else None
default_template = get_default_template()
texts = template_params.get('texts', {})
numbered_dict = get_numbered_params(texts)
organization_info = texts.get('organization', {})
texts['control_panel_parameters'] = generate_control_panel_parameters(organization_info)
texts['control_panel_parameters'] = generate_control_panel_parameters(
organization_info)
texts['etcd'] = {}
texts['etcd']['control_panel_link'] = config_client.get('/public_ip').value
template = update_template(default_template, template_params)
Expand All @@ -70,6 +72,7 @@ def generate_email(config_client, to, subject, template_params,
msg['To'] = to
if reply_to_email:
msg['reply-to'] = reply_to_email
template_params = template_params if template_params else {}
context = _generate_context(template_params, config_client)
msg.attach(_generate_body(context, template_type))
return msg
Expand Down
10 changes: 5 additions & 5 deletions ngui/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
"homepage": "https://gitlab.com/hystax/ngui_server#readme",
"dependencies": {
"@apollo/datasource-rest": "^6.3.0",
"@apollo/server": "^4.10.4",
"@apollo/server": "^4.11.0",
"@apollo/utils.keyvaluecache": "^3.1.0",
"@graphql-tools/load-files": "^7.0.0",
"@graphql-tools/merge": "^9.0.4",
"@graphql-tools/merge": "^9.0.7",
"@types/node": "^20.14.9",
"body-parser": "^1.20.2",
"body-parser": "^1.20.3",
"cors": "^2.8.5",
"express": "^4.21.0",
"graphql": "^16.9.0",
Expand All @@ -39,8 +39,8 @@
},
"devDependencies": {
"@graphql-codegen/cli": "^5.0.2",
"@graphql-codegen/typescript": "^4.0.7",
"@graphql-codegen/typescript-resolvers": "^4.1.0"
"@graphql-codegen/typescript": "^4.0.9",
"@graphql-codegen/typescript-resolvers": "^4.2.1"
},
"pnpm": {
"overrides": {
Expand Down
Loading

0 comments on commit 82f7757

Please sign in to comment.