How to use test_make_user method in Django Test Plus

Best Python code snippet using django-test-plus_python

test_ipseity.py

Source:test_ipseity.py Github

copy

Full Screen

...100 self.assertEqual(101 ipseity.blueprint.BLUEPRINT.config['VERIFY_KEY'],102 pubkey103 )104 def test_make_user(self):105 make_user_response = self.app.post("/user",106 data={'user': "foo", 'pass': "bar"})107 self.assertEqual(make_user_response.status_code, 201)108 def test_make_user_json(self):109 make_user_response = self.app.post("/user",110 json={'user': "foo", 'pass': "bar"})111 self.assertEqual(make_user_response.status_code, 201)112 def test_user_bounce(self):113 self.test_make_user()114 make_user_response = self.app.post("/user",115 data={'user': "foo", 'pass': "bar"})116 self.assertEqual(make_user_response.status_code, 403)117 def test_make_and_authenticate(self):118 self.test_make_user()119 authentication_response = self.app.get("/token",120 data={'user': 'foo', 'pass': 'bar'})121 self.assertEqual(authentication_response.status_code, 200)122 authentication_token = authentication_response.data.decode()123 decoded_token = jwt.decode(124 authentication_token,125 ipseity.blueprint.BLUEPRINT.config['VERIFY_KEY'],126 algorithm=ipseity.blueprint.BLUEPRINT.config['ALGO']127 )128 self.assertEqual(decoded_token['user'], 'foo')129 def test_bad_login(self):130 self.test_make_user()131 authentication_response = self.app.get("/token",132 data={'user': 'foo', 'pass': 'bazz'})133 self.assertEqual(authentication_response.status_code, 404)134 def test_nonexistant_user(self):135 authentication_response = self.app.get("/token",136 data={'user': 'nothere', 'pass': 'a'})137 self.assertEqual(authentication_response.status_code, 404)138 def test_validate_token_json(self):139 self.test_make_user()140 authentication_response = self.app.get("/token",141 data={'user': 'foo', 'pass': 'bar'})142 self.assertEqual(authentication_response.status_code, 200)143 authentication_token = authentication_response.data.decode()144 token_check_response = self.app.get("/check", data={'access_token': authentication_token})145 self.assertEqual(token_check_response.status_code, 200)146 token_check_json = json.loads(token_check_response.data.decode())147 self.assertEqual(token_check_json['user'], 'foo')148 def test_validate_token_header(self):149 self.test_make_user()150 authentication_response = self.app.get("/token",151 data={'user': 'foo', 'pass': 'bar'})152 self.assertEqual(authentication_response.status_code, 200)153 authentication_token = authentication_response.data.decode()154 token_check_response = self.app.get(155 "/user",156 headers={157 "Authorization": "Bearer {}".format(158 authentication_token159 )160 }161 )162 self.assertEqual(token_check_response.status_code, 200)163 def test_invalid_token(self):164 self.test_make_user()165 authentication_response = self.app.get("/token",166 data={'user': 'foo', 'pass': 'bar'})167 self.assertEqual(authentication_response.status_code, 200)168 authentication_token = authentication_response.data.decode()169 if authentication_token[0] != 'a':170 authentication_token = 'a' + authentication_token[1:]171 else:172 authentication_token = 'b' + authentication_token[1:]173 token_check_response = self.app.get("/check", data={'access_token': authentication_token})174 self.assertEqual(token_check_response.status_code, 400)175 def test_decoratored_endpoint(self):176 self.test_make_user()177 authentication_response = self.app.get("/token",178 data={'user': 'foo', 'pass': 'bar'})179 self.assertEqual(authentication_response.status_code, 200)180 authentication_token = authentication_response.data.decode()181 test_response = self.app.get("/user", data={"access_token": authentication_token})182 self.assertEqual(test_response.status_code, 200)183 def test_change_pass(self):184 self.test_make_user()185 authentication_response = self.app.get("/token",186 data={'user': 'foo', 'pass': 'bar'})187 self.assertEqual(authentication_response.status_code, 200)188 authentication_token = authentication_response.data.decode()189 # Change the password190 change_pass_response = self.app.patch(191 "/user",192 data={193 "pass": "baz",194 "access_token": authentication_token195 }196 )197 # Test to be sure the new password is valid198 self.assertEqual(change_pass_response.status_code, 200)199 authentication_response = self.app.get("/token",200 data={'user': 'foo', 'pass': 'baz'})201 self.assertEqual(authentication_response.status_code, 200)202 # Test to be sure the old password is now invalid203 self.assertEqual(change_pass_response.status_code, 200)204 authentication_response = self.app.get("/token",205 data={'user': 'foo', 'pass': 'bar'})206 self.assertEqual(authentication_response.status_code, 404)207 def test_delete_account(self):208 self.test_make_user()209 authentication_response = self.app.get("/token",210 data={'user': 'foo', 'pass': 'bar'})211 self.assertEqual(authentication_response.status_code, 200)212 access_token = authentication_response.data.decode()213 del_self_response = self.app.delete(214 "/user",215 data={'access_token': access_token}216 )217 self.assertEqual(del_self_response.status_code, 204)218 authentication_response = self.app.get("/token",219 data={'user': 'foo', 'pass': 'bar'})220 self.assertEqual(authentication_response.status_code, 403)221 def test_refresh_token(self):222 self.test_make_user()223 # Get an access token224 authentication_response = self.app.get("/token",225 data={'user': 'foo', 'pass': 'bar'})226 self.assertEqual(authentication_response.status_code, 200)227 access_token = authentication_response.data.decode()228 # Use our first access token to generate a refresh token229 refresh_token_response = self.app.get("/refresh_token",230 data={'access_token': access_token})231 self.assertEqual(refresh_token_response.status_code, 200)232 refresh_token = refresh_token_response.data.decode()233 # Use the refresh token to get a new access token234 second_authentication_response = self.app.get("/token",235 data={'user': refresh_token})236 self.assertEqual(second_authentication_response.status_code, 200)237 second_access_token = second_authentication_response.data.decode()238 # Use our second access token to remove the refresh token239 deactivate_refresh_response = self.app.delete(240 "/refresh_token",241 data={242 "access_token": second_access_token,243 "refresh_token": refresh_token244 }245 )246 self.assertEqual(deactivate_refresh_response.status_code, 204)247 # Be sure we can't use that refresh token anymore248 # We get a 400 when we try249 third_auth_attempt_response = self.app.get("/token",250 data={"user": refresh_token})251 self.assertEqual(third_auth_attempt_response.status_code, 400)252 def test_token_limitations(self):253 self.test_make_user()254 # Get an access token255 authentication_response = self.app.get("/token",256 data={'user': 'foo', 'pass': 'bar'})257 self.assertEqual(authentication_response.status_code, 200)258 access_token = authentication_response.data.decode()259 # Use our first access token to generate a refresh token260 refresh_token_response = self.app.get("/refresh_token",261 data={'access_token': access_token})262 self.assertEqual(refresh_token_response.status_code, 200)263 refresh_token = refresh_token_response.data.decode()264 # Use the refresh token to get a new access token265 second_authentication_response = self.app.get("/token",266 data={'user': refresh_token})267 self.assertEqual(second_authentication_response.status_code, 200)268 second_access_token = second_authentication_response.data.decode()269 # Now that we've got our refresh token based access token, lets270 # try to do all the things we can't.271 delete_me_response = self.app.delete("/user",272 data={'access_token': second_access_token})273 self.assertEqual(delete_me_response.status_code, 403)274 new_refresh_token_response = self.app.get("/refresh_token",275 data={'access_token': second_access_token})276 self.assertEqual(new_refresh_token_response.status_code, 403)277 chpass_response = self.app.patch("/user",278 data={'access_token': second_access_token,279 'pass': 'buzz'})280 self.assertEqual(chpass_response.status_code, 403)281 def test_refresh_token_as_access_token(self):282 self.test_make_user()283 # Get an access token284 authentication_response = self.app.get("/token",285 data={'user': 'foo', 'pass': 'bar'})286 self.assertEqual(authentication_response.status_code, 200)287 access_token = authentication_response.data.decode()288 # Use our first access token to generate a refresh token289 refresh_token_response = self.app.get("/refresh_token",290 data={'access_token': access_token})291 self.assertEqual(refresh_token_response.status_code, 200)292 refresh_token = refresh_token_response.data.decode()293 # Try to use a refresh token as an access_token294 refresh_as_access_response = self.app.get("/check",295 data={'access_token': refresh_token})296 self.assertEqual(refresh_as_access_response.status_code, 400)297 # Try to use an access token as a refresh_token298 access_as_refresh_response = self.app.get("/token",299 data={'user': access_token})300 self.assertEqual(access_as_refresh_response.status_code, 400)301 def test_expired_refresh_token(self):302 ipseity.blueprint.BLUEPRINT.config['REFRESH_EXP_DELTA'] = 5303 self.test_make_user()304 # Get an access token305 authentication_response = self.app.get("/token",306 data={'user': 'foo', 'pass': 'bar'})307 self.assertEqual(authentication_response.status_code, 200)308 access_token = authentication_response.data.decode()309 # Use our first access token to generate a refresh token310 refresh_token_response = self.app.get("/refresh_token",311 data={'access_token': access_token})312 self.assertEqual(refresh_token_response.status_code, 200)313 refresh_token = refresh_token_response.data.decode()314 sleep(7) # Let the refresh token expire315 second_authentication_response = self.app.get("/token",316 data={'user': refresh_token})317 self.assertEqual(second_authentication_response.status_code, 400)318 del ipseity.blueprint.BLUEPRINT.config['REFRESH_EXP_DELTA']319 def test_expired_access_token(self):320 ipseity.blueprint.BLUEPRINT.config['ACCESS_EXP_DELTA'] = 5321 self.test_make_user()322 # Get an access token323 authentication_response = self.app.get("/token",324 data={'user': 'foo', 'pass': 'bar'})325 self.assertEqual(authentication_response.status_code, 200)326 access_token = authentication_response.data.decode()327 sleep(7)328 check_response = self.app.get("/check",329 data={'access_token': access_token})330 self.assertEqual(check_response.status_code, 400)331 del ipseity.blueprint.BLUEPRINT.config['ACCESS_EXP_DELTA']332 def test_disallowed_token_pruning(self):333 ipseity.blueprint.BLUEPRINT.config['REFRESH_EXP_DELTA'] = 10334 self.test_make_user()335 # Get an access token336 authentication_response = self.app.get("/token",337 data={'user': 'foo', 'pass': 'bar'})338 self.assertEqual(authentication_response.status_code, 200)339 access_token = authentication_response.data.decode()340 # Use our first access token to generate a lot of refresh tokens341 refresh_tokens = []342 for _ in range(50):343 refresh_token_response = self.app.get("/refresh_token",344 data={'access_token': access_token})345 self.assertEqual(refresh_token_response.status_code, 200)346 refresh_tokens.append(refresh_token_response.data.decode())347 # Then delete them all348 for x in refresh_tokens:349 refresh_token_delete_response = \350 self.app.delete("/refresh_token",351 data={"access_token": access_token,352 "refresh_token": x})353 self.assertEqual(refresh_token_delete_response.status_code, 204)354 # grab our user document now - all the deleted tokens should be in there355 user_doc = ipseity.blueprint.BLUEPRINT.config['authentication_coll'].find_one(356 {"user": "foo"}357 )358 self.assertEqual(len(user_doc['disallowed_tokens']), 50)359 # Wait for them to expire360 sleep(11)361 # Fire a functionality which prunes the database362 # [authentication, getting a refresh token, deleting a refresh token]363 # We'll use authentication364 second_access_token_response = self.app.get("/token",365 data={"user": "foo", "pass": "bar"})366 self.assertEqual(second_access_token_response.status_code, 200)367 # Now grab the user document again, the old tokens should be pruned368 user_doc = ipseity.blueprint.BLUEPRINT.config['authentication_coll'].find_one(369 {"user": "foo"}370 )371 self.assertEqual(len(user_doc['disallowed_tokens']), 0)372 def test_unauthorized_access(self):373 r = self.app.get("/refresh_token")374 self.assertEqual(r.status_code, 401)375 r = self.app.get("/user")376 self.assertEqual(r.status_code, 204)377 r = self.app.delete("/user")378 self.assertEqual(r.status_code, 401)379 r = self.app.patch("/user")380 self.assertEqual(r.status_code, 401)381 def test_malformed_token(self):382 r = self.app.get("/user",383 data={"access_token": "abc123"})384 self.assertEqual(r.status_code, 204)385 def test_delete_access_token(self):386 self.test_make_user()387 # Get an access token388 authentication_response = self.app.get("/token",389 data={'user': 'foo', 'pass': 'bar'})390 self.assertEqual(authentication_response.status_code, 200)391 access_token = authentication_response.data.decode()392 delete_access_token_response = \393 self.app.delete(394 "/refresh_token",395 data={396 "access_token": access_token,397 "refresh_token": access_token398 }399 )400 self.assertEqual(delete_access_token_response.status_code, 400)401 def test_delete_nonexistant_refresh_token(self):402 self.test_make_user()403 # Get an access token404 authentication_response = self.app.get("/token",405 data={'user': 'foo', 'pass': 'bar'})406 self.assertEqual(authentication_response.status_code, 200)407 access_token = authentication_response.data.decode()408 delete_bad_token_response = \409 self.app.delete(410 "/refresh_token",411 data={412 "access_token": access_token,413 "refresh_token": "abc123"414 }415 )416 self.assertEqual(delete_bad_token_response.status_code, 400)...

Full Screen

Full Screen

make_user.py

Source:make_user.py Github

copy

Full Screen

...12print(a)13def format_user(user):14 return ', '.join([str(i) for i in user.values()])15print(format_user(a))16def test_make_user():17 bob = make_user('Bob', 42)18 assert len(bob) == 219 assert 'name' in bob20 assert 'age' in bob21 assert bob['name'] == 'Bob'22 assert bob['age'] == 4223def test_format_user():24 assert format_user(make_user('Joe', 30)) == 'Joe, 30'25 assert format_user(make_user('Ann', 20)) == 'Ann, 20'26test_format_user()...

Full Screen

Full Screen

test_factory.py

Source:test_factory.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2from . import UserFactory3from apps.customuser.models import User4from hamcrest import assert_that, instance_of5def test_make_user(django_db_setup):6 user = UserFactory()...

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