# -*- coding: utf-8 -*- from __future__ import unicode_literals from datetime import datetime from django.contrib.auth import get_user_model from django.contrib.auth.models import Group from django.test import TestCase, Client from django.urls import reverse from django.utils.encoding import force_text from ..models import Post, Comment class BaseIntegrationTest(TestCase): """ Base TestCase for integration tests. """ def setUp(self): self.client = Client() self.user = get_user_model()(username='user', is_staff=True, is_superuser=True) self.user.set_password("password") self.user.save() self.client.login(username='user', password='password') class AdminIndexTest(BaseIntegrationTest): def test_view_ok(self): response = self.client.get(reverse("admin2:dashboard")) self.assertContains(response, reverse("admin2:blog_post_index")) class UserListTest(BaseIntegrationTest): def test_search_users_m2m_group(self): # This test should cause the distinct search path to exectue group = Group.objects.create(name="Test Group") self.user.groups.add(group) params = {"q": "group"} response = self.client.get(reverse("admin2:auth_user_index"), params) self.assertContains(response, 'user') class CommentListTest(BaseIntegrationTest): def test_search_comments(self): # Test search across Foriegn Keys post_1 = Post.objects.create(title="post_1_title", body="body") post_2 = Post.objects.create(title="post_2_title", body="another body") Comment.objects.create(body="comment_post_1_a", post=post_1) Comment.objects.create(body="comment_post_1_b", post=post_1) Comment.objects.create(body="comment_post_2", post=post_2) params = {"q": "post_1_title"} response = self.client.get( reverse("admin2:blog_comment_index"), params) self.assertContains(response, "comment_post_1_a") self.assertContains(response, "comment_post_1_b") self.assertNotContains(response, "comment_post_2") def test_list_selected_hides(self): post_1 = Post.objects.create(title="post_1_title", body="body") Comment.objects.create(body="comment_post1_body", post=post_1) response = self.client.get(reverse("admin2:blog_comment_index")) self.assertNotContains(response, "of 1 selected") class PostListTest(BaseIntegrationTest): def _create_posts(self): Post.objects.bulk_create([ Post( title="post_1_title", body="body", published_date=datetime( month=7, day=22, year=2013 ) ), Post( title="post_2_title", body="body", published_date=datetime( month=5, day=20, year=2012, ) ), Post( title="post_3_title", body="body", published_date=datetime( month=5, day=30, year=2012, ), ), Post( title="post_4_title", body="body", published_date=datetime( month=6, day=20, year=2012, ) ), Post( title="post_5_title", body="body", published_date=datetime( month=6, day=20, year=2012, ) ), ]) def test_view_ok(self): post = Post.objects.create(title="A Post Title", body="body") response = self.client.get(reverse("admin2:blog_post_index")) self.assertContains(response, post.title) def test_list_filter_presence(self): Post.objects.create(title="post_1_title", body="body") Post.objects.create(title="post_2_title", body="another body") response = self.client.get(reverse("admin2:blog_post_index")) self.assertContains(response, 'id="list_filter_container"') def test_list_selected_shows(self): Post.objects.create(title="post_1_title", body="body") response = self.client.get(reverse("admin2:blog_post_index")) self.assertContains(response, 'class="selected-count"') def test_actions_displayed(self): response = self.client.get(reverse("admin2:blog_post_index")) self.assertInHTML( 'Delete selected items', force_text(response.content)) def test_actions_displayed_twice(self): # If actions_on_top and actions_on_bottom are both set response = self.client.get(reverse("admin2:blog_comment_index")) self.assertContains(response, '