|
|
|
|
@@ -8,11 +8,11 @@ from django.urls import reverse
|
|
|
|
|
from django.utils import html, timezone
|
|
|
|
|
|
|
|
|
|
from apps.applets.models import Applet, UserApplet
|
|
|
|
|
from apps.dashboard.forms import (
|
|
|
|
|
from apps.billboard.forms import (
|
|
|
|
|
DUPLICATE_LINE_ERROR,
|
|
|
|
|
EMPTY_LINE_ERROR,
|
|
|
|
|
)
|
|
|
|
|
from apps.dashboard.models import Line, Post
|
|
|
|
|
from apps.billboard.models import Line, Post
|
|
|
|
|
from apps.drama.models import Note
|
|
|
|
|
from apps.lyric.models import User
|
|
|
|
|
|
|
|
|
|
@@ -37,19 +37,19 @@ class NewPostTest(TestCase):
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_can_save_a_POST_request(self):
|
|
|
|
|
self.client.post("/dashboard/new_post", data={"text": "A new post line"})
|
|
|
|
|
self.client.post("/billboard/new-post", data={"text": "A new post line"})
|
|
|
|
|
self.assertEqual(Line.objects.count(), 1)
|
|
|
|
|
new_line = Line.objects.get()
|
|
|
|
|
self.assertEqual(new_line.text, "A new post line")
|
|
|
|
|
|
|
|
|
|
def test_redirects_after_POST(self):
|
|
|
|
|
response = self.client.post("/dashboard/new_post", data={"text": "A new post line"})
|
|
|
|
|
response = self.client.post("/billboard/new-post", data={"text": "A new post line"})
|
|
|
|
|
new_post = Post.objects.get()
|
|
|
|
|
self.assertRedirects(response, f"/dashboard/post/{new_post.id}/")
|
|
|
|
|
self.assertRedirects(response, f"/billboard/post/{new_post.id}/")
|
|
|
|
|
|
|
|
|
|
# Post invalid input helper
|
|
|
|
|
def post_invalid_input(self):
|
|
|
|
|
return self.client.post("/dashboard/new_post", data={"text": ""})
|
|
|
|
|
return self.client.post("/billboard/new-post", data={"text": ""})
|
|
|
|
|
|
|
|
|
|
def test_for_invalid_input_nothing_saved_to_db(self):
|
|
|
|
|
self.post_invalid_input()
|
|
|
|
|
@@ -68,12 +68,12 @@ class NewPostTest(TestCase):
|
|
|
|
|
class PostViewTest(TestCase):
|
|
|
|
|
def test_uses_post_template(self):
|
|
|
|
|
mypost = Post.objects.create()
|
|
|
|
|
response = self.client.get(f"/dashboard/post/{mypost.id}/")
|
|
|
|
|
self.assertTemplateUsed(response, "apps/dashboard/post.html")
|
|
|
|
|
response = self.client.get(f"/billboard/post/{mypost.id}/")
|
|
|
|
|
self.assertTemplateUsed(response, "apps/billboard/post.html")
|
|
|
|
|
|
|
|
|
|
def test_renders_input_form(self):
|
|
|
|
|
mypost = Post.objects.create()
|
|
|
|
|
url = f"/dashboard/post/{mypost.id}/"
|
|
|
|
|
url = f"/billboard/post/{mypost.id}/"
|
|
|
|
|
response = self.client.get(url)
|
|
|
|
|
parsed = lxml.html.fromstring(response.content)
|
|
|
|
|
forms = parsed.cssselect("form[method=POST]")
|
|
|
|
|
@@ -90,7 +90,7 @@ class PostViewTest(TestCase):
|
|
|
|
|
other_post = Post.objects.create()
|
|
|
|
|
Line.objects.create(text="other post line", post=other_post)
|
|
|
|
|
# When/Act
|
|
|
|
|
response = self.client.get(f"/dashboard/post/{correct_post.id}/")
|
|
|
|
|
response = self.client.get(f"/billboard/post/{correct_post.id}/")
|
|
|
|
|
# Then/Assert
|
|
|
|
|
self.assertContains(response, "itemey 1")
|
|
|
|
|
self.assertContains(response, "itemey 2")
|
|
|
|
|
@@ -101,7 +101,7 @@ class PostViewTest(TestCase):
|
|
|
|
|
correct_post = Post.objects.create()
|
|
|
|
|
|
|
|
|
|
self.client.post(
|
|
|
|
|
f"/dashboard/post/{correct_post.id}/",
|
|
|
|
|
f"/billboard/post/{correct_post.id}/",
|
|
|
|
|
data={"text": "A new line for an existing post"},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@@ -115,16 +115,16 @@ class PostViewTest(TestCase):
|
|
|
|
|
correct_post = Post.objects.create()
|
|
|
|
|
|
|
|
|
|
response = self.client.post(
|
|
|
|
|
f"/dashboard/post/{correct_post.id}/",
|
|
|
|
|
f"/billboard/post/{correct_post.id}/",
|
|
|
|
|
data={"text": "A new line for an existing post"},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertRedirects(response, f"/dashboard/post/{correct_post.id}/")
|
|
|
|
|
self.assertRedirects(response, f"/billboard/post/{correct_post.id}/")
|
|
|
|
|
|
|
|
|
|
# Post invalid input helper
|
|
|
|
|
def post_invalid_input(self):
|
|
|
|
|
mypost = Post.objects.create()
|
|
|
|
|
return self.client.post(f"/dashboard/post/{mypost.id}/", data={"text": ""})
|
|
|
|
|
return self.client.post(f"/billboard/post/{mypost.id}/", data={"text": ""})
|
|
|
|
|
|
|
|
|
|
def test_for_invalid_input_nothing_saved_to_db(self):
|
|
|
|
|
self.post_invalid_input()
|
|
|
|
|
@@ -133,7 +133,7 @@ class PostViewTest(TestCase):
|
|
|
|
|
def test_for_invalid_input_renders_post_template(self):
|
|
|
|
|
response = self.post_invalid_input()
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
self.assertTemplateUsed(response, "apps/dashboard/post.html")
|
|
|
|
|
self.assertTemplateUsed(response, "apps/billboard/post.html")
|
|
|
|
|
|
|
|
|
|
def test_for_invalid_input_shows_error_on_page(self):
|
|
|
|
|
response = self.post_invalid_input()
|
|
|
|
|
@@ -150,46 +150,46 @@ class PostViewTest(TestCase):
|
|
|
|
|
Line.objects.create(post=post1, text="lorem ipsum")
|
|
|
|
|
|
|
|
|
|
response = self.client.post(
|
|
|
|
|
f"/dashboard/post/{post1.id}/",
|
|
|
|
|
f"/billboard/post/{post1.id}/",
|
|
|
|
|
data={"text": "lorem ipsum"},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expected_error = html.escape(DUPLICATE_LINE_ERROR)
|
|
|
|
|
self.assertContains(response, expected_error)
|
|
|
|
|
self.assertTemplateUsed(response, "apps/dashboard/post.html")
|
|
|
|
|
self.assertTemplateUsed(response, "apps/billboard/post.html")
|
|
|
|
|
self.assertEqual(Line.objects.all().count(), 1)
|
|
|
|
|
|
|
|
|
|
class MyPostsTest(TestCase):
|
|
|
|
|
def test_my_posts_url_renders_my_posts_template(self):
|
|
|
|
|
user = User.objects.create(email="a@b.cde")
|
|
|
|
|
self.client.force_login(user)
|
|
|
|
|
response = self.client.get(f"/dashboard/users/{user.id}/")
|
|
|
|
|
self.assertTemplateUsed(response, "apps/dashboard/my_posts.html")
|
|
|
|
|
response = self.client.get(f"/billboard/users/{user.id}/")
|
|
|
|
|
self.assertTemplateUsed(response, "apps/billboard/my_posts.html")
|
|
|
|
|
|
|
|
|
|
def test_passes_correct_owner_to_template(self):
|
|
|
|
|
User.objects.create(email="wrongowner@example.com")
|
|
|
|
|
correct_user = User.objects.create(email="a@b.cde")
|
|
|
|
|
self.client.force_login(correct_user)
|
|
|
|
|
response = self.client.get(f"/dashboard/users/{correct_user.id}/")
|
|
|
|
|
response = self.client.get(f"/billboard/users/{correct_user.id}/")
|
|
|
|
|
self.assertEqual(response.context["owner"], correct_user)
|
|
|
|
|
|
|
|
|
|
def test_post_owner_is_saved_if_user_is_authenticated(self):
|
|
|
|
|
user = User.objects.create(email="a@b.cde")
|
|
|
|
|
self.client.force_login(user)
|
|
|
|
|
self.client.post("/dashboard/new_post", data={"text": "new line"})
|
|
|
|
|
self.client.post("/billboard/new-post", data={"text": "new line"})
|
|
|
|
|
new_post = Post.objects.get()
|
|
|
|
|
self.assertEqual(new_post.owner, user)
|
|
|
|
|
|
|
|
|
|
def test_my_posts_redirects_if_not_logged_in(self):
|
|
|
|
|
user = User.objects.create(email="a@b.cde")
|
|
|
|
|
response = self.client.get(f"/dashboard/users/{user.id}/")
|
|
|
|
|
response = self.client.get(f"/billboard/users/{user.id}/")
|
|
|
|
|
self.assertRedirects(response, "/")
|
|
|
|
|
|
|
|
|
|
def test_my_posts_returns_403_for_wrong_user(self):
|
|
|
|
|
user1 = User.objects.create(email="a@b.cde")
|
|
|
|
|
user2 = User.objects.create(email="wrongowner@example.com")
|
|
|
|
|
self.client.force_login(user2)
|
|
|
|
|
response = self.client.get(f"/dashboard/users/{user1.id}/")
|
|
|
|
|
response = self.client.get(f"/billboard/users/{user1.id}/")
|
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
|
|
class SharePostTest(TestCase):
|
|
|
|
|
@@ -197,16 +197,16 @@ class SharePostTest(TestCase):
|
|
|
|
|
our_post = Post.objects.create()
|
|
|
|
|
alice = User.objects.create(email="alice@example.com")
|
|
|
|
|
response = self.client.post(
|
|
|
|
|
f"/dashboard/post/{our_post.id}/share_post",
|
|
|
|
|
f"/billboard/post/{our_post.id}/share-post",
|
|
|
|
|
data={"recipient": "alice@example.com"},
|
|
|
|
|
)
|
|
|
|
|
self.assertRedirects(response, f"/dashboard/post/{our_post.id}/")
|
|
|
|
|
self.assertRedirects(response, f"/billboard/post/{our_post.id}/")
|
|
|
|
|
|
|
|
|
|
def test_post_with_email_adds_user_to_shared_with(self):
|
|
|
|
|
our_post = Post.objects.create()
|
|
|
|
|
alice = User.objects.create(email="alice@example.com")
|
|
|
|
|
self.client.post(
|
|
|
|
|
f"/dashboard/post/{our_post.id}/share_post",
|
|
|
|
|
f"/billboard/post/{our_post.id}/share-post",
|
|
|
|
|
data={"recipient": "alice@example.com"},
|
|
|
|
|
)
|
|
|
|
|
self.assertIn(alice, our_post.shared_with.all())
|
|
|
|
|
@@ -214,12 +214,12 @@ class SharePostTest(TestCase):
|
|
|
|
|
def test_post_with_nonexistent_email_redirects_to_post(self):
|
|
|
|
|
our_post = Post.objects.create()
|
|
|
|
|
response = self.client.post(
|
|
|
|
|
f"/dashboard/post/{our_post.id}/share_post",
|
|
|
|
|
f"/billboard/post/{our_post.id}/share-post",
|
|
|
|
|
data={"recipient": "nobody@example.com"},
|
|
|
|
|
)
|
|
|
|
|
self.assertRedirects(
|
|
|
|
|
response,
|
|
|
|
|
f"/dashboard/post/{our_post.id}/",
|
|
|
|
|
f"/billboard/post/{our_post.id}/",
|
|
|
|
|
fetch_redirect_response=False,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@@ -227,7 +227,7 @@ class SharePostTest(TestCase):
|
|
|
|
|
owner = User.objects.create(email="owner@example.com")
|
|
|
|
|
our_post = Post.objects.create(owner=owner)
|
|
|
|
|
self.client.force_login(owner)
|
|
|
|
|
self.client.post(reverse("share_post", args=[our_post.id]),
|
|
|
|
|
self.client.post(reverse("billboard:share_post", args=[our_post.id]),
|
|
|
|
|
data={"recipient": "owner@example.com"})
|
|
|
|
|
self.assertNotIn(owner, our_post.shared_with.all())
|
|
|
|
|
|
|
|
|
|
@@ -235,7 +235,7 @@ class SharePostTest(TestCase):
|
|
|
|
|
def test_share_post_shows_privacy_safe_message(self):
|
|
|
|
|
our_post = Post.objects.create()
|
|
|
|
|
response = self.client.post(
|
|
|
|
|
f"/dashboard/post/{our_post.id}/share_post",
|
|
|
|
|
f"/billboard/post/{our_post.id}/share-post",
|
|
|
|
|
data={"recipient": "nobody@example.com"},
|
|
|
|
|
follow=True,
|
|
|
|
|
)
|
|
|
|
|
@@ -251,20 +251,20 @@ class ViewAuthPostTest(TestCase):
|
|
|
|
|
self.our_post = Post.objects.create(owner=self.owner)
|
|
|
|
|
|
|
|
|
|
def test_anonymous_user_is_redirected(self):
|
|
|
|
|
response = self.client.get(reverse("view_post", args=[self.our_post.id]))
|
|
|
|
|
response = self.client.get(reverse("billboard:view_post", args=[self.our_post.id]))
|
|
|
|
|
self.assertRedirects(response, "/", fetch_redirect_response=False)
|
|
|
|
|
|
|
|
|
|
def test_non_owner_non_shared_user_gets_403(self):
|
|
|
|
|
stranger = User.objects.create(email="stranger@example.com")
|
|
|
|
|
self.client.force_login(stranger)
|
|
|
|
|
response = self.client.get(reverse("view_post", args=[self.our_post.id]))
|
|
|
|
|
response = self.client.get(reverse("billboard:view_post", args=[self.our_post.id]))
|
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
|
|
def test_shared_with_user_can_access_post(self):
|
|
|
|
|
guest = User.objects.create(email="guest@example.com")
|
|
|
|
|
self.our_post.shared_with.add(guest)
|
|
|
|
|
self.client.force_login(guest)
|
|
|
|
|
response = self.client.get(reverse("view_post", args=[self.our_post.id]))
|
|
|
|
|
response = self.client.get(reverse("billboard:view_post", args=[self.our_post.id]))
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
|
|
@override_settings(COMPRESS_ENABLED=False)
|
|
|
|
|
|