Skip to content

Commit

Permalink
Bugfix/#682 (#736)
Browse files Browse the repository at this point in the history
* Bugfix/#682
* Minor test code improvements
  • Loading branch information
manoramahp authored and oliverroick committed Sep 28, 2016
1 parent a3f5dee commit 153f52d
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 46 deletions.
30 changes: 24 additions & 6 deletions cadasta/organization/tests/test_views_default_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ def setup_template_context(self):
'geojson': '{"type": "FeatureCollection", "features": []}',
'is_superuser': False,
'is_administrator': False,
'has_content': False,
'num_locations': 0,
'num_parties': 0,
'num_resources': 0,
'is_allowed_add_location': False,
'is_allowed_add_resource': False
}

def setup_url_kwargs(self):
Expand All @@ -235,8 +241,11 @@ def test_get_with_superuser(self):
self.user.assign_policies(superuser_role)
response = self.request(user=self.user)
assert response.status_code == 200
assert response.content == self.render_content(is_superuser=True,
is_administrator=True)
expected = self.render_content(is_superuser=True,
is_administrator=True,
is_allowed_add_location=True,
is_allowed_add_resource=True)
assert response.content == expected

def test_get_with_org_admin(self):
OrganizationRole.objects.create(
Expand All @@ -246,7 +255,10 @@ def test_get_with_org_admin(self):
)
response = self.request(user=self.user)
assert response.status_code == 200
assert response.content == self.render_content(is_administrator=True)
expected = self.render_content(is_administrator=True,
is_allowed_add_location=True,
is_allowed_add_resource=True)
assert response.content == expected

def test_get_non_existent_project(self):
with pytest.raises(Http404):
Expand Down Expand Up @@ -333,8 +345,11 @@ def test_get_private_project_with_superuser(self):
self.user.assign_policies(self.superuser_role)
response = self.request(user=self.user)
assert response.status_code == 200
assert response.content == self.render_content(is_superuser=True,
is_administrator=True)
expected = self.render_content(is_superuser=True,
is_administrator=True,
is_allowed_add_location=True,
is_allowed_add_resource=True)
assert response.content == expected

def test_get_archived_project_with_unauthorized_user(self):
self.project.archived = True
Expand Down Expand Up @@ -365,7 +380,10 @@ def test_get_archived_project_with_org_admin(self):
self.project.save()
response = self.request(user=org_admin)
assert response.status_code == 200
assert response.content == self.render_content(is_administrator=True)
expected = self.render_content(is_administrator=True,
is_allowed_add_location=True,
is_allowed_add_resource=True)
assert response.content == expected

def test_get_with_overview_stats(self):
su = SpatialUnitFactory.create(project=self.project)
Expand Down
5 changes: 5 additions & 0 deletions cadasta/organization/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ def is_administrator(self):
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['is_administrator'] = self.is_administrator
user = self.request.user
context['is_allowed_add_location'] = user.has_perm('spatial.create',
self.get_project())
context['is_allowed_add_resource'] = user.has_perm('resource.add',
self.get_project())
return context


Expand Down
28 changes: 19 additions & 9 deletions cadasta/resources/tests/test_views_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def setup_template_context(self, resources=None):
resource_count != self.project.resources.count()
),
'resource_list': resource_list,
'is_allowed_add_resource': True
}

def test_get_list(self):
Expand All @@ -132,20 +133,23 @@ def test_get_list_with_unattached_resource_using_nonunarchiver(self):
response = self.request(user=self.user)
assert response.status_code == 200
context = self.setup_template_context(resources=resources)
assert response.content == self.render_content(**context)
expected = self.render_content(**context)
assert response.content == expected

def test_get_list_with_archived_resource_using_unarchiver(self):
assign_permissions(self.user)
ResourceFactory.create(project=self.project, archived=True)
resources = Resource.objects.filter(project=self.project)
response = self.request(user=self.user)
context = self.setup_template_context(resources=resources)
assert response.content == self.render_content(**context)
expected = self.render_content(**context)
assert response.content == expected

def test_get_with_unauthorized_user(self):
response = self.request(user=UserFactory.create())
assert response.status_code == 200
context = self.setup_template_context(resources=[])
context['is_allowed_add_resource'] = False
assert response.content == self.render_content(**context)

def test_get_with_unauthenticated_user(self):
Expand Down Expand Up @@ -184,7 +188,9 @@ def setup_url_kwargs(self):
def setup_template_context(self):
form = AddResourceFromLibraryForm(content_object=self.project,
project_id=self.project.id)
return {'object': self.project, 'form': form}
return {'object': self.project,
'form': form,
'is_allowed_add_resource': True}

def setup_post_data(self):
return {
Expand Down Expand Up @@ -288,7 +294,9 @@ def setup_url_kwargs(self):

def setup_template_context(self):
form = ResourceForm()
return {'object': self.project, 'form': form}
return {'object': self.project,
'form': form,
'is_allowed_add_resource': True}

def setup_post_data(self):
storage = FakeS3Storage()
Expand Down Expand Up @@ -445,6 +453,7 @@ def setup_template_context(self):
'id': self.tenurerel_attachment.id,
},
],
'is_allowed_add_resource': True
}

def setup_url_kwargs(self):
Expand Down Expand Up @@ -514,7 +523,8 @@ def setup_template_context(self):
'project': self.project.slug,
'resource': self.resource.id,
}
)
),
'is_allowed_add_resource': True
}

def setup_post_data(self):
Expand All @@ -538,17 +548,17 @@ def test_get_form_with_next_query_parameter(self):
response = self.request(user=self.user,
get_data={'next': '/organizations/'})
assert response.status_code == 200
assert response.content == self.render_content(
cancel_url='/organizations/#resources')
expected = self.render_content(cancel_url='/organizations/#resources')
assert response.content == expected

def test_get_form_with_location_next_query_parameter(self):
url = ('https://example.com/organizations/sample-org/'
'projects/sample-proj/records/'
'locations/jvzsiszjzrbpecm69549u2z5/')
response = self.request(user=self.user, get_data={'next': url})
assert response.status_code == 200
assert response.content == self.render_content(
cancel_url=url + '#resources')
expected = self.render_content(cancel_url=url + '#resources')
assert response.content == expected

def test_get_non_existent_project(self):
with pytest.raises(Http404):
Expand Down
48 changes: 19 additions & 29 deletions cadasta/spatial/tests/test_views_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def setup_template_context(self):
return {
'object': self.project,
'object_list': self.locations,
'geojson': geojson
'geojson': geojson,
'is_allowed_add_location': True
}

def setup_url_kwargs(self):
Expand All @@ -96,7 +97,8 @@ def test_get_with_unauthorized_user(self):
user = UserFactory.create()
response = self.request(user=user)
assert response.status_code == 200
assert response.content == self.expected_content
expected = self.render_content(is_allowed_add_location=False)
assert response.content == expected

def test_get_with_unauthenticated_user(self):
response = self.request()
Expand Down Expand Up @@ -149,7 +151,8 @@ def setup_template_context(self):
'selector': self.project.current_questionnaire}
)
),
'geojson': '{"type": "FeatureCollection", "features": []}'
'geojson': '{"type": "FeatureCollection", "features": []}',
'is_allowed_add_location': True
}

def setup_url_kwargs(self):
Expand Down Expand Up @@ -264,7 +267,8 @@ def setup_template_context(self):
'object': self.project,
'location': self.location,
'geojson': '{"type": "FeatureCollection", "features": []}',
'attributes': (('Test field', 'test', ), )
'attributes': (('Test field', 'test', ), ),
'is_allowed_add_location': True
}

def setup_url_kwargs(self):
Expand Down Expand Up @@ -341,7 +345,8 @@ def setup_template_context(self):
return {'object': self.project,
'location': self.location,
'form': forms.LocationForm(instance=self.location),
'geojson': '{"type": "FeatureCollection", "features": []}'}
'geojson': '{"type": "FeatureCollection", "features": []}',
'is_allowed_add_location': True}

def setup_url_kwargs(self):
return {
Expand Down Expand Up @@ -445,7 +450,8 @@ def setup_models(self):
def setup_template_context(self):
return {'object': self.project,
'location': self.location,
'geojson': '{"type": "FeatureCollection", "features": []}'}
'geojson': '{"type": "FeatureCollection", "features": []}',
'is_allowed_add_location': True}

def setup_url_kwargs(self):
return {
Expand Down Expand Up @@ -559,7 +565,8 @@ def setup_template_context(self):
return {'object': self.project,
'location': self.location,
'form': form,
'geojson': '{"type": "FeatureCollection", "features": []}'}
'geojson': '{"type": "FeatureCollection", "features": []}',
'is_allowed_add_location': True}

def setup_url_kwargs(self):
return {
Expand Down Expand Up @@ -680,7 +687,8 @@ def setup_template_context(self):
return {'object': self.project,
'location': self.location,
'form': form,
'geojson': '{"type": "FeatureCollection", "features": []}'}
'geojson': '{"type": "FeatureCollection", "features": []}',
'is_allowed_add_location': True}

def setup_post_data(self):
path = os.path.dirname(settings.BASE_DIR)
Expand Down Expand Up @@ -839,27 +847,8 @@ def setup_template_context(self):
),
'geojson': json.dumps(SpatialUnitGeoJsonSerializer(
[self.spatial_unit], many=True).data),
'is_allowed_add_location': True
}
# return {
# 'object': self.project,
# 'location': self.spatial_unit,
# 'form': forms.TenureRelationshipForm(
# project=self.project,
# spatial_unit=self.spatial_unit,
# schema_selectors=(
# {'name': 'organization',
# 'value': self.project.organization,
# 'selector': self.project.organization.id},
# {'name': 'project',
# 'value': self.project,
# 'selector': self.project.id},
# {'name': 'questionnaire',
# 'value': self.project.current_questionnaire,
# 'selector': self.project.current_questionnaire}
# )),
# 'geojson': json.dumps(SpatialUnitGeoJsonSerializer(
# [self.spatial_unit], many=True).data)
# }

def setup_url_kwargs(self):
return {
Expand Down Expand Up @@ -998,7 +987,8 @@ def test_post_with_authorized_invalid_existing_party_data(self):
)
)
assert response.status_code == 200
assert response.content == self.render_content(form=form)
expected = self.render_content(form=form)
assert response.content == expected
assert TenureRelationship.objects.count() == 0
assert Party.objects.count() == 1
assert Party.objects.first().name == party.name
Expand Down
4 changes: 3 additions & 1 deletion cadasta/templates/organization/project_dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ <h4>Contact information</h4>
</dl>
{% endif %}
<div class="divider-thick"></div>
{% if has_content %}
{% if has_content or not is_allowed_add_location %}
<h4>{% trans "Project stats" %}</h4>
<ul class="list-inline stats">
<li>
Expand Down Expand Up @@ -106,11 +106,13 @@ <h4>{% trans "Project stats" %}</h4>
<h3 style="text-transform: capitalize;">{% trans "Congratulations!" %}</h3>
<p>{% trans "You have successfully created your project. You're now ready to add your first location." %}</p>
{% endif %}
{% if is_allowed_add_location %}
<div class="btn-full">
<a class="btn btn-primary" href="{% url 'locations:add' object.organization.slug object.slug %}">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> {% trans "Add a location" %}
</a>
</div>
{% endif %}
</section>
</div>
<!-- / overview detail -->
Expand Down
12 changes: 11 additions & 1 deletion cadasta/templates/organization/project_wrapper.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,18 @@ <h1>
{% endif %}
</li>
{% endif %}

</ul>
</div>
{% endif %}

<div class="btn-group pull-right btn-add visible-sm-inline visible-md-inline visible-lg-block">
{% if is_allowed_add_location %}
<a class="btn btn-primary" href="{% url 'locations:add' object.organization.slug object.slug %}">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> {% trans "Add location" %}
</a>
{% endif %}
{% if is_allowed_add_resource %}
<button type="button" class="btn btn-primary btn-rt dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only">{%trans "Toggle add" %}</span>
Expand All @@ -69,20 +72,27 @@ <h1>
<!-- <li role="separator" class="divider"></li> -->
<!-- <li><a class="edit" href="#">{% trans "Import data" %}</a></li> -->
</ul>
{% endif %}
</div>
<!-- Single add button for smaller screens-->
<div class="dropdown pull-right visible-xs-inline">
{% if is_allowed_add_location or is_allowed_add_resource %}
<a data-target="#" data-toggle="dropdown" class="btn btn-primary dropdown-toggle" role="button">
<span class="glyphicon glyphicon-plus"></span>
</a>
<ul class="dropdown-menu" aria-labelledby="dLabel">
{% if is_allowed_add_location %}
<li><a href="{% url 'locations:add' object.organization.slug object.slug %}">{% trans "Add location" %}</a></li>
{% endif %}
<!--<li><a href="#">{% trans "Add party" %}</a></li>
<li><a href="#">{% trans "Add relationship" %}</a></li>-->
{% if is_allowed_add_resource %}
<li><a href="{% url 'resources:project_add_existing' object.organization.slug object.slug %}">{% trans "Add resource" %}</a></li>
{% endif %}
<!--<li role="separator" class="divider"></li>
<li><a class="edit" href="#">{% trans "Import data" %}</a></li>-->
</ul>
{% endif %}
</div>
</div>
</div>
Expand Down

0 comments on commit 153f52d

Please sign in to comment.