"""
Binder test suite is constructed here.
"""
import os
import shutil
import unittest

from django.conf import settings
from django.contrib.auth.models import User, Group
import test_settings

from rldjango.apps.binder.models import Pdf
#from rldjango.apps.binder.models import settings
from utils import check_test_mode

from rldjango.apps.binder.tests.models import (
    PdfTests,
    )

from rldjango.apps.binder.tests.views import (
    ViewTests,
    )

class FixtureTests(unittest.TestCase):
    """
    Are test fixtures loaded ?
    """
    def __init__(*args, **kwds):
        check_test_mode()
        unittest.TestCase.__init__(*args, **kwds)

    def test_fixtures_add_user(self):
        user = User(username='bindertestuser')
        user.set_password('password')
        user.save()
        user = User(username='bindertestnotowner')
        user.set_password('password')
        user.save()
        user = User(username='bindertestsuperuser', is_superuser=True)
        user.set_password('password')
        user.save()
        
    def test_fixtures_add_user_to_group(self):
        user = User.objects.get(username='bindertestuser')
        group = Group.objects.get(name='brochureowners')
        
        user.groups.add(group)

    def test_fixtures_dirs(self):
        """
        Check if binder fixtures dir is in FIXTURE_DIRS.
        """
        self.assertTrue(
            test_settings.BINDER_TEST_FIXTURES in settings.FIXTURE_DIRS,
            'BINDER_TEST_FIXTURES not present in FIXTURE_DIRS')

    def test_fixtures(self):
        """
        Check if fixtures were loaded.
        """
        pdf = Pdf.objects.get(pk=1)
        self.assertTrue(pdf.name == 'binder_test_pdf')

    def test_create_fake_dirs(self):
        """
        This is not really test it reates fake uploads sections and
        output folders and copy test pdf to upload.
        """
        for path in (settings.SECTIONS_ROOT, settings.UPLOAD_ROOT, settings.OUTPUT_ROOT):
            if os.path.exists(path):
                shutil.rmtree(path)
            os.makedirs(path)

        shutil.copyfile(
            os.path.join(test_settings.BINDER_TEST_FIXTURES, 'binder_test.pdf'),
            os.path.join(settings.UPLOAD_ROOT, 'binder_test.pdf')
            )

    def test_delete_fake_dirs(self): # pylint: disable-msg=C0103 
        """
        This is not really test cleanup fake folders. This should be
        last test added to test suite.
        """
        for path in (settings.MEDIA_ROOT,):
            if os.path.exists(path):
                shutil.rmtree(path)


def suite():
    """
    Build test suite here.
    """
    check_test_mode()
    tests = unittest.TestSuite()

    # fixtures tests
    tests.addTest(FixtureTests('test_fixtures_add_user'))
    tests.addTest(FixtureTests('test_fixtures_add_user_to_group'))
    tests.addTest(FixtureTests('test_fixtures_dirs'))
    tests.addTest(FixtureTests('test_fixtures'))
    tests.addTest(FixtureTests('test_create_fake_dirs'))

    # pdf tests
    tests.addTest(PdfTests('test_pdf_save'))
    tests.addTest(PdfTests('test_generate_pdf_data'))
    tests.addTest(PdfTests('test_generate_images'))
    
    tests.addTest(ViewTests('test_list'))
    tests.addTest(ViewTests('test_create'))
    tests.addTest(ViewTests('test_create_preselected'))
    tests.addTest(ViewTests('test_create_pdf'))
    tests.addTest(ViewTests('test_search_params'))
    tests.addTest(ViewTests('test_flipbook'))
    tests.addTest(ViewTests('test_email'))
    tests.addTest(ViewTests('test_api'))
    tests.addTest(ViewTests('test_create_customised'))

    # this should be last
    tests.addTest(FixtureTests('test_delete_fake_dirs'))
    return tests
