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 16, 2022
1 parent 96610c2 commit 0a82c42
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions feed/tests/test_feed_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import pytest
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

USERNAME = 'username'
USER_PASS = 'secret_pass'
POST_TITLE = 'post title'
POST_CONTENT = 'some post content'
LIKE_URL = '/like/' # '/like/<int:pk>/'
LIKE_WARNING_MESSAGE = 'You must login in order to like a post'


@pytest.fixture
def user(db):
user = User.objects.create_user(USERNAME, password=USER_PASS)
return user


@pytest.fixture
def post(db, user):
post = Post.posts.create(title=POST_TITLE, content=POST_CONTENT, author=user)
post.save()
return post


@pytest.fixture
def liked_post(post, user):
post.likes.add(user)
return post


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_check_likes = Post.posts.filter(title=post.title, content=post.content, author=post.author).first()
response = client.get(LIKE_URL + str(post_to_check_likes.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_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(LIKE_URL + str(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, request):
# 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(request, username=USERNAME, password=USER_PASS)

if like_user is not None:
client.login(username=USERNAME, password=USER_PASS)
assert like_user not in post_to_like.likes.all()
client.get(LIKE_URL + str(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, request):
# 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(request, username=USERNAME, password=USER_PASS)

if like_user is not None:
client.login(username=USERNAME, password=USER_PASS)
assert like_user in post_to_unlike.likes.all()
client.get(LIKE_URL + str(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 0a82c42

Please sign in to comment.