Best Python code snippet using django-test-plus_python
test_views.py
Source:test_views.py
...17 user_factory = UserFactory18 def test_get_create_journey(self):19 user = self.make_user(username="foo")20 url_name = "journeys:create"21 self.assertLoginRequired(url_name)22 with self.login(user):23 response = self.get(url_name)24 self.response_200(response=response)25 def test_post_create_journey(self):26 user = self.make_user(username="foo")27 self.assertLoginRequired("journeys:create")28 with self.login(user):29 data = {30 "origin": "residence:%s" % ResidenceFactory(user=user).pk,31 "destiny": "campus:%s" % CampusFactory().pk,32 "free_places": 4,33 "time_window": 30,34 "departure": (timezone.now() + datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S"),35 "recurrence": "",36 }37 response = self.post(url_name="journeys:create", data=data)38 self.response_302(response)39 self.assertEquals(1, Journey.objects.count())40 def test_post_create_arrival_journey(self):41 user = self.make_user(username="foo")42 self.assertLoginRequired("journeys:create")43 with self.login(user):44 data = {45 "origin": "residence:%s" % ResidenceFactory(user=user).pk,46 "destiny": "campus:%s" % CampusFactory().pk,47 "free_places": 4,48 "time_window": 30,49 "departure": (timezone.now() + datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S"),50 "arrival": (timezone.now() + datetime.timedelta(days=1, minutes=30)).strftime("%Y-%m-%d %H:%M:%S"),51 "recurrence": "",52 }53 response = self.post(url_name="journeys:create", data=data)54 self.response_302(response)55 self.assertEquals(1, Journey.objects.count())56 def test_post_create_bad_arrival_journey(self):57 user = self.make_user(username="foo")58 self.assertLoginRequired("journeys:create")59 with self.login(user):60 data = {61 "origin": "residence:%s" % ResidenceFactory(user=user).pk,62 "destiny": "campus:%s" % CampusFactory().pk,63 "free_places": 4,64 "time_window": 30,65 "departure": (timezone.now() + datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S"),66 "arrival": (timezone.now() + datetime.timedelta(minutes=30)).strftime("%Y-%m-%d %H:%M:%S"),67 "recurrence": "",68 }69 response = self.post(url_name="journeys:create", data=data)70 self.response_200(response)71 self.assertEquals(0, Journey.objects.count())72 def test_post_create_bad_departure_journey(self):73 user = self.make_user(username="foo")74 self.assertLoginRequired("journeys:create")75 with self.login(user):76 data = {77 "origin": "residence:%s" % ResidenceFactory(user=user).pk,78 "destiny": "campus:%s" % CampusFactory().pk,79 "free_places": 4,80 "time_window": 30,81 "departure": (timezone.now() - datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S"),82 "arrival": (timezone.now() + datetime.timedelta(minutes=30)).strftime("%Y-%m-%d %H:%M:%S"),83 "recurrence": "",84 }85 response = self.post(url_name="journeys:create", data=data)86 self.response_200(response)87 self.assertEquals(0, Journey.objects.count())88 def test_get_edit_journey(self):89 user = self.make_user(username="foo")90 journey = JourneyFactory(template=JourneyTemplateFactory(user=user))91 url_name = "journeys:edit"92 self.assertLoginRequired(url_name, pk=journey.pk)93 with self.login(user):94 response = self.get(url_name, pk=journey.pk)95 self.response_200(response=response)96 def test_post_edit_journey(self):97 user = self.make_user(username="foo")98 template = JourneyTemplateFactory(user=user, kind=GOING)99 journey = JourneyFactory(template=template)100 url_name = "journeys:edit"101 self.assertLoginRequired(url_name, pk=journey.pk)102 with self.login(user):103 data = {104 # "residence": ResidenceFactory(user=user).pk,105 # "campus": CampusFactory().pk,106 # "kind": RETURN,107 "free_places": 3,108 # "time_window": 30,109 "departure": (timezone.now() + datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S"),110 "recurrence": "",111 }112 response = self.post(url_name=url_name, pk=journey.pk, data=data)113 self.response_302(response=response)114 journey = Journey.objects.get(pk=journey.pk)115 self.assertEquals(data["free_places"], journey.free_places)116 def test_get_recommended_journey(self):117 user = self.make_user(username="foo")118 url_name = "journeys:recommended"119 self.assertLoginRequired(url_name)120 with self.login(user):121 response = self.get(url_name)122 self.response_200(response=response)123 def test_get_recommended_journey_filter(self):124 user = self.make_user(username="foo")125 url_name = "journeys:recommended"126 self.assertLoginRequired(url_name)127 with self.login(user):128 response = self.get(url_name, data={"distance": 200, "kind": GOING})129 self.response_200(response=response)130 def test_get_list_journey(self):131 user = self.make_user(username="foo")132 url_name = "journeys:list"133 self.assertLoginRequired(url_name)134 with self.login(user):135 response = self.get(url_name)136 self.response_200(response=response)137 def test_post_join(self):138 user = self.make_user(username="foo")139 template = JourneyTemplateFactory(user=user, kind=GOING)140 journey = JourneyFactory(template=template)141 url_name = "journeys:join"142 self.assertLoginRequired(url_name, pk=journey.pk)143 with self.login(user):144 response = self.post(url_name=url_name, pk=journey.pk)145 self.response_302(response=response)146 def test_post_join_recurrence_all(self):147 user = self.make_user(username="foo")148 template = JourneyTemplateFactory(user=user, kind=GOING)149 journey = JourneyFactory(template=template)150 journeys = [JourneyFactory(template=template) for _ in range(10)]151 url_name = "journeys:join"152 self.assertLoginRequired(url_name, pk=journey.pk)153 with self.login(user):154 data = {155 "join_to": "all"156 }157 response = self.post(url_name=url_name, pk=journey.pk, data=data)158 self.response_302(response=response)159 self.assertEquals(len(journeys) + 1, Passenger.objects.count())160 def test_post_join_recurrence_one(self):161 user = self.make_user(username="foo")162 template = JourneyTemplateFactory(user=user, kind=GOING)163 journey = JourneyFactory(template=template)164 [JourneyFactory(template=template) for _ in range(10)]165 url_name = "journeys:join"166 self.assertLoginRequired(url_name, pk=journey.pk)167 with self.login(user):168 data = {169 "join_to": "one"170 }171 response = self.post(url_name=url_name, pk=journey.pk, data=data)172 self.response_302(response=response)173 self.assertEquals(1, Passenger.objects.count())174 def test_post_leave(self):175 user = self.make_user(username="foo")176 journey = JourneyFactory(template=JourneyTemplateFactory(user=user))177 journey.join_passenger(user)178 url_name = "journeys:leave"179 self.assertLoginRequired(url_name, pk=journey.pk)180 with self.login(user):181 response = self.post(url_name=url_name, pk=journey.pk)182 self.response_302(response=response)183 def test_post_throw_out(self):184 user = self.make_user(username="foo")185 journey = JourneyFactory(template=JourneyTemplateFactory(user=user))186 passenger = journey.join_passenger(user)187 url_name = "journeys:throw-out"188 self.assertLoginRequired(url_name, pk=passenger.pk)189 with self.login(user):190 response = self.post(url_name=url_name, pk=passenger.pk)191 self.response_302(response=response)192 def test_cancel_journey(self):193 user = self.make_user(username="foo")194 template = JourneyTemplateFactory(user=user)195 journey = JourneyFactory(template=template)196 url_name = "journeys:cancel"197 self.assertLoginRequired(url_name, pk=journey.pk)198 with self.login(user):199 response = self.get(url_name, pk=journey.pk)200 self.response_200(response=response)201 def test_post_cancel_journey(self):202 user = self.make_user(username="foo")203 template = JourneyTemplateFactory(user=user)204 journey = JourneyFactory(template=template)205 url_name = "journeys:cancel"206 self.assertLoginRequired(url_name, pk=journey.pk)207 with self.login(user):208 response = self.post(url_name, pk=journey.pk)209 self.response_302(response=response)210 self.assertTrue(Journey.objects.get(pk=journey.pk).disabled)211 def test_delete_journey(self):212 user = self.make_user(username="foo")213 journeys = [JourneyFactory(template=JourneyTemplateFactory(user=user)) for _ in range(10)]214 journey = JourneyFactory(template=JourneyTemplateFactory(user=user))215 url_name = "journeys:delete"216 self.assertLoginRequired(url_name, pk=journey.pk)217 self.assertEquals(len(journeys) + 1, Journey.objects.count())218 with self.login(user):219 response = self.get(url_name, pk=journey.pk)220 self.response_302(response=response)221 self.assertFalse(Journey.objects.filter(pk=journey.pk).exists())222 self.assertEquals(len(journeys), Journey.objects.count())223 def test_delete_parent_journeys(self):224 user = self.make_user(username="foo")225 template = JourneyTemplateFactory(user=user)226 journey = JourneyFactory(template=template)227 journeys = [JourneyFactory(template=template) for _ in range(10)]228 url_name = "journeys:delete"229 self.assertLoginRequired(url_name, pk=journey.pk)230 self.assertEquals(len(journeys) + 1, Journey.objects.count())231 with self.login(user):232 response = self.get(url_name, pk=journey.pk)233 self.response_302(response=response)234 self.assertFalse(Journey.objects.filter(pk=journey.pk).exists())235 self.assertEquals(len(journeys), Journey.objects.count())236 def test_delete_all_journeys(self):237 user = self.make_user(username="foo")238 template = JourneyTemplateFactory(user=user)239 journey = JourneyFactory(template=template)240 journeys = [JourneyFactory(template=template) for _ in range(10)]241 other_journeys = [JourneyFactory() for _ in range(5)]242 url_name = "journeys:delete-all"243 self.assertLoginRequired(url_name, pk=journey.pk)244 self.assertEquals(len(journeys) + len(other_journeys) + 1, Journey.objects.count())245 with self.login(user):246 response = self.get(url_name, pk=journey.pk)247 self.response_302(response=response)248 self.assertFalse(Journey.objects.filter(pk=journey.pk).exists())249 self.assertEquals(len(other_journeys), Journey.objects.count())250@mock.patch('users.models.UPVLoginDataService', new=UPVLoginDataService)251class ResidenceViewTests(TestCase):252 user_factory = UserFactory253 def test_get_create_residence(self):254 user = self.make_user(username="foo")255 url_name = "journeys:create-residence"256 self.assertLoginRequired(url_name)257 with self.login(user):258 response = self.get(url_name)259 self.response_200(response=response)260 def test_post_create_residence(self):261 user = self.make_user(username="foo")262 self.assertLoginRequired("journeys:create-residence")263 with self.login(user):264 data = {265 "name": "Home",266 "address": "foo",267 "position": "POINT (-0.3819 39.4625)",268 "distance": 500,269 }270 response = self.post(url_name="journeys:create-residence", data=data)271 self.response_302(response)272 self.assertEquals(1, Residence.objects.count())273 def test_get_edit_residence(self):274 user = self.make_user(username="foo")275 residence = ResidenceFactory(user=user)276 url_name = "journeys:edit-residence"277 self.assertLoginRequired(url_name, pk=residence.pk)278 with self.login(user):279 response = self.get(url_name, pk=residence.pk)280 self.response_200(response=response)281 def test_post_edit_residence(self):282 user = self.make_user(username="foo")283 residence = ResidenceFactory(user=user)284 url_name = "journeys:edit-residence"285 self.assertLoginRequired(url_name, pk=residence.pk)286 with self.login(user):287 data = {288 "name": "Home",289 "address": "bar",290 "position": "POINT (-0.3819 39.4625)",291 "distance": 500292 }293 response = self.post(url_name=url_name, pk=residence.pk, data=data)294 self.response_302(response=response)295 residence = Residence.objects.get(pk=residence.pk)296 self.assertEquals(data["address"], residence.address)297 def test_residences(self):298 user = self.make_user(username="foo")299 ResidenceFactory(user=user)300 url_name = "journeys:residences"301 self.assertLoginRequired(url_name)302 with self.login(user):303 response = self.get(url_name)304 self.response_200(response=response)305 def test_delete_residence(self):306 user = self.make_user(username="foo")307 residence = ResidenceFactory(user=user)308 url_name = "journeys:delete-residence"309 self.assertLoginRequired(url_name, pk=residence.pk)310 with self.login(user):311 response = self.get(url_name, pk=residence.pk)312 self.response_302(response=response)313 def test_no_delete_residence(self):314 initial_user = self.make_user(username="foo")315 user = UserFactory()316 residence = ResidenceFactory(user=initial_user)317 url_name = "journeys:delete-residence"318 self.assertLoginRequired(url_name, pk=residence.pk)319 with self.login(user):320 response = self.get(url_name, pk=residence.pk)...
test_oauth_routes.py
Source:test_oauth_routes.py
...15 client_type=OAuth_App.CLIENT_PUBLIC,16 authorization_grant_type=OAuth_App.GRANT_PASSWORD,17 redirect_uris="http://example.com",18 )19 self.assertLoginRequired("oauth2_provider:list")20 self.assertLoginRequired("oauth2_provider:detail", pk=app.pk)21 with self.login(basic_user):22 self.get_check_200("oauth2_provider:list")23 self.assertResponseContains("Test333", html=False)24 self.get_check_200("oauth2_provider:detail", pk=app.pk)25 self.assertResponseContains("Test333", html=False)26 def test_application_creation_workflow(self):27 basic_user = self.make_user("basic_user@test.com")28 self.assertLoginRequired("oauth2_provider:register")29 with self.login(basic_user):30 self.get_check_200("oauth2_provider:register")31 form_data = {32 "name": "Test888",33 "client_id": "client_id",34 "client_secret": "client_secret",35 "client_type": OAuth_App.CLIENT_PUBLIC,36 "redirect_uris": "http://example.com",37 "authorization_grant_type": OAuth_App.GRANT_PASSWORD38 }39 self.post("oauth2_provider:register", data=form_data)40 self.response_302()41 self.assertTrue(OAuth_App.objects.filter(name="Test888").exists())42 self.get_check_200("oauth2_provider:list")43 self.assertResponseContains("Test888", html=False)44 app_pk = OAuth_App.objects.get(name="Test888").pk45 self.get_check_200("oauth2_provider:detail", pk=app_pk)46 def test_application_update_workflow(self):47 basic_user = self.make_user("basic_user@test.com")48 app = OAuth_App.objects.create(49 user=basic_user,50 name="Test111",51 client_type=OAuth_App.CLIENT_PUBLIC,52 authorization_grant_type=OAuth_App.GRANT_PASSWORD,53 redirect_uris="http://example.com",54 )55 self.assertLoginRequired("oauth2_provider:update", pk=app.pk)56 with self.login(basic_user):57 self.get_check_200("oauth2_provider:detail", pk=app.pk)58 self.assertResponseContains("Test111", html=False)59 self.get_check_200("oauth2_provider:update", pk=app.pk)60 form_data = {61 "user": basic_user.pk,62 "name": "Test222",63 "client_id": "client_id",64 "client_secret": "client_secret",65 "client_type": OAuth_App.CLIENT_PUBLIC,66 "redirect_uris": "http://example.com",67 "authorization_grant_type": OAuth_App.GRANT_PASSWORD68 }69 self.post("oauth2_provider:update", pk=app.pk, data=form_data)70 self.response_302()71 self.assertFalse(OAuth_App.objects.filter(name="Test111").exists())72 self.assertTrue(OAuth_App.objects.filter(name="Test222").exists())73 self.get_check_200("oauth2_provider:detail", pk=app.pk)74 self.assertResponseContains("Test222", html=False)75 self.assertResponseNotContains("Test111", html=False)76 def test_application_deletion(self):77 basic_user = self.make_user("basic_user@test.com")78 app = OAuth_App.objects.create(79 user=basic_user,80 name="Test555",81 client_type=OAuth_App.CLIENT_PUBLIC,82 authorization_grant_type=OAuth_App.GRANT_PASSWORD,83 redirect_uris="http://example.com",84 )85 self.assertLoginRequired("oauth2_provider:delete", pk=app.pk)86 with self.login(basic_user):87 self.get_check_200("oauth2_provider:detail", pk=app.pk)88 self.assertResponseContains("Test555", html=False)89 self.get_check_200("oauth2_provider:delete", pk=app.pk)90 self.post("oauth2_provider:delete", pk=app.pk)91 self.response_302()92 self.assertFalse(OAuth_App.objects.filter(pk=app.pk).exists())...
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!!