Best Python code snippet using autotest_python
test_views.py
Source:test_views.py
...44 request = self.request_factory.get('/fake-path')45 request.user = self.user46 self.add_session_to_request(request)47 # Session object is self.test_model48 view = setup_test_view(self.ObjectSessionView(), request)49 view.test_model = self.test_model50 view.session_obj = 'test_model'51 view.session_obj_attrs = ['name']52 view.dispatch(view.request, *view.args, **view.kwargs)53 self.assertIn('session_obj', request.session)54 self.assertEqual(55 request.session['session_obj'],56 {view.session_obj: {'name': self.test_model.name}}57 )58 # Session object is self.test_model_259 view = setup_test_view(self.ObjectSessionView(), request)60 view.test_model = self.test_model_261 view.session_obj = 'test_model'62 view.session_obj_attrs = ['name']63 view.dispatch(view.request, *view.args, **view.kwargs)64 self.assertIn('session_obj', request.session)65 self.assertEqual(66 request.session['session_obj'],67 {view.session_obj: {'name': self.test_model_2.name}}68 )69 # Session object is None, and request.session['session_obj'] is deleted.70 view = setup_test_view(self.ObjectSessionView(), request)71 view.test_model = self.test_model_272 view.dispatch(view.request, *view.args, **view.kwargs)73 self.assertNotIn('session_obj', request.session)74class AutocompleteMixinTest(TestCase):75 class AutocompleteView(AutocompleteMixin, View):76 search_model = TestModel77 search_field = 'name'78 search_filter = 'istartswith'79 id_attr = 'id'80 label_attr = 'name'81 value_attr = 'name'82 def setUp(self):83 self.request_factory = RequestFactory()84 self.user = User.objects.create_user(85 username='foo7',86 first_name='Foo',87 last_name='Foo',88 email='foo7@foo.com',89 password='Coffee?69c'90 )91 self.test_model = TestModel.objects.create(name='hello')92 self.request_factory = RequestFactory()93 def get_autocomplete_response(self, term=''):94 kwargs = {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}95 request = self.request_factory.get('/fake-path?term={0}'.format(term), **kwargs)96 response = self.AutocompleteView.as_view()(request)97 return json.loads(response.content)98 def test_autocomplete_results(self):99 result_1 = TestModel.objects.create(name='green')100 result_2 = TestModel.objects.create(name='great')101 result_3 = TestModel.objects.create(name='apple')102 TestModel.objects.create(name='pear')103 results = self.get_autocomplete_response(term='gre')104 expected_results = [105 {'id': result_1.id, 'label': result_1.name, 'value': result_1.name},106 {'id': result_2.id, 'label': result_2.name, 'value': result_2.name}107 ]108 self.assertCountEqual(results, expected_results)109 results = self.get_autocomplete_response(term='gree')110 expected_results = [111 {'id': result_1.id, 'label': result_1.name, 'value': result_1.name}112 ]113 self.assertCountEqual(results, expected_results)114 results = self.get_autocomplete_response(term='ap')115 expected_results = [116 {'id': result_3.id, 'label': result_3.name, 'value': result_3.name}117 ]118 self.assertCountEqual(results, expected_results)119 results = self.get_autocomplete_response(term='xx')120 expected_results = []121 self.assertCountEqual(results, expected_results)122class AttachmentMixinTest(TestCase):123 class AttachmentDetailView(AttachmentMixin, DetailView):124 model = TestUserstampModel125 template_name = 'fake_template.html'126 def get_file_content(self):127 return '### Hello there.'128 def setUp(self):129 self.request_factory = RequestFactory()130 self.user = User.objects.create_user(131 username='ale7',132 first_name='Alejandra',133 last_name='Acosta',134 email='ale7@foo.com',135 password='Coffee?69c'136 )137 self.test_model = TestUserstampModel.objects.create(name='hello')138 self.request_factory = RequestFactory()139 def test_get_filename(self):140 request = self.request_factory.get('/fake-path')141 request.user = self.user142 view = setup_test_view(self.AttachmentDetailView(), request, pk=self.test_model.pk)143 view.dispatch(view.request, *view.args, **view.kwargs)144 self.assertEqual(view.get_filename(), 'document.txt')145 def test_get_content_type(self):146 request = self.request_factory.get('/fake-path')147 request.user = self.user148 view = setup_test_view(self.AttachmentDetailView(), request, pk=self.test_model.pk)149 view.dispatch(view.request, *view.args, **view.kwargs)150 self.assertEqual(view.get_content_type(), 'text/plain')151 def test_get_file_content(self):152 request = self.request_factory.get('/fake-path')153 request.user = self.user154 view = setup_test_view(self.AttachmentDetailView(), request, pk=self.test_model.pk)155 view.dispatch(view.request, *view.args, **view.kwargs)156 self.assertEqual(view.get_file_content(), '### Hello there.')157 def test_make_attachment(self):158 request = self.request_factory.get('/fake-path')159 request.user = self.user160 view = setup_test_view(self.AttachmentDetailView(), request, pk=self.test_model.pk)161 view.dispatch(view.request, *view.args, **view.kwargs)162 view.filename = 'foo.md'163 response = HttpResponse(content_type=view.get_content_type())164 response_attachment = view.make_attachment(response)165 content_disposition = 'attachment; filename=foo.md'166 self.assertEqual(content_disposition, response_attachment['Content-Disposition'])167 def test_write_attachment(self):168 request = self.request_factory.get('/fake-path')169 request.user = self.user170 view = setup_test_view(self.AttachmentDetailView(), request, pk=self.test_model.pk)171 view.dispatch(view.request, *view.args, **view.kwargs)172 view.filename = 'foo.md'173 response = view.write_attachment()174 content_disposition = 'attachment; filename=foo.md'175 self.assertEqual(content_disposition, response['Content-Disposition'])176 self.assertEqual(view.get_file_content().encode('utf-8'), response.content)177class UserstampMixinTest(TestCase):178 class UserstampCreateView(UserstampMixin, CreateView):179 model = TestUserstampModel180 form_class = TestUserstampForm181 template_name = 'fake_template.html'182 class UserstampUpdateView(UserstampMixin, UpdateView):183 model = TestUserstampModel184 form_class = TestUserstampForm185 template_name = 'fake_template.html'186 def get_queryset(self, **kwargs):187 qs = TestUserstampModel.objects.filter(id=self.kwargs['pk'])188 return qs189 def setUp(self):190 self.request_factory = RequestFactory()191 self.user = User.objects.create_user(192 username='ale7',193 first_name='Alejandra',194 last_name='Acosta',195 email='ale7@foo.com',196 password='Coffee?69c'197 )198 self.test_model = TestUserstampModel.objects.create(name='hello')199 self.request_factory = RequestFactory()200 def test_create_view_set_created_by_and_last_updated_by(self):201 request = self.request_factory.post('/fake-path', {'name': 'foofoo'})202 request.user = self.user203 view = setup_test_view(self.UserstampCreateView(), request)204 view.dispatch(view.request, *view.args, **view.kwargs)205 model = TestUserstampModel.objects.get(name='foofoo')206 self.assertEqual(model.created_by, self.user)207 self.assertEqual(model.last_updated_by, self.user)208 def test_update_view_set_last_updated_by(self):209 request = self.request_factory.post('/fake-path', {'name': 'foofoo'})210 request.user = self.user211 view = setup_test_view(self.UserstampUpdateView(), request, pk=self.test_model.pk)212 view.dispatch(view.request, *view.args, **view.kwargs)213 self.test_model.refresh_from_db()214 self.assertIsNone(self.test_model.created_by)215 self.assertEqual(self.test_model.last_updated_by, self.user)216class UserMixinTest(TestCase):217 class TestView(UserMixin, TemplateView):218 template_name = 'fake_template.html'219 def setUp(self):220 self.request_factory = RequestFactory()221 self.user = User.objects.create_user(222 username='ale7',223 first_name='Alejandra',224 last_name='Acosta',225 email='ale7@foo.com',226 password='Coffee?69c'227 )228 self.user_2 = User.objects.create_user(229 username='kfl7',230 first_name='Karen',231 last_name='Fuentes',232 email='kfl7@foo.com',233 password='Coffee?69c'234 )235 def add_session_to_request(self, request):236 middleware = SessionMiddleware()237 middleware.process_request(request)238 request.session.save()239 def test_permissions_requesting_existing_user(self):240 request = self.request_factory.get('/fake-path')241 self.add_session_to_request(request)242 request.user = self.user243 view = setup_test_view(self.TestView(), request, username=self.user.username)244 response = view.dispatch(view.request, *view.args, **view.kwargs)245 self.assertEqual(response.status_code, 200)246 def test_permissions_requesting_non_existing_user(self):247 request = self.request_factory.get('/fake-path')248 self.add_session_to_request(request)249 request.user = self.user250 view = setup_test_view(self.TestView(), request, username='nonuser')251 with self.assertRaises(Http404):252 view.dispatch(view.request, *view.args, **view.kwargs)253 def test_view_context_data(self):254 request = self.request_factory.get('/fake-path')255 self.add_session_to_request(request)256 request.user = self.user257 view = setup_test_view(self.TestView(), request, username=self.user_2.username)258 view.dispatch(view.request, *view.args, **view.kwargs)259 context = view.get_context_data()260 self.assertEqual(context['requested_user'], self.user_2)261class SuperuserRequiredMixinTest(TestCase):262 class TestView(SuperuserRequiredMixin, UserstampMixin, CreateView):263 model = TestUserstampModel264 form_class = TestUserstampForm265 template_name = 'fake_template.html'266 def setUp(self):267 self.request_factory = RequestFactory()268 self.superuser = User.objects.create_superuser(269 username='cfs',270 first_name='Christopher',271 last_name='Sanders',272 email='cfs@nublado.foo',273 password='Coffee?69c'274 )275 self.user = User.objects.create_user(276 username='ale7',277 first_name='Alejandra',278 last_name='Acosta',279 email='ale7@foo.com',280 password='Coffee?69c'281 )282 self.test_model = TestUserstampModel.objects.create(name='hello')283 def add_session_to_request(self, request):284 '''Annotate a request object with a session'''285 middleware = SessionMiddleware()286 middleware.process_request(request)287 request.session.save()288 def test_permissions_superuser(self):289 request = self.request_factory.get('/fake-path')290 self.add_session_to_request(request)291 request.user = self.superuser292 view = setup_test_view(self.TestView(), request)293 response = view.dispatch(view.request, *view.args, **view.kwargs)294 self.assertEqual(response.status_code, 200)295 def test_permissions_non_superuser(self):296 request = self.request_factory.get('/fake-path')297 self.add_session_to_request(request)298 request.user = self.user299 view = setup_test_view(self.TestView(), request)300 with self.assertRaises(PermissionDenied):301 view.dispatch(view.request, *view.args, **view.kwargs)302class UserRequiredMixinTest(TestCase):303 class TestView(UserRequiredMixin, TemplateView):304 template_name = 'fake_template.html'305 def setUp(self):306 self.request_factory = RequestFactory()307 self.superuser = User.objects.create_superuser(308 username='cfs',309 first_name='Christopher',310 last_name='Sanders',311 email='cfs@nublado.foo',312 password='Coffee?69c'313 )314 self.user = User.objects.create_user(315 username='ale7',316 first_name='Alejandra',317 last_name='Acosta',318 email='ale7@foo.com',319 password='Coffee?69c'320 )321 self.user_2 = User.objects.create_user(322 username='kfl7',323 first_name='Karen',324 last_name='Fuentes',325 email='kfl7@foo.com',326 password='Coffee?69c'327 )328 def add_session_to_request(self, request):329 middleware = SessionMiddleware()330 middleware.process_request(request)331 request.session.save()332 def test_permissions_superuser(self):333 request = self.request_factory.get('/fake-path')334 self.add_session_to_request(request)335 request.user = self.superuser336 view = setup_test_view(self.TestView(), request, username=self.user.username)337 response = view.dispatch(view.request, *view.args, **view.kwargs)338 self.assertEqual(response.status_code, 200)339 def test_permissions_requesting_user_is_requested_user(self):340 request = self.request_factory.get('/fake-path')341 self.add_session_to_request(request)342 request.user = self.user343 view = setup_test_view(self.TestView(), request, username=self.user.username)344 response = view.dispatch(view.request, *view.args, **view.kwargs)345 self.assertEqual(response.status_code, 200)346 def test_permissions_requesting_user_not_requested_user(self):347 request = self.request_factory.get('/fake-path')348 self.add_session_to_request(request)349 request.user = self.user_2350 view = setup_test_view(self.TestView(), request, username=self.user.username)351 with self.assertRaises(PermissionDenied):...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!