Commit 60bc7c30 authored by okhin's avatar okhin

59% covergae on organization/admin

parent bb148de0
Pipeline #716 passed with stage
in 1 minute and 4 seconds
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 django.contrib.admin.sites import AdminSite
from django.contrib.auth.models import User
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.campaign.models import Campaign
# Create your tests here.
......@@ -75,3 +78,88 @@ class CategoryViewSet(TestCase):
response = client.get('/campaigns/%(cid)s/categories/' % {'cid': self.campaign.id}, format='json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'[{"id":1,"name":"Feedback"}]')
class MockRequest(object):
pass
class OrganizationAdminTest(TestCase):
def setUp(self):
self.organization = Organization.objects.create(
name='Majestic 12',
sip_key='majestic-12'
)
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 = OrganizationAdmin(Organization, self.site)
request = MockRequest()
request.user = self.super_user
self.assertEqual(list(ma.get_queryset(request)), list(Organization.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(Organization.objects.all()))
class GroupTypeAdminTest(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.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 = GroupTypeAdmin(GroupType, self.site)
request = MockRequest()
request.user = self.super_user
self.assertEqual(list(ma.get_queryset(request)), list(GroupType.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(GroupType.objects.all()))
def test_formfield_forforeignkey(self):
ma = GroupTypeAdmin(GroupType, 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