Skip to content

Commit

Permalink
Merge 0fd6799 into 8beffc8
Browse files Browse the repository at this point in the history
  • Loading branch information
acdha committed May 29, 2019
2 parents 8beffc8 + 0fd6799 commit 49cdfd9
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 191 deletions.
6 changes: 3 additions & 3 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 0 additions & 56 deletions concordia/tests/test_1st_level_views.py

This file was deleted.

102 changes: 102 additions & 0 deletions concordia/tests/test_account_views.py
@@ -0,0 +1,102 @@
"""
Tests for user account-related views
"""
from django.test import TestCase, override_settings
from django.urls import reverse

from concordia.models import User

from .utils import CacheControlAssertions, CreateTestUsers, JSONAssertMixin


@override_settings(RATELIMIT_ENABLE=False)
class ConcordiaViewTests(
CreateTestUsers, JSONAssertMixin, CacheControlAssertions, TestCase
):
"""
This class contains the unit tests for the view in the concordia app.
"""

def test_AccountProfileView_get(self):
"""
Test the http GET on route account/profile
"""

self.login_user()

response = self.client.get(reverse("user-profile"))

self.assertEqual(response.status_code, 200)
self.assertUncacheable(response)
self.assertTemplateUsed(response, template_name="account/profile.html")

self.assertEqual(response.context["user"], self.user)
self.assertContains(response, self.user.username)
self.assertContains(response, self.user.email)

def test_AccountProfileView_post(self):
"""
This unit test tests the post entry for the route account/profile
:param self:
"""
test_email = "tester@example.com"

self.login_user()

response = self.client.post(
reverse("user-profile"), {"email": test_email, "username": "tester"}
)

self.assertEqual(response.status_code, 302)
self.assertUncacheable(response)
self.assertEqual(response.url, reverse("user-profile"))

# Verify the User was correctly updated
updated_user = User.objects.get(email=test_email)
self.assertEqual(updated_user.email, test_email)

def test_AccountProfileView_post_invalid_form(self):
"""
This unit test tests the post entry for the route account/profile but
submits an invalid form
"""
self.login_user()

response = self.client.post(reverse("user-profile"), {"first_name": "Jimmy"})

self.assertEqual(response.status_code, 200)
self.assertUncacheable(response)

# Verify the User was not changed
updated_user = User.objects.get(id=self.user.id)
self.assertEqual(updated_user.first_name, "")

def test_ajax_session_status_anon(self):
response = self.client.get(reverse("ajax-session-status"))
self.assertCachePrivate(response)
data = self.assertValidJSON(response)
self.assertEqual(data, {})

def test_ajax_session_status(self):
self.login_user()

response = self.client.get(reverse("ajax-session-status"))
self.assertCachePrivate(response)
data = self.assertValidJSON(response)

self.assertIn("links", data)
self.assertIn("username", data)

self.assertEqual(data["username"], self.user.username)

def test_ajax_messages(self):
self.login_user()

response = self.client.get(reverse("ajax-messages"))
data = self.assertValidJSON(response)

self.assertIn("messages", data)

# This view cannot be cached because the messages would be displayed
# multiple times:
self.assertUncacheable(response)
132 changes: 132 additions & 0 deletions concordia/tests/test_top_level_views.py
@@ -0,0 +1,132 @@
"""
Tests for for the top-level & “CMS” views
"""

from django.test import TestCase
from django.urls import reverse

from concordia.models import SimplePage

from .utils import CacheControlAssertions, CreateTestUsers, JSONAssertMixin


class TopLevelViewTests(
JSONAssertMixin, CreateTestUsers, CacheControlAssertions, TestCase
):
def test_healthz(self):
data = self.assertValidJSON(self.client.get("/healthz"))

for k in (
"current_time",
"load_average",
"debug",
"database_has_data",
"application_version",
):
self.assertIn(k, data)

def test_homepage(self):
response = self.client.get(reverse("homepage"))

self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "home.html")

def test_contact_us_get(self):
response = self.client.get(reverse("contact"))

self.assertEqual(response.status_code, 200)
self.assertUncacheable(response)
self.assertTemplateUsed(response, "contact.html")

def test_contact_us_with_referrer(self):
test_http_referrer = "http://foo/bar"

response = self.client.get(reverse("contact"), HTTP_REFERER=test_http_referrer)

self.assertEqual(response.status_code, 200)
self.assertUncacheable(response)
self.assertTemplateUsed(response, "contact.html")

self.assertEqual(
response.context["form"].initial["referrer"], test_http_referrer
)

def test_contact_us_as_a_logged_in_user(self):
"""
The contact form should pre-fill your email address if you're logged in
"""

self.login_user()

response = self.client.get(reverse("contact"))

self.assertEqual(response.status_code, 200)
self.assertUncacheable(response)
self.assertTemplateUsed(response, "contact.html")

self.assertEqual(response.context["form"].initial["email"], self.user.email)

def test_contact_us_post(self):
post_data = {
"email": "nobody@example.com",
"subject": "Problem found",
"link": "http://www.loc.gov/nowhere",
"story": "Houston, we got a problem",
}

response = self.client.post(reverse("contact"), post_data)

self.assertEqual(response.status_code, 302)
self.assertUncacheable(response)

def test_contact_us_post_invalid(self):
post_data = {
"email": "nobody@",
"subject": "Problem found",
"story": "Houston, we got a problem",
}

response = self.client.post(reverse("contact"), post_data)

self.assertEqual(response.status_code, 200)

self.assertEqual(
{"email": ["Enter a valid email address."]}, response.context["form"].errors
)

def test_simple_page(self):
s = SimplePage.objects.create(
title="Help Center 123",
body="not the real body",
path=reverse("help-center"),
)

resp = self.client.get(reverse("help-center"))
self.assertEqual(200, resp.status_code)
self.assertEqual(s.title, resp.context["title"])
self.assertEqual(
[(reverse("help-center"), s.title)], resp.context["breadcrumbs"]
)
self.assertEqual(resp.context["body"], f"<p>{s.body}</p>")

def test_nested_simple_page(self):
l1 = SimplePage.objects.create(
title="Help Center", body="not the real body", path=reverse("help-center")
)

l2 = SimplePage.objects.create(
title="Help Center Welcome Guide",
body="This is _not_ the real page",
path=reverse("welcome-guide"),
)

resp = self.client.get(reverse("welcome-guide"))
self.assertEqual(200, resp.status_code)
self.assertEqual(l2.title, resp.context["title"])
self.assertEqual(
resp.context["breadcrumbs"],
[(reverse("help-center"), l1.title), (reverse("welcome-guide"), l2.title)],
)
self.assertHTMLEqual(
resp.context["body"], f"<p>This is <em>not</em> the real page</p>"
)

0 comments on commit 49cdfd9

Please sign in to comment.