Skip to content

Commit

Permalink
Updating various models and routes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiola-m committed Oct 29, 2018
1 parent 9db42c0 commit 06a68f1
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/ej/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from ej.testing import UrlTester
from ej import routes
from django.conf import settings
from django.contrib.auth.models import AnonymousUser


class TestBasicUrls(UrlTester):
Expand All @@ -9,3 +12,20 @@ class TestBasicUrls(UrlTester):
'/menu/',
'/home/',
]


class TestViews:
def test_index_route_logged_user(self, rf, db, user):
request = rf.get('', {})
user.save()
request.user = user
response = routes.index(request)
assert response.status_code == 302
assert response.url == settings.EJ_USER_HOME_PATH

def test_index_anonymous_user(self, rf):
request = rf.get('', {})
request.user = AnonymousUser()
response = routes.index(request)
assert response.status_code == 302
assert response.url == settings.EJ_ANONYMOUS_HOME_PATH
46 changes: 46 additions & 0 deletions src/ej_boards/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def test_edit_conversation(self, rf, board, db, conversation):
response = routes.conversation_edit(request, board, conversation)
assert response.status_code == 302
assert response.url == '/slugs/conversations/conversation/moderate/'
assert routes.report(board, conversation)

def test_edit_invalid_conversation(self, rf, db, board, conversation):
conversation.author = board.owner
Expand Down Expand Up @@ -156,6 +157,51 @@ def test_get_moderate_conversation_not_in_board(self, rf, db, board, conversatio
routes.conversation_moderate(request, board, conversation)


class TestBoardViewRoutest:
def test_get_create_board(self, rf, user):
request = rf.get('', {})
request.user = user
response = routes.board_create(request)
assert response['content_title']
assert response['form']

def test_post_create_valid_board(self, rf, user):
data = {'slug': 'slug',
'title': 'new title',
'description': 'description'}
request = rf.post('', data)
request.user = user
response = routes.board_create(request)
assert response.url == '/slug/'
assert response.status_code == 302

data = {'slug': 'slug2',
'title': 'new title',
'description': 'description'}
request = rf.post('', data)
request.user = user
with pytest.raises(Http404):
routes.board_create(request)

def test_post_create_invalid_board(self, rf, user):
data = {'slug': '',
'title': 'new title',
'description': 'description'}
request = rf.post('', data)
request.user = user
response = routes.board_create(request)
assert not response['form'].is_valid()
assert response['content_title']

def test_list_board_only_one_board(self, rf, user, board):
board.owner = user
request = rf.get('', {})
request.user = user
response = routes.board_list(request)
assert response.status_code == 302
assert response.url == board.get_absolute_url() + 'conversations/'


class TestBoardRoutes(TestCase):
def setUp(self):
self.user = User.objects.create_user('name@server.com', '1234', name='name')
Expand Down
10 changes: 10 additions & 0 deletions src/ej_conversations/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ def test_user_can_get_detail_of_a_comment(self, conversation, comment):
ctx = comments.comment_detail(conversation, comment)
assert ctx['comment'] is comment

def test_comment_list_not_promoted_convesation(self, request_with_user, conversation, user):
conversation.is_promoted = False
with raises(Http404):
comments.comment_list(request_with_user, conversation)

def test_udetail_of_a_comment_not_promoted(self, conversation, comment):
conversation.is_promoted = False
with raises(Http404):
comments.comment_detail(conversation, comment)


class TestAdminViews:
def test_create_conversation(self, rf, user):
Expand Down
14 changes: 13 additions & 1 deletion src/ej_users/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from ej_users.models import User, PasswordResetToken
from ej_users.models import User, PasswordResetToken, generate_token, clean_expired_tokens
from ej_profiles.choices import Race, Gender


Expand Down Expand Up @@ -48,3 +48,15 @@ def test_token_is_not_expired(self, db, user):
token = PasswordResetToken(user=user)
token.save()
assert not token.is_expired

def test_generate_token(self, db, user):
token = generate_token(user)
assert token.user == user
assert token.url

def test_delete_used_token(self, db, user):
token = generate_token(user)
token.is_used = True
token.save()
clean_expired_tokens()
assert not PasswordResetToken.objects.filter(user=user).exists()
40 changes: 39 additions & 1 deletion src/ej_users/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from django.test import TestCase

from ej.testing import UrlTester
from ej_users.models import User
from ej_users.models import User, generate_token
from ej_users import routes


@pytest.fixture
Expand Down Expand Up @@ -67,6 +68,43 @@ def test_login_user_route(self, db, mk_user):
assert response.url == '/'
assert int(client.session['_auth_user_id']) == user.pk

def test_login_route_unexistent_user(self, db, mk_user, rf):
user = mk_user
email = user.email
user.delete()
request = rf.post('', {'email': email, 'password': 'password'})
request.user = None
response = routes.login(request)
assert response['form'].errors


class TestPasswordRoutes:
@pytest.fixture
def token(db, mk_user):
token = generate_token(mk_user)
return token

def test_get_change_password(self, db, token, rf):
user = token.user
request = rf.get('', {})
response = routes.reset_password(request, token)
assert response['user'] == user
assert response['form']
assert not response['isExpired']

def test_post_matching_passwords(self, db, token, rf):
request = rf.post('', {'new_password': 'pass', 'new_password_confirm': 'pass'})
response = routes.reset_password(request, token)
assert response.status_code == 302

def test_post_invalid_change_password(self, db, token, rf):
user = token.user
request = rf.post('', {'new_password': 'pass123', 'new_password_confirm': 'pass'})
response = routes.reset_password(request, token)
assert response['user'] == user
assert response['form']
assert not response['isExpired']


class TestRegisterRoute(TestCase):
def setUp(self):
Expand Down

0 comments on commit 06a68f1

Please sign in to comment.