From 06a68f1d7ae3f248aa39211b1be57943eaa4f915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fab=C3=ADola=20Fleury?= Date: Mon, 29 Oct 2018 14:18:02 -0300 Subject: [PATCH] Updating various models and routes tests --- src/ej/tests/test_routes.py | 20 +++++++++++ src/ej_boards/tests/test_routes.py | 46 ++++++++++++++++++++++++ src/ej_conversations/tests/test_views.py | 10 ++++++ src/ej_users/tests/test_models.py | 14 +++++++- src/ej_users/tests/test_routes.py | 40 ++++++++++++++++++++- 5 files changed, 128 insertions(+), 2 deletions(-) diff --git a/src/ej/tests/test_routes.py b/src/ej/tests/test_routes.py index 8b3d18511..7b38b96be 100644 --- a/src/ej/tests/test_routes.py +++ b/src/ej/tests/test_routes.py @@ -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): @@ -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 diff --git a/src/ej_boards/tests/test_routes.py b/src/ej_boards/tests/test_routes.py index 915c6a41a..b1d9f4fa5 100644 --- a/src/ej_boards/tests/test_routes.py +++ b/src/ej_boards/tests/test_routes.py @@ -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 @@ -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') diff --git a/src/ej_conversations/tests/test_views.py b/src/ej_conversations/tests/test_views.py index e2a33fb2d..06a5dc836 100644 --- a/src/ej_conversations/tests/test_views.py +++ b/src/ej_conversations/tests/test_views.py @@ -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): diff --git a/src/ej_users/tests/test_models.py b/src/ej_users/tests/test_models.py index 6a75d5b3a..33653a44f 100644 --- a/src/ej_users/tests/test_models.py +++ b/src/ej_users/tests/test_models.py @@ -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 @@ -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() diff --git a/src/ej_users/tests/test_routes.py b/src/ej_users/tests/test_routes.py index 7da5bbeea..c89291ce6 100644 --- a/src/ej_users/tests/test_routes.py +++ b/src/ej_users/tests/test_routes.py @@ -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 @@ -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):