Commit d931aa24 authored by okhin's avatar okhin

Still improving coverage

parent 4710efaf
......@@ -57,4 +57,3 @@ class CampaignContactSerializer(serializers.HyperlinkedModelSerializer):
if phone['phone'].startswith(filter):
data['phone'] = phone['phone']
return data
return data
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">'
'<select id="id_campaign" name="campaign">'
'<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">'
'<select id="id_campaign" name="campaign">'
'<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">'
'<select id="id_organization" name="organization">'
'<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">'
'<select id="id_organization" name="organization">'
'<option value="" selected="selected">---------</option>'
'<option value="%d">%s</option>'
'</select>'
'</div>' % (self.organization.id, str(self.organization)))
from collections import OrderedDict
from django.test import TestCase
from picampaign.contact.models import Phone, Contact
from picampaign.campaign.models import Argumentary, Campaign, Organization, CampaignContact
from picampaign.campaign.serializers import ArgumentarySerializer, CampaignContactSerializer
class ArgumentarySerializerTest(TestCase):
def test_serializers(self):
organization = Organization.objects.create(
name='Majestic 12',
sip_key='majestic-12'
)
campaign = Campaign.objects.create(
title='A campaign',
start_date='2000-01-01',
end_date='2100-12-31',
organization=organization
)
Argumentary.objects.create(
lang='en',
text='A pertinent argument',
campaign = campaign
)
self.assertEqual(ArgumentarySerializer(Argumentary.objects.all(), many=True).data, [OrderedDict([('lang', 'en'), ('text', 'A pertinent argument')])])
class CampaignContactSerializerTest(TestCase):
def test_to_representation(self):
organization = Organization.objects.create(
name='Majestic 12',
sip_key='majestic-12'
)
campaign = Campaign.objects.create(
title='A campaign',
start_date='2000-01-01',
end_date='2100-12-31',
organization=organization
)
contact = Contact.objects.create(
first_name='Victor',
last_name='Hugo',
birthdate='1887-02-26',
)
Phone.objects.create(
phone='0123456789',
valid=True,
contact=contact
)
Phone.objects.create(
phone='9876543210',
valid=True,
contact=contact
)
campaigncontact = CampaignContact(
campaign=campaign,
contact=contact
)
serializer = CampaignContactSerializer(campaigncontact)
self.assertEqual(serializer.to_representation(campaigncontact)['phone'], contact.phones.first().phone)
# Let's add a phone filter
campaign.phone_filter='98'
self.assertEqual(serializer.to_representation(campaigncontact)['phone'], contact.phones.last().phone)
from rest_framework.test import APIClient
from django.test import TestCase
from picampaign.campaign.models import Argumentary, Organization, Campaign, CampaignContact
from picampaign.contact.models import Contact, Phone
class ViewSetTest(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.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.phone = Phone.objects.create(
phone='0123456789',
valid=True,
contact=self.contact
)
def test_campaign_viewset(self):
client = APIClient()
response = client.get('/campaigns/', format='json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'[{"id":1,"title":"Campaign Title","description":null,"start_date":"2000-01-01","end_date":"2100-12-31"}]')
def test_campaign_contact_viewset(self):
client = APIClient()
response = client.get('/campaigns/%(cid)d/contacts/' % {'cid': self.campaign.id}, format='json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'[{"id":1,"weight":0,"contact_id":1,"full_name":"Victor Hugo","first_name":"Victor","last_name":"Hugo","phone":"0123456789","groups":[],"photo":""}]')
def test_campaign_argumentary_viewset(self):
client = APIClient()
response = client.get('/campaigns/%(cid)d/arguments/' % {'cid': self.campaign.id}, format='json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'[{"lang":"en","text":"A pertinent argument"}]')
response = client.get('/campaigns/%(cid)d/arguments/%(lang)s/' % {'cid': self.campaign.id, 'lang': self.argumentary.lang}, format='json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'{"lang":"en","text":"A pertinent argument"}')
......@@ -5,7 +5,6 @@ from picampaign.campaign.serializers import (CampaignSerializer,
CampaignContactSerializer,
ArgumentarySerializer)
class CampaignViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint for campaign view
......@@ -13,7 +12,6 @@ class CampaignViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Campaign.objects.all()
serializer_class = CampaignSerializer
class CampaignContactViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint to view contacts related to a campaign
......@@ -26,7 +24,6 @@ class CampaignContactViewSet(viewsets.ReadOnlyModelViewSet):
serializer = self.serializer_class(contacts.all(), many=True)
return Response(serializer.data)
class ArgumentaryViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint to view contacts related to a campaign
......
from django.shortcuts import render
# Create your views here.
......@@ -6,4 +6,4 @@ class FeedbackSerializer(serializers.ModelSerializer):
class Meta:
model = Feedback
fields = ('comment', 'callee')
fields = ('category', 'comment', 'callee')
from django.test import TestCase
from rest_framework.test import APITestCase
# Create your tests here.
from picampaign.contact.models import Contact
from picampaign.campaign.models import CampaignContact, Campaign
from picampaign.organization.models import FeedbackCategory, Organization
from picampaign.feedback.models import Feedback
class FeedbackModelTest(TestCase):
def test_str(self):
organization = Organization.objects.create(
name='Majestic 12',
sip_key='majestic-12'
)
contact = Contact.objects.create(
first_name='Victor',
last_name='Hugo',
birthdate='1889-02-26'
)
campaign = Campaign.objects.create(
title='Test campaign',
start_date='2000-01-01',
end_date='2100-12-31',
organization=organization
)
campaigncontact = CampaignContact.objects.create(
campaign=campaign,
contact=contact
)
feedbackcategory = FeedbackCategory.objects.create(
name='Normal',
organization=organization
)
feedback = Feedback.objects.create(
callee=campaigncontact,
category=feedbackcategory,
comment='Feedback'
)
self.assertEqual(str(feedback), 'feedback for %(callee)s on %(campaign)s' %
{'callee': campaigncontact.contact,
'campaign': campaign})
class FeedbackViewTest(APITestCase):
def test_create(self):
organization = Organization.objects.create(
name='Majestic 12',
sip_key='majestic-12'
)
category = FeedbackCategory.objects.create(
name='Normal',
organization=organization
)
campaign = Campaign.objects.create(
title='Test campaign',
start_date='2000-01-01',
end_date='2100-12-31',
organization=organization
)
contact = Contact.objects.create(
first_name='Victor',
last_name='Hugo',
birthdate='1889-02-26'
)
campaigncontact = CampaignContact.objects.create(
campaign=campaign,
contact=contact
)
data = {'callee': contact.id, 'category': category.id, 'comment': 'Feedback'}
response = self.client.post('/campaigns/%(cid)d/feedbacks/' % {'cid': campaign.id}, data, format='json')
self.assertEqual(response.status_code, 200)
self.assertEqual(Feedback.objects.count(), 1)
self.assertEqual(Feedback.objects.get().comment, 'Feedback')
data = {'callee': contact.id, 'category': 1234567, 'comment': 'Feedback'}
with self.assertRaises(Exception):
self.client.post('/campaigns/%(cid)d/feedbacks/' % {'cid': campaign.id}, data, format='json')
......@@ -12,10 +12,11 @@ class FeedbackViewSet(viewsets.ViewSet):
serializer_class = FeedbackSerializer
def create(self, request, campaign_pk=None):
serializer = FeedbackSerializer(request.DATA)
callee = CampaignContact.objects.get(id=request.DATA['callee'])
category = FeedbackCategory.objects.get(id=request.DATA['category'])
serializer.data['callee'] = callee
serializer.data['category'] = category
feedback = Feedback.objects.create(**serializer.data)
serializer = FeedbackSerializer(data=request.data)
try:
serializer.is_valid()
except Exception as e:
print("Bloup")
raise e
feedback = Feedback.objects.create(**serializer.validated_data)
return Response(feedback.id)
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