tests.py 8 KB
Newer Older
okhin's avatar
okhin committed
1 2
from unittest import mock

okhin's avatar
okhin committed
3 4 5 6 7
from django.contrib.admin.sites import AdminSite
from django.contrib.admin.options import ModelAdmin
from django.contrib.auth.models import User
from django.contrib.messages.storage.fallback import FallbackStorage
from django.test import TestCase, RequestFactory
okhin's avatar
okhin committed
8 9 10
from django.core.files.uploadedfile import SimpleUploadedFile

from picampaign.importer.models import Importer
okhin's avatar
okhin committed
11
from picampaign.contact.models import Contact
okhin's avatar
okhin committed
12 13
from picampaign.campaign.models import Campaign, CampaignContact
from picampaign.organization.models import Group, GroupType, Organization
okhin's avatar
okhin committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
from picampaign.importer.admin import ImporterAdmin

class ImporterAdminTest(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.user = User.objects.create_user(
            username='john',
            email='johndoe@example.com',
            password='top_sikret'
        )
        self.super_user = User.objects.create_superuser(
            username='Jane',
            email='janedoe@exmaple.com',
            password='password'
        )
        self.organization = Organization.objects.create(
            name='Majestic 12',
            sip_key='majestic-12'
        )
        self.campaign = Campaign.objects.create(
            title='Test Campaign',
            start_date='2000-01-01',
            end_date='2100-12-31',
            organization=self.organization
        )
        GroupType.objects.create(
            organization=self.organization,
            name='group'
        )
        GroupType.objects.create(
            organization=self.organization,
            name='chamber'
        )
        GroupType.objects.create(
            organization=self.organization,
            name='committee'
        )
        json_file = None
        with open('picampaign/importer/data_tests.json', u'rb') as f:
            json_file = SimpleUploadedFile('data_tests.json', f.read())

        self.importer = Importer.objects.create(
            name='test importer',
            category='repr',
            campaign = self.campaign,
59 60
            file=json_file,
            url=''
okhin's avatar
okhin committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        )
        self.site = AdminSite()

    def test_get_queryset(self):
        ma = ImporterAdmin(Importer, self.site)
        request = self.factory.get('/admin/picampaign/importers/')
        setattr(request, 'session', 'session')
        messages = FallbackStorage(request)
        setattr(request, '_messags', messages)
        # Superuser can see anything
        request.user = self.super_user
        self.assertEqual(list(ma.get_queryset(request)), list(Importer.objects.all()))
        # Normal user outside of orgs sees nothing
        request.user = self.user
        self.assertEqual(list(ma.get_queryset(request)), [])
        # User in orgs see what they own
        request.user.organizations = Organization.objects.all()
        self.assertEqual(list(ma.get_queryset(request)), list(Importer.objects.all()))

    def test_import_action(self):
        ma = ImporterAdmin(Importer, self.site)
        request = self.factory.get('/admin/picampaign/importers/')
        request.user = self.user
        request.user.organizations = Organization.objects.all()
        # Launch the action now
        self.assertEqual(self.importer.last_imported, None)
        self.assertEqual(self.importer.last_count, None)
        ma.import_action(request, ma.get_queryset(request))
        self.importer.refresh_from_db()
        self.assertEqual(self.importer.last_imported, 3)
        self.assertEqual(self.importer.last_count, 10)
okhin's avatar
okhin committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

class ImporterTest(TestCase):
    def setUp(self):
        self.organization = Organization.objects.create(
            name='Majestic 12',
            sip_key='majestic-12'
        )
        self.campaign = Campaign.objects.create(
            title='Test campaign',
            start_date='2000-01-01',
            end_date='2100-12-31',
            organization=self.organization)
        GroupType.objects.create(
            organization=self.organization,
            name='group'
        )
        GroupType.objects.create(
            organization=self.organization,
            name='chamber'
        )
        GroupType.objects.create(
            organization=self.organization,
            name='committee'
        )

    def test_str(self):
        importer = Importer.objects.create(
            name='test importer',
            category='repr',
            campaign=self.campaign,
        )
        self.assertEqual(str(importer), u'%(name)s: %(format)s %(type)s importer' % {'format': importer.category,
                                                'type': importer.kind(),
                                                'name': importer.name})

    def test_kind(self):
        importer = Importer.objects.create(
            name='test importer',
            category='repr',
Okhin's avatar
Okhin committed
131
            campaign=self.campaign,
okhin's avatar
okhin committed
132 133 134
        )
        self.assertEqual(importer.kind(), None)
        importer.file = 'a'
135
        importer.url = ''
okhin's avatar
okhin committed
136
        self.assertEqual(importer.kind(), 'file')
Okhin's avatar
Okhin committed
137
        importer.file = None
okhin's avatar
okhin committed
138 139 140
        importer.url = 'http://www.example.com/'
        self.assertEqual(importer.kind(), 'url')

okhin's avatar
okhin committed
141
    def test_representative_json(self):
okhin's avatar
okhin committed
142 143 144 145 146 147 148
        json_file = None
        with open('picampaign/importer/data_tests.json', u'rb') as f:
            json_file = SimpleUploadedFile('data_tests.json', f.read())

        importer = Importer.objects.create(
            name='test importer',
            category='repr',
Okhin's avatar
Okhin committed
149
            campaign=self.campaign,
150 151
            file=json_file,
            url=''
okhin's avatar
okhin committed
152 153 154 155 156 157 158 159 160
        )
        self.assertEqual(importer.kind(), 'file')
        importer.handle()
        importer.refresh_from_db()
        # The file import 3 of the 10 meps
        self.assertEqual(importer.last_count, 10)
        self.assertEqual(importer.last_imported, 3)
        self.assertEqual(len(Group.objects.all()), 13)
        self.assertEqual(len(Contact.objects.all()), 3)
okhin's avatar
okhin committed
161 162 163 164 165 166 167 168

    def test_representative_csv(self):
        csv_file = None
        with open('picampaign/importer/data_tests.csv', u'rb') as f:
            csv_file = SimpleUploadedFile('data_tests.csv', f.read())

        importer = Importer.objects.create(
                name='test updater',
Okhin's avatar
Okhin committed
169
                category='update',
okhin's avatar
okhin committed
170
                campaign=self.campaign,
171 172
                file=csv_file,
                url=''
okhin's avatar
okhin committed
173 174 175 176 177 178 179 180 181
        )
        self.assertEqual(importer.kind(), 'file')
        importer.handle()
        importer.refresh_from_db()
        # There's 2 Meps in the file
        self.assertEqual(importer.last_count, 2)
        self.assertEqual(importer.last_imported, 2)
        self.assertEqual(len(Group.objects.all()), 4)
        self.assertEqual(len(Contact.objects.all()), 2)
okhin's avatar
okhin committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

    def test_uniqueness_insert_10(self):
        '''
        This is related to isse #10
        '''
        # First a classic import.
        json_file = None
        with open('picampaign/importer/data_tests.json', u'rb') as f:
            json_file = SimpleUploadedFile('data_tests.json', f.read())

        importer = Importer.objects.create(
            name='test importer',
            category='repr',
            campaign = self.campaign,
            file=json_file,
            url=''
        )
        self.assertEqual(importer.kind(), 'file')
        importer.handle()
        importer.refresh_from_db()
        # The file import 3 of the 10 meps
        self.assertEqual(importer.last_count, 10)
        self.assertEqual(importer.last_imported, 3)
        self.assertEqual(len(Group.objects.all()), 13)
        self.assertEqual(len(Contact.objects.all()), 3)
        self.assertEqual(len(CampaignContact.objects.all()), 3)
        # If we rerun the same import we must have
        # different values
        importer.handle()
        importer.refresh_from_db()
        self.assertEqual(importer.last_count, 10)
        self.assertEqual(importer.last_imported, 3) # We still have 3 contact imported
        self.assertEqual(len(Group.objects.all()), 13) # Still the same groups
        self.assertEqual(len(Contact.objects.all()), 3) # No more contacts though
        self.assertEqual(len(CampaignContact.objects.all()), 3) # Or ContactCampaign