Skip to content

Commit

Permalink
switched to reverse url matching
Browse files Browse the repository at this point in the history
  • Loading branch information
simod committed Oct 13, 2012
1 parent a2444a1 commit 086de6f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 49 deletions.
2 changes: 1 addition & 1 deletion geonode/layers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ def publishing(self):
return self._publishing_cache

def get_absolute_url(self):
return "/data/%s" % (self.typename)
return reverse('layer_detail', args=(self.typename))

def __str__(self):
return "%s Layer" % self.typename
Expand Down
41 changes: 21 additions & 20 deletions geonode/layers/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from django.template.loader import get_template
from django.forms import ValidationError
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from agon_ratings.models import OverallRating

import geonode.layers.utils
Expand Down Expand Up @@ -120,7 +121,7 @@ def test_set_layer_permissions(self):
# Get a layer to work with
layer = Layer.objects.all()[0]

# FIXME Test a comprehensive set of permisssions specifications
# FIXME Test a comprehensive set of permissions specifications

# Set the Permissions

Expand Down Expand Up @@ -152,27 +153,27 @@ def test_ajax_layer_permissions(self):
c = Client()

# Test that an invalid layer.typename is handled for properly
response = c.post("/data/%s/permissions" % invalid_layer_typename,
response = c.post(reverse('layer_permissions', args=(invalid_layer_typename,)),
data=json.dumps(self.perm_spec),
content_type="application/json")
self.assertEquals(response.status_code, 404)

# Test that POST is required
response = c.get("/data/%s/permissions" % valid_layer_typename)
response = c.get(reverse('layer_permissions', args=(valid_layer_typename,)))
self.assertEquals(response.status_code, 405)

# Test that a user is required to have maps.change_layer_permissions

# First test un-authenticated
response = c.post("/data/%s/permissions" % valid_layer_typename,
response = c.post(reverse('layer_permissions', args=(valid_layer_typename,)),
data=json.dumps(self.perm_spec),
content_type="application/json")
self.assertEquals(response.status_code, 401)

# Next Test with a user that does NOT have the proper perms
logged_in = c.login(username='bobby', password='bob')
self.assertEquals(logged_in, True)
response = c.post("/data/%s/permissions" % valid_layer_typename,
response = c.post(reverse('layer_permissions', args=(valid_layer_typename,)),
data=json.dumps(self.perm_spec),
content_type="application/json")
self.assertEquals(response.status_code, 401)
Expand All @@ -181,7 +182,7 @@ def test_ajax_layer_permissions(self):
logged_in = c.login(username='admin', password='admin')
self.assertEquals(logged_in, True)

response = c.post("/data/%s/permissions" % valid_layer_typename,
response = c.post(reverse('layer_permissions', args=(valid_layer_typename,)),
data=json.dumps(self.perm_spec),
content_type="application/json")

Expand Down Expand Up @@ -220,19 +221,19 @@ def test_layer_acls(self):
}

c = Client()
response = c.get('/data/acls', **valid_auth_headers)
response = c.get(reverse('layer_acls'), **valid_auth_headers)
response_json = json.loads(response.content)
self.assertEquals(expected_result, response_json)

# Test that requesting when supplying invalid credentials returns the appropriate error code
response = c.get('/data/acls', **invalid_auth_headers)
response = c.get(reverse('layer_acls'), **invalid_auth_headers)
self.assertEquals(response.status_code, 401)

# Test logging in using Djangos normal auth system
c.login(username='admin', password='admin')

# Basic check that the returned content is at least valid json
response = c.get("/data/acls")
response = c.get(reverse('layer_acls'))
response_json = json.loads(response.content)

# TODO Lots more to do here once jj0hns0n understands the ACL system better
Expand Down Expand Up @@ -262,20 +263,20 @@ def test_perms_info(self):
def test_data(self):
'''/data/ -> Test accessing the data page'''
c = Client()
response = c.get('/data/')
response = c.get(reverse('layer_browse'))
self.failUnlessEqual(response.status_code, 200)

def test_describe_data_2(self):
'''/data/base:CA/metadata -> Test accessing the description of a layer '''
self.assertEqual(2, User.objects.all().count())
c = Client()
response = c.get('/data/base:CA/metadata')
response = c.get(reverse('layer_metadata', args=('base:CA',)))
# Since we are not authenticated, we should not be able to access it
self.failUnlessEqual(response.status_code, 302)
# but if we log in ...
c.login(username='admin', password='admin')
# ... all should be good
response = c.get('/data/base:CA/metadata')
response = c.get(reverse('layer_metadata', args=('base:CA',)))
self.failUnlessEqual(response.status_code, 200)

# Layer Tests
Expand All @@ -285,37 +286,37 @@ def test_upload_layer(self):
c = Client()

# Test redirection to login form when not logged in
response = c.get("/data/upload")
response = c.get(reverse('layer_upload'))
self.assertEquals(response.status_code,302)

# Test return of upload form when logged in
c.login(username="bobby", password="bob")
response = c.get("/data/upload")
response = c.get(reverse('layer_upload'))
self.assertEquals(response.status_code,200)

def test_search(self):
'''/data/search/ -> Test accessing the data search page'''
c = Client()
response = c.get('/data/search/')
response = c.get(reverse('layer_search_page'))
self.failUnlessEqual(response.status_code, 200)

def test_search_api(self):
'''/data/search/api -> Test accessing the data search api JSON'''
c = Client()
response = c.get('/data/search/api')
response = c.get(reverse('layer_search_api'))
self.failUnlessEqual(response.status_code, 200)

def test_describe_data(self):
'''/data/base:CA/metadata -> Test accessing the description of a layer '''
self.assertEqual(2, User.objects.all().count())
c = Client()
response = c.get('/data/base:CA/metadata')
response = c.get(reverse('layer_metadata', args=('base:CA',)))
# Since we are not authenticated, we should not be able to access it
self.failUnlessEqual(response.status_code, 302)
# but if we log in ...
c.login(username='admin', password='admin')
# ... all should be good
response = c.get('/data/base:CA/metadata')
response = c.get(reverse('layer_metadata', args=('base:CA',)))
self.failUnlessEqual(response.status_code, 200)

def test_layer_attributes(self):
Expand All @@ -339,7 +340,7 @@ def test_layer_remove(self):
"""Test layer remove functionality
"""
layer = Layer.objects.all()[0]
url = '/data/%s/remove' % layer.typename
url = reverse('layer_remove', args=(layer.typename,))

c = Client()

Expand Down Expand Up @@ -701,7 +702,7 @@ def test_rating_layer_remove(self):
"""
#Get the layer to work with
layer = Layer.objects.all()[0]
url = '/data/%s/remove' % layer.typename
url = reverse('layer_remove', args=(layer.typename,))
layer_id = layer.id

#Create the rating with the correct content type
Expand Down
2 changes: 1 addition & 1 deletion geonode/layers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def layer_metadata(request, layername, template='layers/layer_metadata.html'):
the_layer.metadata_author = new_author
the_layer.keywords.add(*new_keywords)
the_layer.save()
return HttpResponseRedirect("/data/" + layer.typename)
return HttpResponseRedirect(reverse('layer_detail', args=(layer.typename,)))

if poc.user is None:
poc_form = ContactForm(instance=poc, prefix="poc")
Expand Down
55 changes: 28 additions & 27 deletions geonode/maps/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ def test_map_json(self):
c = Client()

# Test that saving a map when not logged in gives 401
response = c.put("/maps/1/data",data=self.viewer_config,content_type="text/json")
response = c.put(reverse('map_json', args=('1',)),data=self.viewer_config,content_type="text/json")
self.assertEqual(response.status_code, 401)

c.login(username=self.user, password=self.passwd)
response = c.put("/maps/1/data",data=self.viewer_config_alternative,content_type="text/json")
response = c.put(reverse('map_json', args=('1',)),data=self.viewer_config_alternative,content_type="text/json")
self.assertEqual(response.status_code, 204)

map_obj = Map.objects.get(id=1)
Expand All @@ -140,12 +140,12 @@ def test_map_save(self):
c = Client()

# Test that saving a map when not logged in gives 401
response = c.post("/maps/",data=self.viewer_config,content_type="text/json")
response = c.post(reverse('maps_browse'),data=self.viewer_config,content_type="text/json")
self.assertEqual(response.status_code,401)

# Test successful new map creation
c.login(username=self.user, password=self.passwd)
response = c.post("/maps/",data=self.viewer_config,content_type="text/json")
response = c.post(reverse('maps_browse'),data=self.viewer_config,content_type="text/json")
self.assertEquals(response.status_code,201)
map_id = int(response['Location'].split('/')[-1])
c.logout()
Expand All @@ -159,15 +159,15 @@ def test_map_save(self):

# Test an invalid map creation request
c.login(username=self.user, password=self.passwd)
response = c.post("/maps/",data="not a valid viewer config",content_type="text/json")
response = c.post(reverse('maps_browse'),data="not a valid viewer config",content_type="text/json")
self.assertEquals(response.status_code,400)
c.logout()

def test_map_fetch(self):
"""/maps/[id]/data -> Test fetching a map in JSON"""
map_obj = Map.objects.get(id=1)
c = Client()
response = c.get("/maps/%s/data" % map_obj.id)
response = c.get(reverse('map_json', args=(map_obj.id,)))
self.assertEquals(response.status_code, 200)
cfg = json.loads(response.content)
self.assertEquals(cfg["about"]["abstract"], self.default_abstract)
Expand All @@ -189,31 +189,31 @@ def is_wms_layer(x):
def test_newmap_to_json(self):
""" Make some assertions about the data structure produced for serialization
to a new JSON map configuration"""
response = Client().get("/maps/new/data")
response = Client().get(reverse('new_map_json'))
cfg = json.loads(response.content)
self.assertEquals(cfg['defaultSourceType'], "gxp_wmscsource")

def test_map_details(self):
"""/maps/1 -> Test accessing the map browse view function"""
map_obj = Map.objects.get(id=1)
c = Client()
response = c.get("/maps/%s" % map_obj.id)
response = c.get(reverse('map_detail', args=(map_obj.id,)))
self.assertEquals(response.status_code,200)

def test_new_map_without_layers(self):
# TODO: Should this test have asserts in it?
client = Client()
client.get("/maps/new")
client.get(reverse('new_map'))

def test_new_map_with_layer(self):
client = Client()
layer = Layer.objects.all()[0]
client.get("/maps/new?layer=" + layer.typename)
client.get(reverse('new_map') +'?layer=' + layer.typename)

def test_new_map_with_empty_bbox_layer(self):
client = Client()
layer = Layer.objects.all()[0]
client.get("/maps/new?layer=" + layer.typename)
client.get(reverse('new_map') +'?layer=' + layer.typename)

def test_ajax_map_permissions(self):
"""Verify that the ajax_layer_permissions view is behaving as expected
Expand Down Expand Up @@ -275,12 +275,12 @@ def test_map_metadata(self):

# Test successful new map creation
c.login(username=self.user, password=self.passwd)
response = c.post("/maps/",data=self.viewer_config,content_type="text/json")
response = c.post(reverse('maps_browse'),data=self.viewer_config,content_type="text/json")
self.assertEquals(response.status_code,201)
map_id = int(response['Location'].split('/')[-1])
c.logout()

url = '/maps/%s/metadata' % map_id
url = reverse('map_json', args=(map_id,))

# test unauthenticated user to modify map metadata
response = c.post(url)
Expand Down Expand Up @@ -313,12 +313,12 @@ def test_map_remove(self):

# Test successful new map creation
c.login(username=self.user, password=self.passwd)
response = c.post("/maps/",data=self.viewer_config,content_type="text/json")
response = c.post(reverse('maps_browse'),data=self.viewer_config,content_type="text/json")
self.assertEquals(response.status_code,201)
map_id = int(response['Location'].split('/')[-1])
c.logout()

url = '/maps/%s/remove' % map_id
url = reverse('map_remove', args=(map_id,))

# test unauthenticated user to remove map
response = c.post(url)
Expand Down Expand Up @@ -360,13 +360,13 @@ def test_map_embed(self):

# Test successful new map creation
c.login(username=self.user, password=self.passwd)
response = c.post("/maps/",data=self.viewer_config,content_type="text/json")
response = c.post(reverse('maps_browse'),data=self.viewer_config,content_type="text/json")
self.assertEquals(response.status_code,201)
map_id = int(response['Location'].split('/')[-1])
c.logout()

url = '/maps/%s/embed' % map_id
url_no_id = '/maps/embed/'
url = reverse('map_embed', args=(map_id,))
url_no_id = reverse('map_embed')

# Now test with a map id
c.login(username=self.user, password=self.passwd)
Expand Down Expand Up @@ -397,12 +397,12 @@ def test_map_view(self):

# Test successful new map creation
c.login(username=self.user, password=self.passwd)
response = c.post("/maps/",data=self.viewer_config,content_type="text/json")
response = c.post(reverse('maps_browse'),data=self.viewer_config,content_type="text/json")
self.assertEquals(response.status_code,201)
map_id = int(response['Location'].split('/')[-1])
c.logout()

url = '/maps/%s/view' % map_id
url = reverse('map_view', args=(map_id,))

# test unauthenticated user to view map
response = c.get(url)
Expand Down Expand Up @@ -444,7 +444,7 @@ def test_new_map_config(self):

c = Client()

url = '/maps/new/data'
url = reverse('new_map_json')

# Test GET method with COPY
response = c.get(url,{'copy': map_id})
Expand Down Expand Up @@ -492,7 +492,7 @@ def test_maps_search_page(self):
"""
c = Client()

url = '/maps/search/?'
url = reverse('maps_search') + '?'

# Test GET method
response = c.get(url,{'keyword':'keyword'})
Expand All @@ -518,15 +518,15 @@ def test_maps_search(self):

# Test successful new map creation
c.login(username=self.user, password=self.passwd)
response = c.post("/maps/",data=self.viewer_config,content_type="text/json")
response = c.post(reverse('maps_browse'),data=self.viewer_config,content_type="text/json")
self.assertEquals(response.status_code,201)
map_id = int(response['Location'].split('/')[-1])
response = c.post("/maps/",data=self.viewer_config_alternative,content_type="text/json")
response = c.post(reverse('maps_browse'),data=self.viewer_config_alternative,content_type="text/json")
self.assertEquals(response.status_code,201)
map_id_2 = int(response['Location'].split('/')[-1])
c.logout()

url = '/maps/search/api/?'
url = reverse('maps_search_api') + '?'

# Test GET method
response = c.get(url, {'q': '', 'start': 1, 'limit': '', 'sort': '', 'dir': ''}, content_type="text/json")
Expand All @@ -551,15 +551,16 @@ def test_rating_map_remove(self):
c.login(username=self.user, password=self.passwd)

#Create the map
response = c.post("/maps/",data=self.viewer_config,content_type="text/json")
response = c.post(reverse('maps_browse'),data=self.viewer_config,content_type="text/json")
self.assertEquals(response.status_code,201)
map_id = int(response['Location'].split('/')[-1])

#Create the rating with the correct content type
ctype = ContentType.objects.get(model='map')
OverallRating.objects.create(category=1,object_id=map_id,content_type=ctype, rating=3)

#Remove the map
response = c.post("/maps/%s/remove" % map_id)
response = c.post(reverse('map_remove', args=(map_id,)))
self.assertEquals(response.status_code,302)

#Check there are no ratings matching the removed map
Expand Down

0 comments on commit 086de6f

Please sign in to comment.