Navigation Menu

Skip to content

Commit

Permalink
Added more unit tests (and fixed bugs that the unit tests uncovered)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matoking committed Apr 7, 2015
1 parent 6d2ead2 commit ed08082
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pastes/models.py
Expand Up @@ -150,7 +150,7 @@ def add_paste(text, user=None, title="Untitled", expiration=None, visibility=Non
if user is not None:
user_id = user.id

if expiration != Paste.NEVER:
if expiration != Paste.NEVER and expiration != None:
expiration_datetime = Paste.get_expiration_datetime(expiration)
else:
expiration_datetime = None
Expand Down
2 changes: 1 addition & 1 deletion pastes/templates/pastes/show_paste/show_paste.html
Expand Up @@ -22,7 +22,7 @@ <h1 class="panel-title">{{ paste.title }}</h1>
<div class="panel-body">
<div class="container">
<div class="col-md-3">
<p><b>SUBMITTED BY:</b> {% if paste.username %}<b>{{ paste.username }}</b>{% else %}Guest{% endif %}</p>
<p><b>SUBMITTED BY:</b> {% if paste.username %}<b><a href="{% url "users:profile" paste.username %}">{{ paste.username }}</a></b>{% else %}Guest{% endif %}</p>
<p><b>DATE:</b> {{ paste.submitted }}</p>
{% if paste.expiration_date %}
{% with timesince_seconds=paste.expiration_date|timeuntil_in_seconds %}
Expand Down
42 changes: 40 additions & 2 deletions pastes/tests.py
@@ -1,7 +1,8 @@
from django.test import TestCase

from django.core.urlresolvers import reverse

from pastes.models import Paste

class PasteTests(TestCase):
def test_upload_paste(self):
"""
Expand All @@ -15,4 +16,41 @@ def test_upload_paste(self):
follow=True)

self.assertContains(response, "Paste test title")
self.assertContains(response, "This is a test.")
self.assertContains(response, "This is a test.")

def test_cant_upload_empty_paste(self):
"""
Check that an empty paste can't be uploaded
"""
response = self.client.post(reverse("pastes:submit_paste"), { "title": "Paste test title",
"text": "",
"syntax_highlighting": "text",
"expiration": "never",
"visibility": "public"},
follow=True)

# We should be redirected back to the front page on failure
self.assertContains(response, "Upload a new paste")
self.assertNotContains(response, "Paste test title")

def test_raw_paste_displayed_correctly(self):
"""
Create a paste and view it in raw format
"""
text = """This is a raw paste.<b></b>>TEST TEST TEST TEST"""

char_id = Paste.add_paste(text)

response = self.client.get(reverse("raw_paste", kwargs={"char_id": char_id}))

# The response should contain the next text and nothing else
self.assertContains(response, text)
self.assertNotContains(response, "Untitled")

def test_non_existent_paste_displays_error(self):
"""
If user tries to view a non-existing paste a "paste not found" error should be displayed
"""
response = self.client.get(reverse("show_paste", kwargs={"char_id": "420BlzIt"}))

self.assertContains(response, "Paste not found", status_code=404)
8 changes: 8 additions & 0 deletions users/forms.py
Expand Up @@ -45,6 +45,14 @@ def clean_username(self):

return username

def clean_password(self):
password = self.cleaned_data.get("password")

if password == "correct horse battery staple":
raise forms.ValidationError("You'll have to pick a better password than that.")

return password

def clean_confirm_password(self):
"""
Check that the provided passwords match
Expand Down
2 changes: 1 addition & 1 deletion users/templates/users/profile/favorites/favorites.html
Expand Up @@ -21,7 +21,7 @@
</table>
{% include "pagination.html" with destination="users:favorites" url_arg=profile_user.get_username %}
{% else %}
{% if profile_username == user.get_username %}
{% if profile_user.get_username == user.get_username %}
<div class="alert alert-info">
<strong>No favorites added</strong><br>
You haven't added any favorites yet.
Expand Down
2 changes: 1 addition & 1 deletion users/templates/users/profile/home/home_favorites.html
Expand Up @@ -20,7 +20,7 @@ <h3>Recent favorites</h3>
</tbody>
</table>
{% else %}
{% if profile_username == user.get_username %}
{% if profile_user.get_username == user.get_username %}
<div class="alert alert-info">
<strong>No favorites added</strong><br>
You haven't added any favorites yet.
Expand Down
2 changes: 1 addition & 1 deletion users/templates/users/profile/home/home_pastes.html
Expand Up @@ -22,7 +22,7 @@ <h3>Recent pastes</h3>
</tbody>
</table>
{% else %}
{% if profile_username == user.get_username %}
{% if profile_user.get_username == user.get_username %}
<div class="alert alert-info">
<strong>No pastes uploaded</strong><br>
You haven't uploaded any pastes yet.
Expand Down
2 changes: 1 addition & 1 deletion users/templates/users/profile/pastes/pastes.html
Expand Up @@ -21,7 +21,7 @@
</table>
{% include "pagination.html" with destination="users:pastes" url_arg=profile_user.get_username %}
{% else %}
{% if profile_username == user.get_username %}
{% if profile_user.get_username == user.get_username %}
<div class="alert alert-info">
<strong>No pastes uploaded</strong><br>
You haven't uploaded any pastes yet.
Expand Down
98 changes: 97 additions & 1 deletion users/tests.py
@@ -1,3 +1,99 @@
from django.test import TestCase
from django.core.urlresolvers import reverse

# Create your tests here.
def create_test_account(test_case):
"""
Creates user TestUser
"""
test_case.client.post(reverse("users:register"), {"username": "TestUser",
"password": "password",
"confirm_password": "password"})

def login_test_account(test_case):
"""
Logs in as TestUser. User must be created before logging in
"""
test_case.client.post(reverse("users:login"), {"username": "TestUser",
"password": "password"})

def upload_test_paste(test_case):
"""
Upload a test paste
"""
test_case.client.post(reverse("pastes:submit_paste"), { "title": "Paste test title",
"text": "This is a test.",
"syntax_highlighting": "text",
"expiration": "never",
"visibility": "public"},
follow=True)

class UserTests(TestCase):
def test_user_can_register(self):
"""
Register with valid details
"""
response = self.client.post(reverse("users:register"), {"username": "TestUser",
"password": "password",
"confirm_password": "password"})

self.assertContains(response, "Registered!")

def test_user_can_login(self):
"""
Login after creating a test account
"""
create_test_account(self)

response = self.client.post(reverse("users:login"), {"username": "TestUser",
"password": "password"})

self.assertContains(response, "Logged in!")

def test_user_empty_profile_displayed_correctly(self):
"""
Check that new user's profile page is displayed correctly
"""
create_test_account(self)

response = self.client.get(reverse("users:profile", kwargs={"username": "TestUser"}))

self.assertContains(response, "This user hasn't added any favorites yet.")
self.assertContains(response, "This user hasn't uploaded any pastes yet.")

# Now login and check the same page again
login_test_account(self)

response = self.client.get(reverse("users:profile", kwargs={"username": "TestUser"}))

self.assertContains(response, "You haven't uploaded any pastes yet.")
self.assertContains(response, "You haven't added any favorites yet.")

def test_user_can_upload_paste(self):
"""
Upload a paste while logged in and check that the user is added as its
uploader
"""
create_test_account(self)
login_test_account(self)

response = self.client.post(reverse("pastes:submit_paste"), { "title": "Paste test title",
"text": "This is a test.",
"syntax_highlighting": "text",
"expiration": "never",
"visibility": "public"},
follow=True)

self.assertContains(response, "Delete paste")

def test_user_uploaded_paste_displayed_in_profile(self):
"""
Upload a paste as an user and check that it is displayed in the user's profile
"""
create_test_account(self)
login_test_account(self)
upload_test_paste(self)

response = self.client.get(reverse("users:profile", kwargs={"username": "TestUser"}))

self.assertContains(response, "Paste test title")
self.assertNotContains(response, "You haven't uploaded any pastes yet.")

0 comments on commit ed08082

Please sign in to comment.