import csv
import io
from django.test import TestCase
from django.http import HttpResponse
from django.contrib.admin.sites import AdminSite
from django.contrib.auth.models import User
from billjobs.admin import UserAdmin

class MockRequest(object):
            pass

class EmailExportTestCase(TestCase):
    """ Tests for email account export """

    fixtures = ['test_api_user.yaml']

    def setUp(self):
        self.site = AdminSite()
        self.query_set = User.objects.all()

    def test_method_is_avaible(self):
        """ Test UserAdmin class has method export_email """
        self.assertTrue(hasattr(UserAdmin, 'export_email'))

    def test_method_is_model_admin_action(self):
        """ Test method is an custom action for user admin """
        self.assertTrue('export_email' in UserAdmin.actions)

    def test_action_has_a_short_description(self):
        """ Test method has a short description """
        self.assertEqual(UserAdmin.export_email.short_description,
                'Export email of selected users')

    def test_action_return_http_response(self):
        """ Test method return an HttpResponse """
        user_admin = UserAdmin(User, self.site)
        response = user_admin.export_email(request=MockRequest(),
                queryset=self.query_set)
        self.assertIsInstance(response, HttpResponse)

    def test_action_return_csv(self):
        """ Test method return text/csv as http response content type """
        user_admin = UserAdmin(User, self.site)
        response = user_admin.export_email(request=MockRequest(),
                queryset=self.query_set)
        self.assertEqual(response.get('Content-Type'), 'text/csv')

    def test_action_return_csv_content(self):
        """ Test method return csv content """
        user_admin = UserAdmin(User, self.site)
        response = user_admin.export_email(request=MockRequest(),
                queryset=self.query_set)
        output = io.StringIO()
        writer = csv.writer(output)
        for email in User.objects.values_list('email'):
            writer.writerow(email)
        self.assertEqual(response.content.decode(), output.getvalue())

@@ -214,16 +214,66 @@
 erAdmin%0A
+from billjobs.tests.factories import UserFactory%0A%0A
 %0Aclass M
@@ -310,16 +310,17 @@
   pass%0A%0A
+%0A
 class Em
@@ -400,63 +400,46 @@
     
-fixtures = %5B'test_api_user.yaml'%5D%0A%0A    def setUp(self):
+def setUp(self):%0A        UserFactory()
 %0A   
@@ -1032,16 +1032,25 @@
         
+         
 'Export 
@@ -1258,32 +1258,49 @@
 in.export_email(
+%0A                
 request=MockRequ
@@ -1305,32 +1305,16 @@
 quest(),
-%0A               
  queryse
@@ -1581,32 +1581,49 @@
 in.export_email(
+%0A                
 request=MockRequ
@@ -1628,32 +1628,16 @@
 quest(),
-%0A               
  queryse
@@ -1906,16 +1906,33 @@
 t_email(
+%0A                
 request=
@@ -1945,32 +1945,16 @@
 quest(),
-%0A               
  queryse

use UserFactory instead of fixture file