duck-booking-tool/api/tests.py

144 lines
4.6 KiB
Python
Raw Normal View History

from django.test import TestCase, Client
2014-12-23 08:56:04 +00:00
from django.contrib.auth.models import User
2015-01-22 15:22:57 +00:00
from django.conf import settings
2015-10-19 13:04:15 +00:00
from django_webtest import WebTest
2014-12-23 08:56:04 +00:00
2015-01-22 15:22:57 +00:00
import json
from booking.ducklevel import level_to_up_minutes
2014-12-23 08:56:04 +00:00
from booking.models import Species, Location, Duck, Competence, DuckCompetence
2015-10-19 13:04:15 +00:00
class DuckClassTest(WebTest):
csrf_checks = False
2015-01-22 15:22:57 +00:00
def setUp(self):
good_minutes = level_to_up_minutes(settings.COMP_WARN_LEVEL + 1)
bad_minutes = level_to_up_minutes(settings.COMP_WARN_LEVEL)
2015-10-19 13:04:15 +00:00
self.user = User.objects.create_user(username='test',
password='test')
2015-01-22 15:22:57 +00:00
spec = Species(name = 'duck')
spec.save()
loc = Location(name = 'temp')
loc.save()
2015-10-19 13:04:15 +00:00
self.comp_bad = Competence(name = 'test1', added_by = self.user)
2015-01-22 15:22:57 +00:00
self.comp_bad.save()
2015-10-19 13:04:15 +00:00
self.comp_good = Competence(name = 'test2',
added_by = self.user)
2015-01-22 15:22:57 +00:00
self.comp_good.save()
2015-10-19 13:04:15 +00:00
self.duck = Duck(species = spec,
location = loc,
donated_by = self.user,
color='123456')
2015-01-22 15:22:57 +00:00
self.duck.save()
2015-10-19 13:04:15 +00:00
dcomp = DuckCompetence(duck = self.duck,
comp = self.comp_bad,
up_minutes = bad_minutes,
down_minutes = 0)
2015-01-22 15:22:57 +00:00
dcomp.save()
2015-10-19 13:04:15 +00:00
dcomp = DuckCompetence(duck = self.duck,
comp = self.comp_good,
up_minutes = good_minutes,
down_minutes = 0)
2015-01-22 15:22:57 +00:00
dcomp.save()
def test_book_nonlogged(self):
2015-10-19 13:04:15 +00:00
page = self.app.post('/api/v1/ducks/1/book/', expect_errors=True)
self.assertEqual(page.status_code, 403)
2015-01-22 15:22:57 +00:00
def test_book_nonexist(self):
2015-10-19 13:04:15 +00:00
# Try to book a non-existing duck
page = self.app.post(
'/api/v1/ducks/9999/book/',
params={
'competence': self.comp_good.pk,
},
user=self.user,
expect_errors=True)
self.assertEqual(404, page.status_code)
2015-01-22 15:22:57 +00:00
# Try to book an existing Duck for a non-existing competence
2015-10-19 13:04:15 +00:00
page = self.app.post(
'/api/v1/ducks/%d/book/' % self.duck.pk,
params={
'competence': 9999
},
user=self.user,
expect_errors=True)
self.assertEqual(404, page.status_code)
2015-01-22 15:22:57 +00:00
def test_book_warn(self):
test_data = {
2015-10-19 13:04:15 +00:00
'competence': self.comp_bad.pk,
2015-01-22 15:22:57 +00:00
}
2015-10-19 13:04:15 +00:00
url = '/api/v1/ducks/%d/book/' % self.duck.pk
2015-01-22 15:22:57 +00:00
2015-10-19 13:04:15 +00:00
page = self.app.post(url, params=test_data, user=self.user)
self.assertEquals(200, page.status_code)
2015-01-22 15:22:57 +00:00
2015-10-19 13:04:15 +00:00
page_json = json.loads(page.content)
self.assertEquals(page_json['status'], 'bad-comp')
2015-01-22 15:22:57 +00:00
test_data['force'] = 1
2015-10-19 13:04:15 +00:00
page = self.app.post(url, params=test_data, user=self.user)
self.assertEqual(200, page.status_code)
2015-01-22 15:22:57 +00:00
2015-10-19 13:04:15 +00:00
page_json = json.loads(page.content)
self.assertEquals(page_json['status'], 'ok')
2015-01-22 15:22:57 +00:00
def test_book_good(self):
test_data = {
2015-10-19 13:04:15 +00:00
"competence": self.comp_good.pk
2015-01-22 15:22:57 +00:00
}
2015-10-19 13:04:15 +00:00
url = '/api/v1/ducks/%d/book/' % self.duck.pk
2015-01-22 15:22:57 +00:00
# Book the duck
2015-10-19 13:04:15 +00:00
page = self.app.post(url, params=test_data, user=self.user)
self.assertEquals(200, page.status_code)
2015-01-22 15:22:57 +00:00
2015-10-19 13:04:15 +00:00
page_json = json.loads(page.content)
self.assertEqual(page_json['status'], 'ok')
2015-01-22 15:22:57 +00:00
# Try to book again, it should fail
2015-10-19 13:04:15 +00:00
page = self.app.post(url, params=test_data, user=self.user)
self.assertEqual(200, page.status_code)
page_json = json.loads(page.content)
self.assertEqual('already-booked', page_json['status'])
def test_duck_donation(self):
# Duck donation should not be allowed without logging in
page = self.app.get('/api/v1/ducks/donate/', expect_errors=True)
self.assertEquals(page.status_code, 403)
# Duck donation should not be allowed withoud logging in
page = self.app.post('/api/v1/ducks/donate/', expect_errors=True)
self.assertEquals(page.status_code, 403)
page = self.app.post(
'/api/v1/ducks/donate/',
params={
'species': 1,
'color': '123456',
},
user=self.user)
page_json = json.loads(page.content)
def test_duck_details(self):
url = '/api/v1/ducks/%d/' % self.duck.pk
page = self.app.get(url)
self.assertEqual(200, page.status_code)
page_json = json.loads(page.content)
self.assertEqual(len(page_json), 3)
self.assertEquals('123456', page_json['color'])