How to use response_302 method in Django Test Plus

Best Python code snippet using django-test-plus_python

test_views.py

Source:test_views.py Github

copy

Full Screen

...16 self.assertSetEqual(set(surveys), {survey1, survey2})17 def test_survey_list_anonymous(self):18 """Verify anonymous user is redirected"""19 self.get("formly:survey_list")20 self.response_302()21 def test_survey_detail_creator(self):22 """Verify survey creator can see survey list"""23 survey1 = self._survey()24 survey2 = self._survey()25 with self.login(self.user):26 self.get("formly:survey_detail", pk=survey1.pk)27 self.response_200()28 surveys = self.context["surveys"]29 self.assertSetEqual(set(surveys), {survey1, survey2})30 selected = self.context["selected_survey"]31 self.assertEqual(selected, survey1)32 def test_survey_detail_not_creator(self):33 """Verify user who didn't create survey is not allowed"""34 not_creator = self.make_user("not_creator")35 survey = self._survey()36 with self.login(not_creator):37 self.get("formly:survey_detail", pk=survey.pk)38 self.response_403()39 def test_page_detail_creator(self):40 """Verify survey creator can see page detail"""41 self.survey = self._survey()42 page1 = self._page()43 page2 = self._page() # Create another page for good measure44 with self.login(self.user):45 self.get("formly:page_detail", pk=page1.pk)46 self.response_200()47 pages = self.context["pages"]48 self.assertSetEqual(set(pages), {page1, page2})49 selected = self.context["selected_page"]50 self.assertEqual(selected, page1)51 def test_page_detail_not_creator(self):52 """Verify user who didn't create survey is not allowed"""53 not_creator = self.make_user("not_creator")54 self.survey = self._survey()55 page = self._page()56 self._page() # Create another page for good measure57 with self.login(not_creator):58 self.get("formly:page_detail", pk=page.pk)59 self.response_403()60 def test_survey_create_authenticated_get(self):61 """Verify authenticated user can access survey creation"""62 with self.login(self.user):63 self.get("formly:survey_create")64 self.response_200()65 self.assertInContext("form")66 def test_survey_create_anonymous_get(self):67 """Verify anonymous user is redirected"""68 self.get("formly:survey_create")69 self.response_302()70 def test_survey_create_post(self):71 """Verify authenticated user can create a survey"""72 survey_name = "Excellent Survey"73 post_data = {74 "name": survey_name75 }76 with self.login(self.user):77 self.post("formly:survey_create", data=post_data)78 self.response_302()79 survey = Survey.objects.get(name=survey_name)80 self.assertEqual(survey.creator, self.user)81 def test_survey_change_name_creator(self):82 """Verify survey creator can change survey name"""83 survey = self._survey()84 survey_name = "Excellent Survey"85 post_data = {86 "name": survey_name87 }88 with self.login(self.user):89 self.post("formly:survey_change_name", pk=survey.pk, data=post_data)90 self.response_200()91 self.assertTrue(Survey.objects.get(name=survey_name))92 json_data = json.loads(self.last_response.content.decode("utf-8"))93 self.assertEqual(json_data["status"], "OK")94 self.assertEqual(json_data["name"], survey_name)95 def test_survey_change_name_not_creator(self):96 """Verify user who didn't create survey is not allowed"""97 not_creator = self.make_user("not_creator")98 survey = self._survey()99 with self.login(not_creator):100 self.post("formly:survey_change_name", pk=survey.pk)101 self.response_403()102 def test_survey_change_name_anonymous(self):103 """Verify anonymous user is redirected"""104 self.post("formly:survey_change_name", pk=55)105 self.response_302()106 def test_survey_publish_creator(self):107 """Verify survey creator can publish survey"""108 survey = self._survey()109 self.assertFalse(survey.published)110 with self.login(self.user):111 self.post("formly:survey_publish", pk=survey.pk)112 self.response_302()113 survey.refresh_from_db()114 self.assertTrue(survey.published)115 def test_survey_publish_not_creator(self):116 """Verify user who didn't create survey is not allowed"""117 not_creator = self.make_user("not_creator")118 survey = self._survey()119 with self.login(not_creator):120 self.post("formly:survey_publish", pk=survey.pk)121 self.response_403()122 def test_survey_publish_anonymous(self):123 """Verify anonymous user is redirected"""124 survey = self._survey()125 self.post("formly:survey_publish", pk=survey.pk)126 self.response_302()127 self.assertFalse(survey.published)128 def test_survey_duplicate_creator(self):129 """Verify survey creator can publish survey"""130 survey = self._survey()131 page1 = self._page(survey=survey)132 field1 = self._field(survey=survey, page=page1)133 self._fieldchoice(field=field1)134 self._fieldchoice(field=field1)135 field2 = self._field(survey=survey, page=page1)136 self._fieldchoice(field=field2)137 self._fieldchoice(field=field2)138 page2 = self._page(survey=survey)139 self._field(survey=survey, page=page2)140 self._field(survey=survey, page=page2)141 with self.login(self.user):142 self.post("formly:survey_duplicate", pk=survey.pk, follow=True)143 self.response_200()144 selected = self.context["selected_survey"]145 self.assertNotEqual(selected.pk, survey.pk)146 self.assertEqual(selected.name, survey.name)147 def test_survey_duplicate_not_creator(self):148 """Verify user who didn't create survey is not allowed"""149 not_creator = self.make_user("not_creator")150 survey = self._survey()151 with self.login(not_creator):152 self.post("formly:survey_duplicate", pk=survey.pk)153 self.response_403()154 def test_survey_duplicate_anonymous(self):155 """Verify anonymous user is redirected"""156 survey = self._survey()157 self.post("formly:survey_duplicate", pk=survey.pk)158 self.response_302()159 self.assertFalse(survey.published)160 def test_page_create_anonymous_post(self):161 """Verify anonymous user is redirected"""162 self.post("formly:page_create", pk=55)163 self.response_302()164 def test_page_create_post(self):165 """Verify authenticated user can create a survey"""166 survey = self._survey()167 with self.login(self.user):168 self.post("formly:page_create", pk=survey.pk, follow=True)169 page = Page.objects.get(survey=survey)170 self.assertRedirects(self.last_response, page.get_absolute_url())171 def test_page_create_not_creator(self):172 """Verify user who didn't create survey is not allowed"""173 not_creator = self.make_user("not_creator")174 survey = self._survey()175 with self.login(not_creator):176 self.post("formly:page_create", pk=survey.pk)177 self.response_403()178 def test_field_create_anonymous_post(self):179 """Verify anonymous user is redirected"""180 self.post("formly:field_create", pk=55)181 self.response_302()182 def test_field_create_post(self):183 """Verify authenticated user can create a field"""184 survey = self._survey()185 page = self._page(survey=survey)186 with self.login(self.user):187 self.post("formly:field_create", pk=page.pk, follow=True)188 field = Field.objects.get(page=page, survey=survey)189 self.assertRedirects(self.last_response, field.get_absolute_url())190 self.assertEqual(field.label, "New Field")191 self.assertEqual(field.field_type, Field.TEXT_FIELD)192 self.assertEqual(field.ordinal, 1)193 def test_field_create_not_creator(self):194 """Verify user who didn't create survey is not allowed"""195 not_creator = self.make_user("not_creator")196 survey = self._survey()197 page = self._page(survey=survey)198 with self.login(not_creator):199 self.post("formly:field_create", pk=page.pk)200 self.response_403()201 def test_page_update_creator_get(self):202 """Verify survey creator is allowed"""203 self.survey = self._survey()204 page1 = self._page()205 with self.login(self.user):206 self.get("formly:page_update", pk=page1.pk)207 self.response_200()208 page = self.context["page"]209 self.assertEqual(page, page1)210 self.assertTrue(self.context["form"])211 def test_page_update_not_creator_get(self):212 """Verify user who didn't create survey is not allowed"""213 not_creator = self.make_user("not_creator")214 self.survey = self._survey()215 page = self._page()216 self._page() # Create another page for good measure217 with self.login(not_creator):218 self.get("formly:page_update", pk=page.pk)219 self.response_403()220 def test_page_update_page_update(self):221 """Verify survey creator can update page"""222 self.survey = self._survey()223 page1 = self._page()224 post_data = dict(225 action="page_update",226 subtitle="Submarine Title",227 )228 with self.login(self.user):229 self.post("formly:page_update", pk=page1.pk, data=post_data, follow=True)230 page = self.context["selected_page"]231 self.assertRedirects(self.last_response, page.get_absolute_url())232 self.assertEqual(page.subtitle, post_data["subtitle"])233 def test_page_update_add_field(self):234 """Verify survey creator can add field to page"""235 self.survey = self._survey()236 page1 = self._page()237 post_data = {238 "action": "field_add",239 "fields-label": "Field Hockey",240 "fields-field_type": Field.TEXT_FIELD,241 "fields-expected_answers": 1,242 }243 with self.login(self.user):244 self.post("formly:page_update", pk=page1.pk, data=post_data, follow=True)245 page = self.context["selected_page"]246 self.assertRedirects(self.last_response, page.get_absolute_url())247 self.assertTrue(248 Field.objects.get(label=post_data["fields-label"], survey=self.survey, page=page1)249 )250 def test_field_move_up_not_creator(self):251 """Verify user who didn't create survey is not allowed"""252 not_creator = self.make_user("not_creator")253 self.survey = self._survey()254 page = self._page()255 field = self._field(page=page)256 with self.login(not_creator):257 self.post("formly:field_move_up", pk=field.pk)258 self.response_403()259 def test_field_move_up_anonymous_post(self):260 """Verify anonymous user is redirected"""261 self.post("formly:field_move_up", pk=55)262 self.response_302()263 def test_field_move_up(self):264 """Verify survey creator can move a field up"""265 self.survey = self._survey()266 page = self._page()267 field1 = self._field(page=page, ordinal=1)268 field2 = self._field(page=page, ordinal=2)269 with self.login(self.user):270 self.post("formly:field_move_up", pk=field2.pk)271 field1.refresh_from_db()272 field2.refresh_from_db()273 self.assertTrue(field2.ordinal < field1.ordinal)274 def test_field_move_down_not_creator(self):275 """Verify user who didn't create survey is not allowed"""276 not_creator = self.make_user("not_creator")277 self.survey = self._survey()278 page = self._page()279 field = self._field(page=page)280 with self.login(not_creator):281 self.post("formly:field_move_down", pk=field.pk)282 self.response_403()283 def test_field_move_down_anonymous_post(self):284 """Verify anonymous user is redirected"""285 self.post("formly:field_move_down", pk=55)286 self.response_302()287 def test_field_move_down(self):288 """Verify survey creator can move a field down"""289 self.survey = self._survey()290 page = self._page()291 field1 = self._field(page=page, ordinal=1)292 field2 = self._field(page=page, ordinal=2)293 with self.login(self.user):294 self.post("formly:field_move_down", pk=field1.pk)295 field1.refresh_from_db()296 field2.refresh_from_db()297 self.assertTrue(field2.ordinal < field1.ordinal)298 def test_survey_results_not_creator(self):299 """Verify user who didn't create survey is not allowed"""300 survey = self._survey()301 not_creator = self.make_user("not_creator")302 with self.login(not_creator):303 self.get("formly:survey_results", pk=survey.pk)304 self.response_403()305 def test_survey_results(self):306 """Verify survey creator can obtain survey results"""307 survey = self._survey()308 with self.login(self.user):309 self.get("formly:survey_results", pk=survey.pk)310 self.assertTemplateUsed(template_name="formly/results/home.html")311 def test_remap_answer_get(self):312 survey = self._survey()313 page = self._page(survey=survey)314 surveyresult = self._surveyresult(survey=survey)315 field = self._field(316 page=page,317 survey=survey,318 field_type=Field.MULTIPLE_TEXT,319 expected_answers=4320 )321 fieldresult = self._fieldresult(322 survey=survey,323 question=field,324 answer={"answer": ["thing", "thiing", "thang", "tang"]},325 page=page,326 result=surveyresult327 )328 with self.login(self.user):329 self.get(330 "formly:survey_results_remap",331 pk=fieldresult.pk,332 answer_string="thing",333 )334 self.assertTemplateUsed(template_name="formly/results/remap.html")335 def test_remap_answer_post(self):336 survey = self._survey()337 page = self._page(survey=survey)338 surveyresult = self._surveyresult(survey=survey)339 field = self._field(page=page, survey=survey, field_type=Field.MULTIPLE_TEXT, expected_answers=4)340 fieldresult = self._fieldresult(341 survey=survey,342 question=field,343 answer={"answer": ["thing", "thiing", "thang", "tang"]},344 page=page,345 result=surveyresult346 )347 with self.login(self.user):348 post_data = {349 "mapping": ["thiing"]350 }351 self.post(352 "formly:survey_results_remap",353 pk=fieldresult.pk,354 answer_string="thing",355 data=post_data,356 )357 self.response_302()358 def test_remap_answer_post_ajax(self):359 survey = self._survey()360 page = self._page(survey=survey)361 surveyresult = self._surveyresult(survey=survey)362 field = self._field(page=page, survey=survey, field_type=Field.MULTIPLE_TEXT, expected_answers=4)363 fieldresult = self._fieldresult(364 survey=survey,365 question=field,366 answer={"answer": ["thing", "thiing", "thang", "tang"]},367 page=page,368 result=surveyresult369 )370 with self.login(self.user):371 post_data = {372 "mapping": ["thiing"]373 }374 self.post(375 "formly:survey_results_remap",376 pk=fieldresult.pk,377 answer_string="thing",378 data=post_data,379 extra={380 "HTTP_X_REQUESTED_WITH": "XMLHttpRequest"381 }382 )383 self.response_200()384 def test_field_add_choice_not_creator(self):385 """Verify user who didn't create survey is not allowed"""386 not_creator = self.make_user("not_creator")387 self.survey = self._survey()388 page = self._page()389 field = self._field(page=page)390 with self.login(not_creator):391 self.post("formly:field_add_choice", pk=field.pk)392 self.response_403()393 def test_field_add_choice(self):394 """Verify survey creator can add field choice"""395 self.survey = self._survey()396 page1 = self._page()397 field1 = self._field(page=page1)398 post_data = {399 "choices-label": "New Field Choice!"400 }401 original_choices = FieldChoice.objects.count()402 with self.login(self.user):403 self.post("formly:field_add_choice", pk=field1.pk, data=post_data, follow=True)404 self.assertRedirects(self.last_response, field1.get_absolute_url())405 self.assertEqual(FieldChoice.objects.count(), original_choices + 1)406 def test_field_add_choice_bad_data(self):407 """Verify survey creator sees expected response with bad POST data"""408 self.survey = self._survey()409 page1 = self._page()410 field1 = self._field(page=page1)411 post_data = {412 "nothing": "New Field Choice!"413 }414 with self.login(self.user):415 self.post("formly:field_add_choice", pk=field1.pk, data=post_data)416 self.response_200()417 self.assertTemplateUsed(template_name="formly/design/survey_list.html")418 def test_likert_scale_set_anonymous(self):419 """Verify anonymous user is redirected"""420 self.post("formly:ajax_likert_scale_set", field_pk=55, scale_pk=56)421 self.response_302()422 def test_likert_scale_set(self):423 """Verify authenticated user can set a field ordinal scale"""424 self.survey = self._survey()425 page = self._page()426 field = self._field(page=page, field_type=Field.LIKERT_FIELD)427 scale = self._ordinal_scale(kind=OrdinalScale.ORDINAL_KIND_LIKERT)428 self.assertFalse(field.scale) # ensure no scale associated429 with self.login(self.user):430 self.post("formly:ajax_likert_scale_set", field_pk=field.pk, scale_pk=scale.pk)431 field.refresh_from_db()432 self.assertEqual(field.scale, scale)433 self.assertTemplateUsed(template_name="formly/design/_likert_scales.html")434 def test_likert_scale_create_anonymous(self):435 """Verify anonymous user is redirected"""436 self.post("formly:ajax_likert_scale_create", field_pk=55)437 self.response_302()438 def test_likert_scale_create(self):439 """Verify authenticated user can create a field ordinal scale"""440 self.survey = self._survey()441 page = self._page()442 field = self._field(page=page, field_type=Field.LIKERT_FIELD)443 self.assertFalse(field.scale) # ensure no scale associated444 post_data = dict(445 name="Tiny Scale",446 scale="1,2,3",447 )448 with self.login(self.user):449 self.post("formly:ajax_likert_scale_create", field_pk=field.pk, data=post_data)450 field.refresh_from_db()451 self.assertTrue(field.scale)452 self.assertEqual(field.scale.kind, OrdinalScale.ORDINAL_KIND_LIKERT)453 self.assertTemplateUsed(template_name="formly/design/_likert_scale_form.html")454 self.assertTemplateUsed(template_name="formly/design/_likert_scales.html")455 def test_rating_scale_set_anonymous(self):456 """Verify anonymous user is redirected"""457 self.post("formly:ajax_rating_scale_set", field_pk=55, scale_pk=56)458 self.response_302()459 def test_rating_scale_set(self):460 """Verify authenticated user can set a field ordinal scale"""461 self.survey = self._survey()462 page = self._page()463 field = self._field(page=page, field_type=Field.RATING_FIELD)464 scale = self._ordinal_scale(kind=OrdinalScale.ORDINAL_KIND_RATING)465 self.assertFalse(field.scale) # ensure no scale associated466 with self.login(self.user):467 self.post("formly:ajax_rating_scale_set", field_pk=field.pk, scale_pk=scale.pk)468 field.refresh_from_db()469 self.assertEqual(field.scale, scale)470 self.assertTemplateUsed(template_name="formly/design/_rating_scales.html")471 def test_rating_scale_create_anonymous(self):472 """Verify anonymous user is redirected"""473 self.post("formly:ajax_rating_scale_create", field_pk=55)474 self.response_302()475 def test_rating_scale_create(self):476 """Verify authenticated user can create a field ordinal scale"""477 self.survey = self._survey()478 page = self._page()479 field = self._field(page=page, field_type=Field.RATING_FIELD)480 self.assertFalse(field.scale) # ensure no scale associated481 post_data = dict(482 name="Bigger Scale",483 scale="1,2,3,4,5,6",484 )485 with self.login(self.user):486 self.post("formly:ajax_rating_scale_create", field_pk=field.pk, data=post_data)487 field.refresh_from_db()488 self.assertTrue(field.scale)489 self.assertEqual(field.scale.kind, OrdinalScale.ORDINAL_KIND_RATING)490 self.assertTemplateUsed(template_name="formly/design/_rating_scale_form.html")491 self.assertTemplateUsed(template_name="formly/design/_rating_scales.html")492 def test_field_update_anonymous(self):493 """Verify anonymous user is redirected"""494 self.post("formly:field_update", pk=55)495 self.response_302()496 def test_field_update_bad_data(self):497 """Verify expected response when POST data is bad/incomplete"""498 self.survey = self._survey()499 page = self._page()500 field = self._field(page=page, field_type=Field.RATING_FIELD)501 post_data = dict(502 field_type=Field.TEXT_FIELD,503 expected_answers=1,504 )505 with self.login(self.user):506 self.post("formly:field_update", pk=field.pk, data=post_data)507 self.response_200()508 self.assertTemplateUsed(template_name="formly/design/survey_list.html")509 def test_field_update(self):510 """Verify field is updated as expected"""511 self.survey = self._survey()512 page = self._page()513 field = self._field(page=page, field_type=Field.RATING_FIELD)514 post_data = dict(515 action="field_update",516 label="Field Hockey",517 field_type=Field.TEXT_FIELD,518 expected_answers=1,519 )520 with self.login(self.user):521 self.post("formly:field_update", pk=field.pk, data=post_data)522 self.assertRedirects(self.last_response, field.get_absolute_url())523 def test_field_update_not_creator(self):524 """Verify user who didn't create survey is not allowed"""525 not_creator = self.make_user("not_creator")526 self.survey = self._survey()527 page = self._page()528 field = self._field(page=page)529 with self.login(not_creator):530 self.post("formly:field_update", pk=field.pk)531 self.response_403()532 def test_choice_delete_anonymous(self):533 """Verify anonymous user is redirected"""534 self.post("formly:ajax_choice_delete", pk=55)535 self.response_302()536 def test_choice_delete_not_creator(self):537 """Verify user who didn't create survey is not allowed"""538 not_creator = self.make_user("not_creator")539 self.survey = self._survey()540 page = self._page()541 field = self._field(page=page)542 choice = self._fieldchoice(field=field)543 with self.login(not_creator):544 self.post("formly:ajax_choice_delete", pk=choice.pk)545 self.response_403()546 def test_choice_delete(self):547 """Verify survey creator can delete field choice"""548 self.survey = self._survey()549 page = self._page()550 field = self._field(page=page)551 choice = self._fieldchoice(field=field)552 self.assertIn(choice, field.choices.all())553 self.assertEqual(field.choices.count(), 1)554 with self.login(self.user):555 self.post("formly:ajax_choice_delete", pk=choice.pk)556 self.response_200()557 self.assertFalse(field.choices.all())558 def test_choice_update_anonymous(self):559 """Verify anonymous user is redirected"""560 self.post("formly:choice_update", pk=55)561 self.response_302()562 def test_choice_update_not_creator(self):563 """Verify user who didn't create survey is not allowed"""564 not_creator = self.make_user("not_creator")565 self.survey = self._survey()566 page = self._page()567 field = self._field(page=page)568 choice = self._fieldchoice(field=field)569 with self.login(not_creator):570 self.post("formly:choice_update", pk=choice.pk)571 self.response_403()572 def test_choice_update_get(self):573 """Verify survey creator can access field choice update"""574 self.survey = self._survey()575 page = self._page()576 field = self._field(page=page)577 choice = self._fieldchoice(field=field)578 with self.login(self.user):579 self.get("formly:choice_update", pk=choice.pk)580 self.response_200()581 self.assertTemplateUsed(template_name="formly/design/choice_form.html")582 def test_choice_update(self):583 """Verify survey creator can update field choice"""584 self.survey = self._survey()585 page = self._page()586 field = self._field(page=page)587 choice = self._fieldchoice(field=field)588 post_data = dict(589 label="New Choice",590 )591 with self.login(self.user):592 self.post("formly:choice_update", pk=choice.pk, data=post_data)593 choice.refresh_from_db()594 self.assertEqual(choice.label, post_data["label"])595 def test_survey_delete_view(self):596 """Verify survey creator can delete survey"""597 self.survey = self._survey()598 with self.login(self.user):599 self.post("formly:survey_delete", pk=self.survey.pk)600 self.assertFalse(Survey.objects.count())601 def test_survey_delete_view_not_creator(self):602 """Verify user who didn't create survey is not allowed"""603 not_creator = self.make_user("not_creator")604 self.survey = self._survey()605 with self.login(not_creator):606 self.post("formly:survey_delete", pk=self.survey.pk)607 self.response_403()608 def test_page_delete_view(self):609 """Verify survey creator can delete survey"""610 self.survey = self._survey()611 page = self._page()612 with self.login(self.user):613 self.post("formly:page_delete", pk=page.pk)614 self.assertFalse(Page.objects.count())615 def test_page_delete_view_not_creator(self):616 """Verify user who didn't create page is not allowed"""617 not_creator = self.make_user("not_creator")618 self.survey = self._survey()619 page = self._page()620 with self.login(not_creator):621 self.post("formly:page_delete", pk=page.pk)622 self.response_403()623 def test_field_delete_view(self):624 """Verify survey creator can delete survey"""625 self.survey = self._survey()626 page = self._page()627 field = self._field(page=page)628 with self.login(self.user):629 self.post("formly:field_delete", pk=field.pk)630 self.assertFalse(Field.objects.count())631 def test_field_delete_view_not_creator(self):632 """Verify user who didn't create field is not allowed"""633 not_creator = self.make_user("not_creator")634 self.survey = self._survey()635 page = self._page()636 field = self._field(page=page)637 with self.login(not_creator):638 self.post("formly:field_delete", pk=field.pk)639 self.response_403()640 def test_choice_delete_view(self):641 """Verify survey creator can delete survey"""642 self.survey = self._survey()643 page = self._page()644 field = self._field(page=page)645 choice = self._fieldchoice(field=field)646 with self.login(self.user):647 self.post("formly:choice_delete", pk=choice.pk)648 self.assertFalse(FieldChoice.objects.count())649 def test_choice_delete_view_not_creator(self):650 """Verify survey creator can delete survey"""651 not_creator = self.make_user("not_creator")652 self.survey = self._survey()653 page = self._page()654 field = self._field(page=page)655 choice = self._fieldchoice(field=field)656 with self.login(not_creator):657 self.post("formly:choice_delete", pk=choice.pk)658 self.response_403()659 def test_take_survey(self):660 self.survey = self._survey()661 page1 = self._page()662 field1 = self._field(page=page1)663 page2 = self._page()664 field2 = self._field(page=page2)665 survey_taker = self.make_user("survey_taker")666 with self.login(survey_taker):667 # Start the survey668 self.get("formly:take_survey", pk=self.survey.pk)669 # Post answers to first page670 post_data = {671 "{}".format(field1.name): "Five",672 }673 self.post("formly:take_survey", pk=self.survey.pk, data=post_data)674 self.assertRedirects(self.last_response, self.survey.get_run_url())675 # Post answers to second page676 post_data = {677 "{}".format(field2.name): "Six",678 }679 self.post("formly:take_survey", pk=self.survey.pk, data=post_data)680 self.response_302()681 self.assertEqual(field1.results.get().answer, {"answer": "Five"})...

Full Screen

Full Screen

tests.py

Source:tests.py Github

copy

Full Screen

...35 'password1': 'zxczxc123',36 'password2': 'zxczxc123'37 }38 self.post("registration_register", data=data)39 self.response_302()40 # self.assertFalse(self.get_context("form").errors)41 self.assertTrue(42 User.objects.filter(username='u4',43 profile__type='hitman').exists())44 with self.subTest("Add user to the boss"):45 user = User.objects.get(username="u4")46 with self.login(username="u2"):47 data = {48 'user': user.id,49 'manager': User.objects.get(username="u2").id50 }51 self.post("manage", data=data)52 self.response_302()53 user_boss = User.objects.get(username="u2")54 self.assertTrue(55 user_boss.profile.manages.filter(id=user.id).exists())56 with self.subTest("Create hit to the new user"):57 with self.login(username="u2"):58 data = {59 'assigned': user.id,60 'target': self.seeder.faker.name(),61 'description': self.seeder.faker.sentence(),62 }63 self.post("create_hit", data=data)64 self.response_302()65 self.assertTrue(66 Hit.objects.filter(assigned=user,67 status="assigned").count() == 1)68 with self.subTest("Change status of the assigment"):69 with self.login(username="u4", password='zxczxc123'):70 hit = Hit.objects.filter(assigned=user,71 status="assigned").last()72 data = {'change_status': 'completed'}73 self.post("update_hit", pk=hit.id, data=data)74 hit = Hit.objects.get(id=hit.id)75 self.assertTrue(hit.status == 'completed')76 with self.subTest("Check listening assinged and created by"):77 with self.login(username="u2"):78 self.get("dashboard")79 table = self.get_context("table")80 hits = Hit.objects.filter(81 Q(assigned__username="u2") | Q(created_by__username="u2"))82 self.assertTrue(hits.count() == table.data.data.count())83 def test_bigboss_flow(self):84 with self.login(username="u3"):85 with self.subTest("View all hits for the bigboss"):86 self.get("dashboard")87 table = self.get_context("table")88 hits = Hit.objects.all()89 self.assertTrue(hits.count() == table.data.data.count())90 with self.subTest("The bigboss cann't change hit closed"):91 hit = Hit.objects.all().last()92 hit.status = "failed"93 hit.save()94 self.get("hit_view", pk=hit.id)95 self.response_200()96 self.assertFalse("form_assigned" in self.last_response.context)97 with self.subTest("The bigboss can change assigment"):98 hit = Hit.objects.all().last()99 hit.status = "assigned"100 hit.save()101 self.get("hit_view", pk=hit.id)102 self.response_200()103 self.assertInContext("form_assigned")104 data = {105 'assigned': 'u3',106 }107 self.post("update_hit", pk=hit.id, data=data)108 self.response_302()109 with self.subTest("the bigboss can assign hitmans to the boss"):110 self.get_check_200("manage")111 user = self.make_user("u5")112 Profile.objects.create(type="hitman", user=user)113 data = {114 'manager': User.objects.get(username="u2").id,115 'user': user.id,116 }117 self.post("manage", data=data)118 self.response_302()119 boss = Profile.objects.get(user__username="u2")120 self.assertTrue(boss.manages.filter(id=user.id).exists())121 with self.subTest("remove the hitman"):122 data = {123 'manager': User.objects.get(username="u2").id,124 'user': user.id,125 }126 self.post("manage", data=data)127 self.response_302()128 boss = Profile.objects.get(user__username="u2")129 self.assertFalse(boss.manages.filter(id=user.id).exists())130 def test_flow_manage_users(self):131 with self.login(username="u1"):132 self.get("hitmen")133 self.response_404()134 with self.subTest("boss can view here people"):135 with self.login(username="u2"):136 self.get("hitmen")137 self.response_200()138 self.assertInContext("table")139 table = self.get_context("table")140 self.assertEqual(1, table.data.data.count())141 with self.subTest("leader can view all"):142 with self.login(username="u3"):143 self.get("hitmen")144 self.response_200()145 table = self.get_context("table")146 self.assertTrue(table.data.data.count() > 1)147 with self.subTest("leader can change status of hitman"):148 with self.login(username="u3"):149 profile = Profile.objects.get(type="hitman")150 data = {151 'status': 'inactive',152 'description': 'Deactivated!',153 'type': profile.type154 }155 self.post("hitman_update", pk=profile.id, data=data)156 self.response_302()157 profile = Profile.objects.get(type="hitman")158 self.assertEqual(profile.status, 'inactive')159 def test_boss_hitman_flow(self):160 with self.subTest("boss can change status of hitman"):161 with self.login(username="u2"):162 self.assertEqual(User.objects.get(username="u2").profile.type,163 "boss")164 profile = Profile.objects.get(type="hitman")165 data = {166 'status': 'inactive',167 'description': 'Deactivated!',168 }169 self.post("hitman_update", pk=profile.id, data=data)170 self.response_302()171 profile = Profile.objects.get(type="hitman")172 self.assertEqual(profile.status, 'inactive')173 def test_bulk_flow(self):174 with self.login(username="u3"):175 self.get_check_200("bulk")176 user = Profile.objects.get(type="hitman").user177 self.post("bulk", data={'assigned': user.id})178 self.response_302()179class TestHitman(TestMixin, TestCase):180 def test_views(self):181 self.get_check_200("index")182 with self.login(username="u2"):183 self.get("index")184 self.response_302()185 self.get("/hits")186 self.response_301()187 self.get("/hitmen")188 self.response_301()189 self.get("/hits/bulk")190 self.response_302()191 self.get("/register")192 self.response_301()193 self.get("/logout")194 self.response_301()195 def test_hit_detail(self):196 h = Hit.objects.filter(assigned__username="u1").first()197 with self.subTest("view own hit") and self.login(username="u1"):198 self.get_check_200("hit_view", pk=h.id)199 h = Hit.objects.exclude(assigned__username="u1").first()200 with self.subTest("view others hit") and self.login(username="u1"):201 self.get("hit_view", pk=h.id)202 self.response_404()203 def test_create_hit(self):204 user = User.objects.get(username='u1')205 with self.login(username='u2'):206 self.get("create_hit")207 form = self.get_context("form")208 self.assertTrue(form.fields["assigned"].queryset.count() == 1)209 data = {210 'target': "target1",211 'description': "description",212 'assigned': user.id,213 }214 self.post("create_hit", data=data)215 self.response_302()216 def test_assing_user(self):217 user = User.objects.get(username='u1')218 with self.login(username=user.username):219 response = self.get("dashboard")220 qs = response.context["table"].data.data221 for i in qs:222 with self.subTest(i=i):223 self.assertTrue(i.assigned == user)224 def test_update_view(self):225 h = Hit.objects.filter(assigned__username="u1").first()226 h.status = 'assigned'227 h.save()228 with self.login(username="u1"):229 data = {'change_status': 'failed'}230 with self.subTest("success"):231 response = self.post("update_hit", pk=h.id, data=data)232 self.response_302(response)233 h = Hit.objects.get(id=h.id)234 self.assertTrue(h.status == 'failed', h.status)235 with self.subTest("notFound"):236 h = Hit.objects.exclude(assigned__username="u1").first()237 oldstatus = h.status238 response = self.post("update_hit", pk=h.id, data=data)239 self.response_302(response)240 h = Hit.objects.get(id=h.id)...

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