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 """

    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')

@@ -1,20 +1,41 @@
+import csv%0Aimport io%0A
 from django.test imp
@@ -341,16 +341,54 @@
 rt %22%22%22%0A%0A
+    fixtures = %5B'account_test.yaml'%5D%0A%0A
     def 
@@ -1675,8 +1675,486 @@
 t/csv')%0A
+%0A    def test_action_return_csv_content(self):%0A        %22%22%22 Test method return csv content %22%22%22%0A        user_admin = UserAdmin(User, self.site)%0A        response = user_admin.export_email(request=MockRequest(),%0A                queryset=self.query_set)%0A        output = io.StringIO()%0A        writer = csv.writer(output)%0A        for email in User.objects.values_list('email'):%0A            writer.writerow(email)%0A        self.assertEqual(response.content.decode(), output.getvalue())%0A

Test export email return csv content