Permalink
Browse files

finished on testing mobile views

  • Loading branch information...
1 parent 5827475 commit 8119df44bd925ea57d1347bb959e581a5d9da122 @peterbe peterbe committed Oct 25, 2011
Showing with 294 additions and 31 deletions.
  1. +286 −9 apps/mobile/tests.py
  2. +1 −1 apps/mobile/urls.py
  3. +7 −21 apps/mobile/views.py
View
@@ -41,7 +41,7 @@
from django.core.urlresolvers import reverse
from django.utils import simplejson as json
from dates.tests.test_views import ViewsTest as BaseViewsTest
-from dates.models import Entry
+from dates.models import Entry, Hours
class MobileViewsTest(BaseViewsTest):
@@ -141,22 +141,299 @@ def test_right_now_json(self):
ok_('harry' in names[0])
def test_left_json(self):
- pass
+ url = reverse('mobile.left')
+ response = self.client.get(url)
+ eq_(response.status_code, 200)
+ ok_(json.loads(response.content)['error'])
+
+ self._login()
+ response = self.client.get(url)
+ struct = json.loads(response.content)
+ eq_(struct['missing'], ['country', 'start date'])
+
+ peter, = User.objects.all()
+ profile = peter.get_profile()
+ profile.country = 'US'
+ profile.save()
+
+ response = self.client.get(url)
+ struct = json.loads(response.content)
+ eq_(struct['missing'], ['start date'])
+
+ profile = peter.get_profile()
+ today = datetime.date.today()
+ profile.start_date = today - datetime.timedelta(days=30)
+ profile.save()
+ response = self.client.get(url)
+ struct = json.loads(response.content)
+ eq_(struct['less_than_a_year'], 30)
+
+ profile.start_date = today - datetime.timedelta(days=400)
+ profile.save()
+
+ response = self.client.get(url)
+ struct = json.loads(response.content)
+
+ hours = struct['hours']
+ ok_(isinstance(hours, int))
+ ok_(hours > 0)
+
+ hours = struct['days']
+ ok_(isinstance(hours, basestring))
+
def test_notify(self):
- pass
+ url = reverse('mobile.notify')
+ response = self.client.get(url)
+ eq_(response.status_code, 405)
+
+ response = self.client.post(url, {})
+ eq_(response.status_code, 200)
+ ok_(json.loads(response.content)['error'])
+
+ self._login()
+ today = datetime.date.today()
+ data = {
+ 'start': 'xxx'
+ }
+ response = self.client.post(url, data)
+ struct = json.loads(response.content)
+ ok_(struct['form_errors']['start'])
+ ok_(struct['form_errors']['end'])
+
+ user, = User.objects.all()
+ # first create an unfinished entry and notice it will be deleted
+ Entry.objects.create(
+ user=user,
+ start=today + datetime.timedelta(days=10),
+ end=today + datetime.timedelta(days=10),
+ details='Unfinished'
+ )
+ # ...and one that won't be deleted
+ Entry.objects.create(
+ user=user,
+ start=today + datetime.timedelta(days=12),
+ end=today + datetime.timedelta(days=12),
+ total_hours=8,
+ details='Finished'
+ )
+
+ data = {
+ 'start': today,
+ 'end': today + datetime.timedelta(days=1),
+ 'details': '\tSailing! ',
+ }
+ response = self.client.post(url, data)
+ struct = json.loads(response.content)
+ ok_(struct['entry'])
+ entry = Entry.objects.get(pk=struct['entry'])
+ eq_(entry.start, today)
+ eq_(entry.end, today + datetime.timedelta(days=1))
+ eq_(entry.total_hours, None)
+ eq_(entry.details, 'Sailing!')
+
+ ok_(Entry.objects.get(details='Finished'))
+ ok_(not Entry.objects.filter(details='Unfinished').count())
+
+ def test_save_hours(self):
+ url = reverse('mobile.save_hours')
+ response = self.client.get(url)
+ eq_(response.status_code, 405)
+
+ # not logged in
+ response = self.client.post(url, {})
+ eq_(response.status_code, 200)
+ ok_(json.loads(response.content)['error'])
+
+ self._login()
+ # logged in but not providing entry ID
+ response = self.client.post(url, {})
+ eq_(response.status_code, 400)
+
+ response = self.client.post(url, {'entry': 999})
+ eq_(response.status_code, 404)
+
+ # create an entry that belongs to someone else
+ bob = User.objects.create(username='bob')
+ bobs_entry = Entry.objects.create(
+ user=bob,
+ start=datetime.date.today(),
+ end=datetime.date.today(),
+ total_hours=8,
+ )
+
+ response = self.client.post(url, {'entry': bobs_entry.pk})
+ eq_(response.status_code, 403)
+
+ # Create an incomplete entry
+ peter = User.objects.get(username='peter') # see _login()
+ today = datetime.date.today()
+ entry = Entry.objects.create(
+ user=peter,
+ start=today + datetime.timedelta(days=1),
+ end=today + datetime.timedelta(days=2),
+ details='Boo!'
+ )
+
+ data = {
+ 'entry': entry.pk,
+ }
+ response = self.client.post(url, data)
+ eq_(response.status_code, 200)
+ struct = json.loads(response.content)
+ ok_(struct['form_errors'])
- def test_hours(self):
- pass
+ def fmt(d):
+ return d.strftime('d-%Y%m%d')
+
+ data[fmt(today + datetime.timedelta(days=1))] = 8
+ data[fmt(today + datetime.timedelta(days=2))] = 4
+ response = self.client.post(url, data)
+ eq_(response.status_code, 200)
+ struct = json.loads(response.content)
+ ok_(struct['ok'])
+
+ entry = Entry.objects.get(pk=data['entry'])
+ eq_(entry.total_hours, 8 + 4)
+
+ hours = Hours.objects.filter(entry=entry)
+ eq_(hours.count(), 2)
def test_hours_json(self):
- pass
+ url = reverse('mobile.hours')
+ response = self.client.get(url)
+ eq_(response.status_code, 200)
+ ok_(json.loads(response.content)['error'])
+
+ self._login()
+ response = self.client.get(url, {'entry': ''})
+ eq_(response.status_code, 200)
+ ok_(json.loads(response.content)['error'])
+
+ # create an entry that belongs to someone else
+ bob = User.objects.create(username='bob')
+ bobs_entry = Entry.objects.create(
+ user=bob,
+ start=datetime.date.today(),
+ end=datetime.date.today(),
+ total_hours=8,
+ )
+
+ response = self.client.get(url, {'entry': bobs_entry.pk})
+ eq_(response.status_code, 403)
+
+ # make a fake entry so that the current days are prefilled
+ today = datetime.date(2011, 10, 25) # a Tuesday
+ peter = User.objects.get(username='peter') # see _login()
+ entry = Entry.objects.create(
+ user=peter,
+ start=today,
+ end=today + datetime.timedelta(days=1),
+ )
+
+ other_entry = Entry.objects.create(
+ user=peter,
+ start=today + datetime.timedelta(days=1),
+ end=today + datetime.timedelta(days=1),
+ total_hours=4
+ )
+ other_hours = Hours.objects.create(
+ entry=other_entry,
+ date=today + datetime.timedelta(days=1),
+ hours=4,
+ )
+
+ response = self.client.get(url, {'entry': entry.pk})
+ eq_(response.status_code, 200)
+ struct = json.loads(response.content)
+ hours1 = struct[0]
+ hours2 = struct[1]
+
+ def fmt(d):
+ return d.strftime('d-%Y%m%d')
+
+ eq_(hours1['full_day'], today.strftime(settings.DEFAULT_DATE_FORMAT))
+ eq_(hours1['value'], 8)
+ eq_(hours1['key'], fmt(today))
+ tomorrow = today + datetime.timedelta(days=1)
+ eq_(hours2['full_day'], tomorrow.strftime(settings.DEFAULT_DATE_FORMAT))
+ eq_(hours2['value'], 4)
+ eq_(hours2['key'], fmt(tomorrow))
+
def test_settings_json(self):
- pass
+ url = reverse('mobile.settings')
+ response = self.client.get(url)
+ eq_(response.status_code, 200)
+ ok_(json.loads(response.content)['error'])
+
+ self._login()
+ peter = User.objects.get(username='peter')
+ profile = peter.get_profile()
+ response = self.client.get(url)
+ eq_(response.status_code, 200)
+ struct = json.loads(response.content)
+ eq_(struct['username'], peter.username)
+ eq_(struct['email'], peter.email)
+ ok_(peter.get_full_name() in struct['full_name'])
+ ok_(peter.email in struct['full_name'])
+
+ profile.country = 'GB'
+ profile.city = 'London'
+ profile.start_date = datetime.date(2011, 4, 1)
+ profile.save()
+ response = self.client.get(url)
+ struct = json.loads(response.content)
+ eq_(struct['start_date'], profile.start_date.strftime('%Y-%m-%d'))
+ eq_(struct['country'], 'GB')
+ eq_(struct['city'], 'London')
def test_save_settings(self):
- pass
+ url = reverse('mobile.save_settings')
+ response = self.client.get(url)
+ eq_(response.status_code, 405)
+
+ # not logged in
+ response = self.client.post(url, {})
+ eq_(response.status_code, 200)
+ ok_(json.loads(response.content)['error'])
+
+ self._login()
+ peter = User.objects.get(username='peter')
+ profile = peter.get_profile()
+ data = {
+ 'city': 'London',
+ 'country': 'GB',
+ 'start_date': '2010-xxx',
+ }
+ response = self.client.post(url, data)
+ eq_(response.status_code, 200)
+ struct = json.loads(response.content)
+ ok_(struct['form_errors']['start_date'])
+ data['start_date'] = '2010-04-01'
+ response = self.client.post(url, data)
+ eq_(response.status_code, 200)
+ struct = json.loads(response.content)
+ ok_(struct['ok'])
+
+ from users.models import UserProfile
+ profile, = UserProfile.objects.all()
+ eq_(profile.country, 'GB')
+ eq_(profile.city, 'London')
+ eq_(profile.start_date, datetime.date(2010, 4, 1))
def test_exit_mobile(self):
- pass
+ self._login()
+ # if you end up on the mobile pages and want out,
+ # it'll set a cookie
+ url = reverse('mobile.exit')
+ response = self.client.get(url)
+ eq_(response.status_code, 302)
+ eq_(urlparse(response['location']).path, '/')
+ ok_(self.client.cookies['no-mobile'].value)
+
+ # undo that by visiting the mobile home page again
+ url = reverse('mobile.home')
+ response = self.client.get(url)
+ eq_(response.status_code, 200)
+ ok_(not self.client.cookies['no-mobile'].value)
View
@@ -44,6 +44,6 @@
url(r'^settings/$', views.save_settings, name='mobile.save_settings'),
url(r'^notify/$', views.notify, name='mobile.notify'),
url(r'^hours.json$', views.hours_json, name='mobile.hours'),
- url(r'^hours/$', views.hours, name='mobile.save_hours'),
+ url(r'^hours/$', views.save_hours, name='mobile.save_hours'),
url(r'^exit/$', views.exit_mobile, name='mobile.exit'),
)
View
@@ -34,29 +34,15 @@
# ***** END LICENSE BLOCK *****
import datetime
-#from urllib import urlencode
-#from collections import defaultdict
import jingo
from django import http
from django.contrib.auth.decorators import login_required
from django.db import transaction
from django.views.decorators.http import require_POST
-#from django.core.urlresolvers import reverse
+from django.views.decorators.csrf import csrf_exempt
from django.conf import settings
from django.shortcuts import redirect, get_object_or_404
-#from django.contrib import messages
-#from django.db.models import Q
-#from django.template import Context, loader
-#from django.core.mail import get_connection, EmailMessage
from dates.models import Entry, Hours
-#from users.models import UserProfile
-#from users.utils import ldap_lookup
-#from django.core.validators import validate_email
-#from django.core.exceptions import ValidationError
-#from .utils import parse_datetime, DatetimeParseError
-#from .utils.pto_left import get_hours_left
-#import utils
-#import forms
from dates.decorators import json_view
from dates.utils import get_weekday_dates
@@ -126,7 +112,6 @@ def left(request):
return get_left(profile)
-from django.views.decorators.csrf import csrf_exempt
@csrf_exempt # XXX fix this
@require_POST
@transaction.commit_on_success
@@ -149,9 +134,6 @@ def notify(request):
end=end,
details=details,
)
- print "ENTRY", entry.pk
- print start, '---', end
- print
clean_unfinished_entries(entry)
request.session['notify_extra'] = notify
return {'entry': entry.pk}
@@ -163,10 +145,14 @@ def notify(request):
@require_POST
@transaction.commit_on_success
@json_view
-def hours(request):
+def save_hours(request):
if not request.user.is_authenticated(): # XXX improve this
return {'error': 'Not logged in'}
+ if not request.POST.get('entry'):
+ return http.HttpResponseBadRequest("No entry parameter provided")
entry = get_object_or_404(Entry, pk=request.POST['entry'])
+ if entry.user != request.user:
+ return http.HttpResponseForbidden("Not your entry")
from dates.forms import HoursForm
from dates.views import save_entry_hours, send_email_notification
@@ -194,7 +180,7 @@ def hours_json(request):
return {'error': 'No entry pre-loaded'}
entry = get_object_or_404(Entry, pk=entry_id)
if entry.user != request.user:
- return HttpResponseForbidden("Not your entry")
+ return http.HttpResponseForbidden("Not your entry")
days = []
for date in get_weekday_dates(entry.start, entry.end):

0 comments on commit 8119df4

Please sign in to comment.