Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.18 KB | None | 0 0
  1. import time
  2. import pytest
  3. import threading
  4.  
  5. from redis import Redis
  6. from rq import SimpleWorker, Queue
  7. from dependency_injector.providers import Object, Factory
  8.  
  9. from microrpc.exc import RPCError
  10.  
  11. from seagull.shared.config import Config
  12. from seagull.shared.uri_parser import URIParser
  13. from seagull.tools.rpc_ext.rpc_runners.run_rpc_client import create_client
  14.  
  15. from seagull.email_sender.app.domains.email.send_message import EmailSender
  16. from seagull.email_sender.app.domains.email.email_domain import EmailDomain
  17. from seagull.email_sender.app.rpc_server import start_server as rpc_start_server
  18. from seagull.email_sender import default_config
  19.  
  20.  
  21. class EmailSenderFixtures:
  22.     """Create email sender fixtures."""
  23.  
  24.     @pytest.fixture(scope='module')
  25.     def server_config(self):
  26.         """Create server config."""
  27.  
  28.         config = Config().from_object(default_config)
  29.         config.from_py_file("./test_config.py")
  30.         config.VERBOSITY = True
  31.  
  32.         # rq and scheduler config
  33.         rq_config = URIParser.parse_uri(config.get("RQ"))
  34.         prefix = rq_config.special_params.get("queries_prefix")
  35.         config.QUEUE_PREFIX = prefix
  36.         config.REDIS_URL = rq_config.transport_path
  37.         config.WORKER_NAME = rq_config.special_params.get("worker_name", "default")
  38.         config.DEFAULT_RESULT_TTL = rq_config.special_params.get("result_ttl", 10)
  39.         config.SCHEDULE_INTERVAL = rq_config.special_params.get("scheduler_interval", 5)
  40.         config.QUERIES_NAME = [("{}_{}".format(prefix, lev) if prefix else lev)
  41.                                for lev in ("low", "medium", "high")]
  42.  
  43.         # rpc config
  44.         rpc_config = URIParser.parse_uri(config.get("RPC"))
  45.         config.RPC_URL = rpc_config.transport_path
  46.         config.REDIS_SEND_CHANNEL = rpc_config.special_params.get("send",
  47.                                                                   "email_sender_send")
  48.         config.REDIS_RECEIVE_CHANNEL = rpc_config.special_params.get("receive",
  49.                                                                      "email_sender_receive")
  50.  
  51.         return config
  52.  
  53.     @pytest.fixture(scope='module')
  54.     def rpc_server(self, server_config):
  55.         """Start RPC server."""
  56.  
  57.         worker = threading.Thread(target=rpc_start_server, args=[server_config])
  58.         worker.daemon = True
  59.         worker.start()
  60.         time.sleep(1.5)
  61.  
  62.         yield True
  63.  
  64.     @pytest.fixture(scope='module')
  65.     def client_config(self):
  66.         """Create client config."""
  67.  
  68.         config = Config().from_object(default_config)
  69.         config.from_py_file("./test_config.py")
  70.  
  71.         rpc_config = URIParser.parse_uri(config.get("RPC"))
  72.         config.RPC_URL = rpc_config.transport_path
  73.         config.REDIS_SEND_CHANNEL = rpc_config.special_params.get("receive",
  74.                                                                   "email_sender_receive")
  75.         config.REDIS_RECEIVE_CHANNEL = rpc_config.special_params.get("send",
  76.                                                                      "email_sender_send")
  77.  
  78.         return config
  79.  
  80.     @pytest.fixture(scope="module")
  81.     def rq_server(self, request, server_config):
  82.         """Start RQ server."""
  83.  
  84.         connection = Redis.from_url(server_config.REDIS_URL)
  85.         queues_dict = {level: Queue(connection=connection) for level in ("high", "medium", "low")}
  86.         EmailDomain.DIService.rq_queues.override(Object(queues_dict))
  87.         worker = SimpleWorker(queues_dict.values(), connection=connection)
  88.         failed_queue = Queue('failed', connection=connection)
  89.  
  90.         return worker, failed_queue
  91.  
  92.     @pytest.fixture(scope="module")
  93.     def mock_smtp(self, server_config):
  94.         """Create mock of send_email function."""
  95.  
  96.         def mock_send_email(email_obj, receives, server_config):
  97.             """Mock of smtp send_email function."""
  98.  
  99.             return email_obj, receives
  100.  
  101.         EmailSender.send_email = staticmethod(mock_send_email)
  102.         mock_smtp_factory = Factory(EmailSender, config=Object(server_config))
  103.         EmailDomain.DIService.email_sender.override(mock_smtp_factory)
  104.  
  105.  
  106. class TestEmailSenderClient(EmailSenderFixtures):
  107.     """Test email sender method."""
  108.  
  109.     without_body_and_html = {
  110.         "importance": "high",
  111.         "body": None,
  112.         "html": None,
  113.         "sender": "manager@zakolka.net.ua",
  114.         "receives": ["admin@zakolka.net.ua"],
  115.         "subject": "subject",
  116.         "send_separately": False,
  117.         "send_at": None,
  118.         "attachments": None
  119.     }
  120.  
  121.     without_body = {
  122.         "importance": "high",
  123.         "body": None,
  124.         "html": "html",
  125.         "sender": "manager@zakolka.net.ua",
  126.         "receives": ["admin@zakolka.net.ua"],
  127.         "subject": "subject",
  128.         "send_separately": False,
  129.         "send_at": None,
  130.         "attachments": None
  131.     }
  132.  
  133.     with_body_and_html = {
  134.         "importance": "high",
  135.         "body": "body",
  136.         "html": "html",
  137.         "sender": "manager@zakolka.net.ua",
  138.         "receives": ["admin@zakolka.net.ua"],
  139.         "subject": "subject",
  140.         "send_separately": False,
  141.         "send_at": None,
  142.         "attachments": None
  143.     }
  144.  
  145.     with_attachments = {
  146.         "importance": "high",
  147.         "body": "body",
  148.         "html": "html",
  149.         "sender": "manager@zakolka.net.ua",
  150.         "receives": ["admin@zakolka.net.ua"],
  151.         "subject": "subject",
  152.         "send_separately": False,
  153.         "send_at": None,
  154.         "attachments": [{"name": "name", "body": "body"}]
  155.     }
  156.  
  157.     with_bad_attachments = {
  158.         "importance": "high",
  159.         "body": "body",
  160.         "html": "html",
  161.         "sender": "manager@zakolka.net.ua",
  162.         "receives": ["admin@zakolka.net.ua"],
  163.         "subject": "subject",
  164.         "send_separately": False,
  165.         "send_at": None,
  166.         "attachments": [{"bad_name": "name", "body": "body"}]
  167.     }
  168.  
  169.     with_send_separately = {
  170.         "importance": "high",
  171.         "body": "body",
  172.         "html": "html",
  173.         "sender": "manager@zakolka.net.ua",
  174.         "receives": ["admin@zakolka.net.ua"],
  175.         "subject": "subject",
  176.         "send_separately": True,
  177.         "send_at": None,
  178.         "attachments": None
  179.     }
  180.  
  181.     with_bad_importance = {
  182.         "importance": "very high",
  183.         "body": None,
  184.         "html": None,
  185.         "sender": "manager@zakolka.net.ua",
  186.         "receives": ["admin@zakolka.net.ua"],
  187.         "subject": "subject",
  188.         "send_separately": False,
  189.         "send_at": None,
  190.         "attachments": None
  191.     }
  192.  
  193.     without_receives = {
  194.         "importance": "high",
  195.         "body": None,
  196.         "html": None,
  197.         "sender": "manager@zakolka.net.ua",
  198.         "receives": None,
  199.         "subject": "subject",
  200.         "send_separately": False,
  201.         "send_at": None,
  202.         "attachments": None
  203.     }
  204.  
  205.     @pytest.fixture()
  206.     def rpc_client(self, client_config):
  207.         """Create RPC Client."""
  208.  
  209.         return create_client(client_config, prefix="email")
  210.  
  211.     @pytest.mark.usefixtures("rpc_server", "mock_smtp")
  212.     @pytest.mark.parametrize("message_attributes, failed", [
  213.         (without_body_and_html, False),
  214.         (without_body,          False),
  215.         (with_body_and_html,    False),
  216.         (with_attachments,      False),
  217.         (with_send_separately,  False),
  218.         (with_bad_attachments,  True),
  219.     ])
  220.     def test_send_email(self, message_attributes, failed, rpc_client, rq_server):
  221.         """Test normal email sending."""
  222.  
  223.         worker, failed_queue = rq_server
  224.         failed_queue.empty()
  225.  
  226.         rpc_client.send_message(**message_attributes)
  227.         worker.work(burst=True)
  228.  
  229.         if failed:
  230.             assert failed_queue.count == 1
  231.         else:
  232.             assert failed_queue.count == 0
  233.  
  234.     @pytest.mark.usefixtures("rpc_server", "mock_smtp")
  235.     @pytest.mark.parametrize("message_attribute, error", [
  236.         (with_bad_importance, RPCError),
  237.         (without_receives,    RPCError),
  238.     ])
  239.     def test_error_raising(self, message_attribute, error, rpc_client, rq_server):
  240.         """Test errors raising."""
  241.  
  242.         worker, _ = rq_server
  243.         pytest.raises(error, rpc_client.send_message, **message_attribute)
  244.  
  245.         worker.work(burst=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement