Refactor API tests

This commit is contained in:
Gergely Polonkai 2015-10-19 15:04:15 +02:00
parent 5521342151
commit d911a253c0

View File

@ -1,65 +1,22 @@
from django.test import TestCase, Client from django.test import TestCase, Client
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.conf import settings from django.conf import settings
from django_webtest import WebTest
import json import json
from booking.ducklevel import level_to_up_minutes from booking.ducklevel import level_to_up_minutes
from booking.models import Species, Location, Duck, Competence, DuckCompetence from booking.models import Species, Location, Duck, Competence, DuckCompetence
def get_response_encoding(response): class DuckClassTest(WebTest):
encoding = settings.DEFAULT_CHARSET csrf_checks = False
if response.has_header('content-encoding'):
encoding = response['content-encoding']
return encoding
class ApiTest(TestCase):
def setUp(self):
self.client = Client()
species = Species(name = 'duck')
species.save()
loc = Location(name = 'test')
loc.save()
user = User()
user.save()
self.duck = Duck(
species = species,
location = loc,
donated_by = user)
self.duck.save()
comp = Competence(name = 'test', added_by = user)
comp.save()
duckcomp = DuckCompetence(duck = self.duck, comp = comp)
duckcomp.save()
def test_duck_comp_list(self):
response = self.client.get('/api/v1/duck/1/competence.json')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.context['comp_list']), 1)
class DuckBookingTest(TestCase):
username = 'admin'
password = 'password'
def setUp(self): def setUp(self):
good_minutes = level_to_up_minutes(settings.COMP_WARN_LEVEL + 1) good_minutes = level_to_up_minutes(settings.COMP_WARN_LEVEL + 1)
bad_minutes = level_to_up_minutes(settings.COMP_WARN_LEVEL) bad_minutes = level_to_up_minutes(settings.COMP_WARN_LEVEL)
self.duck_id = 1
self.comp_id = 1
self.admin = User.objects.create_user( self.user = User.objects.create_user(username='test',
username = self.username, password='test')
password = self.password)
self.admin.save()
spec = Species(name = 'duck') spec = Species(name = 'duck')
spec.save() spec.save()
@ -67,120 +24,120 @@ class DuckBookingTest(TestCase):
loc = Location(name = 'temp') loc = Location(name = 'temp')
loc.save() loc.save()
self.comp_bad = Competence( self.comp_bad = Competence(name = 'test1', added_by = self.user)
pk = self.comp_id,
name = 'test1',
added_by = self.admin)
self.comp_id += 1
self.comp_bad.save() self.comp_bad.save()
self.comp_good = Competence( self.comp_good = Competence(name = 'test2',
pk = self.comp_id, added_by = self.user)
name = 'test2',
added_by = self.admin)
self.comp_id += 1
self.comp_good.save() self.comp_good.save()
self.duck = Duck( self.duck = Duck(species = spec,
pk = self.duck_id,
species = spec,
location = loc, location = loc,
donated_by = self.admin) donated_by = self.user,
self.duck_id += 1 color='123456')
self.duck.save() self.duck.save()
dcomp = DuckCompetence( dcomp = DuckCompetence(duck = self.duck,
duck = self.duck,
comp = self.comp_bad, comp = self.comp_bad,
up_minutes = bad_minutes, up_minutes = bad_minutes,
down_minutes = 0) down_minutes = 0)
dcomp.save() dcomp.save()
dcomp = DuckCompetence( dcomp = DuckCompetence(duck = self.duck,
duck = self.duck,
comp = self.comp_good, comp = self.comp_good,
up_minutes = good_minutes, up_minutes = good_minutes,
down_minutes = 0) down_minutes = 0)
dcomp.save() dcomp.save()
self.client = Client()
def send_booking_json(self, json_data):
return self.client.post(
'/api/v1/duck/book/',
json.dumps(json_data),
HTTP_X_REQUESTED_WITH = 'XMLHttpRequest',
content_type = 'application/json')
def test_book_nonlogged(self): def test_book_nonlogged(self):
self.client.logout() page = self.app.post('/api/v1/ducks/1/book/', expect_errors=True)
self.assertEqual(page.status_code, 403)
response = self.send_booking_json({
"duck_id": self.duck.pk,
"comp_id": self.comp_good.pk
})
self.assertEqual(response.status_code, 401)
def test_book_nonexist(self): def test_book_nonexist(self):
self.client.login(username = self.username, password = self.password) # Try to book a non-existing duck
page = self.app.post(
# Try to book a non-existing Duck '/api/v1/ducks/9999/book/',
response = self.send_booking_json({ params={
"duck_id": self.duck.pk + 1, 'competence': self.comp_good.pk,
"comp_id": self.comp_good.pk },
}) user=self.user,
self.assertEqual(response.status_code, 404) expect_errors=True)
self.assertEqual(404, page.status_code)
# Try to book an existing Duck for a non-existing competence # Try to book an existing Duck for a non-existing competence
response = self.send_booking_json({ page = self.app.post(
"duck_id": self.duck.pk, '/api/v1/ducks/%d/book/' % self.duck.pk,
"comp_id": 3 params={
}) 'competence': 9999
self.assertEqual(response.status_code, 404) },
user=self.user,
expect_errors=True)
self.assertEqual(404, page.status_code)
def test_book_warn(self): def test_book_warn(self):
test_data = { test_data = {
"duck_id": self.duck.pk, 'competence': self.comp_bad.pk,
"comp_id": self.comp_bad.pk
} }
self.client.login(username = self.username, password = self.password) url = '/api/v1/ducks/%d/book/' % self.duck.pk
response = self.send_booking_json(test_data) page = self.app.post(url, params=test_data, user=self.user)
self.assertEqual(response.status_code, 200) self.assertEquals(200, page.status_code)
j = json.loads(response.content.decode(get_response_encoding(response))) page_json = json.loads(page.content)
self.assertIn('success', j) self.assertEquals(page_json['status'], 'bad-comp')
self.assertEquals(j['success'], 1)
test_data['force'] = 1 test_data['force'] = 1
response = self.send_booking_json(test_data) page = self.app.post(url, params=test_data, user=self.user)
self.assertEqual(response.status_code, 200) self.assertEqual(200, page.status_code)
j = json.loads(response.content.decode(get_response_encoding(response))) page_json = json.loads(page.content)
self.assertIn('success', j) self.assertEquals(page_json['status'], 'ok')
self.assertEquals(j['success'], 2)
def test_book_good(self): def test_book_good(self):
test_data = { test_data = {
"duck_id": self.duck.pk, "competence": self.comp_good.pk
"comp_id": self.comp_good.pk
} }
self.client.login(username = self.username, password = self.password)
url = '/api/v1/ducks/%d/book/' % self.duck.pk
# Book the duck # Book the duck
response = self.send_booking_json(test_data) page = self.app.post(url, params=test_data, user=self.user)
self.assertEqual(response.status_code, 200) self.assertEquals(200, page.status_code)
j = json.loads(response.content.decode(get_response_encoding(response))) page_json = json.loads(page.content)
self.assertIn('success', j) self.assertEqual(page_json['status'], 'ok')
self.assertEqual(j['success'], 2)
# Try to book again, it should fail # Try to book again, it should fail
response = self.send_booking_json(test_data) page = self.app.post(url, params=test_data, user=self.user)
self.assertEqual(response.status_code, 200) self.assertEqual(200, page.status_code)
j = json.loads(response.content.decode(get_response_encoding(response))) page_json = json.loads(page.content)
self.assertIn('success', j) self.assertEqual('already-booked', page_json['status'])
self.assertEqual(j['success'], 0)
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'])