Archived
Adds tests and production code for updating an address though the member api, additionally adds fixtures and functions to load them
This commit is contained in:
1 parent
32ed0c17e7
commit
2078d323ed
7 files changed
+137
-25
No files matched your search
+1
-4
@@ -5,11 +5,8 @@ from dusken.models import Member, Institution, Address, Country
|
||||
class MemberAdmin(admin.ModelAdmin):
|
||||
exclude = ( 'password', 'last_login', )
|
||||
|
||||
class InstitutionAdmin(admin.ModelAdmin):
|
||||
pass
|
||||
|
||||
admin.site.unregister(User)
|
||||
admin.site.register(Member, MemberAdmin)
|
||||
admin.site.register(Institution, InstitutionAdmin)
|
||||
admin.site.register(Institution)
|
||||
admin.site.register(Address)
|
||||
admin.site.register(Country)
|
||||
+25
-7
@@ -72,18 +72,36 @@ class MemberResource(ModelResource):
|
||||
"""
|
||||
Catches POST and PATCH requests and intercepts data.
|
||||
"""
|
||||
if bundle.obj.user_ptr_id is not None: # happens only if user exists
|
||||
for key, value in bundle.data.items():
|
||||
if key == 'username':
|
||||
if value != bundle.obj.username:
|
||||
# We can't change username.
|
||||
raise ImmediateHttpResponse(HttpForbidden("You can't change your username."))
|
||||
return bundle
|
||||
# Check if the user exists and return an error when attempting to change the username
|
||||
if bundle.obj.user_ptr_id is not None and not bundle.data['username'] == bundle.obj.username:
|
||||
raise ImmediateHttpResponse(HttpForbidden("You can't change your username."))
|
||||
return bundle
|
||||
|
||||
def obj_update(self, bundle, request, **kwargs):
|
||||
if bundle.data.get('password') is not None:
|
||||
bundle.obj.set_password(bundle.data['password'])
|
||||
bundle.obj.save()
|
||||
|
||||
if 'address' in bundle.data:
|
||||
new_address = bundle.data.get('address')
|
||||
address = bundle.obj.address
|
||||
|
||||
if 'street_address' in new_address:
|
||||
address.street_address = new_address['street_address']
|
||||
if 'city' in new_address:
|
||||
address.city = new_address['city']
|
||||
if 'postal_code' in new_address:
|
||||
address.postal_code = new_address['postal_code']
|
||||
if 'country' in new_address:
|
||||
try:
|
||||
country = Country.objects.get(name=new_address['country'])
|
||||
except Country.DoesNotExist, e:
|
||||
raise ImmediateHttpResponse(HttpForbidden('The country "{}" does not exist.'.format(new_address['country'])))
|
||||
else:
|
||||
address.country = country
|
||||
|
||||
address.save()
|
||||
|
||||
return super(MemberResource, self).obj_update(bundle, request, **kwargs)
|
||||
|
||||
def dehydrate(self, bundle):
|
||||
|
||||
+89
-14
@@ -1,7 +1,9 @@
|
||||
import logging
|
||||
|
||||
from support.test import ResourceTestCase
|
||||
from dusken.models import Member
|
||||
from dusken.models import Address, Country, Member
|
||||
|
||||
from utils.tests import test_fixtures
|
||||
|
||||
class MemberTestBase(ResourceTestCase):
|
||||
|
||||
@@ -19,7 +21,7 @@ class MemberTestBase(ResourceTestCase):
|
||||
|
||||
# URIs to the member api
|
||||
self.all_members_url = '/api/v1/member/'
|
||||
self.member_url = self.all_members_url + '{}/'.format(self.member.pk)
|
||||
self.member_url = self.all_members_url + '{}/'
|
||||
|
||||
|
||||
class MemberTest(MemberTestBase):
|
||||
@@ -49,7 +51,7 @@ class MemberTest(MemberTestBase):
|
||||
"""
|
||||
Tests that we can get a premade user correctly.
|
||||
"""
|
||||
resp = self.api_client.get(self.member_url, format='json')
|
||||
resp = self.api_client.get(self.member_url.format(self.member.pk), format='json')
|
||||
self.assertValidJSONResponse(resp)
|
||||
|
||||
# Check if the returned data is correct:
|
||||
@@ -127,7 +129,7 @@ class MemberTest(MemberTestBase):
|
||||
'phone_number' : 90541242
|
||||
}
|
||||
|
||||
resp = self.api_client.patch(self.member_url,
|
||||
resp = self.api_client.patch(self.member_url.format(self.member.pk),
|
||||
format='json',
|
||||
data=data)
|
||||
self.assertHttpAccepted(resp)
|
||||
@@ -146,7 +148,7 @@ class MemberTest(MemberTestBase):
|
||||
newusername = oldusername + 'lolzorz'
|
||||
|
||||
data = { 'username' : newusername }
|
||||
resp = self.api_client.patch(self.member_url, format='json', data=data)
|
||||
resp = self.api_client.patch(self.member_url.format(self.member.pk), format='json', data=data)
|
||||
self.assertHttpForbidden(resp)
|
||||
|
||||
try:
|
||||
@@ -161,20 +163,93 @@ class MemberTest(MemberTestBase):
|
||||
pass # ...the test.
|
||||
|
||||
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||
class MemberAdressTest(MemberTestBase):
|
||||
class MemberAddressTest(MemberTestBase):
|
||||
|
||||
def test_member_update_address(self):
|
||||
fixtures = test_fixtures()
|
||||
|
||||
def setUp(self):
|
||||
super(MemberAddressTest, self).setUp()
|
||||
|
||||
self.user_id = 1
|
||||
|
||||
member = Member.objects.get(id=self.user_id)
|
||||
member.address = Address.objects.get(id=1)
|
||||
member.save()
|
||||
|
||||
def test_member_address_update_street(self):
|
||||
data = {
|
||||
"address": {
|
||||
"street" : "somestreet",
|
||||
}
|
||||
"street_address" : "somestreet 2",
|
||||
}
|
||||
}
|
||||
|
||||
response = self.api_client.patch(self.member_url,
|
||||
format='json',
|
||||
data=data)
|
||||
response = self.api_client.patch(
|
||||
self.member_url.format(self.user_id),
|
||||
format='json',
|
||||
data=data)
|
||||
|
||||
self.assertHttpAccepted(response)
|
||||
self.assertEquals('somestreet 2', Member.objects.get(id=self.user_id).address.street_address)
|
||||
|
||||
def test_member_address_update_city(self):
|
||||
data = {
|
||||
"address": {
|
||||
"city": "Amsterdam"
|
||||
}
|
||||
}
|
||||
|
||||
response = self.api_client.patch(
|
||||
self.member_url.format(self.user_id),
|
||||
format='json',
|
||||
data=data)
|
||||
|
||||
self.assertHttpAccepted(response)
|
||||
self.assertEquals('Amsterdam', Member.objects.get(id=self.user_id).address.city)
|
||||
|
||||
def test_member_address_update_postcode(self):
|
||||
data = {
|
||||
"address": {
|
||||
"postal_code": "0977RT"
|
||||
}
|
||||
}
|
||||
|
||||
response = self.api_client.patch(
|
||||
self.member_url.format(self.user_id),
|
||||
format='json',
|
||||
data=data)
|
||||
|
||||
self.assertHttpAccepted(response)
|
||||
self.assertEquals('0977RT', Member.objects.get(id=self.user_id).address.postal_code)
|
||||
|
||||
def test_member_address_update_country(self):
|
||||
data = {
|
||||
"address": {
|
||||
"country": "Netherlands"
|
||||
}
|
||||
}
|
||||
|
||||
response = self.api_client.patch(
|
||||
self.member_url.format(self.user_id),
|
||||
format='json',
|
||||
data=data)
|
||||
|
||||
self.assertHttpAccepted(response)
|
||||
self.assertEquals(
|
||||
Country.objects.get(name="Netherlands"),
|
||||
Member.objects.get(id=self.user_id).address.country)
|
||||
|
||||
def test_member_adress_update_unknown_country(self):
|
||||
data = {
|
||||
"address": {
|
||||
"country": "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
response = self.api_client.patch(
|
||||
self.member_url.format(self.user_id),
|
||||
format='json',
|
||||
data=data)
|
||||
|
||||
self.assertHttpForbidden(response)
|
||||
self.assertEquals('The country "unknown" does not exist.', response.content)
|
||||
|
||||
print response
|
||||
print Member.objects.get(username="robert").address
|
||||
Whitespace-only changes.
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
@@ -0,0 +1,18 @@
|
||||
import os.path
|
||||
from django.core.management import call_command
|
||||
|
||||
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||
def test_fixtures():
|
||||
''' Returns a list with the locations of all defined fixtures. '''
|
||||
return [os.path.join('dusken', 'fixtures', 'address.json'),
|
||||
os.path.join('dusken', 'fixtures', 'country.json'),
|
||||
os.path.join('dusken', 'fixtures', 'institution.json'),
|
||||
]
|
||||
|
||||
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||
def load_test_fixtures():
|
||||
''' Explicitly load all defined fixtures. This can be useful when
|
||||
you only want to load the fixtures for some testcases, but not
|
||||
for all tests in a test class.'''
|
||||
for f in test_fixtures():
|
||||
call_command('loaddata', f, verbosity=0)
|
||||
@@ -0,0 +1 @@
|
||||
# Create your views here.
|
||||
Reference in new issue
Block a user