Skip to content

Commit

Permalink
test coverage
Browse files Browse the repository at this point in the history
(Finetooth) zmd@ExpectedReturn:~/Code/Finetooth$ coverage run
--source="core" ./manage.py test core && coverage report >
~/Code/Misc/finetooth_coverage_after.txt && cd ~/Code/Misc/ && diff
finetooth_coverage_before.txt finetooth_coverage_after.txt
Creating test database for alias 'default'...
.....................
----------------------------------------------------------------------
Ran 21 tests in 0.532s

OK
Destroying test database for alias 'default'...
14c14
< core/models                                         54      9    83%
---
> core/models                                         54      7    87%
19c19
< core/tests/view_tests                               68      0   100%
---
> core/tests/view_tests                              100      0   100%
22,25c22,25
< core/views/service                                  42     13    69%
< core/views/view_utils                               52     36    31%
< core/views/views                                    95     40    58%
< core/votable                                        56     17    70%
---
> core/views/service                                  42      8    81%
> core/views/view_utils                               52     18    65%
> core/views/views                                    95     14    85%
> core/votable                                        56      0   100%
27c27
< TOTAL                                              534    132    75%
---
> TOTAL                                              566     64    89%

[#31]
  • Loading branch information
zackmdavis committed Oct 18, 2014
1 parent e1ed970 commit 64170b5
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 8 deletions.
89 changes: 85 additions & 4 deletions core/tests/view_tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os, sys
from datetime import datetime

from django.test import TestCase
Expand All @@ -7,6 +8,37 @@
from core.models import FinetoothUser, Post
from core.tests import factories as f


class SignupTest(TestCase):

def test_can_sign_up(self):
response = self.client.post(
reverse('sign_up'),
{'username': "signup_testr", 'password': "moeDukr(,rpdCesLlrqr",
'email': "[email protected]"}
)
# XX: The call to assertRedirects was printing an empty
# dictionary to standard out (if accidentally left a debugging
# print statement in Django core (?!), I couldn't find it)
original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
self.assertRedirects(response, '/')
sys.stdout = original_stdout
self.assertTrue(
FinetoothUser.objects.filter(username="signup_testr").exists()
)

def test_cannnot_claim_extant_username(self):
f.FinetoothUserFactory.create(username="username_squatter")
response = self.client.post(
reverse('sign_up'),
{'username': "username_squatter", 'password': "oclxJums^whyysmtam",
'email': "[email protected]"},
follow=True
)
self.assertIn(b"Username already exists.", response.content)


class TaggingTest(TestCase):

def setUp(self):
Expand All @@ -17,30 +49,62 @@ def setUp(self):
self.client.login(
username=self.the_user.username, password=f.FACTORY_USER_PASSWORD
)
self.extant_tag = f.TagFactory.create()
self.the_post.tag_set.add(self.extant_tag)

def test_user_can_tag_own_post(self):
self.client.post(reverse('tag', args=(self.the_post.pk,)),
{'label': "user can tag own post"})
{'label': "taggable"})
tags = self.the_post.tag_set
self.assertEqual(1, tags.count())
self.assertEqual("user can tag own post", tags.first().label)
self.assertTrue("taggable", tags.filter(label="taggable").exists())

def test_user_cannot_tag_post_of_other(self):
response = self.client.post(
reverse('tag', args=(self.other_post.pk,)),
{'label': "user cannot tag other user's post"}
{'label': "untaggable"}
)
self.assertEqual(403, response.status_code)
tags = self.other_post.tag_set
self.assertEqual(0, tags.count())

def test_user_cannot_double_apply_same_tag(self):
tags_before = self.the_post.tag_set.all()
response = self.client.post(
reverse('tag', args=(self.the_post.pk,)),
{'label': self.extant_tag.label}
)
self.assertEqual(400, response.status_code)
self.assertQuerysetEqual(
self.the_post.tag_set.all(),
[repr(t) for t in tags_before]
)

class CommentingTest(TestCase):

def setUp(self):
self.the_user = f.FinetoothUserFactory.create()
self.the_post = f.PostFactory.create()

def test_can_submit_comment(self):
self.client.login(
username=self.the_user.username, password=f.FACTORY_USER_PASSWORD
)
response = self.client.post(
reverse("add_comment", args=(self.the_post.pk,)),
{'content': "the way that we got that one cool shot"}
)
comment = self.the_post.comment_set.filter(commenter=self.the_user)[0]
fragment_identifier = "#comment-{}".format(comment.pk)
self.assertRedirects(
response,
reverse(
"show_post",
args=(self.the_post.year,
self.the_post.month,
self.the_post.slug)
) + fragment_identifier
)

def test_do_not_panic_on_blank_comment(self):
self.client.login(
username=self.the_user.username, password=f.FACTORY_USER_PASSWORD
Expand Down Expand Up @@ -74,6 +138,23 @@ def test_can_vote_on_post(self):
self.assertEqual(('D', 1), self.the_post.scored_plaintext()[6])
self.assertEqual(('d', 0), self.the_post.scored_plaintext()[-1])

def test_must_be_logged_in_to_vote(self):
response = self.client.post(
reverse('vote', args=("post", self.the_post.pk)),
{'selection': "unvotable", 'value': 1}
)
self.assertEqual(response.status_code, 401)

def test_cannot_submit_invalid_vote(self):
self.client.login(
username=self.the_user.username, password=f.FACTORY_USER_PASSWORD
)
response = self.client.post(
reverse('vote', args=("post", self.the_post.pk)),
{'selection': "unvotable", 'value': 1}
)
self.assertEqual(response.status_code, 400)


class ProfileEditingTest(TestCase):

Expand Down
2 changes: 1 addition & 1 deletion core/views/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def tag(request, post_pk):
return HttpResponseForbidden("You can't tag other user's posts.")
tag = Tag.objects.filter(label=label).first()
if tag:
if post.tag_set.filter(pk=tag.pk):
if post.tag_set.filter(pk=tag.pk).exists():
return HttpResponseBadRequest(
"This post is already tagged {}".format(label)
)
Expand Down
3 changes: 3 additions & 0 deletions core/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def sign_up(request):
username = request.POST["username"]
email = request.POST["email"]
password = request.POST["password"]
# TODO: require confirm_password as well; it would be sad
# to be locked out of your new account because you
# mistyped your intended password and didn't notice
FinetoothUser.objects.create_user(username, email, password)
messages.success(request, "Account creation successful!")
new_user = authenticate(username=request.POST['username'],
Expand Down
2 changes: 1 addition & 1 deletion core/votable.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def render(self):
for token in parsed_content:
if isinstance(token, str): # text
scored_characters = [scored_plaintext_stack.pop()
for _ in range(len(token))]
for _ in range(len(token))]
join_to_render.append(
"".join(
"<span data-value=\"{}\">{}</span>".format(
Expand Down
4 changes: 2 additions & 2 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# TODO: review
# https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
# XXX TODO FIXME DANGER: the security warning on the previous line is
# big deal; if/when deploying this somewhere, change this and DO NOT
# keep the real value in a publicly-visible Git repo
SECRET_KEY = '4y+opi-^iz@s+(#io3b2l1+w89c44vos%9npcxap8=#+4c11!n'
SECRET_KEY = "fake_development_unsecret_key"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
Expand Down

0 comments on commit 64170b5

Please sign in to comment.