import datetime
import logging
import unittest

from flask import session
from werkzeug.datastructures import MultiDict

from cla_public.app import create_app
from cla_public.apps.contact.views import confirmation_email
from cla_public.apps.contact.forms import ContactForm


logging.getLogger('MARKDOWN').setLevel(logging.WARNING)


def submit(**kwargs):
    data = {
        'full_name': 'John Smith',
        'email': 'john.smith@example.com',
        'contact_type': 'callback',
        'callback-contact_number': '0123456789',
        'callback-safe_to_contact': 'SAFE'}

    if datetime.datetime.now().time() > datetime.time(hour=17, minute=30):
        # use tomorrow because no more callbacks available today
        another_day = datetime.date.today() + datetime.timedelta(days=1)
        if another_day.weekday() in (5, 6):
            # use monday or tuesday next week to avoid weekend
            another_day += datetime.timedelta(days=2)
        data.update({
            'callback-time-specific_day': 'specific_day',
            'callback-time-day': '%04d%02d%02d' % (another_day.year,
                                                   another_day.month,
                                                   another_day.day),
            'callback-time-time_in_day': '0900',
        })
    else:
        data.update({
            'callback-time-specific_day': 'today',
            'callback-time-time_today': '0900',
        })

    data.update(kwargs)
    return ContactForm(MultiDict(data), csrf_enabled=False)


class TestMail(unittest.TestCase):

    def setUp(self):
        self.app = create_app('config/testing.py')
        self.ctx = self.app.test_request_context()
        self.ctx.push()
        self.client = self.app.test_client()
        session.checker['case_ref'] = 'XX-XXXX-XXXX'

    def tearDown(self):
        self.ctx.pop()

    def receive_email(self, msg):
        with self.app.mail.record_messages() as outbox:
            self.app.mail.send(msg)
            assert len(outbox) == 1
            return outbox[0]

    def test_confirmation_email(self):
        with self.client:
            form = submit()
            msg = confirmation_email(form.data)
            msg = self.receive_email(msg)

            assert msg.subject == 'Your Civil Legal Advice reference number'
            assert 'Dear John Smith' in msg.body
            assert 'reference number is XX-XXXX-XXXX' in msg.body
            assert 'call you back on 0123456789' in msg.body
            assert 'time you selected ({0:%A, %d %B at %H:%M})'.format(
                form.data['callback']['time']) in msg.body
            assert 'We will leave a message' in msg.body

    def test_confirmation_email_not_safe(self):
        with self.client:
            form = submit(**{'callback-safe_to_contact': 'NO_MESSAGE'})
            msg = confirmation_email(form.data)
            msg = self.receive_email(msg)

            assert 'We will not leave a message' in msg.body

    def test_confirmation_email_no_callback(self):
        with self.client:
            form = submit(contact_type='call')
            msg = confirmation_email(form.data)
            msg = self.receive_email(msg)

            assert 'reference number is XX-XXXX-XXXX' in msg.body
            assert 'You can now call CLA' in msg.body

@@ -194,16 +194,23 @@
  import 
+create_
 confirma
@@ -220,16 +220,16 @@
 n_email%0A
-
 from cla
@@ -1527,16 +1527,186 @@
 False)%0A%0A
+def submit_and_store_in_session(**kwargs):%0A    form = submit(**kwargs)%0A    session.checker%5B'ContactForm'%5D = form.data%0A    session.store_checker_details()%0A    return form%0A
 %0Aclass T
@@ -2310,16 +2310,37 @@
 = submit
+_and_store_in_session
 ()%0A     
@@ -2344,32 +2344,39 @@
           msg = 
+create_
 confirmation_ema
@@ -2977,12 +2977,50 @@
 bmit
-(**%7B
+_and_store_in_session(**%7B%0A                
 'cal
@@ -3055,16 +3055,29 @@
 MESSAGE'
+%0A            
 %7D)%0A     
@@ -3081,32 +3081,39 @@
           msg = 
+create_
 confirmation_ema
@@ -3333,16 +3333,37 @@
 = submit
+_and_store_in_session
 (contact
@@ -3394,16 +3394,23 @@
   msg = 
+create_
 confirma

Fix Py tests