def test_context_data(self): """ 対象ユーザの詳細ページのHTMLに 渡されるcontextの内容のテスト """ book = factory.BookFactory() favorite = factory.FavoriteFactory(user=self.user, book=book) comment = factory.CommentFactory(user=self.user, book=book) another = factory.CustomUserFactory(email='*****@*****.**') following_relation = Relation(user=self.user, followed=another) following_relation.save() followed_relation = Relation(user=another, followed=self.user) followed_relation.save() response = self.client.get(reverse('accounts:detail', kwargs={'pk': self.user.uuid}), follow=True) self.assertEqual(response.context.get('customuser'), self.user) self.assertIn(favorite, response.context.get('favorite_list')) self.assertIn(comment, response.context.get('comment_list')) self.assertIn(following_relation, response.context.get('following_list')) self.assertIn(followed_relation, response.context.get('followed_list'))
def test_str(self): """ __str__のテスト """ favorite = factory.FavoriteFactory() self.assertEqual(str(favorite), str(favorite.uuid))
def test_context_data(self): """ context_dataの中身のテスト """ book = factory.BookFactory() factory.FavoriteFactory(user=self.user, book=book) response = self.client.get(reverse('book:list'), follow=True) self.assertIn(book, response.context.get('book_list')) self.assertIn(book, response.context.get('fav_book_list')) self.assertTrue(response.context.get('form'))
def test_delete_on_book_detail(self): """ 書籍詳細ページでの削除実行後のページ遷移のテスト """ favorite = factory.FavoriteFactory(user=self.user, book=self.book) data = { 'favorite_uuid': favorite.uuid, 'book_uuid': self.book.uuid } response = self.client.post(reverse('book:delete_favorite'), data, follow=True) self.assertRedirects(response, reverse('book:detail', kwargs={'pk': self.book.uuid}))
def test_delete_on_customuser_detail(self): """ ユーザ詳細ページでの削除実行後のページ遷移のテスト """ favorite = factory.FavoriteFactory(user=self.user, book=self.book) data = { 'favorite_uuid': favorite.uuid, 'template_name': 'customuser_detail', 'user_uuid': self.user.uuid } response = self.client.post(reverse('book:delete_favorite'), data, follow=True) self.assertRedirects(response, reverse('accounts:detail', kwargs={'pk': self.user.uuid}))
def test_context_data(self): """ context_dataの中身のテスト """ comment = factory.CommentFactory(user=self.user, book=self.book) favorite = factory.FavoriteFactory(user=self.user, book=self.book) response = self.client.get(reverse('book:detail', args=[self.book.uuid])) context_data = response.context self.assertEqual(self.book, context_data.get('book')) self.assertIn(comment, context_data.get('comment_list')) self.assertTrue(context_data.get('form')) self.assertEqual(favorite, context_data.get('favorite'))
def test_non_login_user_cannnot_delete(self): """ 非ログインユーザはお気に入りの削除ができないことをテスト """ favorite = factory.FavoriteFactory(user=self.user, book=self.book) self.client.logout() data = { 'favorite_uuid': favorite.uuid, 'template_name': 'book_list' } response = self.client.post(reverse('book:delete_favorite'), data, follow=True) self.assertEqual(response.status_code, HTTPStatus.FORBIDDEN)
def test_only_owner_can_delete(self): """ 所有者はお気に入りの削除ができることをテスト """ favorite = factory.FavoriteFactory(user=self.user, book=self.book) data = { 'favorite_uuid': favorite.uuid, 'template_name': 'book_list' } response = self.client.post(reverse('book:delete_favorite'), data) is_favorite_exist = Favorite.objects.filter(book=self.book, user=self.user).exists() self.assertFalse(is_favorite_exist) self.assertRedirects(response, reverse('book:list'))
def test_non_owner_cannot_delete(self): """ 非所有者はお気に入りの削除ができないことをテスト """ favorite = factory.FavoriteFactory(user=self.user, book=self.book) self.client.logout() non_owner = factory.CustomUserFactory(email='*****@*****.**') self.client.login(email=non_owner.email, password='******') data = { 'favorite_uuid': favorite.uuid, 'template_name': 'book_list' } response = self.client.post(reverse('book:delete_favorite'), data) self.assertEqual(response.status_code, HTTPStatus.FORBIDDEN)