import os
import shutil

from django.conf import settings
from django.core.urlresolvers import reverse
from django.test import Client
from django.test import TestCase

from .testapp.models import Test

from .utils import (
    create_superuser,
    create_img_file,
    create_doc_file,
    create_attachments_img,
    create_attachments_doc)

from ..models import Image, Document


class AttachmentsAdminTest(TestCase):
    
    def setUp(self):
        
        if not os.path.exists(settings.MEDIA_ROOT):
            os.mkdir(settings.MEDIA_ROOT)
        
        create_img_file()
        create_doc_file()
        
        self.img = create_attachments_img('test.jpg')
        self.doc = create_attachments_doc('test.txt')
        
        create_superuser()
        
        self.client = Client()
        self.client.login(username='admin', password='secret')
    
    
    def tearDown(self):
        self.img.delete()
        self.doc.delete()
        shutil.rmtree(settings.MEDIA_ROOT, ignore_errors=True)
    
    
    def test_image_list_view_thumbnail(self):
        resp = self.client.get(reverse('admin:attachments_image_changelist'))
        html = """<a href="/admin/attachments/image/1/"><img border="0" alt="" src="/media/attachments/images/test.jpg.80x80_q95_crop.jpg"></a>"""
        self.assertContains(resp, html, html=True)
    
    
    def test_document_list_view(self):
        resp = self.client.get(reverse('admin:attachments_document_changelist'))
        self.assertEqual(resp.status_code, 200)


class AttachmentsAdminUploadTest(TestCase):
    
    def setUp(self):
        
        if not os.path.exists(settings.MEDIA_ROOT):
            os.mkdir(settings.MEDIA_ROOT)
        
        create_img_file()
        create_doc_file()
        
        create_superuser()
        
        self.client = Client()
        self.client.login(username='admin', password='secret')
    
    
    def tearDown(self):
        shutil.rmtree(settings.MEDIA_ROOT, ignore_errors=True)
    
    
    def test_image_upload_view(self):
        
        # confirm there are no images
        
        self.assertFalse(Image.objects.all())
        
        # upload an image
        
        img = os.path.join(settings.MEDIA_ROOT, 'test.jpg')
        
        with open(img, 'rb') as img_file:
            resp = self.client.post(
                reverse('attachments:ajax_upload_image'),
                {'file': img_file}, 
                HTTP_X_REQUESTED_WITH='XMLHttpRequest')
            self.assertEqual(resp.status_code, 200)
        
        # check that our uploaded image exists
        
        img_file = Image.objects.all()[0]
        self.assertEqual(img_file.filename, 'test.jpg')
    
    
    def test_document_upload_view(self):
        
        # confirm there are no documents
        
        self.assertFalse(Document.objects.all())
        
        # upload a document
        
        doc = os.path.join(settings.MEDIA_ROOT, 'test.txt')
        
        with open(doc, 'rb') as doc_file:
            resp = self.client.post(
                reverse('attachments:ajax_upload_document'),
                {'file': doc_file}, 
                HTTP_X_REQUESTED_WITH='XMLHttpRequest')
            self.assertEqual(resp.status_code, 200)
        
        # check that our uploaded document exists
        
        doc_file = Document.objects.all()[0]
        self.assertEqual(doc_file.filename, 'test.txt')


class AttachmentsAdminBadUploadTest(TestCase):
    
    def setUp(self):
        
        if not os.path.exists(settings.MEDIA_ROOT):
            os.mkdir(settings.MEDIA_ROOT)
        
        create_img_file(filename='test.tiff')
        create_doc_file(filename='test.woof')
        
        create_superuser()
        
        self.client = Client()
        self.client.login(username='admin', password='secret')
    
    
    def tearDown(self):
        shutil.rmtree(settings.MEDIA_ROOT, ignore_errors=True)
    
    
    def test_bad_image_upload_view(self):
        
        # confirm there are no images
        
        self.assertFalse(Image.objects.all())
        
        # try to upload the bad image
        
        img = os.path.join(settings.MEDIA_ROOT, 'test.tiff')
        
        with open(img, 'rb') as img_file:
            resp = self.client.post(
                reverse('attachments:ajax_upload_image'),
                {'file': img_file}, 
                HTTP_X_REQUESTED_WITH='XMLHttpRequest')
            self.assertEqual(resp.status_code, 404)
        
        # make sure the bad image was not uploaded
        
        self.assertFalse(Image.objects.all())
    
    
    def test_bad_document_upload_view(self):
        
        # confirm there are no documents
        
        self.assertFalse(Document.objects.all())
        
        # try to upload the bad document
        
        doc = os.path.join(settings.MEDIA_ROOT, 'test.woof')
        
        with open(doc, 'rb') as doc_file:
            resp = self.client.post(
                reverse('attachments:ajax_upload_document'),
                {'file': doc_file}, 
                HTTP_X_REQUESTED_WITH='XMLHttpRequest')
            self.assertEqual(resp.status_code, 404)
        
        # make sure the bad document was not uploaded
        
        self.assertFalse(Document.objects.all())