Skip to content

Commit

Permalink
Adding relevant tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paOmer committed Apr 23, 2022
1 parent 532a9eb commit ec66e12
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions feed/tests/test_feed_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from feed.models import Post
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
from django.contrib.messages import get_messages


USERNAME1 = 'username1'
Expand All @@ -10,13 +11,20 @@
POST_TITLE = 'post title'
POST_CONTENT = 'some post content'
POST_DETAIL_URL = '/post/'
LOGIN_URL = '/login/'
LIKE_WARNING_MESSAGE = 'You must login in order to like a post'
REDIRECT_URL_STATUS = 302
PAGE_NOT_FOUND = 404


def post_delete_url(post_id):
return f"/post/{post_id}/delete/"


def post_like_url(post_id):
return f"/post/{post_id}/like/"


@pytest.fixture
def users(db):
user1 = User.objects.create_user(USERNAME1, password=USER_PASS)
Expand All @@ -31,6 +39,12 @@ def post(db, users):
return post


@pytest.fixture
def liked_post(post, users):
post.likes.add(users[0])
return post


@pytest.mark.django_db
class TestPostDetailView:
def test_detail_view_page_entrypoint(self, post, client):
Expand Down Expand Up @@ -91,3 +105,62 @@ def test_success_delete_using_delete_view(self, request, client, post):
else:
# The user login detail are wrong
assert False


class TestLikeView:
def test_like_view_return_warning_when_logged_out(self, post, client):
# Testing that when logged out, if trying to access the like url
# a warning message returns
post_to_like = Post.posts.filter(title=post.title, content=post.content, author=post.author).first()
response = client.get(post_like_url(post_to_like.id))
messages = [m.message for m in get_messages(response.wsgi_request)]
assert len(messages) == 1
assert messages[0] == LIKE_WARNING_MESSAGE

def test_like_view_redirects_to_login_when_logged_out(self, post, client):
# Testing that when logged out, if trying to access the like url
# the user gets redirected to the login url
post_to_like = Post.posts.filter(title=post.title, content=post.content, author=post.author).first()
response = client.get(post_like_url(post_to_like.id))
assert response.status_code == REDIRECT_URL_STATUS
assert response.url == LOGIN_URL

def test_like_url_doesnt_adds_like_to_post(self, post, client):
# Testing that when logged out, if trying to access the like url
# it doesnt adds a like to a post
post_to_check_likes = Post.posts.filter(title=post.title, content=post.content, author=post.author).first()
assert post_to_check_likes.likes.all().count() == 0
client.get(post_like_url(post_to_check_likes.id))
assert post_to_check_likes.likes.all().count() == 0

def test_like_url_adds_like_to_post(self, post, client):
# Testing that the like url, adds a like to a specific post
post_to_like = Post.posts.filter(title=post.title, content=post.content, author=post.author).first()
like_user = authenticate(username=USERNAME1, password=USER_PASS)

if like_user is not None:
client.login(username=USERNAME1, password=USER_PASS)
assert like_user not in post_to_like.likes.all()
client.get(post_like_url(post_to_like.id))
assert like_user in post_to_like.likes.all()
else:
# Login details are invalid
assert False

def test_like_url_unlikes(self, liked_post, client):
# Testing that the like url, unlike a post where the logged in
# user have already liked the post
post_to_unlike = Post.posts.filter(
title=liked_post.title,
content=liked_post.content,
author=liked_post.author).first()
like_user = authenticate(username=USERNAME1, password=USER_PASS)

if like_user is not None:
client.login(username=USERNAME1, password=USER_PASS)
assert like_user in post_to_unlike.likes.all()
client.get(post_like_url(post_to_unlike.id))
assert like_user not in post_to_unlike.likes.all()
else:
# Login details are invalid
assert False

0 comments on commit ec66e12

Please sign in to comment.