from datetime import datetime, timedelta

from django.conf import settings
from django.contrib.auth.models import User
from django.test import TestCase
from mock import Mock, patch

from projects.models import Project
from challenges.models import Challenge, Submission, Phase, Category


def _create_project_and_challenge():
    """Create and return a sample project with a sample challenge."""
    project = Project.objects.create(name='Project', slug='project',
                                          allow_participation=True)
    end_date = datetime.now() + timedelta(days=365)
    challenge = Challenge.objects.create(title='Challenge',
                                              slug='challenge',
                                              end_date=end_date,
                                              project=project)
    return project, challenge


class PermalinkTest(TestCase):
    
    def setUp(self):
        self.project, self.challenge = _create_project_and_challenge()
    
    def test_permalink(self):
        self.assertEqual(self.challenge.get_absolute_url(),
                         '/project/challenges/challenge/')
    
    def tearDown(self):
        for model in [Challenge, Project]:
            model.objects.all().delete()


class SingleChallengePermalinkTest(TestCase):
    
    urls = 'challenges.tests.single_challenge_urls'
    
    def setUp(self):
        self.project, self.challenge = _create_project_and_challenge()
    
    def test_single_challenge_permalink(self):
        """Test permalink generation on an Ignite-style one-challenge site."""
        self.assertEqual(self.challenge.get_absolute_url(), '/')
    
    def tearDown(self):
        for model in [Challenge, Project]:
            model.objects.all().delete()


class EntriesToLive(TestCase):
    
    def setUp(self):
        self.project = Project.objects.create(
            name=u'A project for a test',
            allow_participation=True
        )
        self.challenge = Challenge.objects.create(
            title=u'Testing my submissions',
            end_date=u'2020-11-30 12:23:28',
            project=self.project,
            moderate=True
        )
        self.phase = Phase.objects.create(
            name=u'Phase 1', challenge=self.challenge, order=1
        )
        self.user = User.objects.create_user('bob', 'bob@example.com', 'bob')
        self.submission = Submission.objects.create(
            title=u'A submission to test',
            description=u'<h3>Testing bleach</h3>',
            is_live=True,
            phase=self.phase,
            created_by=self.user.get_profile()
        )

    def test_entry_hidden(self):
        """
        The user wants the entry to go live but it needs moderating first
        """
        self.assertEqual(self.submission.is_live, False)
    
    def test_phase_unicode(self):
        """Test the string representation of a phase."""
        self.assertEqual(unicode(self.phase),
                         u'Phase 1 (Testing my submissions)')
    
    def test_bleach_clearning(self):
        """
        Check that we're stripping out bad content - <h3> isn't in our 
        allowed list
        """
        self.assertEqual(self.submission.description_html,
                         '&lt;h3&gt;Testing bleach&lt;/h3&gt;')


class CategoryManager(TestCase):
    
    def setUp(self):
        self.project = Project.objects.create(
            name=u'Test Project',
            allow_participation=True
        )
        self.challenge = Challenge.objects.create(
            title=u'Testing categories',
            end_date=u'2020-11-30 12:23:28',
            description=u'Blah',
            project=self.project,
            moderate=False
        )
        self.phase = Phase.objects.create(
            name=u'Phase 1',
            order=1,
            challenge=self.challenge
        )
        self.user = User.objects.create_user('bob', 'bob@bob.com', 'bob')
        self.c1 = Category.objects.create(
            name=u'Testing',
            slug=u'testing'
        )
        self.c2 = Category.objects.create(
            name=u'Ross',
            slug=u'ross'
        )
        self.submission = Submission.objects.create(
            title=u'Category',
            brief_description=u'Blah',
            description=u'Foot',
            phase=self.phase,
            created_by=self.user.get_profile()
        )


    def test_initial_return(self):
        """
        Test that with no categories containing submissions returns nothing
        """
        self.assertEqual(Category.objects.get_active_categories(), False)

    def test_results_after_submission(self):
        """
        Test that we return only categories with submissions in
        """
        self.submission.categories.add(self.c1)
        self.cats = Category.objects.get_active_categories()
        self.assertEqual(len(self.cats), 1)

@@ -33,16 +33,65 @@
 imedelta
+%0Afrom dateutil.relativedelta import relativedelta
 %0A%0Afrom d
@@ -4952,8 +4952,987 @@
 ts), 1)%0A
+%0A%0Aclass Phases(TestCase):%0A%0A    def setUp(self):%0A        self.project, self.challenge = _create_project_and_challenge()%0A        %0A        self.p1 = Phase.objects.create(%0A            name=u'Phase 1',%0A            order=1,%0A            challenge=self.challenge,%0A            start_date = datetime.now(),%0A            end_date = datetime.now() + relativedelta( months = +1 )%0A        )%0A        self.p2 = Phase.objects.create(%0A            name=u'Phase 2',%0A            order=2,%0A            challenge=self.challenge,%0A            start_date = datetime.now() + relativedelta( months = +2 ),%0A            end_date = datetime.now() + relativedelta( months = +3 )%0A        )%0A %0A    %0A    def test_get_current_open(self):%0A       current = Phase.objects.get_current_phase(self.challenge.slug)%0A       self.assertEqual(len(current), 1)%0A       self.assertEqual(current%5B0%5D.name, 'Phase 1')%0A%0A    def tearDown(self):%0A        for model in %5BChallenge, Project, Phase%5D:%0A            model.objects.all().delete()%0A%0A

Test for get_current_phase()