'Test layer replace functionality'

def test_layer_replace(self):

    vector_file = os.path.join(gisdata.VECTOR_DATA, 'san_andres_y_providencia_administrative.shp')
    vector_layer = file_upload(vector_file, overwrite=True)
    raster_file = os.path.join(gisdata.RASTER_DATA, 'test_grid.tif')
    raster_layer = file_upload(raster_file, overwrite=True)
    self.client.login(username='admin', password='admin')
    raster_replace_url = reverse('layer_replace', args=[raster_layer.service_typename])
    response = self.client.get(raster_replace_url)
    self.assertEquals(response.status_code, 200)
    self.assertEquals(response.context['is_featuretype'], False)
    vector_replace_url = reverse('layer_replace', args=[vector_layer.service_typename])
    response = self.client.get(vector_replace_url)
    self.assertEquals(response.status_code, 200)
    self.assertEquals(response.context['is_featuretype'], True)
    response = self.client.post(vector_replace_url, {'base_file': open(raster_file, 'rb')})
    self.assertEquals(response.status_code, 400)
    response_dict = json.loads(response.content)
    self.assertEquals(response_dict['success'], False)
    new_vector_file = os.path.join(gisdata.VECTOR_DATA, 'san_andres_y_providencia_poi.shp')
    (layer_path, __) = os.path.splitext(new_vector_file)
    layer_base = open((layer_path + '.shp'), 'rb')
    layer_dbf = open((layer_path + '.dbf'), 'rb')
    layer_shx = open((layer_path + '.shx'), 'rb')
    layer_prj = open((layer_path + '.prj'), 'rb')
    response = self.client.post(vector_replace_url, {'base_file': layer_base, 'dbf_file': layer_dbf, 'shx_file': layer_shx, 'prj_file': layer_prj})
    self.assertEquals(response.status_code, 200)
    response_dict = json.loads(response.content)
    self.assertEquals(response_dict['success'], True)
    new_vector_layer = Layer.objects.get(pk=vector_layer.pk)
    self.assertNotEqual(vector_layer.bbox_x0, new_vector_layer.bbox_x0)
    self.assertNotEqual(vector_layer.bbox_x1, new_vector_layer.bbox_x1)
    self.assertNotEqual(vector_layer.bbox_y0, new_vector_layer.bbox_y0)
    self.assertNotEqual(vector_layer.bbox_y1, new_vector_layer.bbox_y1)
    self.client.logout()
    self.client.login(username='norman', password='norman')
    response = self.client.post(vector_replace_url, {'base_file': layer_base, 'dbf_file': layer_dbf, 'shx_file': layer_shx, 'prj_file': layer_prj})
    self.assertEquals(response.status_code, 401)
