"""This modules holds notifications tests for ``accounts`` app."""

import unittest

from django.core import mail
from django.core.urlresolvers import reverse
from django.test.utils import override_settings

from mtp.apps.common.tests.test_cases import MTPTestCase
from mtp.apps.common.tests.waffle_test_utils import override_switch


class MTPUserNotificationTestcase(MTPTestCase):
    """Test cases for the email notifications of the ``MTPUser`` model."""

    @override_settings(INSTANCE_IS_MASTER=True)
    @override_switch('enable_user_welcome_mail', active=True)
    def test_user_welcome_from_user_add_view(self):
        """Test that the ``UserCreateView`` view should send a mail to the given user."""

        # We need to be logged in as an admin
        self.login_as_admin()

        # Call the view
        self.client.post(reverse('accounts:user-create'), {
            'username': 'test',
            'first_name': 'test',
            'last_name': 'test',
            'email': 'test@test.com',
            'natural_language': 'en',
            'pass1': 'testpassword',
            'pass2': 'testpassword'
        }, follow=True)

        # We should get a new mail to send
        self.assertEqual(len(mail.outbox), 1)

        # The recipient is the user
        self.assertEqual('test@test.com', mail.outbox[0].to[0])

    @override_switch('enable_user_welcome_mail', active=False)
    def test_user_welcome_not_send_when_waffle_off_from_user_add_view(self):
        """Test that the ``UserCreateView`` view should NOT send a mail to the given user."""

        # We need to be logged in as an admin
        self.login_as_admin()

        # Call the view
        self.client.post(reverse('accounts:user-create'), {
            'username': 'test',
            'first_name': 'test',
            'last_name': 'test',
            'email': 'test@test.com',
            'natural_language': 'en'
        }, follow=True)

        # We should NOT get a new mail to send
        self.assertEqual(len(mail.outbox), 0)

    def call_api_user_create_view(self):
        """Shortcut method to call the ``UserViewSet`` create view"""

        # Login
        self.login_as_trainer()

        # Call the view
        self.client.post(reverse('apiv2:user-list'), {
            'username': 'test',
            'first_name': 'test',
            'last_name': 'test',
            'email': 'test@test.com',
            'language': 'en',
            'password': 'fake_password',
        })

    @override_settings(INSTANCE_IS_MASTER=True)
    @override_switch('enable_user_welcome_mail', active=True)
    def test_user_welcome_from_api_user_create_view(self):
        """Test that the ``UserViewSet`` create view should send a mail to the given user."""

        self.call_api_user_create_view()

        # We should get a new mail to send
        self.assertEqual(len(mail.outbox), 1)

        # The recipient is the new user
        self.assertEqual('test@test.com', mail.outbox[0].to[0])

    @override_switch('enable_user_welcome_mail', active=False)
    def test_user_welcome_not_send_from_api_user_create_view(self):
        """Test that the ``UserViewSet`` create view should NOT send a mail to the given user."""

        self.call_api_user_create_view()

        # We should NOT get a new mail to send
        self.assertEqual(len(mail.outbox), 0)

    @unittest.skip('The view to download the trainee content dont exist anymore')
    def test_user_welcome_from_welcome_view(self):
        """Test that the ``user_welcome`` view should send a mail to the
        given user."""

        # We need to be logged in as a trainer
        self.login_as_trainer()

        # Call the view
        self.client.post(self.trainee.get_welcome_url())

        # We should get a new mail to send
        self.assertEqual(len(mail.outbox), 1)

        # The mail subject is correct
        self.assertEqual(
            mail.outbox[0].subject,
            "Welcome to the Magency digital training platform."
        )

        # The password is not in the body
        self.assertIn(
            "********",
            mail.outbox[0].body
        )

        # The recipient is the user
        self.assertEqual(self.trainee.email, mail.outbox[0].to[0])
