Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Commit

Permalink
for #97: add/improve push view tests
Browse files Browse the repository at this point in the history
  • Loading branch information
groovecoder committed Mar 28, 2016
1 parent 30f6030 commit 209d205
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 34 deletions.
3 changes: 0 additions & 3 deletions push/static/styles/deletion.styl

This file was deleted.

4 changes: 0 additions & 4 deletions push/static/styles/list.styl
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#push-applications table .button {
margin-left: 10px;
}

#add-push-application input[type="text"] {
width: 800px;
max-width: 100%;
Expand Down
10 changes: 5 additions & 5 deletions push/templates/push/deletion.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
<fieldset>
{% csrf_token %}
<legend>{% blocktrans %}Are you sure you want to delete?{% endblocktrans %}</legend>
<table>
<tr><th width="20%" class="push-delete-form-table-label">{% trans "App Name:" %}</th><td>{{ app.name }}</td></tr>
<tr><th class="push-delete-form-table-label">{% trans "VAPID Key:" %}</th><td>{{ app.vapid_key }}</td></tr>
<tr><th class="push-delete-form-table-label">{% trans "VAPID Key Status:" %}</th><td>{{ app.vapid_key_status }}</td></tr>
</table>
<dl>
<dt>{% trans "App Name:" %}</dt><dd>{{ app.name }}</dd>
<dt>{% trans "VAPID Key:" %}</dt><dd>{{ app.vapid_key }}</dd>
<dt>{% trans "VAPID Key Status:" %}</dt><dd>{{ app.vapid_key_status }}</dd>
</dl>
<input type="submit" class="alert button" value="{% trans "Confirm Delete" %}">
</fieldset>
</form>
Expand Down
6 changes: 3 additions & 3 deletions push/templates/push/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ <h2>{% trans "Push Applications" %}</h2>
<th>{% trans "Application Name" %}</th>
<th>VAPID {% trans "Key" %}</th>
<th>VAPID {% trans "Key Status" %}</th>
<th></th>
<th>{% trans "Actions" %}</th>
</tr>
</thead>
<tbody>
Expand All @@ -87,11 +87,11 @@ <h2>{% trans "Push Applications" %}</h2>
<td>{{ app.vapid_key|truncatechars:20 }}</td>
<td>
{{ app.vapid_key_status }}
</td>
<td>
{% if app.can_validate %}
<a class="button" href="{% url 'push.validation' app.id %}">{% trans "Validate" %}</a>
{% endif %}
</td>
<td>
<a class="alert button" href="{% url 'push.deletion' app.id %}">{% trans "Delete" %}</a>
</td>
{% endfor %}
Expand Down
39 changes: 37 additions & 2 deletions push/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

from ..forms import PushAppForm
from ..models import PushApplication, MessagesAPIError
from ..views import Landing, List, Details, Validation, UserOwnsPushAppMixin
from ..views import (Deletion, Details, Landing, List, UserOwnsPushAppMixin,
Validation)
from . import messages_api_response_json_messages


Expand Down Expand Up @@ -35,8 +36,16 @@ def test_true_if_users_app(self):
mixin.kwargs = {'pk': app.id}
self.assertTrue(mixin.test_func())

def test_adds_app_to_context_data(self):
user = mommy.make(User)
app = mommy.make(PushApplication, user=user)
mixin = UserOwnsPushAppMixin()
mixin.request = fudge.Fake().has_attr(user=user)
context = mixin.get_context_data(pk=app.id)
self.assertEqual(app, context['app'])

class PushViewTests(TestCase):

class PushViewGETTests(TestCase):
def setUp(self):
self.user = mommy.make(User)
self.request = fudge.Fake().has_attr(user=self.user)
Expand All @@ -46,10 +55,12 @@ def test_template_names(self):
listing = List()
details = Details()
validate = Validation()
deletion = Deletion()
self.assertIn('landing.html', landing.template_name)
self.assertIn('list.html', listing.template_name)
self.assertIn('details.html', details.template_name)
self.assertIn('validation.html', validate.template_name)
self.assertIn('deletion.html', deletion.template_name)

def test_listing_context_form_and_apps_for_user(self):
listing = List()
Expand Down Expand Up @@ -118,3 +129,27 @@ def test_details_context_with_MessagesAPIError_calls_warning(

context = details.get_context_data(pk=app.id)
self.assertEqual(0, len(context['app_messages']))


class PushDeletionViewTests(TestCase):
def setUp(self):
self.user = mommy.make(User)
self.app = mommy.make(PushApplication, user=self.user)
self.request = fudge.Fake().has_attr(user=self.user)

def test_post_unkown_pk_404(self):
self.assertEqual(1, len(PushApplication.objects.all()))
deletion = Deletion()
with self.assertRaises(Http404):
deletion.post(self.request, 9999)
self.assertEqual(1, len(PushApplication.objects.all()))

@fudge.patch("push.views.messages")
def test_post_pk_deletes_and_redirects(self, messages):
messages.expects('success')
self.assertEqual(1, len(PushApplication.objects.all()))
deletion = Deletion()
deletion.request = self.request
resp = deletion.post(self.request, self.app.id)
self.assertEqual(0, len(PushApplication.objects.all()))
self.assertEqual(302, resp.status_code)
25 changes: 8 additions & 17 deletions push/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,26 @@ def test_func(self):
push_app = get_object_or_404(PushApplication, pk=self.kwargs['pk'])
return push_app.created_by(self.request.user)

def get_context_data(self, **kwargs):
push_app = get_object_or_404(PushApplication, pk=kwargs['pk'])
context = dict({'app': push_app})
return context


class Details(UserOwnsPushAppMixin, TemplateView):
template_name = 'push/details.html'
raise_exception = True

def get_context_data(self, **kwargs):
push_app = get_object_or_404(PushApplication, pk=kwargs['pk'])

context = super(Details, self).get_context_data(**kwargs)
# TODO: refactor push_app.get_messages() from view into template
push_app = context['app']
app_messages = {'messages': []}
try:
app_messages = push_app.get_messages()
except MessagesAPIError as e:
messages.warning(self.request, e.message)
context.update({
'app': push_app,
'app_messages': app_messages['messages']
})
context.update({'app_messages': app_messages['messages']})

return context

Expand All @@ -65,11 +67,8 @@ class Validation(UserOwnsPushAppMixin, TemplateView):
raise_exception = True

def get_context_data(self, **kwargs):
push_app = get_object_or_404(PushApplication, pk=self.kwargs['pk'])

context = super(Validation, self).get_context_data(**kwargs)
context.update({
'app': push_app,
'vapid_validation_form': VapidValidationForm(),
})

Expand All @@ -89,14 +88,6 @@ class Deletion(UserOwnsPushAppMixin, TemplateView):
template_name = 'push/deletion.html'
raise_exception = True

def get_context_data(self, **kwargs):
push_app = get_object_or_404(PushApplication, pk=self.kwargs['pk'])

context = super(Deletion, self).get_context_data(**kwargs)
context.update({'app': push_app})

return context

def post(self, request, pk, *args, **kwargs):
push_app = get_object_or_404(PushApplication, pk=pk)
num_deleted, app_deleted = push_app.delete()
Expand Down

0 comments on commit 209d205

Please sign in to comment.