Commit 19a49f82 authored by okhin's avatar okhin

99% of Coverage \o/

parent 60bc7c30
Pipeline #717 passed with stage
in 57 seconds
......@@ -42,14 +42,10 @@ class GroupAdmin(admin.ModelAdmin):
if request.user.is_superuser:
return qs
user_orgs = [x.id for x in request.user.organizations.all()]
return qs.filter(organization__in=user_orgs)
return qs.filter(type__organization__in=user_orgs)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "organization":
orgs = request.user.organizations.all()
query = {'id__in': orgs}
kwargs["queryset"] = Organization.objects.filter(**query)
elif db_field.name == "type":
if db_field.name == "type":
orgs = request.user.organizations.all()
query = {'organization__in': orgs}
kwargs["queryset"] = GroupType.objects.filter(**query)
......
......@@ -4,7 +4,7 @@ from django.test import TestCase
from rest_framework.test import APIClient
from picampaign.organization.models import Organization, GroupType, Group, FeedbackCategory
from picampaign.organization.admin import OrganizationAdmin, GroupTypeAdmin
from picampaign.organization.admin import OrganizationAdmin, GroupTypeAdmin, GroupAdmin, CategoryAdmin
from picampaign.campaign.models import Campaign
# Create your tests here.
......@@ -163,3 +163,116 @@ class GroupTypeAdminTest(TestCase):
'</select>'
'</div>' % {'id': self.organization.id, 'org': self.organization})
class GroupAdminTest(TestCase):
def setUp(self):
self.organization = Organization.objects.create(
name='Majestic 12',
sip_key='majestic-12'
)
self.grouptype = GroupType.objects.create(
name='Group',
organization=self.organization
)
self.group = Group.objects.create(
name='Test Group',
type=self.grouptype
)
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 = GroupAdmin(Group, self.site)
request = MockRequest()
request.user = self.super_user
self.assertEqual(list(ma.get_queryset(request)), list(Group.objects.all()))
request.user = self.user
self.assertEqual(list(ma.get_queryset(request)), [])
request.user.organizations = Organization.objects.all()
self.assertEqual(list(ma.get_queryset(request)), list(Group.objects.all()))
def test_formfield_forforeignkey(self):
ma = GroupAdmin(Group, self.site)
request = MockRequest()
request.user = self.user
form = ma.get_form(request)()
self.assertHTMLEqual(str(form['type']),
'<div class="related-widget-wrapper">'
'<select id="id_type" name="type">'
'<option value="" selected="selected">---------</option>'
'</select>'
'</div>')
request.user.organizations = Organization.objects.all()
form = ma.get_form(request)()
self.assertHTMLEqual(str(form['type']),
'<div class="related-widget-wrapper">'
'<select id="id_type" name="type">'
'<option value="" selected="selected">---------</option>'
'<option value="%(id)d">%(typ)s</option>'
'</select>'
'</div>' % {'id': self.grouptype.id, 'typ': self.grouptype})
class CategoryAdminTest(TestCase):
def setUp(self):
self.organization = Organization.objects.create(
name='Majestic 12',
sip_key='majestic-12'
)
self.group = FeedbackCategory.objects.create(
name='Test Feedback',
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 = CategoryAdmin(FeedbackCategory, self.site)
request = MockRequest()
request.user = self.super_user
self.assertEqual(list(ma.get_queryset(request)), list(FeedbackCategory.objects.all()))
request.user = self.user
self.assertEqual(list(ma.get_queryset(request)), [])
request.user.organizations = Organization.objects.all()
self.assertEqual(list(ma.get_queryset(request)), list(FeedbackCategory.objects.all()))
def test_formfield_forforeignkey(self):
ma = CategoryAdmin(FeedbackCategory, self.site)
request = MockRequest()
request.user = self.user
form = ma.get_form(request)()
self.assertHTMLEqual(str(form['organization']),
'<div class="related-widget-wrapper">'
'<select id="id_organization" name="organization" required>'
'<option value="" selected="selected">---------</option>'
'</select>'
'</div>')
request.user.organizations = Organization.objects.all()
form = ma.get_form(request)()
self.assertHTMLEqual(str(form['organization']),
'<div class="related-widget-wrapper">'
'<select id="id_organization" name="organization" required>'
'<option value="" selected="selected">---------</option>'
'<option value="%(id)d">%(org)s</option>'
'</select>'
'</div>' % {'id': self.organization.id, 'org': self.organization})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment