How to use create_role method in tempest

Best Python code snippet using tempest_python

test_users_ws.py

Source:test_users_ws.py Github

copy

Full Screen

...55 database.session.commit()56 except:57 database.session.rollback()58def test_post_first_user_success(database, cleanup):59 new_role = new_role = create_role(*owner_role)60 database.session.add(new_role)61 new_role = create_role(*user_role)62 database.session.add(new_role)63 new_user = create_user(*user1)64 database.session.add(new_user)65 database.session.commit()66 message = {"email": "someemail@email.com", "password": "123secretpassword"}67 result = signup_user_socket(message)68 result = loads(result)69 assert result["success"] == True70 assert result["user"]["id"] == 271 assert len(result["user"]["private_key"]) == 6472 assert result["user"]["role"]["id"] == 273 assert result["user"]["email"] == "someemail@email.com"74def test_post_first_user_missing_role(client, database, cleanup):75 new_role = new_role = create_role(*owner_role)76 database.session.add(new_role)77 new_user = create_user(*user1)78 database.session.add(new_user)79 database.session.commit()80 message = {"email": "someemail@email.com", "password": "123secretpassword"}81 result = signup_user_socket(message)82 result = loads(result)83 assert result["error"] == "Role ID not found!"84def test_post_user_with_role(client, database, cleanup):85 new_role = new_role = create_role(*owner_role)86 database.session.add(new_role)87 new_role = create_role(*user_role)88 database.session.add(new_role)89 new_user = create_user(*user1)90 database.session.add(new_user)91 database.session.commit()92 message = {93 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",94 "email": "someemail@email.com",95 "password": "123secretpassword",96 "role": 1,97 }98 result = signup_user_socket(message)99 result = loads(result)100 assert result["success"] == True101 assert result["user"]["id"] == 2102 assert len(result["user"]["private_key"]) == 64103 assert result["user"]["role"]["id"] == 1104 assert result["user"]["email"] == "someemail@email.com"105def test_post_user_invalid_key(client, database, cleanup):106 new_role = new_role = create_role(*owner_role)107 database.session.add(new_role)108 new_role = create_role(*user_role)109 database.session.add(new_role)110 new_user = create_user(*user1)111 database.session.add(new_user)112 database.session.commit()113 message = {114 "private-key": "alasthiskeyisntvalid",115 "email": "someemail@email.com",116 "password": "123secretpassword",117 "role": 1,118 }119 result = signup_user_socket(message)120 result = loads(result)121 assert result["error"] == "Invalid credentials!"122def test_post_user_with_missing_role(client, database, cleanup):123 new_role = new_role = create_role(*owner_role)124 database.session.add(new_role)125 new_role = create_role(*user_role)126 database.session.add(new_role)127 new_user = create_user(*user1)128 database.session.add(new_user)129 database.session.commit()130 message = {131 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",132 "email": "someemail@email.com",133 "password": "123secretpassword",134 "role": 3,135 }136 result = signup_user_socket(message)137 result = loads(result)138 assert result["error"] == "Role ID not found!"139def test_login_user_valid_credentials(client, database, cleanup):140 new_role = new_role = create_role(*owner_role)141 database.session.add(new_role)142 new_role = create_role(*user_role)143 database.session.add(new_role)144 new_user = create_user(*user2)145 database.session.add(new_user)146 database.session.commit()147 message = {148 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",149 "email": "tech@gibberish.com",150 "password": "&UP!SN!;J4Mx;+A]",151 }152 result = login_user_socket(message)153 result = loads(result)154 assert result["success"] == True155 token = result["token"]156 content = jwt.decode(token, app.config["SECRET_KEY"], algorithms="HS256")157 assert content["id"] == 1158def test_login_user_invalid_key(client, database, cleanup):159 new_role = new_role = create_role(*owner_role)160 database.session.add(new_role)161 new_role = create_role(*user_role)162 database.session.add(new_role)163 new_user = create_user(*user2)164 database.session.add(new_user)165 database.session.commit()166 message = {167 "private-key": "imaninvalidkeyalright",168 "email": "tech@gibberish.com",169 "password": "&UP!SN!;J4Mx;+A]",170 }171 result = login_user_socket(message)172 result = loads(result)173 assert result["error"] == "Invalid credentials!"174def test_login_user_missing_key(client, database, cleanup):175 new_role = new_role = create_role(*owner_role)176 database.session.add(new_role)177 new_role = create_role(*user_role)178 database.session.add(new_role)179 new_user = create_user(*user2)180 database.session.add(new_user)181 database.session.commit()182 message = {"email": "tech@gibberish.com", "password": "&UP!SN!;J4Mx;+A]"}183 result = login_user_socket(message)184 result = loads(result)185 assert result["error"] == "Missing request key!"186def test_login_user_invalid_email(client, database, cleanup):187 new_role = new_role = create_role(*owner_role)188 database.session.add(new_role)189 new_role = create_role(*user_role)190 database.session.add(new_role)191 new_user = create_user(*user2)192 database.session.add(new_user)193 database.session.commit()194 message = {195 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",196 "email": "perhaps@perhaps.com",197 "password": "&UP!SN!;J4Mx;+A]",198 }199 result = login_user_socket(message)200 result = loads(result)201 assert result["error"] == "Invalid credentials!"202def test_login_user_invalid_password(client, database, cleanup):203 new_role = new_role = create_role(*owner_role)204 database.session.add(new_role)205 new_role = create_role(*user_role)206 database.session.add(new_role)207 new_user = create_user(*user2)208 database.session.add(new_user)209 database.session.commit()210 message = {211 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",212 "email": "tech@gibberish.com",213 "password": "@123456notmypassword",214 }215 result = login_user_socket(message)216 result = loads(result)217 assert result["error"] == "Invalid credentials!"218# GET ALL USERS219def test_get_users_success(client, database, cleanup):220 new_role = create_role(*admin_role)221 database.session.add(new_role)222 new_role = create_role(*user_role)223 database.session.add(new_role)224 new_user = create_user(*user2)225 database.session.add(new_user)226 new_user = create_user(*user3)227 database.session.add(new_user)228 database.session.commit()229 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])230 message = {231 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",232 "token": token.decode("UTF-8"),233 }234 result = get_all_users_socket(message)235 result = loads(result)236 assert len(result["users"]) == 2237 assert result["users"][0]["id"] == 1238 assert result["users"][1]["id"] == 2239def test_get_users_missing_key(client, database, cleanup):240 new_role = create_role(*admin_role)241 database.session.add(new_role)242 new_role = create_role(*user_role)243 database.session.add(new_role)244 new_user = create_user(*user2)245 database.session.add(new_user)246 new_user = create_user(*user3)247 database.session.add(new_user)248 database.session.commit()249 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])250 message = {"token": token.decode("UTF-8")}251 result = get_all_users_socket(message)252 result = loads(result)253 assert result["error"] == "Missing request key!"254def test_get_users_missing_token(client, database, cleanup):255 new_role = create_role(*admin_role)256 database.session.add(new_role)257 new_role = create_role(*user_role)258 database.session.add(new_role)259 new_user = create_user(*user2)260 database.session.add(new_user)261 new_user = create_user(*user3)262 database.session.add(new_user)263 database.session.commit()264 message = {265 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced"266 }267 result = get_all_users_socket(message)268 result = loads(result)269 assert result["error"] == "Missing request key!"270def test_get_users_invalid_key(client, database, cleanup):271 new_role = create_role(*admin_role)272 database.session.add(new_role)273 new_role = create_role(*user_role)274 database.session.add(new_role)275 new_user = create_user(*user2)276 database.session.add(new_user)277 new_user = create_user(*user3)278 database.session.add(new_user)279 database.session.commit()280 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])281 message = {282 "private-key": "invalid312987as12they0come",283 "token": token.decode("UTF-8"),284 }285 result = get_all_users_socket(message)286 result = loads(result)287 assert result["error"] == "Invalid credentials!"288def test_get_users_invalid_token(client, database, cleanup):289 new_role = create_role(*admin_role)290 database.session.add(new_role)291 new_role = create_role(*user_role)292 database.session.add(new_role)293 new_user = create_user(*user2)294 database.session.add(new_user)295 new_user = create_user(*user3)296 database.session.add(new_user)297 database.session.commit()298 token = jwt.encode({"id": 1}, "peppperplsiwouldhavesome")299 message = {300 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",301 "token": token.decode("UTF-8"),302 }303 result = get_all_users_socket(message)304 result = loads(result)305 assert result["error"] == "Invalid credentials!"306# GET SPECIFIC USER307def test_get_one_user_success(client, database, cleanup):308 new_role = create_role(*admin_role)309 database.session.add(new_role)310 new_role = create_role(*user_role)311 database.session.add(new_role)312 new_user = create_user(*user2)313 database.session.add(new_user)314 new_user = create_user(*user3)315 database.session.add(new_user)316 database.session.commit()317 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])318 message = {319 "id": 2,320 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",321 "token": token.decode("UTF-8"),322 }323 result = get_specific_user_socket(message)324 result = loads(result)325 assert result["user"]["id"] == 2326 assert result["user"]["email"] == "anemail@anemail.com"327def test_get_one_user_missing_key(client, database, cleanup):328 new_role = create_role(*admin_role)329 database.session.add(new_role)330 new_role = create_role(*user_role)331 database.session.add(new_role)332 new_user = create_user(*user2)333 database.session.add(new_user)334 new_user = create_user(*user3)335 database.session.add(new_user)336 database.session.commit()337 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])338 message = {"id": 1, "token": token.decode("UTF-8")}339 result = get_specific_user_socket(message)340 result = loads(result)341 assert result["error"] == "Missing request key!"342def test_get_one_user_missing_token(client, database, cleanup):343 new_role = create_role(*admin_role)344 database.session.add(new_role)345 new_role = create_role(*user_role)346 database.session.add(new_role)347 new_user = create_user(*user2)348 database.session.add(new_user)349 new_user = create_user(*user3)350 database.session.add(new_user)351 database.session.commit()352 message = {353 "id": 1,354 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",355 }356 result = get_specific_user_socket(message)357 result = loads(result)358 assert result["error"] == "Missing request key!"359def test_get_one_user_invalid_key(client, database, cleanup):360 new_role = create_role(*admin_role)361 database.session.add(new_role)362 new_role = create_role(*user_role)363 database.session.add(new_role)364 new_user = create_user(*user2)365 database.session.add(new_user)366 new_user = create_user(*user3)367 database.session.add(new_user)368 database.session.commit()369 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])370 message = {371 "id": 1,372 "private-key": "invalid312987as12they0come",373 "token": token.decode("UTF-8"),374 }375 result = get_specific_user_socket(message)376 result = loads(result)377 assert result["error"] == "Invalid credentials!"378def test_get_one_user_invalid_token(client, database, cleanup):379 new_role = create_role(*admin_role)380 database.session.add(new_role)381 new_role = create_role(*user_role)382 database.session.add(new_role)383 new_user = create_user(*user2)384 database.session.add(new_user)385 new_user = create_user(*user3)386 database.session.add(new_user)387 database.session.commit()388 token = jwt.encode({"id": 1}, "peppperplsiwouldhavesome")389 message = {390 "id": 2,391 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",392 "token": token.decode("UTF-8"),393 }394 result = get_specific_user_socket(message)395 result = loads(result)396 assert result["error"] == "Invalid credentials!"397def test_get_one_missing_user(client, database, cleanup):398 new_role = create_role(*admin_role)399 database.session.add(new_role)400 new_role = create_role(*user_role)401 database.session.add(new_role)402 new_user = create_user(*user2)403 database.session.add(new_user)404 new_user = create_user(*user3)405 database.session.add(new_user)406 database.session.commit()407 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])408 message = {409 "id": 3,410 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",411 "token": token.decode("UTF-8"),412 }413 result = get_specific_user_socket(message)414 result = loads(result)415 assert result["error"] == "User ID not found!"416# PUT USER EMAIL417def test_put_other_user_email_success(client, database, cleanup):418 new_role = create_role(*admin_role)419 database.session.add(new_role)420 new_role = create_role(*user_role)421 database.session.add(new_role)422 new_user = create_user(*user2)423 database.session.add(new_user)424 new_user = create_user(*user3)425 database.session.add(new_user)426 database.session.commit()427 assert database.session.query(User).get(2).email == "anemail@anemail.com"428 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])429 message = {430 "id": 2,431 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",432 "token": token.decode("UTF-8"),433 "email": "brandnew@brandnewemail.com",434 }435 result = change_user_email_socket(message)436 result = loads(result)437 assert result["user"]["id"] == 2438 assert result["user"]["email"] == "brandnew@brandnewemail.com"439 assert database.session.query(User).get(2).email == "brandnew@brandnewemail.com"440def test_put_other_user_email_missing_key(client, database, cleanup):441 new_role = create_role(*admin_role)442 database.session.add(new_role)443 new_role = create_role(*user_role)444 database.session.add(new_role)445 new_user = create_user(*user2)446 database.session.add(new_user)447 new_user = create_user(*user3)448 database.session.add(new_user)449 database.session.commit()450 assert database.session.query(User).get(2).email == "anemail@anemail.com"451 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])452 message = {453 "id": 2,454 "token": token.decode("UTF-8"),455 "email": "brandnew@brandnewemail.com",456 }457 result = change_user_email_socket(message)458 result = loads(result)459 assert result["error"] == "Missing request key!"460def test_put_other_user_email_missing_token(client, database, cleanup):461 new_role = create_role(*admin_role)462 database.session.add(new_role)463 new_role = create_role(*user_role)464 database.session.add(new_role)465 new_user = create_user(*user2)466 database.session.add(new_user)467 new_user = create_user(*user3)468 database.session.add(new_user)469 database.session.commit()470 assert database.session.query(User).get(2).email == "anemail@anemail.com"471 message = {472 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced"473 }474 message = {"id": 2, "email": "brandnew@brandnewemail.com"}475 result = change_user_email_socket(message)476 result = loads(result)477 assert result["error"] == "Missing request key!"478def test_put_user_email_invalid_key(client, database, cleanup):479 new_role = create_role(*admin_role)480 database.session.add(new_role)481 new_role = create_role(*user_role)482 database.session.add(new_role)483 new_user = create_user(*user2)484 database.session.add(new_user)485 new_user = create_user(*user3)486 database.session.add(new_user)487 database.session.commit()488 assert database.session.query(User).get(2).email == "anemail@anemail.com"489 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])490 message = {491 "id": 2,492 "private-key": "acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",493 "token": token.decode("UTF-8"),494 "email": "brandnew@brandnewemail.com",495 }496 result = change_user_email_socket(message)497 result = loads(result)498 assert result["error"] == "Invalid credentials!"499def test_put_user_email_invalid_token(client, database, cleanup):500 new_role = create_role(*admin_role)501 database.session.add(new_role)502 new_role = create_role(*user_role)503 database.session.add(new_role)504 new_user = create_user(*user2)505 database.session.add(new_user)506 new_user = create_user(*user3)507 database.session.add(new_user)508 database.session.commit()509 assert database.session.query(User).get(2).email == "anemail@anemail.com"510 token = jwt.encode({"id": 1}, "secretitis")511 message = {512 "id": 2,513 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",514 "token": token.decode("UTF-8"),515 "email": "brandnew@brandnewemail.com",516 }517 result = change_user_email_socket(message)518 result = loads(result)519 assert result["error"] == "Invalid credentials!"520def test_put_other_user_email_unauthorized(client, database, cleanup):521 new_role = create_role(*admin_role)522 database.session.add(new_role)523 new_role = create_role(*user_role)524 database.session.add(new_role)525 new_user = create_user(*user2)526 database.session.add(new_user)527 new_user = create_user(*user3)528 database.session.add(new_user)529 database.session.commit()530 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])531 message = {532 "id": 1,533 "private-key": "acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",534 "token": token.decode("UTF-8"),535 "email": "brandnew@brandnewemail.com",536 }537 result = change_user_email_socket(message)538 result = loads(result)539 assert result["error"] == "User is not authorized for this operation!"540def test_put_own_user_email_success(client, database, cleanup):541 new_role = create_role(*admin_role)542 database.session.add(new_role)543 new_role = create_role(*user_role)544 database.session.add(new_role)545 new_user = create_user(*user2)546 database.session.add(new_user)547 new_user = create_user(*user3)548 database.session.add(new_user)549 database.session.commit()550 assert database.session.query(User).get(2).email == "anemail@anemail.com"551 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])552 message = {553 "id": 2,554 "private-key": "acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",555 "token": token.decode("UTF-8"),556 "email": "brandnew@brandnewemail.com",557 }558 result = change_user_email_socket(message)559 result = loads(result)560 assert result["user"]["id"] == 2561 assert result["user"]["email"] == "brandnew@brandnewemail.com"562 assert database.session.query(User).get(2).email == "brandnew@brandnewemail.com"563def test_put_user_email_missing_role(client, database, cleanup):564 new_role = create_role(*admin_role)565 database.session.add(new_role)566 new_user = create_user(*user2)567 database.session.add(new_user)568 new_user = create_user(*user3)569 database.session.add(new_user)570 database.session.commit()571 assert database.session.query(User).get(2).email == "anemail@anemail.com"572 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])573 message = {574 "id": 2,575 "private-key": "acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",576 "token": token.decode("UTF-8"),577 "email": "brandnew@brandnewemail.com",578 }579 result = change_user_email_socket(message)580 result = loads(result)581 assert result["error"] == "Role ID not found!"582def test_put_other_user_email_missing_user(client, database, cleanup):583 new_role = create_role(*admin_role)584 database.session.add(new_role)585 new_role = create_role(*user_role)586 database.session.add(new_role)587 new_user = create_user(*user2)588 database.session.add(new_user)589 database.session.commit()590 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])591 message = {592 "id": 2,593 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",594 "token": token.decode("UTF-8"),595 "email": "brandnew@brandnewemail.com",596 }597 result = change_user_email_socket(message)598 result = loads(result)599 assert result["error"] == "User ID not found!"600# PUT USER ROLE601def test_put_other_user_role_success(client, database, cleanup):602 new_role = create_role(*admin_role)603 database.session.add(new_role)604 new_role = create_role(*user_role)605 database.session.add(new_role)606 new_user = create_user(*user2)607 database.session.add(new_user)608 new_user = create_user(*user3)609 database.session.add(new_user)610 database.session.commit()611 assert database.session.query(User).get(2).role == 2612 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])613 message = {614 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",615 "token": token.decode("UTF-8"),616 "role": 1,617 "id": 2,618 }619 result = change_user_role_socket(message)620 result = loads(result)621 assert result["user"]["id"] == 2622 assert result["user"]["role"]["id"] == 1623 assert database.session.query(User).get(2).role == 1624def test_put_other_user_role_missing_key(client, database, cleanup):625 new_role = create_role(*admin_role)626 database.session.add(new_role)627 new_role = create_role(*user_role)628 database.session.add(new_role)629 new_user = create_user(*user2)630 database.session.add(new_user)631 new_user = create_user(*user3)632 database.session.add(new_user)633 database.session.commit()634 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])635 message = {"token": token.decode("UTF-8"), "role": 1, "id": 2}636 result = change_user_role_socket(message)637 result = loads(result)638 assert result["error"] == "Missing request key!"639def test_put_other_user_role_missing_token(client, database, cleanup):640 new_role = create_role(*admin_role)641 database.session.add(new_role)642 new_role = create_role(*user_role)643 database.session.add(new_role)644 new_user = create_user(*user2)645 database.session.add(new_user)646 new_user = create_user(*user3)647 database.session.add(new_user)648 database.session.commit()649 message = {650 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced"651 }652 message = {"role": 1, "id": 2}653 result = change_user_role_socket(message)654 result = loads(result)655 assert result["error"] == "Missing request key!"656def test_put_user_role_invalid_key(client, database, cleanup):657 new_role = create_role(*admin_role)658 database.session.add(new_role)659 new_role = create_role(*user_role)660 database.session.add(new_role)661 new_user = create_user(*user2)662 database.session.add(new_user)663 new_user = create_user(*user3)664 database.session.add(new_user)665 database.session.commit()666 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])667 message = {668 "private-key": "acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",669 "token": token.decode("UTF-8"),670 "role": 1,671 "id": 2,672 }673 result = change_user_role_socket(message)674 result = loads(result)675 assert result["error"] == "Invalid credentials!"676def test_put_user_role_invalid_token(client, database, cleanup):677 new_role = create_role(*admin_role)678 database.session.add(new_role)679 new_role = create_role(*user_role)680 database.session.add(new_role)681 new_user = create_user(*user2)682 database.session.add(new_user)683 new_user = create_user(*user3)684 database.session.add(new_user)685 database.session.commit()686 token = jwt.encode({"id": 1}, "secretitis")687 message = {688 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",689 "token": token.decode("UTF-8"),690 "role": 1,691 "id": 2,692 }693 result = change_user_role_socket(message)694 result = loads(result)695 assert result["error"] == "Invalid credentials!"696def test_put_other_user_role_unauthorized(client, database, cleanup):697 new_role = create_role(*admin_role)698 database.session.add(new_role)699 new_role = create_role(*user_role)700 database.session.add(new_role)701 new_user = create_user(*user2)702 database.session.add(new_user)703 new_user = create_user(*user3)704 database.session.add(new_user)705 database.session.commit()706 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])707 message = {708 "private-key": "acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",709 "token": token.decode("UTF-8"),710 "role": 2,711 "id": 1,712 }713 result = change_user_role_socket(message)714 result = loads(result)715 assert result["error"] == "User is not authorized for this operation!"716def test_put_own_user_role_sucess(client, database, cleanup):717 new_role = new_role = create_role(*owner_role)718 database.session.add(new_role)719 new_role = create_role(*admin_role)720 database.session.add(new_role)721 new_role = create_role(*user_role)722 database.session.add(new_role)723 new_user = create_user(*user5)724 database.session.add(new_user)725 new_user = create_user(*user4)726 database.session.add(new_user)727 new_user = User(728 email="anemail@anemail.com",729 hashed_password="2amt5MXKdLhEEL8FiQLcl8Mp0FNhZI6",730 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",731 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",732 role=3,733 )734 database.session.add(new_user)735 database.session.commit()736 assert database.session.query(User).get(2).role == 2737 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])738 message = {739 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",740 "token": token.decode("UTF-8"),741 "role": 3,742 "id": 2,743 }744 result = change_user_role_socket(message)745 result = loads(result)746 assert result["user"]["id"] == 2747 assert result["user"]["role"]["id"] == 3748 assert database.session.query(User).get(2).role == 3749def test_put_first_user_unauthorized(client, database, cleanup):750 new_role = new_role = create_role(*owner_role)751 database.session.add(new_role)752 new_role = create_role(*admin_role)753 database.session.add(new_role)754 new_role = create_role(*user_role)755 database.session.add(new_role)756 new_user = create_user(*user5)757 database.session.add(new_user)758 new_user = create_user(*user4)759 database.session.add(new_user)760 new_user = User(761 email="anemail@anemail.com",762 hashed_password="2amt5MXKdLhEEL8FiQLcl8Mp0FNhZI6",763 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",764 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",765 role=3,766 )767 database.session.add(new_user)768 database.session.commit()769 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])770 message = {771 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",772 "token": token.decode("UTF-8"),773 "role": 3,774 "id": 1,775 }776 result = change_user_role_socket(message)777 result = loads(result)778 assert result["error"] == "User is not authorized for this operation!"779def test_put_other_user_role_owner_unauthorized(client, database, cleanup):780 new_role = new_role = create_role(*owner_role)781 database.session.add(new_role)782 new_role = create_role(*admin_role)783 database.session.add(new_role)784 new_role = create_role(*user_role)785 database.session.add(new_role)786 new_user = create_user(*user5)787 database.session.add(new_user)788 new_user = create_user(*user4)789 database.session.add(new_user)790 new_user = User(791 email="anemail@anemail.com",792 hashed_password="2amt5MXKdLhEEL8FiQLcl8Mp0FNhZI6",793 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",794 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",795 role=3,796 )797 database.session.add(new_user)798 database.session.commit()799 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])800 message = {801 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",802 "token": token.decode("UTF-8"),803 "role": 1,804 "id": 3,805 }806 result = change_user_role_socket(message)807 result = loads(result)808 assert result["error"] == "User is not authorized for this operation!"809def test_put_other_user_role_owner_success(client, database, cleanup):810 new_role = new_role = create_role(*owner_role)811 database.session.add(new_role)812 new_role = create_role(*admin_role)813 database.session.add(new_role)814 new_role = create_role(*user_role)815 database.session.add(new_role)816 new_user = create_user(*user5)817 database.session.add(new_user)818 new_user = create_user(*user4)819 database.session.add(new_user)820 new_user = User(821 email="anemail@anemail.com",822 hashed_password="2amt5MXKdLhEEL8FiQLcl8Mp0FNhZI6",823 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",824 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",825 role=3,826 )827 database.session.add(new_user)828 database.session.commit()829 assert database.session.query(User).get(3).role == 3830 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])831 message = {832 "private-key": "4de2d41486ceaffdf0c1778e50cea00000d6549ffe808fa860ecd4e91d9ee1b1",833 "token": token.decode("UTF-8"),834 "role": 1,835 "id": 3,836 }837 result = change_user_role_socket(message)838 result = loads(result)839 assert result["user"]["id"] == 3840 assert result["user"]["role"]["id"] == 1841 assert database.session.query(User).get(3).role == 1842def test_put_user_role_missing_role(client, database, cleanup):843 new_role = create_role(*admin_role)844 database.session.add(new_role)845 new_user = create_user(*user2)846 database.session.add(new_user)847 new_user = create_user(*user3)848 database.session.add(new_user)849 database.session.commit()850 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])851 message = {852 "private-key": "acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",853 "token": token.decode("UTF-8"),854 "role": 2,855 "id": 2,856 }857 result = change_user_role_socket(message)858 result = loads(result)859 assert result["error"] == "Role ID not found!"860def test_put_other_user_role_missing_user(client, database, cleanup):861 new_role = create_role(*admin_role)862 database.session.add(new_role)863 new_role = create_role(*user_role)864 database.session.add(new_role)865 new_user = create_user(*user2)866 database.session.add(new_user)867 database.session.commit()868 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])869 message = {870 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",871 "token": token.decode("UTF-8"),872 "role": 2,873 "id": 2,874 }875 result = change_user_role_socket(message)876 result = loads(result)877 assert result["error"] == "User ID not found!"878# PUT USER PASSWORD879def test_put_other_user_password_success(client, database, cleanup):880 new_role = create_role(*admin_role)881 database.session.add(new_role)882 new_role = create_role(*user_role)883 database.session.add(new_role)884 new_user = create_user(*user2)885 database.session.add(new_user)886 new_user = User(887 email="anemail@anemail.com",888 hashed_password="wi6hJCTz9QN1GcKc2ZJk7ReZ1LshNsu",889 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",890 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",891 role=2,892 )893 database.session.add(new_user)894 database.session.commit()895 user = database.session.query(User).get(2)896 assert checkpw(897 b"ownerpassword123@@",898 user.salt.encode("UTF-8") + user.hashed_password.encode("UTF-8"),899 )900 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])901 new_password = "BrandNewPassword123"902 message = {903 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",904 "token": token.decode("UTF-8"),905 "password": new_password,906 "id": 2,907 }908 result = change_user_password_socket(message)909 result = loads(result)910 assert result["user"]["id"] == 2911 assert checkpw(912 new_password.encode("UTF-8"),913 user.salt.encode("UTF-8") + user.hashed_password.encode("UTF-8"),914 )915def test_put_user_password_missing_key(client, database, cleanup):916 new_role = create_role(*admin_role)917 database.session.add(new_role)918 new_role = create_role(*user_role)919 database.session.add(new_role)920 new_user = create_user(*user2)921 database.session.add(new_user)922 new_user = create_user(*user3)923 database.session.add(new_user)924 database.session.commit()925 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])926 new_password = "BrandNewPassword123"927 message = {"token": token.decode("UTF-8"), "id": 2, "password": new_password}928 result = change_user_password_socket(message)929 result = loads(result)930 assert result["error"] == "Missing request key!"931def test_put_user_password_missing_token(client, database, cleanup):932 new_role = create_role(*admin_role)933 database.session.add(new_role)934 new_role = create_role(*user_role)935 database.session.add(new_role)936 new_user = create_user(*user2)937 database.session.add(new_user)938 new_user = create_user(*user3)939 database.session.add(new_user)940 database.session.commit()941 new_password = "BrandNewPassword123"942 message = {943 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",944 "id": 2,945 "password": new_password,946 }947 result = change_user_password_socket(message)948 result = loads(result)949 assert result["error"] == "Missing request key!"950def test_put_user_password_invalid_key(client, database, cleanup):951 new_role = create_role(*admin_role)952 database.session.add(new_role)953 new_role = create_role(*user_role)954 database.session.add(new_role)955 new_user = create_user(*user2)956 database.session.add(new_user)957 new_user = create_user(*user3)958 database.session.add(new_user)959 database.session.commit()960 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])961 new_password = "BrandNewPassword123"962 message = {963 "private-key": "acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",964 "token": token.decode("UTF-8"),965 "id": 2,966 "password": new_password,967 }968 result = change_user_password_socket(message)969 result = loads(result)970 assert result["error"] == "Invalid credentials!"971def test_put_user_password_invalid_token(client, database, cleanup):972 new_role = create_role(*admin_role)973 database.session.add(new_role)974 new_role = create_role(*user_role)975 database.session.add(new_role)976 new_user = create_user(*user2)977 database.session.add(new_user)978 new_user = create_user(*user3)979 database.session.add(new_user)980 database.session.commit()981 token = jwt.encode({"id": 1}, "secretitis")982 new_password = "BrandNewPassword123"983 message = {984 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",985 "token": token.decode("UTF-8"),986 "id": 2,987 "password": new_password,988 }989 result = change_user_password_socket(message)990 result = loads(result)991 assert result["error"] == "Invalid credentials!"992def test_put_other_user_password_unauthorized(client, database, cleanup):993 new_role = create_role(*admin_role)994 database.session.add(new_role)995 new_role = create_role(*user_role)996 database.session.add(new_role)997 new_user = create_user(*user2)998 database.session.add(new_user)999 new_user = create_user(*user3)1000 database.session.add(new_user)1001 database.session.commit()1002 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])1003 new_password = "BrandNewPassword123"1004 message = {1005 "private-key": "acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",1006 "token": token.decode("UTF-8"),1007 "id": 1,1008 "password": new_password,1009 }1010 result = change_user_password_socket(message)1011 result = loads(result)1012 assert result["error"] == "User is not authorized for this operation!"1013def test_put_own_user_password_success(client, database, cleanup):1014 new_role = new_role = create_role(*owner_role)1015 database.session.add(new_role)1016 new_role = create_role(*admin_role)1017 database.session.add(new_role)1018 new_role = create_role(*user_role)1019 database.session.add(new_role)1020 new_user = create_user(*user5)1021 database.session.add(new_user)1022 new_user = create_user(*user4)1023 database.session.add(new_user)1024 new_user = User(1025 email="anemail@anemail.com",1026 hashed_password="wi6hJCTz9QN1GcKc2ZJk7ReZ1LshNsu",1027 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",1028 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",1029 role=3,1030 )1031 database.session.add(new_user)1032 database.session.commit()1033 user = database.session.query(User).get(3)1034 assert checkpw(1035 b"ownerpassword123@@",1036 user.salt.encode("UTF-8") + user.hashed_password.encode("UTF-8"),1037 )1038 token = jwt.encode({"id": 3}, app.config["SECRET_KEY"])1039 new_password = "BrandNewPassword123"1040 message = {1041 "private-key": "acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",1042 "token": token.decode("UTF-8"),1043 "id": 3,1044 "password": new_password,1045 }1046 result = change_user_password_socket(message)1047 result = loads(result)1048 assert result["user"]["id"] == 31049 assert checkpw(1050 new_password.encode("UTF-8"),1051 user.salt.encode("UTF-8") + user.hashed_password.encode("UTF-8"),1052 )1053def test_put_other_user_email_missing_user(client, database, cleanup):1054 new_role = create_role(*admin_role)1055 database.session.add(new_role)1056 new_role = create_role(*user_role)1057 database.session.add(new_role)1058 new_user = create_user(*user2)1059 database.session.add(new_user)1060 database.session.commit()1061 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])1062 new_password = "BrandNewPassword123"1063 message = {1064 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",1065 "token": token.decode("UTF-8"),1066 "id": 2,1067 "password": new_password,1068 }1069 result = change_user_password_socket(message)1070 result = loads(result)1071 assert result["error"] == "User ID not found!"1072# DELETE USER1073def test_delete_other_user_success(client, database, cleanup):1074 new_role = create_role(*admin_role)1075 database.session.add(new_role)1076 new_role = create_role(*user_role)1077 database.session.add(new_role)1078 new_user = create_user(*user2)1079 database.session.add(new_user)1080 new_user = User(1081 email="anemail@anemail.com",1082 hashed_password="wi6hJCTz9QN1GcKc2ZJk7ReZ1LshNsu",1083 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",1084 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",1085 role=2,1086 )1087 database.session.add(new_user)1088 database.session.commit()1089 assert database.session.query(User).get(2) is not None1090 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])1091 message = {1092 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",1093 "id": 2,1094 "token": token.decode("UTF-8"),1095 }1096 result = delete_user_socket(message)1097 result = loads(result)1098 assert database.session.query(User).get(2) is None1099def test_delete_user_missing_key(client, database, cleanup):1100 new_role = create_role(*admin_role)1101 database.session.add(new_role)1102 new_role = create_role(*user_role)1103 database.session.add(new_role)1104 new_user = create_user(*user2)1105 database.session.add(new_user)1106 new_user = create_user(*user3)1107 database.session.add(new_user)1108 database.session.commit()1109 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])1110 message = {"id": 1, "token": token.decode("UTF-8")}1111 result = delete_user_socket(message)1112 result = loads(result)1113 assert result["error"] == "Missing request key!"1114def test_delete_user_missing_token(client, database, cleanup):1115 new_role = create_role(*admin_role)1116 database.session.add(new_role)1117 new_role = create_role(*user_role)1118 database.session.add(new_role)1119 new_user = create_user(*user2)1120 database.session.add(new_user)1121 new_user = create_user(*user3)1122 database.session.add(new_user)1123 database.session.commit()1124 message = {1125 "id": 2,1126 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",1127 }1128 result = delete_user_socket(message)1129 result = loads(result)1130 assert result["error"] == "Missing request key!"1131def test_delete_user_invalid_key(client, database, cleanup):1132 new_role = create_role(*admin_role)1133 database.session.add(new_role)1134 new_role = create_role(*user_role)1135 database.session.add(new_role)1136 new_user = create_user(*user2)1137 database.session.add(new_user)1138 new_user = create_user(*user3)1139 database.session.add(new_user)1140 database.session.commit()1141 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])1142 message = {1143 "id": 2,1144 "private-key": "acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",1145 "token": token.decode("UTF-8"),1146 }1147 result = delete_user_socket(message)1148 result = loads(result)1149 assert result["error"] == "Invalid credentials!"1150def test_delete_user_invalid_token(client, database, cleanup):1151 new_role = create_role(*admin_role)1152 database.session.add(new_role)1153 new_role = create_role(*user_role)1154 database.session.add(new_role)1155 new_user = create_user(*user2)1156 database.session.add(new_user)1157 new_user = create_user(*user3)1158 database.session.add(new_user)1159 database.session.commit()1160 token = jwt.encode({"id": 1}, "secretitis")1161 message = {1162 "id": 2,1163 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",1164 "token": token.decode("UTF-8"),1165 }1166 result = delete_user_socket(message)1167 result = loads(result)1168 assert result["error"] == "Invalid credentials!"1169def test_delete_other_user_unauthorized(client, database, cleanup):1170 new_role = create_role(*admin_role)1171 database.session.add(new_role)1172 new_role = create_role(*user_role)1173 database.session.add(new_role)1174 new_user = create_user(*user2)1175 database.session.add(new_user)1176 new_user = create_user(*user3)1177 database.session.add(new_user)1178 database.session.commit()1179 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])1180 message = {1181 "id": 1,1182 "private-key": "acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",1183 "token": token.decode("UTF-8"),1184 }1185 result = delete_user_socket(message)1186 result = loads(result)1187 assert result["error"] == "User is not authorized for this operation!"1188def test_delete_own_user_success(client, database, cleanup):1189 new_role = new_role = create_role(*owner_role)1190 database.session.add(new_role)1191 new_role = create_role(*admin_role)1192 database.session.add(new_role)1193 new_role = create_role(*user_role)1194 database.session.add(new_role)1195 new_user = create_user(*user5)1196 database.session.add(new_user)1197 new_user = create_user(*user4)1198 database.session.add(new_user)1199 new_user = User(1200 email="anemail@anemail.com",1201 hashed_password="wi6hJCTz9QN1GcKc2ZJk7ReZ1LshNsu",1202 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",1203 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",1204 role=3,1205 )1206 database.session.add(new_user)1207 database.session.commit()1208 assert database.session.query(User).get(3) is not None1209 token = jwt.encode({"id": 3}, app.config["SECRET_KEY"])1210 message = {1211 "id": 3,1212 "private-key": "acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",1213 "token": token.decode("UTF-8"),1214 }1215 result = delete_user_socket(message)1216 result = loads(result)1217 assert database.session.query(User).get(3) is None1218def test_delete_other_user_missing_user(client, database, cleanup):1219 new_role = create_role(*admin_role)1220 database.session.add(new_role)1221 new_role = create_role(*user_role)1222 database.session.add(new_role)1223 new_user = create_user(*user2)1224 database.session.add(new_user)1225 database.session.commit()1226 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])1227 message = {1228 "id": 2,1229 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",1230 "token": token.decode("UTF-8"),1231 }1232 result = delete_user_socket(message)1233 result = loads(result)1234 assert result["error"] == "User ID not found!"1235# SEARCH USERS1236def test_search_users_success(client, database, cleanup):1237 new_role = create_role(*admin_role)1238 database.session.add(new_role)1239 new_role = create_role(*user_role)1240 database.session.add(new_role)1241 new_user = create_user(*user2)1242 database.session.add(new_user)1243 new_user = User(1244 email="anemail@anemail.com",1245 hashed_password="wi6hJCTz9QN1GcKc2ZJk7ReZ1LshNsu",1246 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",1247 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",1248 role=2,1249 )1250 database.session.add(new_user)1251 database.session.commit()1252 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])1253 message = {1254 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",1255 "token": token.decode("UTF-8"),1256 "email": "anemail@anemail.com",1257 }1258 result = search_users_socket(message)1259 result = loads(result)1260 assert result["success"] == True1261 assert len(result["users"]) == 11262 assert result["users"][0]["id"] == 21263def test_search_users_two_matches(client, database, cleanup):1264 new_role = new_role = create_role(*owner_role)1265 database.session.add(new_role)1266 new_role = create_role(*admin_role)1267 database.session.add(new_role)1268 new_role = create_role(*user_role)1269 database.session.add(new_role)1270 new_user = create_user(*user5)1271 database.session.add(new_user)1272 new_user = User(1273 email="tech@gibberish.com",1274 hashed_password="2amt5MXKdLhEEL8FiQLcl8Mp0FNhZI6",1275 salt="$2b$12$tufn64/0gSIAdprqBrRzC.",1276 private_key="fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",1277 role=3,1278 )1279 database.session.add(new_user)1280 new_user = User(1281 email="anemail@anemail.com",1282 hashed_password="wi6hJCTz9QN1GcKc2ZJk7ReZ1LshNsu",1283 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",1284 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",1285 role=3,1286 )1287 database.session.add(new_user)1288 database.session.commit()1289 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])1290 message = {1291 "private-key": "4de2d41486ceaffdf0c1778e50cea00000d6549ffe808fa860ecd4e91d9ee1b1",1292 "token": token.decode("UTF-8"),1293 "role": 3,1294 }1295 result = search_users_socket(message)1296 result = loads(result)1297 assert result["success"] == True1298 assert len(result["users"]) == 21299 assert set([el["id"] for el in result["users"]]) == set([2, 3])1300def test_search_users_nomatch(client, database, cleanup):1301 new_role = new_role = create_role(*owner_role)1302 database.session.add(new_role)1303 new_role = create_role(*admin_role)1304 database.session.add(new_role)1305 new_role = create_role(*user_role)1306 database.session.add(new_role)1307 new_user = User(1308 email="tech@gibberish.com",1309 hashed_password="RcEEa25p/APCVGFaBaiZpytLieFsv22",1310 salt="$2b$12$OazL5oj8/lxxOV5a5j2Nme",1311 private_key="4de2d41486ceaffdf0c1778e50cea00000d6549ffe808fa860ecd4e91d9ee1b1",1312 role=1,1313 )1314 database.session.add(new_user)1315 new_user = User(1316 email="tech@gibberish.com",1317 hashed_password="2amt5MXKdLhEEL8FiQLcl8Mp0FNhZI6",1318 salt="$2b$12$tufn64/0gSIAdprqBrRzC.",1319 private_key="fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",1320 role=3,1321 )1322 database.session.add(new_user)1323 new_user = User(1324 email="anemail@anemail.com",1325 hashed_password="wi6hJCTz9QN1GcKc2ZJk7ReZ1LshNsu",1326 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",1327 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",1328 role=3,1329 )1330 database.session.add(new_user)1331 database.session.commit()1332 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])1333 message = {1334 "private-key": "4de2d41486ceaffdf0c1778e50cea00000d6549ffe808fa860ecd4e91d9ee1b1",1335 "token": token.decode("UTF-8"),1336 "role": 1,1337 "email": "anemail@anemail.com",1338 }1339 result = search_users_socket(message)1340 result = loads(result)1341 assert result["success"] == True1342 assert len(result["users"]) == 01343def test_search_users_missing_key(client, database, cleanup):1344 new_role = create_role(*admin_role)1345 database.session.add(new_role)1346 new_role = create_role(*user_role)1347 database.session.add(new_role)1348 new_user = create_user(*user2)1349 database.session.add(new_user)1350 new_user = create_user(*user3)1351 database.session.add(new_user)1352 database.session.commit()1353 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])1354 message = {"token": token.decode("UTF-8"), "email": "anemail@anemail.com"}1355 result = search_users_socket(message)1356 result = loads(result)1357 assert result["error"] == "Missing request key!"1358def test_search_users_missing_token(client, database, cleanup):1359 new_role = create_role(*admin_role)1360 database.session.add(new_role)1361 new_role = create_role(*user_role)1362 database.session.add(new_role)1363 new_user = create_user(*user2)1364 database.session.add(new_user)1365 new_user = create_user(*user3)1366 database.session.add(new_user)1367 database.session.commit()1368 message = {1369 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",1370 "email": "anemail@anemail.com",1371 }1372 result = search_users_socket(message)1373 result = loads(result)1374 assert result["error"] == "Missing request key!"1375def test_search_users_invalid_key(client, database, cleanup):1376 new_role = create_role(*admin_role)1377 database.session.add(new_role)1378 new_role = create_role(*user_role)1379 database.session.add(new_role)1380 new_user = create_user(*user2)1381 database.session.add(new_user)1382 new_user = create_user(*user3)1383 database.session.add(new_user)1384 database.session.commit()1385 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])1386 message = {1387 "private-key": "acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",1388 "token": token.decode("UTF-8"),1389 "email": "anemail@anemail.com",1390 }1391 result = search_users_socket(message)1392 result = loads(result)1393 assert result["error"] == "Invalid credentials!"1394def test_search_users_invalid_token(client, database, cleanup):1395 new_role = create_role(*admin_role)1396 database.session.add(new_role)1397 new_role = create_role(*user_role)1398 database.session.add(new_role)1399 new_user = create_user(*user2)1400 database.session.add(new_user)1401 new_user = create_user(*user3)1402 database.session.add(new_user)1403 database.session.commit()1404 token = jwt.encode({"id": 1}, "secretitis")1405 message = {1406 "private-key": "fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",1407 "token": token.decode("UTF-8"),1408 "email": "anemail@anemail.com",1409 }1410 result = search_users_socket(message)1411 result = loads(result)...

Full Screen

Full Screen

test_users_http.py

Source:test_users_http.py Github

copy

Full Screen

...56def test_post_role_user_data_no_key(client):57 result = client.post("/users", data="{bad", content_type="application/json")58 assert result.status_code == 40059def test_post_user_bad_data_with_key(client, database, cleanup):60 new_role = create_role(*owner_role)61 new_user = create_user(*user1)62 database.session.add(new_role)63 database.session.add(new_user)64 database.session.commit()65 result = client.post("/users", data="{bad", content_type="application/json")66 assert result.status_code == 40067def test_post_std_user_success(client, database, cleanup):68 new_role = create_role(*owner_role)69 database.session.add(new_role)70 new_role = create_role(*user_role)71 database.session.add(new_role)72 new_user = create_user(*user1)73 database.session.add(new_user)74 database.session.commit()75 payload = {"email": "someemail@email.com", "password": "123secretpassword"}76 result = client.post("/users", data=dumps(payload), content_type="application/json")77 assert result.status_code == 20078 assert result.get_json() == {"message": "User created successfully!"}79def test_post_std_user_missing_role(client, database, cleanup):80 new_role = create_role(*owner_role)81 database.session.add(new_role)82 new_role = create_role(*user_role)83 database.session.add(new_role)84 new_user = create_user(*user1)85 database.session.add(new_user)86 database.session.commit()87 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])88 headers = {89 "token": token.decode("UTF-8"),90 }91 payload = {92 "email": "someemail@email.com",93 "password": "123secretpassword",94 "role": "Unexpected Role",95 }96 result = client.post(97 "/users", data=dumps(payload), headers=headers, content_type="application/json"98 )99 assert result.get_json() == {"error": "Role ID not found!"}100# TODO: Update unit tests below101def test_post_user_with_role(client, database, cleanup):102 new_role = create_role(*owner_role)103 database.session.add(new_role)104 new_role = create_role(*user_role)105 database.session.add(new_role)106 new_user = create_user(*user1)107 database.session.add(new_user)108 database.session.commit()109 headers = {}110 payload = {111 "email": "someemail@email.com",112 "password": "123secretpassword",113 "role": "User",114 }115 result = client.post(116 "/users", data=dumps(payload), headers=headers, content_type="application/json"117 )118 assert result.status_code == 200119 assert result.get_json() == {"message": "User created successfully!"}120def test_login_user_valid_credentials(client, database, cleanup):121 new_role = create_role(*owner_role)122 database.session.add(new_role)123 new_role = create_role(*user_role)124 database.session.add(new_role)125 new_user = User(126 email="tech@gibberish.com",127 hashed_password="2amt5MXKdLhEEL8FiQLcl8Mp0FNhZI6",128 salt="$2b$12$tufn64/0gSIAdprqBrRzC.",129 private_key="fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",130 role=1,131 )132 database.session.add(new_user)133 database.session.commit()134 headers = {}135 payload = {"email": "tech@gibberish.com", "password": "&UP!SN!;J4Mx;+A]"}136 result = client.post(137 "/users/login",138 data=dumps(payload),139 headers=headers,140 content_type="application/json",141 )142 assert result.status_code == 200143 token = result.get_json()["token"]144 content = jwt.decode(token, app.config["SECRET_KEY"], algorithms="HS256")145 assert content["id"] == 1146def test_login_user_invalid_email(client, database, cleanup):147 new_role = create_role(*owner_role)148 database.session.add(new_role)149 new_role = create_role(*user_role)150 database.session.add(new_role)151 new_user = create_user(*user1)152 database.session.add(new_user)153 database.session.commit()154 payload = {"email": "p@perhaps.com", "password": "&UP!SN!;J4Mx;+A]"}155 result = client.post(156 "/users/login",157 data=dumps(payload),158 content_type="application/json",159 )160 assert result.status_code == 403161 assert result.get_json()["error"] == "Invalid credentials!"162def test_login_user_invalid_password(client, database, cleanup):163 new_role = create_role(*owner_role)164 database.session.add(new_role)165 new_role = create_role(*user_role)166 database.session.add(new_role)167 new_user = User(168 email="tech@gibberish.com",169 hashed_password="2amt5MXKdLhEEL8FiQLcl8Mp0FNhZI6",170 salt="$2b$12$tufn64/0gSIAdprqBrRzC.",171 private_key="fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",172 role=1,173 )174 database.session.add(new_user)175 database.session.commit()176 headers = {}177 payload = {"email": "tech@gibberish.com", "password": "@123456notmypassword"}178 result = client.post(179 "/users/login",180 data=dumps(payload),181 headers=headers,182 content_type="application/json",183 )184 assert result.status_code == 403185 assert result.get_json()["error"] == "Invalid credentials!"186# GET ALL USERS187def test_get_users_success(client, database, cleanup):188 new_role = create_role(*admin_role)189 database.session.add(new_role)190 new_role = create_role(*user_role)191 database.session.add(new_role)192 new_user = create_user(*user1)193 database.session.add(new_user)194 new_user = create_user(*user2)195 database.session.add(new_user)196 database.session.commit()197 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])198 headers = {199 "token": token.decode("UTF-8"),200 }201 result = client.get("/users", headers=headers, content_type="application/json")202 assert result.status_code == 200203 assert len(result.get_json()) == 2204 assert result.get_json()[0]["id"] == 1205 assert result.get_json()[1]["id"] == 2206def test_get_users_missing_token(client, database, cleanup):207 new_role = create_role(*admin_role)208 database.session.add(new_role)209 new_role = create_role(*user_role)210 database.session.add(new_role)211 new_user = create_user(*user1)212 database.session.add(new_user)213 new_user = create_user(*user2)214 database.session.add(new_user)215 database.session.commit()216 headers = {}217 result = client.get("/users", headers=headers, content_type="application/json")218 assert result.status_code == 400219 assert result.get_json()["error"] == "Missing request key!"220def test_get_users_invalid_token(client, database, cleanup):221 new_role = create_role(*admin_role)222 database.session.add(new_role)223 new_role = create_role(*user_role)224 database.session.add(new_role)225 new_user = create_user(*user1)226 database.session.add(new_user)227 new_user = create_user(*user2)228 database.session.add(new_user)229 database.session.commit()230 token = jwt.encode({"id": 1}, "peppperplsiwouldhavesome")231 headers = {232 "token": token.decode("UTF-8"),233 }234 result = client.get("/users", headers=headers, content_type="application/json")235 assert result.status_code == 403236 assert result.get_json()["error"] == "Invalid credentials!"237# GET SPECIFIC USER238def test_get_one_user_success(client, database, cleanup):239 new_role = create_role(*admin_role)240 database.session.add(new_role)241 new_role = create_role(*user_role)242 database.session.add(new_role)243 new_user = create_user(*user1)244 database.session.add(new_user)245 new_user = create_user(*user2)246 database.session.add(new_user)247 database.session.commit()248 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])249 headers = {250 "token": token.decode("UTF-8"),251 }252 result = client.get("/users/2", headers=headers, content_type="application/json")253 assert result.status_code == 200254 assert result.get_json()["id"] == 2255 assert result.get_json()["email"] == "anemail@anemail.com"256def test_get_one_user_missing_token(client, database, cleanup):257 new_role = create_role(*admin_role)258 database.session.add(new_role)259 new_role = create_role(*user_role)260 database.session.add(new_role)261 new_user = create_user(*user1)262 database.session.add(new_user)263 new_user = create_user(*user2)264 database.session.add(new_user)265 database.session.commit()266 headers = {}267 result = client.get("/users/1", headers=headers, content_type="application/json")268 assert result.status_code == 400269 assert result.get_json()["error"] == "Missing request key!"270def test_get_one_user_invalid_token(client, database, cleanup):271 new_role = create_role(*admin_role)272 database.session.add(new_role)273 new_role = create_role(*user_role)274 database.session.add(new_role)275 new_user = create_user(*user1)276 database.session.add(new_user)277 new_user = create_user(*user2)278 database.session.add(new_user)279 database.session.commit()280 token = jwt.encode({"id": 1}, "peppperplsiwouldhavesome")281 headers = {282 "token": token.decode("UTF-8"),283 }284 result = client.get("/users/2", headers=headers, content_type="application/json")285 assert result.status_code == 403286 assert result.get_json()["error"] == "Invalid credentials!"287def test_get_one_missing_user(client, database, cleanup):288 new_role = create_role(*admin_role)289 database.session.add(new_role)290 new_role = create_role(*user_role)291 database.session.add(new_role)292 new_user = create_user(*user1)293 database.session.add(new_user)294 new_user = create_user(*user2)295 database.session.add(new_user)296 database.session.commit()297 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])298 headers = {299 "token": token.decode("UTF-8"),300 }301 result = client.get("/users/3", headers=headers, content_type="application/json")302 assert result.status_code == 404303 assert result.get_json()["error"] == "User not found!"304# PUT USER EMAIL305def test_put_other_user_email_success(client, database, cleanup):306 new_role = create_role(*admin_role)307 database.session.add(new_role)308 new_role = create_role(*user_role)309 database.session.add(new_role)310 new_user = create_user(*user1)311 database.session.add(new_user)312 new_user = create_user(*user2)313 database.session.add(new_user)314 database.session.commit()315 assert database.session.query(User).get(2).email == "anemail@anemail.com"316 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])317 headers = {318 "token": token.decode("UTF-8"),319 }320 payload = {"email": "brandnew@brandnewemail.com"}321 result = client.put(322 "/users/2/email",323 headers=headers,324 data=dumps(payload),325 content_type="application/json",326 )327 assert result.status_code == 200328 assert result.get_json() == {"message": "User updated successfully!"}329def test_put_other_user_email_missing_token(client, database, cleanup):330 new_role = create_role(*admin_role)331 database.session.add(new_role)332 new_role = create_role(*user_role)333 database.session.add(new_role)334 new_user = create_user(*user1)335 database.session.add(new_user)336 new_user = create_user(*user2)337 database.session.add(new_user)338 database.session.commit()339 assert database.session.query(User).get(2).email == "anemail@anemail.com"340 headers = {}341 payload = {"email": "brandnew@brandnewemail.com"}342 result = client.put(343 "/users/2/email",344 headers=headers,345 data=dumps(payload),346 content_type="application/json",347 )348 assert result.status_code == 400349 assert result.get_json()["error"] == "Missing request key!"350def test_put_user_email_invalid_token(client, database, cleanup):351 new_role = create_role(*admin_role)352 database.session.add(new_role)353 new_role = create_role(*user_role)354 database.session.add(new_role)355 new_user = create_user(*user1)356 database.session.add(new_user)357 new_user = create_user(*user2)358 database.session.add(new_user)359 database.session.commit()360 assert database.session.query(User).get(2).email == "anemail@anemail.com"361 token = jwt.encode({"id": 1}, "secretitis")362 headers = {363 "token": token.decode("UTF-8"),364 }365 payload = {"email": "brandnew@brandnewemail.com"}366 result = client.put(367 "/users/2/email",368 headers=headers,369 data=dumps(payload),370 content_type="application/json",371 )372 assert result.status_code == 403373 assert result.get_json()["error"] == "Invalid credentials!"374def test_put_other_user_email_unauthorized(client, database, cleanup):375 new_role = create_role(*admin_role)376 database.session.add(new_role)377 new_role = create_role(*user_role)378 database.session.add(new_role)379 new_user = create_user(*user1)380 database.session.add(new_user)381 new_user = create_user(*user2)382 database.session.add(new_user)383 database.session.commit()384 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])385 headers = {386 "token": token.decode("UTF-8"),387 }388 payload = {"email": "brandnew@brandnewemail.com"}389 result = client.put(390 "/users/1/email",391 headers=headers,392 data=dumps(payload),393 content_type="application/json",394 )395 assert result.status_code == 403396 assert result.get_json()["error"] == "You're not allowed to change other user data!"397def test_put_own_user_email_success(client, database, cleanup):398 new_role = create_role(*admin_role)399 database.session.add(new_role)400 new_role = create_role(*user_role)401 database.session.add(new_role)402 new_user = create_user(*user1)403 database.session.add(new_user)404 new_user = create_user(*user2)405 database.session.add(new_user)406 database.session.commit()407 assert database.session.query(User).get(2).email == "anemail@anemail.com"408 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])409 headers = {410 "token": token.decode("UTF-8"),411 }412 payload = {"email": "brandnew@brandnewemail.com"}413 result = client.put(414 "/users/2/email",415 headers=headers,416 data=dumps(payload),417 content_type="application/json",418 )419 assert result.status_code == 200420 assert result.get_json() == {"message": "User updated successfully!"}421def test_put_user_email_missing_role(client, database, cleanup):422 new_role = create_role(*admin_role)423 database.session.add(new_role)424 new_user = create_user(*user1)425 database.session.add(new_user)426 new_user = create_user(*user2)427 database.session.add(new_user)428 database.session.commit()429 assert database.session.query(User).get(2).email == "anemail@anemail.com"430 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])431 headers = {432 "token": token.decode("UTF-8"),433 }434 payload = {"email": "brandnew@brandnewemail.com"}435 result = client.put(436 "/users/2/email",437 headers=headers,438 data=dumps(payload),439 content_type="application/json",440 )441 assert result.status_code == 200442 assert result.get_json() == {"message": "User updated successfully!"}443def test_put_other_user_email_missing_user(client, database, cleanup):444 new_role = create_role(*admin_role)445 database.session.add(new_role)446 new_role = create_role(*user_role)447 database.session.add(new_role)448 new_user = create_user(*user1)449 database.session.add(new_user)450 database.session.commit()451 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])452 headers = {453 "token": token.decode("UTF-8"),454 }455 payload = {"email": "brandnew@brandnewemail.com"}456 result = client.put(457 "/users/5/email",458 headers=headers,459 data=dumps(payload),460 content_type="application/json",461 )462 assert result.status_code == 404463 assert result.get_json()["error"] == "User not found!"464# PUT USER ROLE465def test_put_other_user_role_success(client, database, cleanup):466 new_role = create_role(*admin_role)467 database.session.add(new_role)468 new_role = create_role(*user_role)469 database.session.add(new_role)470 new_user = create_user(*user1)471 database.session.add(new_user)472 new_user = create_user(*user2)473 database.session.add(new_user)474 database.session.commit()475 assert database.session.query(User).get(2).role == 2476 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])477 headers = {478 "token": token.decode("UTF-8"),479 }480 payload = {"role": 1}481 result = client.put(482 "/users/2/role",483 headers=headers,484 data=dumps(payload),485 content_type="application/json",486 )487 assert result.status_code == 200488 assert result.get_json() == {"message": "User updated successfully!"}489def test_put_other_user_role_missing_token(client, database, cleanup):490 new_role = create_role(*admin_role)491 database.session.add(new_role)492 new_role = create_role(*user_role)493 database.session.add(new_role)494 new_user = create_user(*user1)495 database.session.add(new_user)496 new_user = create_user(*user2)497 database.session.add(new_user)498 database.session.commit()499 headers = {}500 payload = {"role": 1}501 result = client.put(502 "/users/2/role",503 headers=headers,504 data=dumps(payload),505 content_type="application/json",506 )507 assert result.status_code == 400508 assert result.get_json()["error"] == "Missing request key!"509def test_put_user_role_invalid_token(client, database, cleanup):510 new_role = create_role(*admin_role)511 database.session.add(new_role)512 new_role = create_role(*user_role)513 database.session.add(new_role)514 new_user = create_user(*user1)515 database.session.add(new_user)516 new_user = create_user(*user2)517 database.session.add(new_user)518 database.session.commit()519 token = jwt.encode({"id": 1}, "secretitis")520 headers = {521 "token": token.decode("UTF-8"),522 }523 payload = {"role": 1}524 result = client.put(525 "/users/2/role",526 headers=headers,527 data=dumps(payload),528 content_type="application/json",529 )530 assert result.status_code == 403531 assert result.get_json()["error"] == "Invalid credentials!"532def test_put_other_user_role_unauthorized(client, database, cleanup):533 new_role = create_role(*admin_role)534 database.session.add(new_role)535 new_role = create_role(*user_role)536 database.session.add(new_role)537 new_user = create_user(*user1)538 database.session.add(new_user)539 new_user = create_user(*user2)540 database.session.add(new_user)541 database.session.commit()542 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])543 headers = {544 "token": token.decode("UTF-8"),545 }546 payload = {"role": 2}547 result = client.put(548 "/users/1/role",549 headers=headers,550 data=dumps(payload),551 content_type="application/json",552 )553 assert result.status_code == 403554 assert result.get_json()["error"] == "You're not allowed to change other user data!"555def test_put_own_user_role_sucess(client, database, cleanup):556 new_role = create_role(*owner_role)557 database.session.add(new_role)558 new_role = create_role(*admin_role)559 database.session.add(new_role)560 new_role = create_role(*user_role)561 database.session.add(new_role)562 new_user = User(563 email="owner@owner.com",564 hashed_password="RcEEa25p/APCVGFaBaiZpytLieFsv22",565 salt="$2b$12$OazL5oj8/lxxOV5a5j2Nme",566 private_key="4de2d41486ceaffdf0c1778e50cea00000d6549ffe808fa860ecd4e91d9ee1b1",567 role=1,568 )569 database.session.add(new_user)570 new_user = create_user(*user4)571 database.session.add(new_user)572 new_user = create_user(*user3)573 database.session.add(new_user)574 database.session.commit()575 assert database.session.query(User).get(2).role == 2576 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])577 headers = {578 "token": token.decode("UTF-8"),579 }580 payload = {"role": 3}581 result = client.put(582 "/users/2/role",583 headers=headers,584 data=dumps(payload),585 content_type="application/json",586 )587 assert result.status_code == 200588 assert result.get_json() == {"message": "User updated successfully!"}589def test_put_first_user_unauthorized(client, database, cleanup):590 # Create Owner role591 new_role = create_role(*owner_role)592 database.session.add(new_role)593 # Create Admin role594 new_role = create_role(*admin_role)595 database.session.add(new_role)596 # Create User role597 new_role = create_role(*user_role)598 database.session.add(new_role)599 # New Owner User600 new_user = User(601 email="owner@owner.com",602 hashed_password="RcEEa25p/APCVGFaBaiZpytLieFsv22",603 salt="$2b$12$OazL5oj8/lxxOV5a5j2Nme",604 private_key="4de2d41486ceaffdf0c1778e50cea00000d6549ffe808fa860ecd4e91d9ee1b1",605 role=1,606 )607 database.session.add(new_user)608 new_user = create_user(*user4)609 database.session.add(new_user)610 new_user = create_user(*user3)611 database.session.add(new_user)612 database.session.commit()613 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])614 headers = {615 "token": token.decode("UTF-8"),616 }617 payload = {"role": 3}618 result = client.put(619 "/users/1/role",620 headers=headers,621 data=dumps(payload),622 content_type="application/json",623 )624 assert result.status_code == 403625 assert (626 result.get_json()["error"] == "You're not allowed to change Owner user roles!"627 )628def test_put_other_user_role_owner_unauthorized(client, database, cleanup):629 new_role = create_role(*owner_role)630 database.session.add(new_role)631 new_role = create_role(*admin_role)632 database.session.add(new_role)633 new_role = create_role(*user_role)634 database.session.add(new_role)635 new_user = User(636 email="owner@owner.com",637 hashed_password="RcEEa25p/APCVGFaBaiZpytLieFsv22",638 salt="$2b$12$OazL5oj8/lxxOV5a5j2Nme",639 private_key="4de2d41486ceaffdf0c1778e50cea00000d6549ffe808fa860ecd4e91d9ee1b1",640 role=1,641 )642 database.session.add(new_user)643 new_user = create_user(*user4)644 database.session.add(new_user)645 new_user = create_user(*user3)646 database.session.add(new_user)647 database.session.commit()648 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])649 headers = {650 "token": token.decode("UTF-8"),651 }652 payload = {"role": 1}653 result = client.put(654 "/users/3/role",655 headers=headers,656 data=dumps(payload),657 content_type="application/json",658 )659 assert result.status_code == 403660 assert result.get_json()["error"] == "You can't change it to Owner role!"661def test_put_other_user_role_owner(client, database, cleanup):662 new_role = create_role(*owner_role)663 database.session.add(new_role)664 new_role = create_role(*admin_role)665 database.session.add(new_role)666 new_role = create_role(*user_role)667 database.session.add(new_role)668 new_user = User(669 email="owner@owner.com",670 hashed_password="RcEEa25p/APCVGFaBaiZpytLieFsv22",671 salt="$2b$12$OazL5oj8/lxxOV5a5j2Nme",672 private_key="4de2d41486ceaffdf0c1778e50cea00000d6549ffe808fa860ecd4e91d9ee1b1",673 role=1,674 )675 database.session.add(new_user)676 new_user = create_user(*user4)677 database.session.add(new_user)678 new_user = create_user(*user3)679 database.session.add(new_user)680 database.session.commit()681 assert database.session.query(User).get(3).role == 3682 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])683 headers = {684 "token": token.decode("UTF-8"),685 }686 payload = {"role": 1}687 result = client.put(688 "/users/3/role",689 headers=headers,690 data=dumps(payload),691 content_type="application/json",692 )693 assert result.status_code == 403694 assert result.get_json()["error"] == "You can't change it to Owner role!"695def test_put_user_role_missing_role(client, database, cleanup):696 new_role = create_role(*admin_role)697 database.session.add(new_role)698 new_user = create_user(*user1)699 database.session.add(new_user)700 new_user = create_user(*user2)701 database.session.add(new_user)702 database.session.commit()703 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])704 headers = {705 "token": token.decode("UTF-8"),706 }707 payload = {"role": 2}708 result = client.put(709 "/users/2/role",710 headers=headers,711 data=dumps(payload),712 content_type="application/json",713 )714 assert result.status_code == 404715 assert result.get_json()["error"] == "Role ID not found!"716def test_put_other_user_role_missing_user(client, database, cleanup):717 new_role = create_role(*admin_role)718 database.session.add(new_role)719 new_role = create_role(*user_role)720 database.session.add(new_role)721 new_user = create_user(*user1)722 database.session.add(new_user)723 database.session.commit()724 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])725 headers = {726 "token": token.decode("UTF-8"),727 }728 payload = {"role": 2}729 result = client.put(730 "/users/2/role",731 headers=headers,732 data=dumps(payload),733 content_type="application/json",734 )735 assert result.status_code == 404736 assert result.get_json()["error"] == "User not found!"737# PUT USER PASSWORD738def test_put_other_user_password_success(client, database, cleanup):739 new_role = create_role(*admin_role)740 database.session.add(new_role)741 new_role = create_role(*user_role)742 database.session.add(new_role)743 new_user = create_user(*user1)744 database.session.add(new_user)745 new_user = User(746 email="anemail@anemail.com",747 hashed_password="wi6hJCTz9QN1GcKc2ZJk7ReZ1LshNsu",748 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",749 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",750 role=2,751 )752 database.session.add(new_user)753 database.session.commit()754 user = database.session.query(User).get(2)755 assert checkpw(756 b"ownerpassword123@@",757 user.salt.encode("UTF-8") + user.hashed_password.encode("UTF-8"),758 )759 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])760 headers = {761 "token": token.decode("UTF-8"),762 }763 new_password = "BrandNewPassword123"764 payload = {"password": new_password}765 result = client.put(766 "/users/2/password",767 headers=headers,768 data=dumps(payload),769 content_type="application/json",770 )771 assert result.status_code == 200772 assert result.get_json() == {"message": "User updated successfully!"}773 assert checkpw(774 new_password.encode("UTF-8"),775 user.salt.encode("UTF-8") + user.hashed_password.encode("UTF-8"),776 )777def test_put_user_password_missing_token(client, database, cleanup):778 new_role = create_role(*admin_role)779 database.session.add(new_role)780 new_role = create_role(*user_role)781 database.session.add(new_role)782 new_user = create_user(*user1)783 database.session.add(new_user)784 new_user = create_user(*user2)785 database.session.add(new_user)786 database.session.commit()787 headers = {}788 new_password = "BrandNewPassword123"789 payload = {"password": new_password}790 result = client.put(791 "/users/2/password",792 headers=headers,793 data=dumps(payload),794 content_type="application/json",795 )796 assert result.status_code == 400797 assert result.get_json()["error"] == "Missing request key!"798def test_put_user_password_invalid_token(client, database, cleanup):799 new_role = create_role(*admin_role)800 database.session.add(new_role)801 new_role = create_role(*user_role)802 database.session.add(new_role)803 new_user = create_user(*user1)804 database.session.add(new_user)805 new_user = create_user(*user2)806 database.session.add(new_user)807 database.session.commit()808 token = jwt.encode({"id": 1}, "secretitis")809 headers = {810 "token": token.decode("UTF-8"),811 }812 new_password = "BrandNewPassword123"813 payload = {"password": new_password}814 result = client.put(815 "/users/2/password",816 headers=headers,817 data=dumps(payload),818 content_type="application/json",819 )820 assert result.status_code == 403821 assert result.get_json()["error"] == "Invalid credentials!"822def test_put_other_user_password_unauthorized(client, database, cleanup):823 new_role = create_role(*admin_role)824 database.session.add(new_role)825 new_role = create_role(*user_role)826 database.session.add(new_role)827 new_user = create_user(*user1)828 database.session.add(new_user)829 new_user = create_user(*user2)830 database.session.add(new_user)831 database.session.commit()832 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])833 headers = {834 "token": token.decode("UTF-8"),835 }836 new_password = "BrandNewPassword123"837 payload = {"password": new_password}838 result = client.put(839 "/users/1/password",840 headers=headers,841 data=dumps(payload),842 content_type="application/json",843 )844 assert result.status_code == 403845 assert result.get_json()["error"] == "You're not allowed to change other user data!"846def test_put_own_user_password_success(client, database, cleanup):847 new_role = create_role(*owner_role)848 database.session.add(new_role)849 new_role = create_role(*admin_role)850 database.session.add(new_role)851 new_role = create_role(*user_role)852 database.session.add(new_role)853 new_user = User(854 email="owner@owner.com",855 hashed_password="RcEEa25p/APCVGFaBaiZpytLieFsv22",856 salt="$2b$12$OazL5oj8/lxxOV5a5j2Nme",857 private_key="4de2d41486ceaffdf0c1778e50cea00000d6549ffe808fa860ecd4e91d9ee1b1",858 role=1,859 )860 database.session.add(new_user)861 new_user = create_user(*user4)862 database.session.add(new_user)863 new_user = User(864 email="anemail@anemail.com",865 hashed_password="wi6hJCTz9QN1GcKc2ZJk7ReZ1LshNsu",866 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",867 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",868 role=3,869 )870 database.session.add(new_user)871 database.session.commit()872 user = database.session.query(User).get(3)873 assert checkpw(874 b"ownerpassword123@@",875 user.salt.encode("UTF-8") + user.hashed_password.encode("UTF-8"),876 )877 token = jwt.encode({"id": 3}, app.config["SECRET_KEY"])878 headers = {879 "token": token.decode("UTF-8"),880 }881 new_password = "BrandNewPassword123"882 payload = {"password": new_password}883 result = client.put(884 "/users/3/password",885 headers=headers,886 data=dumps(payload),887 content_type="application/json",888 )889 assert result.status_code == 200890 assert result.get_json() == {"message": "User updated successfully!"}891 assert checkpw(892 new_password.encode("UTF-8"),893 user.salt.encode("UTF-8") + user.hashed_password.encode("UTF-8"),894 )895def test_put_other_user_email_missing_user(client, database, cleanup):896 new_role = create_role(*admin_role)897 database.session.add(new_role)898 new_role = create_role(*user_role)899 database.session.add(new_role)900 new_user = create_user(*user1)901 database.session.add(new_user)902 database.session.commit()903 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])904 headers = {905 "token": token.decode("UTF-8"),906 }907 new_password = "BrandNewPassword123"908 payload = {"password": new_password}909 result = client.put(910 "/users/2/password",911 headers=headers,912 data=dumps(payload),913 content_type="application/json",914 )915 assert result.status_code == 404916 assert result.get_json()["error"] == "User not found!"917# DELETE USER918def test_delete_other_user_success(client, database, cleanup):919 new_role = create_role(*admin_role)920 database.session.add(new_role)921 new_role = create_role(*user_role)922 database.session.add(new_role)923 new_user = create_user(*user1)924 database.session.add(new_user)925 new_user = User(926 email="anemail@anemail.com",927 hashed_password="wi6hJCTz9QN1GcKc2ZJk7ReZ1LshNsu",928 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",929 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",930 role=2,931 )932 database.session.add(new_user)933 database.session.commit()934 assert database.session.query(User).get(2) is not None935 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])936 headers = {937 "token": token.decode("UTF-8"),938 }939 result = client.delete("/users/2", headers=headers, content_type="application/json")940 assert result.status_code == 200941 assert result.get_json() == {"message": "User deleted successfully!"}942 assert database.session.query(User).get(2) is None943def test_delete_user_missing_token(client, database, cleanup):944 new_role = create_role(*admin_role)945 database.session.add(new_role)946 new_role = create_role(*user_role)947 database.session.add(new_role)948 new_user = create_user(*user1)949 database.session.add(new_user)950 new_user = create_user(*user2)951 database.session.add(new_user)952 database.session.commit()953 headers = {}954 result = client.delete("/users/2", headers=headers, content_type="application/json")955 assert result.status_code == 400956 assert result.get_json()["error"] == "Missing request key!"957def test_delete_user_invalid_token(client, database, cleanup):958 new_role = create_role(*admin_role)959 database.session.add(new_role)960 new_role = create_role(*user_role)961 database.session.add(new_role)962 new_user = create_user(*user1)963 database.session.add(new_user)964 new_user = create_user(*user2)965 database.session.add(new_user)966 database.session.commit()967 token = jwt.encode({"id": 1}, "secretitis")968 headers = {969 "token": token.decode("UTF-8"),970 }971 result = client.delete("/users/2", headers=headers, content_type="application/json")972 assert result.status_code == 403973 assert result.get_json()["error"] == "Invalid credentials!"974def test_delete_other_user_unauthorized(client, database, cleanup):975 new_role = create_role(*admin_role)976 database.session.add(new_role)977 new_role = create_role(*user_role)978 database.session.add(new_role)979 new_user = create_user(*user1)980 database.session.add(new_user)981 new_user = create_user(*user2)982 database.session.add(new_user)983 database.session.commit()984 token = jwt.encode({"id": 2}, app.config["SECRET_KEY"])985 headers = {986 "token": token.decode("UTF-8"),987 }988 result = client.delete("/users/1", headers=headers, content_type="application/json")989 assert result.status_code == 403990 assert (991 result.get_json()["error"]992 == "You're not allowed to delete this user information!"993 )994def test_delete_own_user(client, database, cleanup):995 new_role = create_role(*owner_role)996 database.session.add(new_role)997 new_role = create_role(*admin_role)998 database.session.add(new_role)999 new_role = create_role(*user_role)1000 database.session.add(new_role)1001 new_user = User(1002 email="owner@owner.com",1003 hashed_password="RcEEa25p/APCVGFaBaiZpytLieFsv22",1004 salt="$2b$12$OazL5oj8/lxxOV5a5j2Nme",1005 private_key="4de2d41486ceaffdf0c1778e50cea00000d6549ffe808fa860ecd4e91d9ee1b1",1006 role=1,1007 )1008 database.session.add(new_user)1009 new_user = create_user(*user4)1010 database.session.add(new_user)1011 new_user = User(1012 email="anemail@anemail.com",1013 hashed_password="wi6hJCTz9QN1GcKc2ZJk7ReZ1LshNsu",1014 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",1015 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",1016 role=3,1017 )1018 database.session.add(new_user)1019 database.session.commit()1020 assert database.session.query(User).get(3) is not None1021 token = jwt.encode({"id": 3}, app.config["SECRET_KEY"])1022 headers = {1023 "token": token.decode("UTF-8"),1024 }1025 result = client.delete("/users/3", headers=headers, content_type="application/json")1026 user_groups = database.session.query(UserGroup).filter_by(user=3).all()1027 assert result.status_code == 4031028 assert (1029 result.get_json()["error"]1030 == "You're not allowed to delete this user information!"1031 )1032 assert database.session.query(User).get(3) is not None1033def test_delete_other_user_missing_user(client, database, cleanup):1034 new_role = create_role(*admin_role)1035 database.session.add(new_role)1036 new_role = create_role(*user_role)1037 database.session.add(new_role)1038 new_user = User(1039 email="tech@gibberish.com",1040 hashed_password="2amt5MXKdLhEEL8FiQLcl8Mp0FNhZI6",1041 salt="$2b$12$tufn64/0gSIAdprqBrRzC.",1042 private_key="fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",1043 role=1,1044 )1045 database.session.add(new_user)1046 database.session.commit()1047 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])1048 headers = {1049 "token": token.decode("UTF-8"),1050 }1051 result = client.delete("/users/2", headers=headers, content_type="application/json")1052 assert result.status_code == 4041053 assert result.get_json()["error"] == "User not found!"1054# SEARCH USERS1055# TODO: Update search unit tests1056def test_search_users_success(client, database, cleanup):1057 new_role = create_role(*admin_role)1058 database.session.add(new_role)1059 new_role = create_role(*user_role)1060 database.session.add(new_role)1061 new_user = User(1062 email="tech@gibberish.com",1063 hashed_password="2amt5MXKdLhEEL8FiQLcl8Mp0FNhZI6",1064 salt="$2b$12$tufn64/0gSIAdprqBrRzC.",1065 private_key="fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",1066 role=1,1067 )1068 database.session.add(new_user)1069 new_user = User(1070 email="anemail@anemail.com",1071 hashed_password="wi6hJCTz9QN1GcKc2ZJk7ReZ1LshNsu",1072 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",1073 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",1074 role=2,1075 )1076 database.session.add(new_user)1077 database.session.commit()1078 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])1079 headers = {1080 "token": token.decode("UTF-8"),1081 }1082 payload = {"email": "anemail@anemail.com"}1083 result = client.post(1084 "/users/search",1085 headers=headers,1086 data=dumps(payload),1087 content_type="application/json",1088 )1089 assert result.status_code == 2001090 assert len(result.get_json()) == 11091 assert result.get_json()[0]["id"] == 21092def test_search_users_nomatch(client, database, cleanup):1093 new_role = create_role(*owner_role)1094 database.session.add(new_role)1095 new_role = create_role(*admin_role)1096 database.session.add(new_role)1097 new_role = create_role(*user_role)1098 database.session.add(new_role)1099 new_user = User(1100 email="owner@owner.com",1101 hashed_password="RcEEa25p/APCVGFaBaiZpytLieFsv22",1102 salt="$2b$12$OazL5oj8/lxxOV5a5j2Nme",1103 private_key="4de2d41486ceaffdf0c1778e50cea00000d6549ffe808fa860ecd4e91d9ee1b1",1104 role=1,1105 )1106 database.session.add(new_user)1107 new_user = User(1108 email="tech@gibberish.com",1109 hashed_password="2amt5MXKdLhEEL8FiQLcl8Mp0FNhZI6",1110 salt="$2b$12$tufn64/0gSIAdprqBrRzC.",1111 private_key="fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",1112 role=3,1113 )1114 database.session.add(new_user)1115 new_user = User(1116 email="anemail@anemail.com",1117 hashed_password="wi6hJCTz9QN1GcKc2ZJk7ReZ1LshNsu",1118 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",1119 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",1120 role=3,1121 )1122 database.session.add(new_user)1123 new_group = Group(name="Hospital_X")1124 database.session.add(new_group)1125 new_group = Group(name="Hospital_Y")1126 database.session.add(new_group)1127 new_group = Group(name="Hospital_Z")1128 database.session.add(new_group)1129 new_usergroup = UserGroup(user=1, group=3)1130 database.session.add(new_usergroup)1131 new_usergroup = UserGroup(user=2, group=1)1132 database.session.add(new_usergroup)1133 new_usergroup = UserGroup(user=3, group=1)1134 database.session.add(new_usergroup)1135 database.session.commit()1136 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])1137 headers = {1138 "token": token.decode("UTF-8"),1139 }1140 payload = {"role": 8}1141 result = client.post(1142 "/users/search",1143 headers=headers,1144 data=dumps(payload),1145 content_type="application/json",1146 )1147 assert result.status_code == 2001148 assert len(result.get_json()) == 01149def test_search_users_onematch(client, database, cleanup):1150 new_role = create_role(*owner_role)1151 database.session.add(new_role)1152 new_role = create_role(*admin_role)1153 database.session.add(new_role)1154 new_role = create_role(*user_role)1155 database.session.add(new_role)1156 new_user = User(1157 email="tech@gibberish.com",1158 hashed_password="RcEEa25p/APCVGFaBaiZpytLieFsv22",1159 salt="$2b$12$OazL5oj8/lxxOV5a5j2Nme",1160 private_key="4de2d41486ceaffdf0c1778e50cea00000d6549ffe808fa860ecd4e91d9ee1b1",1161 role=1,1162 )1163 database.session.add(new_user)1164 new_user = User(1165 email="tech@gibberish.com",1166 hashed_password="2amt5MXKdLhEEL8FiQLcl8Mp0FNhZI6",1167 salt="$2b$12$tufn64/0gSIAdprqBrRzC.",1168 private_key="fd062d885b24bda173f6aa534a3418bcafadccecfefe2f8c6f5a8db563549ced",1169 role=3,1170 )1171 database.session.add(new_user)1172 new_user = User(1173 email="anemail@anemail.com",1174 hashed_password="wi6hJCTz9QN1GcKc2ZJk7ReZ1LshNsu",1175 salt="$2b$12$rj8MnLcKBxAgL7GUHrYn6O",1176 private_key="acfc10d15d7ec9f7cd05a312489af2794619c6f11e9af34671a5f33da48c1de2",1177 role=3,1178 )1179 database.session.add(new_user)1180 new_group = Group(name="Hospital_X")1181 database.session.add(new_group)1182 new_group = Group(name="Hospital_Y")1183 database.session.add(new_group)1184 new_group = Group(name="Hospital_Z")1185 database.session.add(new_group)1186 new_usergroup = UserGroup(user=1, group=3)1187 database.session.add(new_usergroup)1188 new_usergroup = UserGroup(user=2, group=1)1189 database.session.add(new_usergroup)1190 new_usergroup = UserGroup(user=3, group=1)1191 database.session.add(new_usergroup)1192 database.session.commit()1193 token = jwt.encode({"id": 1}, app.config["SECRET_KEY"])1194 headers = {1195 "token": token.decode("UTF-8"),1196 }1197 payload = {"role": 3, "group": 1, "email": "tech@gibberish.com"}1198 result = client.post(1199 "/users/search",1200 headers=headers,1201 data=dumps(payload),1202 content_type="application/json",1203 )1204 assert result.status_code == 2001205 assert len(result.get_json()) == 11206 assert result.get_json()[0]["id"] == 21207def test_search_users_missing_token(client, database, cleanup):1208 new_role = create_role(*admin_role)1209 database.session.add(new_role)1210 new_role = create_role(*user_role)1211 database.session.add(new_role)1212 new_user = create_user(*user1)1213 database.session.add(new_user)1214 new_user = create_user(*user2)1215 database.session.add(new_user)1216 database.session.commit()1217 headers = {}1218 payload = {"email": "anemail@anemail.com"}1219 result = client.post(1220 "/users/search",1221 headers=headers,1222 data=dumps(payload),1223 content_type="application/json",1224 )1225 assert result.status_code == 4001226 assert result.get_json()["error"] == "Missing request key!"1227def test_search_users_invalid_token(client, database, cleanup):1228 new_role = create_role(*admin_role)1229 database.session.add(new_role)1230 new_role = create_role(*user_role)1231 database.session.add(new_role)1232 new_user = create_user(*user1)1233 database.session.add(new_user)1234 new_user = create_user(*user2)1235 database.session.add(new_user)1236 database.session.commit()1237 token = jwt.encode({"id": 1}, "secretitis")1238 headers = {1239 "token": token.decode("UTF-8"),1240 }1241 payload = {"email": "anemail@anemail.com"}1242 result = client.post(1243 "/users/search",1244 headers=headers,...

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 tempest 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