Best Python code snippet using tempest_python
test_user_view_unit.py
Source:test_user_view_unit.py
1import json2from app.api.users import views3from app.tests import mock_objects4# Test user creation passes5def test_add_user(test_app, monkeypatch):6 monkeypatch.setattr(7 views, "get_user_by_email", mock_objects.get_no_user_by_email,8 )9 monkeypatch.setattr(views, "add_user", mock_objects.add_user)10 client = test_app.test_client()11 response = client.post(12 "/users",13 data=json.dumps(14 {15 "username": "test_user",16 "email": "test_user@email.com",17 "password": "test_password",18 }19 ),20 headers={21 "Accept": "application/json",22 "Content-Type": "application/json",23 },24 )25 assert response.status_code == 20126 data = response.get_json()27 assert "id" in data.keys()28 assert "test_user@email.com" in data["message"]29# Test user creation fails due to empty data30def test_add_user_empty_data(test_app):31 client = test_app.test_client()32 response = client.post(33 "/users",34 data=json.dumps({}),35 headers={36 "Accept": "application/json",37 "Content-Type": "application/json",38 },39 )40 assert response.status_code == 40041 data = response.get_json()42 assert "Input payload validation failed" in data["message"]43# Test user creation fails due to invalid data44def test_add_user_invalid_data(test_app):45 client = test_app.test_client()46 response = client.post(47 "/users",48 data=json.dumps({"email": "test_user@email.com"}),49 headers={50 "Accept": "application/json",51 "Content-Type": "application/json",52 },53 )54 assert response.status_code == 40055 data = response.get_json()56 assert "Input payload validation failed" in data["message"]57# Test user creation fails due to duplicate entry58def test_add_user_duplicate_email(test_app, monkeypatch):59 monkeypatch.setattr(60 views, "get_user_by_email", mock_objects.get_user_by_email,61 )62 monkeypatch.setattr(views, "add_user", mock_objects.add_user)63 client = test_app.test_client()64 response = client.post(65 "/users",66 data=json.dumps(67 {68 "username": "test_user",69 "email": "test_user@email.com",70 "password": "test_password",71 }72 ),73 headers={74 "Accept": "application/json",75 "Content-Type": "application/json",76 },77 )78 assert response.status_code == 40079 data = response.get_json()80 assert "test_user@email.com is already registered" in data["message"]81# Test user creation fails due to invalid content-type header82def test_add_user_invalid_header(test_app):83 client = test_app.test_client()84 response = client.post(85 "/users",86 data=json.dumps({"email": "test_user@email.com"}),87 headers={"Accept": "application/json"},88 )89 assert response.status_code == 41590 data = response.get_json()91 assert "define Content-Type header" in data["message"]92 response = client.post(93 "/users",94 data=json.dumps({"email": "test_user@email.com"}),95 headers={"Content-Type": "application/json"},96 )97 assert response.status_code == 41598 data = response.get_json()99 assert "supported is application/json" in data["message"]100# Test fetching user list passes101def test_get_users(test_app, monkeypatch):102 monkeypatch.setattr(103 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,104 )105 monkeypatch.setattr(views, "get_all_users", mock_objects.get_all_users)106 client = test_app.test_client()107 response = client.get(108 "/users",109 headers={110 "Accept": "application/json",111 "Authorization": "Bearer access_token",112 },113 )114 assert response.status_code == 200115 data = response.get_json()116 assert len(data) == 2117 assert "test_user_one" in data[0]["username"]118 assert "test_user_one@mail.com" in data[0]["email"]119 assert "password" not in data[0]120 assert "test_user_two" in data[1]["username"]121 assert "test_user_two@mail.com" in data[1]["email"]122 assert "password" not in data[1]123# Test fetching user list fails due to missing token124def test_get_users_missing_token(test_app, monkeypatch):125 monkeypatch.setattr(126 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,127 )128 monkeypatch.setattr(views, "get_all_users", mock_objects.get_all_users)129 client = test_app.test_client()130 response = client.get("/users", headers={"Accept": "application/json"})131 assert response.status_code == 403132 data = response.get_json()133 assert "Token required" in data["message"]134# Test fetching user list fails due to expired token135def test_get_users_expired_token(test_app, monkeypatch):136 monkeypatch.setattr(137 views,138 "get_user_id_by_token",139 mock_objects.get_expired_token_exception,140 )141 monkeypatch.setattr(views, "get_all_users", mock_objects.get_all_users)142 client = test_app.test_client()143 response = client.get(144 "/users",145 headers={146 "Accept": "application/json",147 "Authorization": "Bearer access_token",148 },149 )150 assert response.status_code == 401151 data = response.get_json()152 assert "Token expired" in data["message"]153# Test fetching user list fails due to invalid token154def test_get_users_invalid_token(test_app, monkeypatch):155 monkeypatch.setattr(156 views,157 "get_user_id_by_token",158 mock_objects.get_invalid_token_exception,159 )160 monkeypatch.setattr(views, "get_all_users", mock_objects.get_all_users)161 client = test_app.test_client()162 response = client.get(163 "/users",164 headers={165 "Accept": "application/json",166 "Authorization": "Bearer access_token",167 },168 )169 assert response.status_code == 401170 data = response.get_json()171 assert "Invalid token" in data["message"]172# Test fetching single user passes173def test_single_user(test_app, monkeypatch):174 monkeypatch.setattr(175 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,176 )177 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)178 client = test_app.test_client()179 response = client.get(180 "/users/1",181 headers={182 "Accept": "application/json",183 "Authorization": "Bearer access_token",184 },185 )186 assert response.status_code == 200187 data = response.get_json()188 assert data["id"] == 1, data189 assert data["username"] == "test_user"190 assert data["email"] == "test_user@mail.com"191 assert "password" not in data.keys()192# Test fetching single user fails due to incorrect id193def test_single_user_invalid_id(test_app, monkeypatch):194 monkeypatch.setattr(195 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,196 )197 monkeypatch.setattr(198 views, "get_user_by_id", mock_objects.get_no_user_by_id199 )200 client = test_app.test_client()201 response = client.get(202 "/users/1",203 headers={204 "Accept": "application/json",205 "Authorization": "Bearer access_token",206 },207 )208 assert response.status_code == 404209 data = response.get_json()210 assert "does not exist" in data["message"]211# Test fetching single user fails due to missing token212def test_single_user_missing_token(test_app, monkeypatch):213 monkeypatch.setattr(214 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,215 )216 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)217 client = test_app.test_client()218 response = client.get("/users/1", headers={"Accept": "application/json"})219 assert response.status_code == 403220 data = response.get_json()221 assert "Token required" in data["message"]222# Test fetching single user fails due to expired token223def test_single_user_expired_token(test_app, monkeypatch):224 monkeypatch.setattr(225 views,226 "get_user_id_by_token",227 mock_objects.get_expired_token_exception,228 )229 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)230 client = test_app.test_client()231 response = client.get(232 "/users/1",233 headers={234 "Accept": "application/json",235 "Authorization": "Bearer access_token",236 },237 )238 assert response.status_code == 401239 data = response.get_json()240 assert "Token expired" in data["message"]241# Test fetching single user fails due to invalid token242def test_single_user_invalid_token(test_app, monkeypatch):243 monkeypatch.setattr(244 views,245 "get_user_id_by_token",246 mock_objects.get_invalid_token_exception,247 )248 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)249 client = test_app.test_client()250 response = client.get(251 "/users/1",252 headers={253 "Accept": "application/json",254 "Authorization": "Bearer access_token",255 },256 )257 assert response.status_code == 401258 data = response.get_json()259 assert "Invalid token" in data["message"]260# Test removing a user passes261def test_remove_user(test_app, monkeypatch):262 monkeypatch.setattr(263 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,264 )265 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)266 monkeypatch.setattr(views, "remove_user", mock_objects.remove_user)267 client = test_app.test_client()268 response = client.delete(269 "/users/1",270 headers={271 "Accept": "application/json",272 "Authorization": "Bearer access_token",273 },274 )275 assert response.status_code == 204276# Test removing a user fails due to invalid id277def test_remove_user_invalid_id(test_app, monkeypatch):278 monkeypatch.setattr(279 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,280 )281 monkeypatch.setattr(282 views, "get_user_by_id", mock_objects.get_no_user_by_id283 )284 client = test_app.test_client()285 response = client.delete(286 "/users/1",287 headers={288 "Accept": "application/json",289 "Authorization": "Bearer access_token",290 },291 )292 assert response.status_code == 404293 data = response.get_json()294 assert "does not exist" in data["message"]295# Test removing a user fails due to missing token296def test_remove_user_missing_token(test_app, monkeypatch):297 monkeypatch.setattr(298 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,299 )300 monkeypatch.setattr(301 views, "get_user_by_id", mock_objects.get_no_user_by_id302 )303 client = test_app.test_client()304 response = client.delete(305 "/users/1", headers={"Accept": "application/json"}306 )307 assert response.status_code == 403308 data = response.get_json()309 assert "Token required" in data["message"]310# Test removing a user fails due to expired token311def test_remove_user_expired_token(test_app, monkeypatch):312 monkeypatch.setattr(313 views,314 "get_user_id_by_token",315 mock_objects.get_expired_token_exception,316 )317 monkeypatch.setattr(318 views, "get_user_by_id", mock_objects.get_no_user_by_id319 )320 client = test_app.test_client()321 response = client.delete(322 "/users/1",323 headers={324 "Accept": "application/json",325 "Authorization": "Bearer access_token",326 },327 )328 assert response.status_code == 401329 data = response.get_json()330 assert "Token expired" in data["message"]331# Test removing a user fails due to invalid token332def test_remove_user_invalid_token(test_app, monkeypatch):333 monkeypatch.setattr(334 views,335 "get_user_id_by_token",336 mock_objects.get_invalid_token_exception,337 )338 monkeypatch.setattr(339 views, "get_user_by_id", mock_objects.get_no_user_by_id340 )341 client = test_app.test_client()342 response = client.delete(343 "/users/1",344 headers={345 "Accept": "application/json",346 "Authorization": "Bearer access_token",347 },348 )349 assert response.status_code == 401350 data = response.get_json()351 assert "Invalid token" in data["message"]352# Test update a user passes353def test_update_user(test_app, monkeypatch):354 monkeypatch.setattr(355 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,356 )357 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)358 monkeypatch.setattr(views, "update_user", mock_objects.update_user)359 client = test_app.test_client()360 response = client.put(361 "/users/1",362 data=json.dumps(363 {364 "username": "test_user_update",365 "email": "test_user_update@mail.com",366 }367 ),368 headers={369 "Accept": "application/json",370 "Authorization": "Bearer access_token",371 "Content-Type": "application/json",372 },373 )374 assert response.status_code == 200375 data = response.get_json()376 assert data["id"] == 1377 assert data["username"] == "test_user_update"378 assert data["email"] == "test_user_update@mail.com"379# Test update a user fails due to empty data380def test_update_user_empty_data(test_app, monkeypatch):381 monkeypatch.setattr(382 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,383 )384 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)385 monkeypatch.setattr(views, "update_user", mock_objects.update_user)386 client = test_app.test_client()387 response = client.put(388 "/users/1",389 data=json.dumps({}),390 headers={391 "Accept": "application/json",392 "Authorization": "Bearer access_token",393 "Content-Type": "application/json",394 },395 )396 assert response.status_code == 400397 data = response.get_json()398 assert "Input payload validation failed" in data["message"]399# Test update a user fails due to invalid id400def test_update_user_invalid_id(test_app, monkeypatch):401 monkeypatch.setattr(402 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,403 )404 monkeypatch.setattr(405 views, "get_user_by_id", mock_objects.get_no_user_by_id406 )407 client = test_app.test_client()408 response = client.put(409 "/users/1",410 data=json.dumps(411 {412 "username": "test_user_update",413 "email": "test_user_update@mail.com",414 }415 ),416 headers={417 "Accept": "application/json",418 "Authorization": "Bearer access_token",419 "Content-Type": "application/json",420 },421 )422 assert response.status_code == 404423 data = response.get_json()424 assert "does not exist" in data["message"]425# Test update a user fails due to invalid headers426def test_update_user_invalid_headers(test_app):427 client = test_app.test_client()428 response = client.put(429 "/users",430 data=json.dumps({"email": "test_user@email.com"}),431 headers={"Accept": "application/json"},432 )433 assert response.status_code == 415434 data = response.get_json()435 assert "define Content-Type header" in data["message"]436 response = client.put(437 "/users",438 data=json.dumps({"email": "test_user@email.com"}),439 headers={"Content-Type": "application/json"},440 )441 assert response.status_code == 415442 data = response.get_json()443 assert "supported is application/json" in data["message"]444# Test update a user fails due to missing token445def test_update_user_missing_token(test_app, monkeypatch):446 monkeypatch.setattr(447 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,448 )449 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)450 client = test_app.test_client()451 response = client.put(452 "/users/1",453 data=json.dumps(454 {455 "username": "test_user_update",456 "email": "test_user_update@mail.com",457 }458 ),459 headers={460 "Accept": "application/json",461 "Content-Type": "application/json",462 },463 )464 assert response.status_code == 403465 data = response.get_json()466 assert "Token required" in data["message"]467# Test update a user fails due to expired token468def test_update_user_expired_token(test_app, monkeypatch):469 monkeypatch.setattr(470 views,471 "get_user_id_by_token",472 mock_objects.get_expired_token_exception,473 )474 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)475 client = test_app.test_client()476 response = client.put(477 "/users/1",478 data=json.dumps(479 {480 "username": "test_user_update",481 "email": "test_user_update@mail.com",482 }483 ),484 headers={485 "Accept": "application/json",486 "Authorization": "Bearer access_token",487 "Content-Type": "application/json",488 },489 )490 assert response.status_code == 401491 data = response.get_json()492 assert "Token expired" in data["message"]493# Test update a user fails due to invalid token494def test_update_user_invalid_token(test_app, monkeypatch):495 monkeypatch.setattr(496 views,497 "get_user_id_by_token",498 mock_objects.get_invalid_token_exception,499 )500 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)501 client = test_app.test_client()502 response = client.put(503 "/users/1",504 data=json.dumps(505 {506 "username": "test_user_update",507 "email": "test_user_update@mail.com",508 }509 ),510 headers={511 "Accept": "application/json",512 "Authorization": "Bearer access_token",513 "Content-Type": "application/json",514 },515 )516 assert response.status_code == 401517 data = response.get_json()...
test_user_view.py
Source:test_user_view.py
1import json2from app.api.users import views3from tests import mock_objects4# Test fetching user list passes5def test_get_users(test_app, monkeypatch):6 monkeypatch.setattr(7 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,8 )9 monkeypatch.setattr(views, "get_all_users", mock_objects.get_all_users)10 client = test_app.test_client()11 response = client.get(12 "/users",13 headers={14 "Accept": "application/json",15 "Authorization": "Bearer access_token",16 },17 )18 assert response.status_code == 20019 data = response.get_json()20 assert len(data) == 221 assert "test_user_one" in data[0]["username"]22 assert "test_user_one@mail.com" in data[0]["email"]23 assert "password" not in data[0]24 assert "test_user_two" in data[1]["username"]25 assert "test_user_two@mail.com" in data[1]["email"]26 assert "password" not in data[1]27# Test fetching user list fails due to missing token28def test_get_users_missing_token(test_app, monkeypatch):29 monkeypatch.setattr(30 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,31 )32 monkeypatch.setattr(views, "get_all_users", mock_objects.get_all_users)33 client = test_app.test_client()34 response = client.get("/users", headers={"Accept": "application/json"})35 assert response.status_code == 40336 data = response.get_json()37 assert "Token required" in data["message"]38# Test fetching user list fails due to expired token39def test_get_users_expired_token(test_app, monkeypatch):40 monkeypatch.setattr(41 views,42 "get_user_id_by_token",43 mock_objects.get_expired_token_exception,44 )45 monkeypatch.setattr(views, "get_all_users", mock_objects.get_all_users)46 client = test_app.test_client()47 response = client.get(48 "/users",49 headers={50 "Accept": "application/json",51 "Authorization": "Bearer access_token",52 },53 )54 assert response.status_code == 40155 data = response.get_json()56 assert "Token expired" in data["message"]57# Test fetching user list fails due to invalid token58def test_get_users_invalid_token(test_app, monkeypatch):59 monkeypatch.setattr(60 views,61 "get_user_id_by_token",62 mock_objects.get_invalid_token_exception,63 )64 monkeypatch.setattr(views, "get_all_users", mock_objects.get_all_users)65 client = test_app.test_client()66 response = client.get(67 "/users",68 headers={69 "Accept": "application/json",70 "Authorization": "Bearer access_token",71 },72 )73 assert response.status_code == 40174 data = response.get_json()75 assert "Invalid token" in data["message"]76# Test fetching single user passes77def test_single_user(test_app, monkeypatch):78 monkeypatch.setattr(79 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,80 )81 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)82 client = test_app.test_client()83 response = client.get(84 "/users/1",85 headers={86 "Accept": "application/json",87 "Authorization": "Bearer access_token",88 },89 )90 assert response.status_code == 20091 data = response.get_json()92 assert data["id"] == 1, data93 assert data["username"] == "test_user"94 assert data["email"] == "test_user@mail.com"95 assert "password" not in data.keys()96# Test fetching single user fails due to incorrect id97def test_single_user_invalid_id(test_app, monkeypatch):98 monkeypatch.setattr(99 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,100 )101 monkeypatch.setattr(102 views, "get_user_by_id", mock_objects.get_no_user_by_id103 )104 client = test_app.test_client()105 response = client.get(106 "/users/1",107 headers={108 "Accept": "application/json",109 "Authorization": "Bearer access_token",110 },111 )112 assert response.status_code == 404113 data = response.get_json()114 assert "does not exist" in data["message"]115# Test fetching single user fails due to missing token116def test_single_user_missing_token(test_app, monkeypatch):117 monkeypatch.setattr(118 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,119 )120 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)121 client = test_app.test_client()122 response = client.get("/users/1", headers={"Accept": "application/json"})123 assert response.status_code == 403124 data = response.get_json()125 assert "Token required" in data["message"]126# Test fetching single user fails due to expired token127def test_single_user_expired_token(test_app, monkeypatch):128 monkeypatch.setattr(129 views,130 "get_user_id_by_token",131 mock_objects.get_expired_token_exception,132 )133 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)134 client = test_app.test_client()135 response = client.get(136 "/users/1",137 headers={138 "Accept": "application/json",139 "Authorization": "Bearer access_token",140 },141 )142 assert response.status_code == 401143 data = response.get_json()144 assert "Token expired" in data["message"]145# Test fetching single user fails due to invalid token146def test_single_user_invalid_token(test_app, monkeypatch):147 monkeypatch.setattr(148 views,149 "get_user_id_by_token",150 mock_objects.get_invalid_token_exception,151 )152 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)153 client = test_app.test_client()154 response = client.get(155 "/users/1",156 headers={157 "Accept": "application/json",158 "Authorization": "Bearer access_token",159 },160 )161 assert response.status_code == 401162 data = response.get_json()163 assert "Invalid token" in data["message"]164# Test removing a user passes165def test_remove_user(test_app, monkeypatch):166 monkeypatch.setattr(167 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,168 )169 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)170 monkeypatch.setattr(views, "remove_user", mock_objects.remove_user)171 client = test_app.test_client()172 response = client.delete(173 "/users/1",174 headers={175 "Accept": "application/json",176 "Authorization": "Bearer access_token",177 },178 )179 assert response.status_code == 204180# Test removing a user fails due to invalid id181def test_remove_user_invalid_id(test_app, monkeypatch):182 monkeypatch.setattr(183 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,184 )185 monkeypatch.setattr(186 views, "get_user_by_id", mock_objects.get_no_user_by_id187 )188 client = test_app.test_client()189 response = client.delete(190 "/users/1",191 headers={192 "Accept": "application/json",193 "Authorization": "Bearer access_token",194 },195 )196 assert response.status_code == 404197 data = response.get_json()198 assert "does not exist" in data["message"]199# Test removing a user fails due to missing token200def test_remove_user_missing_token(test_app, monkeypatch):201 monkeypatch.setattr(202 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,203 )204 monkeypatch.setattr(205 views, "get_user_by_id", mock_objects.get_no_user_by_id206 )207 client = test_app.test_client()208 response = client.delete(209 "/users/1", headers={"Accept": "application/json"}210 )211 assert response.status_code == 403212 data = response.get_json()213 assert "Token required" in data["message"]214# Test removing a user fails due to expired token215def test_remove_user_expired_token(test_app, monkeypatch):216 monkeypatch.setattr(217 views,218 "get_user_id_by_token",219 mock_objects.get_expired_token_exception,220 )221 monkeypatch.setattr(222 views, "get_user_by_id", mock_objects.get_no_user_by_id223 )224 client = test_app.test_client()225 response = client.delete(226 "/users/1",227 headers={228 "Accept": "application/json",229 "Authorization": "Bearer access_token",230 },231 )232 assert response.status_code == 401233 data = response.get_json()234 assert "Token expired" in data["message"]235# Test removing a user fails due to invalid token236def test_remove_user_invalid_token(test_app, monkeypatch):237 monkeypatch.setattr(238 views,239 "get_user_id_by_token",240 mock_objects.get_invalid_token_exception,241 )242 monkeypatch.setattr(243 views, "get_user_by_id", mock_objects.get_no_user_by_id244 )245 client = test_app.test_client()246 response = client.delete(247 "/users/1",248 headers={249 "Accept": "application/json",250 "Authorization": "Bearer access_token",251 },252 )253 assert response.status_code == 401254 data = response.get_json()255 assert "Invalid token" in data["message"]256# Test update a user passes257def test_update_user(test_app, monkeypatch):258 monkeypatch.setattr(259 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,260 )261 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)262 monkeypatch.setattr(views, "update_user", mock_objects.update_user)263 client = test_app.test_client()264 response = client.put(265 "/users/1",266 data=json.dumps(267 {268 "username": "test_user_update",269 "email": "test_user_update@mail.com",270 }271 ),272 headers={273 "Accept": "application/json",274 "Authorization": "Bearer access_token",275 "Content-Type": "application/json",276 },277 )278 assert response.status_code == 200279 data = response.get_json()280 assert data["id"] == 1281 assert data["username"] == "test_user_update"282 assert data["email"] == "test_user_update@mail.com"283# Test update a user fails due to empty data284def test_update_user_empty_data(test_app, monkeypatch):285 monkeypatch.setattr(286 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,287 )288 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)289 monkeypatch.setattr(views, "update_user", mock_objects.update_user)290 client = test_app.test_client()291 response = client.put(292 "/users/1",293 data=json.dumps({}),294 headers={295 "Accept": "application/json",296 "Authorization": "Bearer access_token",297 "Content-Type": "application/json",298 },299 )300 assert response.status_code == 400301 data = response.get_json()302 assert "Input payload validation failed" in data["message"]303# Test update a user fails due to invalid id304def test_update_user_invalid_id(test_app, monkeypatch):305 monkeypatch.setattr(306 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,307 )308 monkeypatch.setattr(309 views, "get_user_by_id", mock_objects.get_no_user_by_id310 )311 client = test_app.test_client()312 response = client.put(313 "/users/1",314 data=json.dumps(315 {316 "username": "test_user_update",317 "email": "test_user_update@mail.com",318 }319 ),320 headers={321 "Accept": "application/json",322 "Authorization": "Bearer access_token",323 "Content-Type": "application/json",324 },325 )326 assert response.status_code == 404327 data = response.get_json()328 assert "does not exist" in data["message"]329# Test update a user fails due to invalid headers330def test_update_user_invalid_headers(test_app):331 client = test_app.test_client()332 response = client.put(333 "/users",334 data=json.dumps({"email": "test_user@email.com"}),335 headers={"Accept": "application/json"},336 )337 assert response.status_code == 415338 data = response.get_json()339 assert "define Content-Type header" in data["message"]340 response = client.put(341 "/users",342 data=json.dumps({"email": "test_user@email.com"}),343 headers={"Content-Type": "application/json"},344 )345 assert response.status_code == 415346 data = response.get_json()347 assert "supported is application/json" in data["message"]348# Test update a user fails due to missing token349def test_update_user_missing_token(test_app, monkeypatch):350 monkeypatch.setattr(351 views, "get_user_id_by_token", mock_objects.get_user_id_by_token,352 )353 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)354 client = test_app.test_client()355 response = client.put(356 "/users/1",357 data=json.dumps(358 {359 "username": "test_user_update",360 "email": "test_user_update@mail.com",361 }362 ),363 headers={364 "Accept": "application/json",365 "Content-Type": "application/json",366 },367 )368 assert response.status_code == 403369 data = response.get_json()370 assert "Token required" in data["message"]371# Test update a user fails due to expired token372def test_update_user_expired_token(test_app, monkeypatch):373 monkeypatch.setattr(374 views,375 "get_user_id_by_token",376 mock_objects.get_expired_token_exception,377 )378 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)379 client = test_app.test_client()380 response = client.put(381 "/users/1",382 data=json.dumps(383 {384 "username": "test_user_update",385 "email": "test_user_update@mail.com",386 }387 ),388 headers={389 "Accept": "application/json",390 "Authorization": "Bearer access_token",391 "Content-Type": "application/json",392 },393 )394 assert response.status_code == 401395 data = response.get_json()396 assert "Token expired" in data["message"]397# Test update a user fails due to invalid token398def test_update_user_invalid_token(test_app, monkeypatch):399 monkeypatch.setattr(400 views,401 "get_user_id_by_token",402 mock_objects.get_invalid_token_exception,403 )404 monkeypatch.setattr(views, "get_user_by_id", mock_objects.get_user_by_id)405 client = test_app.test_client()406 response = client.put(407 "/users/1",408 data=json.dumps(409 {410 "username": "test_user_update",411 "email": "test_user_update@mail.com",412 }413 ),414 headers={415 "Accept": "application/json",416 "Authorization": "Bearer access_token",417 "Content-Type": "application/json",418 },419 )420 assert response.status_code == 401421 data = response.get_json()...
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!!