Permalink
Browse files

Added a tox.ini file and some tests

  • Loading branch information...
MegaMark16 committed Dec 28, 2012
1 parent 57d83cc commit 607b9cecc2e6e9f667510a7670acecee007ddb60
Showing with 138 additions and 15 deletions.
  1. +1 −0 live_support/fixtures/users.json
  2. +96 −0 live_support/tests.py
  3. +10 −10 live_support/urls.py
  4. +10 −5 live_support/views.py
  5. +21 −0 tox.ini
@@ -0,0 +1 @@
+[{"pk": 1, "model": "auth.user", "fields": {"username": "test", "first_name": "", "last_name": "", "is_active": true, "is_superuser": true, "is_staff": true, "last_login": "2012-12-27T16:42:11.882", "groups": [], "user_permissions": [], "password": "pbkdf2_sha256$10000$O8jQ4PUZtofq$c5WGcqB+Luz7gUZreXF8APn1yKXIq0+93dZhEreUvIQ=", "email": "test@example.com", "date_joined": "2012-12-27T16:42:11.882"}}]
View
@@ -0,0 +1,96 @@
+import unittest
+
+from django.test import TestCase
+from django.test.client import Client, ClientHandler
+from django.core.urlresolvers import reverse
+from django.core.cache import cache
+from models import Chat
+
+class ClientTests(TestCase):
+ urls = 'live_support.urls'
+ fixtures = ['live_support/fixtures/users.json',]
+
+ def setUp(self):
+ pass
+
+ def test_start_chat(self):
+ # Login and call get_messages to set the admin_active cache
+ login = self.client.login(username='test', password='test')
+ self.client.get(reverse('get_messages'))
+
+ resp = self.client.post(reverse('start_chat'), {
+ 'name': 'Test Name',
+ 'details': 'Test Details'
+ })
+ self.assertEqual(resp.status_code, 302)
+ resp2 = self.client.get(resp['location'])
+ self.assertEqual(resp2.status_code, 200)
+ self.assertTrue(resp2.context['chat'].hash_key in resp['location'])
+
+ def test_leave_message(self):
+ # Make sure no admins are logged in
+ cache.set('admin_active', None)
+ # Because no admin is logged in calling start_chat just leaves
+ # a message and returns a thank you response
+ resp = self.client.post(reverse('start_chat'), {
+ 'name': 'Test Name',
+ 'details': 'Test Message'
+ })
+ self.assertEqual(resp.status_code, 200)
+ self.assertEqual(resp.content, 'Thank you for contacting us')
+
+ def test_get_messages_prompts_login(self):
+ # If you aren't logged in you cannot call get_messages without
+ # a chat hash_key or you will be directed to log in
+ resp = self.client.get(reverse('get_messages'))
+ self.assertEqual(resp.status_code, 302)
+ self.assertIn('/login/', resp['location'])
+
+ def test_get_messages_with_hash_key_returns_messages(self):
+ chat = Chat.objects.create(name='Test Chat', details='Details')
+ chat.messages.create(name=chat.name, message='new message text')
+ resp = self.client.get(reverse('client_get_messages', args=[chat.hash_key]))
+ self.assertTrue('new message text' in resp.content)
+
+ def test_get_latest_message(self):
+ chat = Chat.objects.create(name='Test Chat', details='Details')
+ message1 = chat.messages.create(name=chat.name, message='message one')
+ message2 = chat.messages.create(name=chat.name, message='message two')
+ url = reverse('client_get_messages', args=[chat.hash_key])
+ resp = self.client.get('%s?%s=%s' % (url, chat.id, message1.id))
+ self.assertIn('two', resp.content)
+ self.assertNotIn('one', resp.content)
+
+
+class AdminTests(TestCase):
+ urls = 'live_support.urls'
+ fixtures = ['live_support/fixtures/users.json',]
+
+ def setUp(self):
+ pass
+
+ def test_get_all_messages(self):
+ login = self.client.login(username='test', password='test')
+ chat = Chat.objects.create(name='Test Chat', details='Details')
+ chat.messages.create(name=chat.name, message='new message text')
+ url = reverse('get_messages')
+ resp = self.client.get('%s?%s=0' % (url, chat.id))
+ self.assertTrue('new message text' in resp.content)
+
+ def test_getting_messages_with_invalid_args(self):
+ login = self.client.login(username='test', password='test')
+ chat = Chat.objects.create(name='Test Chat', details='Details')
+ message1 = chat.messages.create(name=chat.name, message='message one')
+ message2 = chat.messages.create(name=chat.name, message='message two')
+ url = reverse('get_messages')
+ resp = self.client.get('%s?bad=good&%s=%s' % (url, chat.id, message1.id))
+ self.assertIn('two', resp.content)
+ self.assertNotIn('one', resp.content)
+
+ def test_send_admin_message(self):
+ login = self.client.login(username='test', password='test')
+ chat = Chat.objects.create(name='Test Chat', details='Details')
+ resp = self.client.post(reverse('post_message', args=[chat.id]),
+ { 'message': 'admin test message'},
+ **{'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'})
+ self.assertIn('admin test message', resp.content)
View
@@ -2,15 +2,15 @@
from live_support import views
urlpatterns = patterns('',
- url('^$', views.start_chat),
- url('^(?P<support_group_id>\d+)/$', views.start_chat),
- url('^ajax/get_messages/$', views.get_messages),
- url('^ajax/(?P<chat_id>\d+)/post_message/$', views.post_message),
- url('^ajax/(?P<chat_id>\d+)/end_chat/$', views.end_chat),
- url('^ajax/(?P<chat_id>\d+)/join_chat/$', views.join_chat),
- url('^(?P<chat_uuid>[\w-]+)/end_chat/$', views.client_end_chat),
- url('^(?P<chat_uuid>[\w-]+)/get_messages/$', views.client_get_messages),
- url('^(?P<chat_uuid>[\w-]+)/post_message/$', views.client_post_message),
- url('^(?P<chat_uuid>[\w-]+)/$', views.client_chat),
+ url('^$', views.start_chat, name='start_chat'),
+ url('^(?P<support_group_id>\d+)/$', views.start_chat, name='start_chat_for_group'),
+ url('^ajax/get_messages/$', views.get_messages, name='get_messages'),
+ url('^ajax/(?P<chat_id>\d+)/post_message/$', views.post_message, name='post_message'),
+ url('^ajax/(?P<chat_id>\d+)/end_chat/$', views.end_chat, name='end_chat'),
+ url('^ajax/(?P<chat_id>\d+)/join_chat/$', views.join_chat, name='join_chat'),
+ url('^(?P<chat_uuid>[\w-]+)/end_chat/$', views.client_end_chat, name='client_end_chat'),
+ url('^(?P<chat_uuid>[\w-]+)/get_messages/$', views.client_get_messages, name='client_get_messages'),
+ url('^(?P<chat_uuid>[\w-]+)/post_message/$', views.client_post_message, name='client_post_message'),
+ url('^(?P<chat_uuid>[\w-]+)/$', views.client_chat, name='client_chat'),
)
View
@@ -78,10 +78,15 @@ def get_messages(request):
chats = {}
for k, v in request.GET.iteritems():
alive = True
- messages = ChatMessage.objects.filter(chat__id=k)
+ try:
+ messages = ChatMessage.objects.filter(chat__id=k)
+ except ValueError:
+ next
if v:
- messages = ChatMessage.objects.filter(chat__id=k, id__gt=v)
-
+ try:
+ messages = messages.filter(id__gt=v)
+ except ValueError:
+ pass
# Check to see if the end user has recently checked for new messages
# for this chat session by checking for the cache entry using the
# chat id. If they haven't asked for new messages in the past 30
@@ -234,8 +239,8 @@ def start_chat(request, support_group_id=None):
if admin_active:
request.session['chat_hash_key'] = chat.hash_key
return HttpResponseRedirect(reverse(
- 'live_support.views.client_chat',
- args=[chat.hash_key, ])
+ 'client_chat',
+ args=[chat.hash_key,])
)
else:
return HttpResponse('Thank you for contacting us')
View
21 tox.ini
@@ -0,0 +1,21 @@
+[tox]
+envlist = django13
+
+
+[base]
+sitepackages = False
+setenv =
+ DJANGO_SETTINGS_MODULE=demo.settings
+ PYTHONPATH={toxinidir}
+
+[testenv:django13]
+deps =
+ django==1.4
+ south
+ ipdb
+sitepackages = {[base]sitepackages}
+setenv = {[base]setenv}
+commands =
+ {envbindir}/django-admin.py syncdb --noinput
+ {envbindir}/django-admin.py migrate --noinput
+ {envbindir}/django-admin.py test live_support

0 comments on commit 607b9ce

Please sign in to comment.