test_admin.py 7.29 KB
Newer Older
okhin's avatar
okhin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 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 59 60 61 62
from django.contrib.admin.sites import AdminSite
from django.contrib.admin.options import ModelAdmin
from django.contrib.auth.models import User
from django.test import TestCase

from picampaign.campaign.models import Campaign, Contact, CampaignContact, Argumentary
from picampaign.campaign.admin import CampaignAdmin, CampaignContactAdmin, ArgumentaryAdmin
from picampaign.organization.models import Organization

class MockRequest(object):
    pass

class ArugmentaryAdminTest(TestCase):
    def setUp(self):
        self.organization = Organization.objects.create(
            name='Majestic 12',
            sip_key='majestic-12'
        )
        self.campaign = Campaign.objects.create(
            title='Campaign Title',
            start_date='2000-01-01',
            end_date='2100-12-31',
            organization=self.organization
        )
        self.argumentary = Argumentary.objects.create(
            lang='en',
            text='A pertinent argument',
            campaign=self.campaign
        )
        self.super_user = User.objects.create_superuser(
            username='john',
            email='johndoe@example.com',
            password='password',
        )
        self.user = User.objects.create_user(
            username='jane',
            email='janedoe@example.com',
            password='password'
        )
        self.site = AdminSite()

    def test_get_queryset(self):
        ma = ArgumentaryAdmin(Argumentary, self.site)
        request = MockRequest()
        # SuperUser should have it all
        request.user = self.super_user
        self.assertEqual(list(ma.get_queryset(request)), list(Argumentary.objects.all()))
        # User with no orgs should have nothing
        request.user = self.user
        self.assertEqual(list(ma.get_queryset(request)), [])
        # User with orgs should have contacts from associated campaign
        request.user.organizations = Organization.objects.all()
        self.assertEqual(list(ma.get_queryset(request)), list(Argumentary.objects.all()))

    def test_formfield_for_foreignkey(self):
        ma = ArgumentaryAdmin(Argumentary, self.site)
        request = MockRequest()
        # Organization fields should be filtered
        request.user = self.user
        form = ma.get_form(request)()
        self.assertHTMLEqual(str(form['campaign']),
                '<div class="related-widget-wrapper">'
63
                '<select id="id_campaign" name="campaign" required>'
okhin's avatar
okhin committed
64 65 66 67 68 69 70 71 72
                '<option value="" selected="selected">---------</option>'
                '</select>'
                '</div>'
                )
        # If we add an org, we shoudl have it
        request.user.organizations = Organization.objects.all()
        form = ma.get_form(request)()
        self.assertHTMLEqual(str(form['campaign']),
                '<div class="related-widget-wrapper">'
73
                '<select id="id_campaign" name="campaign" required>'
okhin's avatar
okhin committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
                '<option value="" selected="selected">---------</option>'
                '<option value="%d">%s</option>'
                '</select>'
                '</div>' % (self.campaign.id, str(self.campaign)))

class CampaignContactAdminTest(TestCase):
    def setUp(self):
        self.organization = Organization.objects.create(
            name='Majestic 12',
            sip_key='majestic-12'
        )
        self.campaign = Campaign.objects.create(
            title='Campaign Title',
            start_date='2000-01-01',
            end_date='2100-12-31',
            organization=self.organization
        )
        self.contact = Contact.objects.create(
            first_name='Victor',
            last_name='Hugo',
            birthdate='1802-02-26'
        )
        self.campaigncontact = CampaignContact.objects.create(
            campaign=self.campaign,
            contact=self.contact
        )
        self.super_user = User.objects.create_superuser(
            username='john',
            email='johndoe@example.com',
            password='password',
        )
        self.user = User.objects.create_user(
            username='jane',
            email='janedoe@example.com',
            password='password'
        )
        self.site = AdminSite()

    def test_get_queryset(self):
        ma = CampaignContactAdmin(CampaignContact, self.site)
        request = MockRequest()
        # SuperUser should have it all
        request.user = self.super_user
        self.assertEqual(list(ma.get_queryset(request)), list(CampaignContact.objects.all()))
        # User with no orgs should have nothing
        request.user = self.user
        self.assertEqual(list(ma.get_queryset(request)), [])
        # User with orgs should have contacts from associated campaign
        request.user.organizations = Organization.objects.all()
        self.assertEqual(list(ma.get_queryset(request)), list(CampaignContact.objects.all()))

class CampaignAdminTest(TestCase):
    def setUp(self):
        self.organization = Organization.objects.create(
            name='Majestic 12',
            sip_key='majestic-12'
        )
        self.campaign = Campaign.objects.create(
            title='Campaign Title',
            start_date='2000-01-01',
            end_date='2100-12-31',
            organization = self.organization
        )
        self.super_user = User.objects.create_superuser(
            username='john',
            email='johndoe@example.com',
            password='password',
        )
        self.user = User.objects.create_user(
            username='jane',
            email='janedoe@example.com',
            password='password'
        )
        self.site = AdminSite()

    def test_get_queryset(self):
        ma = CampaignAdmin(Campaign, self.site)
        request = MockRequest()
        # SuperUser should have access to everything
        request.user = self.super_user
        self.assertEqual(list(ma.get_queryset(request)), list(Campaign.objects.all()))
        # NormalUser outside of organization should have access to nothing
        request.user = self.user
        self.assertEqual(list(ma.get_queryset(request)), [])
        # NormalUser which is part of orgs should have access to them
        request.user.organizations = Organization.objects.all()
        self.assertEqual(list(ma.get_queryset(request)), list(Campaign.objects.all()))

    def test_formfield_for_foreignkey(self):
        ma = CampaignAdmin(Campaign, self.site)
        request = MockRequest()
        # Organization fields should be filtered
        request.user = self.user
        form = ma.get_form(request)()
        self.assertHTMLEqual(str(form['organization']),
                '<div class="related-widget-wrapper">'
170
                '<select id="id_organization" name="organization" required>'
okhin's avatar
okhin committed
171 172 173 174 175 176 177 178 179
                '<option value="" selected="selected">---------</option>'
                '</select>'
                '</div>'
                )
        # If we add an org, we shoudl have it
        request.user.organizations = Organization.objects.all()
        form = ma.get_form(request)()
        self.assertHTMLEqual(str(form['organization']),
                '<div class="related-widget-wrapper">'
180
                '<select id="id_organization" name="organization" required>'
okhin's avatar
okhin committed
181 182 183 184
                '<option value="" selected="selected">---------</option>'
                '<option value="%d">%s</option>'
                '</select>'
                '</div>' % (self.organization.id, str(self.organization)))