"""This module contains the tests for the ``eventlog`` app"""

from django.conf import settings
from django.core.urlresolvers import reverse
from django.test.utils import override_settings

from mtp.apps.common.tests.test_cases import MTPTestCase, DEFAULT_PASSWORD

from .models import ACTION_CHOICES as EVENT_ACTIONS, EventLog


class EventLogTestCase(MTPTestCase):
    """The Tests for signals of the `eventlog` app"""

    @override_settings(INSTANCE_IS_SLAVE=False, INSTANCE_IS_MASTER=True)
    def test_user_login_on_cms_should_record_new_event(self):
        """Test the recording of the event when a user login on the cms"""

        # We login the user into the cms
        url = "%s?next=cms" % settings.LOGIN_URL
        response = self.client.post(
            url,
            {
                'username': self.trainer.username,
                'password': DEFAULT_PASSWORD
            }
        )

        # User should be successfully logged in and redirect
        self.assertEqual(response.status_code, 302)

        # We should get a new event in the log
        self.assertEqual(
            len(EventLog.objects.filter(action=EVENT_ACTIONS.CMS_LOGIN)),
            1
        )
