from django.contrib.auth.models import User
from django.test import TestCase, override_settings
from django_bouncy.models import Complaint

from astrobin.tests.common import *


class HomeTest(TestCase):
    def setUp(self):
        self.user = User.objects.create_user('test', 'test@test.com', 'password')

    @override_settings(PREMIUM_MAX_REVISIONS_FREE_2020=2)
    def test_global_stream(self):
        url = reverse('activity_stream_fragment', kwargs={'section': 'global'})
        self.client.login(username='test', password='password')

        # Uploading an image shows up in the stream
        response, image = test_utils_upload_image(self)
        test_utils_approve_image(image)
        response = self.client.get(url)
        self.assertContains(response, 'UPLOADED_IMAGE.actor-%d.action-object-%d.en-us' % (self.user.pk, image.pk))

        # Uploading another image shows up in the stream too
        response, image2 = test_utils_upload_image(self)
        test_utils_approve_image(image2)
        response = self.client.get(url)
        self.assertContains(response, 'UPLOADED_IMAGE.actor-%d.action-object-%d.en-us' % (self.user.pk, image2.pk))

        # Uploading a revision removes the action about the image upload
        response, revision = test_utils_upload_revision(self, image)
        response = self.client.get(url)
        self.assertNotContains(response, 'UPLOADED_IMAGE.actor-%d.action-object-%d.en-us' % (self.user.pk, image.pk))
        # Still contains action for image2 tho
        self.assertContains(response, 'UPLOADED_IMAGE.actor-%d.action-object-%d.en-us' % (self.user.pk, image2.pk))
        self.assertContains(response, 'UPLOADED_REVISION.actor-%d.action-object-%d.target-%d.en-us' % (
            self.user.pk, revision.pk, image.pk))

        # Uploading another revision removes the previous revisions' stream actions
        response, revision2 = test_utils_upload_revision(self, image)
        response = self.client.get(url)
        self.assertNotContains(response, 'UPLOADED_IMAGE.actor-%d.action-object-%d.en-us' % (self.user.pk, revision.pk))
        self.assertContains(response, 'UPLOADED_REVISION.actor-%d.action-object-%d.target-%d.en-us' % (
            self.user.pk, revision2.pk, image.pk))


    def test_complaint_alert(self):
        self.client.login(username='test', password='password')
        Complaint.objects.create(
            address=self.user.email,
            mail_timestamp=datetime.datetime.now()
        )
        url = reverse('index')

        response = self.client.get(url)

        self.assertContains(
            response, "AstroBin is not delivering you emails because you have marked some of them as spam.")
