from django.http import HttpRequest
from django.test import SimpleTestCase, TestCase
from django.test.client import Client
from django.urls import reverse
from users.models import CustomUser
from main.models import Fanfic
from unittest import mock
class DashboardTests(TestCase):
def setUp(self):
self.normal_user = CustomUser.objects.create(
name_surname="name",
country="AM",
date_of_birth="2000-07-02",
email="em@gm.com",
username='testuser',
password="12345")
self.client = Client()
self.client.force_login(user=self.normal_user)
def test_home_status_code(self):
response = self.client.get(reverse('common:home'), follow=True)
self.assertEquals(response.status_code, 200)
def test_dashboard_status_code(self):
response = self.client.get(reverse('common:dashboard'), follow=True)
self.assertEquals(response.status_code, 200)
class ExternalAddTests(TestCase):
def setUp(self):
self.normal_user = CustomUser.objects.create(
name_surname="name",
country="AM",
date_of_birth="2000-07-02",
email="em@gm.com",
username='testuser',
password="12345")
Fanfic.objects.create(name="Testek fanfic", author="michaelRuiz",
web="http://web-fanfic.com", genre1="adv",
status="c")
self.client = Client()
self.client.force_login(user=self.normal_user)
def test_external_add_status_code(self):
response = self.client.get(reverse('common:external_add'))
self.assertEquals(response.status_code, 200)
@mock.patch('django.core.handlers.wsgi.WSGIRequest.is_ajax')
@mock.patch('main.views.url_without_errors')
def test_post_external_add_bad_url(self, mock_url_without_errors,
mock_is_ajax):
''' Try to add fanfic with bad url '''
mock_url_without_errors.return_value = "Error: bad url"
mock_is_ajax.return_value = True
post_data = {'url_fanfic': 'bad-url'}
response = self.client.post(reverse('common:external_add'),
data=post_data)
self.assertEquals(response.status_code, 200)
@mock.patch('django.core.handlers.wsgi.WSGIRequest.is_ajax')
@mock.patch('main.views_functions.Fanfic.get_title_and_author')
@mock.patch('main.views.url_without_errors')
def test_post_external_add_with_similars(self,
mock_url_without_errors,
mock_title_and_author,
mock_is_ajax):
''' Try to add fanfic that has similar ones '''
mock_url_without_errors.return_value = "good-url"
mock_title_and_author.return_value = ("test fanfic", "PetrovDiminov")
mock_is_ajax.return_value = True
post_data = {'url_fanfic': 'good-url'}
response = self.client.post(reverse('common:external_add'),
data=post_data)
self.assertEquals(response.status_code, 200)
|