How to use assert_http_400_bad_request method in Django Test Plus

Best Python code snippet using django-test-plus_python

test_comment.py

Source:test_comment.py Github

copy

Full Screen

...102 comment = Comment.objects.first()103 like_count = comment.like_comment.count()104 self.assertEqual(like_count, 1)105 self.post(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')106 self.assert_http_400_bad_request()107 self.post(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')108 self.assert_http_403_forbidden()109 comment = Comment.objects.first()110 like_count = comment.like_comment.count()111 self.assertEqual(like_count, 1)112 with self.login(username='user2', password='strong_password_2'):113 self.post(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')114 self.assert_http_201_created()115 comment = Comment.objects.first()116 like_count = comment.like_comment.count()117 self.assertEqual(like_count, 2)118 def test_comment_like_get(self):119 self.post(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')120 with self.login(username='user1', password='strong_password_1'):121 self.create_board()122 self.create_post(board_pk=self.board_pk)123 self.create_comment(board_pk=self.board_pk, post_pk=self.post_pk)124 self.get(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')125 self.assert_http_400_bad_request()126 with self.login(username='user2', password='strong_password_2'):127 self.post(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')128 self.get(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')129 self.assert_http_200_ok()130 self.get(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')131 self.assert_http_403_forbidden()132 def test_comment_like_delete(self):133 with self.login(username='user1', password='strong_password_1'):134 self.create_board()135 self.create_post(board_pk=self.board_pk)136 self.create_comment(board_pk=self.board_pk, post_pk=self.post_pk)137 self.post(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')138 self.delete(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')139 self.assert_http_403_forbidden()140 comment = Comment.objects.first()141 like_count = comment.like_comment.count()142 self.assertEqual(like_count, 1)143 with self.login(username='user2', password='strong_password_2'):144 self.delete(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')145 self.assert_http_400_bad_request()146 comment = Comment.objects.first()147 like_count = comment.like_comment.count()148 self.assertEqual(like_count, 1)149 with self.login(username='user1', password='strong_password_1'):150 self.delete(f'/board/{self.board_pk}/post/{self.post_pk}/comment/{self.comment_pk}/like/')151 self.assert_http_204_no_content()152 comment = Comment.objects.first()153 like_count = comment.like_comment.count()154 self.assertEqual(like_count, 0)155 def test_comment_serializer(self):156 with self.login(username='user1', password='strong_password_1'):157 self.create_board()158 self.create_post(board_pk=self.board_pk)159 self.create_comment(board_pk=self.board_pk, post_pk=self.post_pk)...

Full Screen

Full Screen

test_post.py

Source:test_post.py Github

copy

Full Screen

...99 post = Post.objects.first()100 like_count = post.like_post.count()101 self.assertEqual(like_count, 1)102 self.post(f'/board/{self.board_pk}/post/{self.post_pk}/like/')103 self.assert_http_400_bad_request()104 post = Post.objects.first()105 like_count = post.like_post.count()106 self.assertEqual(like_count, 1)107 def test_post_like_get(self):108 with self.login(username='user1', password='strong_password_1'):109 self.create_board()110 self.create_post(self.board_pk)111 self.get(f'/board/{self.board_pk}/post/{self.post_pk}/like/')112 self.assert_http_403_forbidden()113 with self.login(username='user1', password='strong_password_1'):114 self.get(f'/board/{self.board_pk}/post/{self.post_pk}/like/')115 self.assert_http_400_bad_request()116 self.post(f'/board/{self.board_pk}/post/{self.post_pk}/like/')117 response = self.get(f'/board/{self.board_pk}/post/{self.post_pk}/like/')118 self.assert_http_200_ok()119 self.assertEqual(response.data[0]['username'], 'user1')120 def test_post_like_delete(self):121 with self.login(username='user1', password='strong_password_1'):122 self.create_board()123 self.create_post(self.board_pk)124 self.post(f'/board/{self.board_pk}/post/{self.post_pk}/like/')125 self.delete(f'/board/{self.board_pk}/post/{self.post_pk}/like/')126 self.assert_http_403_forbidden()127 with self.login(username='user2', password='strong_password_2'):128 self.delete(f'/board/{self.board_pk}/post/{self.post_pk}/like/')129 self.assert_http_400_bad_request()130 with self.login(username='user1', password='strong_password_1'):131 self.delete(f'/board/{self.board_pk}/post/{self.post_pk}/like/')132 self.assert_http_204_no_content()133 def test_post_serializer(self):134 with self.login(username='user1', password='strong_password_1'):135 self.create_board()136 self.create_post(self.board_pk)137 response = self.get(f'/board/{self.board_pk}/post/{self.post_pk}/')138 res_data = response.data139 self.assertEqual(res_data['author']['username'], 'user1')140 self.assertTrue(res_data['is_author'])141 self.assertFalse(res_data['is_like'])142 self.assertEqual(res_data['title'], 'title')143 self.assertEqual(res_data['content'], 'content')...

Full Screen

Full Screen

test_year.py

Source:test_year.py Github

copy

Full Screen

...28 # test min year error29 res = self.post(url, data={'from_year': Year.MIN_YEAR - 1,30 'to_year': Year.MIN_YEAR + 3},31 extra={'format': 'json'})32 self.assert_http_400_bad_request(res)33 # test from > to error34 res = self.post(url, data={'from_year': Year.MIN_YEAR + 10,35 'to_year': Year.MIN_YEAR + 1},36 extra={'format': 'json'})37 self.assert_http_400_bad_request(res)38 from_year, to_year = Year.MIN_YEAR, Year.MIN_YEAR + 2039 res = self.post(url, data={'from_year': from_year, 'to_year': to_year},40 extra={'format': 'json'})41 self.assert_http_201_created(res)42 res_years = [r['year'] for r in res.data]43 qs = Year.objects.values_list('year', flat=True)44 expected = list(range(from_year, to_year + 1))45 self.assertQuerysetEqual(qs, expected, ordered=False)46 self.assertQuerysetEqual(res_years, expected, ordered=False)47 res = self.post(url, data={'from_year': '2130', 'to_year': 2140,},48 extra={'format': 'json'})49 self.assert_http_201_created(res)50 res_years = [r['year'] for r in res.data]51 qs = Year.objects.values_list('year', flat=True)52 expected = list(range(2130, 2141))53 self.assertQuerysetEqual(qs, expected, ordered=False)54 self.assertQuerysetEqual(res_years, expected, ordered=False)55 res = self.post(url, data={'from_year': '2130.2', 'to_year': 2140,},56 extra={'format': 'json'})57 self.assert_http_400_bad_request(res)58 def test_query_params(self):59 """test the query params"""60 Year.objects.all().delete()61 for y in range(2020, 2050):62 year = Year.objects.create(year=y, is_prognosis=not(y % 5))63 url = 'years-list'64 # all years65 res = self.get_check_200(url,66 extra={'format': 'json'})67 self.assertListEqual([r['year'] for r in res.data],68 list(range(2020, 2050, 1)))69 # prognosis years70 res = self.get_check_200(url,71 data={'is_prognosis': True,},...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Django Test Plus automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful