def test_put_over_missing_role(client, database, cleanup): new_role = create_role(*admin_role) database.session.add(new_role) new_role = create_role(*owner_role) database.session.add(new_role) new_user = create_user(*user_2) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) headers = { "private_key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb", "token": token.decode("UTF-8"), } result = client.put( "/roles/3", data=dumps(payload), content_type="application/json", headers=headers, ) assert result.status_code == 404 assert result.get_json()["error"] == "Role ID not found!"
def test_put_role_success(client, database, cleanup): new_role = create_role(*admin_role) database.session.add(new_role) new_role = create_role(*owner_role) database.session.add(new_role) new_user = create_user(*user_2) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) payload = { "id": 1, "role": role, "private-key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb", "token": token.decode("UTF-8"), } result = put_role_socket(payload) result = loads(result) expected_role = role expected_role["id"] = 1 assert result["role"] == expected_role
def test_put_role_success(client, database, cleanup): new_role = create_role(*admin_role) database.session.add(new_role) new_role = create_role(*owner_role) database.session.add(new_role) new_user = create_user(*user_2) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) headers = { "private_key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb", "token": token.decode("UTF-8"), } result = client.put( "/roles/1", data=dumps(payload), content_type="application/json", headers=headers, ) expected_role = payload.copy() expected_role["id"] = 1 assert result.status_code == 200 assert result.get_json()["role"] == expected_role
def test_put_role_missing_key(client, database, cleanup): new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) payload = {"role": role, "id": 1, "token": token.decode("UTF-8")} result = put_role_socket(payload) result = loads(result) assert result["error"] == "Missing request key!"
def test_post_role_invalid_key(client, database, cleanup): new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) headers = {"private-key": "IdoNotExist", "token": token.decode("UTF-8")} result = client.post( "/roles", data=dumps(payload), content_type="application/json", headers=headers ) assert result.status_code == 403 assert result.get_json()["error"] == "Invalid credentials!"
def test_put_role_missing_token(client, database, cleanup): new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() payload = { "private-key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb" } result = put_role_socket(payload) result = loads(result) assert result["error"] == "Missing request key!"
def test_delete_role_missing_key(client, database, cleanup): new_role = create_role(*owner_role) database.session.add(new_role) new_role = create_role(*admin_role) database.session.add(new_role) new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) headers = {"token": token.decode("UTF-8")} result = client.delete("/roles/2", content_type="application/json", headers=headers) assert result.status_code == 400 assert result.get_json()["error"] == "Missing request key!"
def test_get_role_invalid_key(client, database, cleanup): new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) payload = { "id": 1, "private-key": "IdoNotExist", "token": token.decode("UTF-8") } result = get_role_socket(payload) result = loads(result) assert result["error"] == "Invalid credentials!"
def test_put_role_missing_key(client, database, cleanup): new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) headers = {"token": token.decode("UTF-8")} result = client.put( "/roles/1", data=dumps(payload), headers=headers, content_type="application/json", ) assert result.status_code == 400 assert result.get_json()["error"] == "Missing request key!"
def test_delete_role_missing_token(client, database, cleanup): new_role = create_role(*owner_role) database.session.add(new_role) new_role = create_role(*admin_role) database.session.add(new_role) new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() headers = { "private_key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb" } result = client.delete("/roles/2", content_type="application/json", headers=headers) assert result.status_code == 400 assert result.get_json()["error"] == "Missing request key!"
def test_get_role_missing_role(client, database, cleanup): new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) payload = { "id": 1, "private-key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb", "token": token.decode("UTF-8"), } result = get_role_socket(payload) result = loads(result) assert result["error"] == "Role ID not found!"
def test_delete_role_unauthorized_user(client, database, cleanup): new_role = create_role(*admin_role) new_user = create_user(*user_1) database.session.add(new_role) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) headers = { "private_key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb", "token": token.decode("UTF-8"), } result = client.delete("/roles/1", headers=headers, content_type="application/json") assert result.status_code == 403 assert result.get_json()["error"] == "User is not authorized for this operation!"
def test_get_all_roles_user_with_missing_role(client, database, cleanup): new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) headers = { "private-key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb", "token": token.decode("UTF-8"), } result = client.get("/roles", content_type="application/json", headers=headers) assert result.status_code == 404 assert result.get_json()["error"] == "Role ID not found!"
def test_post_role_invalid_token(client, database, cleanup): new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() token = jwt.encode({"asdsadad": 124356}, app.config["SECRET_KEY"]) headers = { "private_key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb", "token": token.decode("UTF-8"), } result = client.post( "/roles", data=dumps(payload), content_type="application/json", headers=headers ) assert result.status_code == 403 assert result.get_json()["error"] == "Invalid credentials!"
def test_post_role_bad_data(client, database, cleanup): new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) headers = { "private_key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb", "token": token.decode("UTF-8"), } result = client.post( "/roles", data="{bad", headers=headers, content_type="application/json" ) assert result.status_code == 400 assert result.get_json()["error"] == JSON_DECODE_ERR_MSG
def test_post_role_invalid_token(client, database, cleanup): new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() token = jwt.encode({"asdsadad": 124356}, app.config["SECRET_KEY"]) payload = { "role": role, "private-key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb", "token": token.decode("UTF-8"), } result = create_role_socket(payload) result = loads(result) assert result["error"] == "Invalid credentials!"
def test_put_role_missing_token(client, database, cleanup): new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() headers = { "private_key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb" } result = client.put( "/roles/1", data=dumps(payload), headers=headers, content_type="application/json", ) assert result.status_code == 400 assert result.get_json()["error"] == "Missing request key!"
def test_delete_role_invalid_token(client, database, cleanup): new_role = create_role(*owner_role) database.session.add(new_role) new_role = create_role(*admin_role) database.session.add(new_role) new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, "213p4u4trgsvczxnwdaere67yiukyhj") headers = { "private_key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb", "token": token.decode("UTF-8"), } result = client.delete("/roles/2", content_type="application/json", headers=headers) assert result.status_code == 403 assert result.get_json()["error"] == "Invalid credentials!"
def test_delete_role_invalid_key(client, database, cleanup): new_role = create_role(*owner_role) database.session.add(new_role) new_role = create_role(*admin_role) database.session.add(new_role) new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) headers = { "private_key": "1230896843rtfsvdjb123453212098792171766n", "token": token.decode("UTF-8"), } result = client.delete("/roles/2", content_type="application/json", headers=headers) assert result.status_code == 403 assert result.get_json()["error"] == "Invalid credentials!"
def test_delete_role_unauthorized_user(client, database, cleanup): new_role = create_role(*admin_role) new_user = create_user(*user_1) database.session.add(new_role) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) payload = { "id": 1, "private-key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb", "token": token.decode("UTF-8"), } result = delete_role_socket(payload) result = loads(result) assert result["error"] == "User is not authorized for this operation!"
def test_delete_role_invalid_key(client, database, cleanup): new_role = create_role(*owner_role) database.session.add(new_role) new_role = create_role(*admin_role) database.session.add(new_role) new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) payload = { "id": 2, "private-key": "1230896843rtfsvdjb123453212098792171766n", "token": token.decode("UTF-8"), } result = delete_role_socket(payload) result = loads(result) assert result["error"] == "Invalid credentials!"
def test_put_role_invalid_key(client, database, cleanup): new_role = create_role(*owner_role) new_user = create_user(*user_1) database.session.add(new_role) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) payload = { "id": 1, "role": role, "private-key": "dsapksasdp12-04290u83t5r752tyvdwhbsacnxz", "token": token.decode("UTF-8"), } result = put_role_socket(payload) result = loads(result) assert result["error"] == "Invalid credentials!"
def test_delete_role_invalid_token(client, database, cleanup): new_role = create_role(*owner_role) database.session.add(new_role) new_role = create_role(*admin_role) database.session.add(new_role) new_user = create_user(*user_1) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, "213p4u4trgsvczxnwdaere67yiukyhj") payload = { "id": 2, "private-key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb", "token": token.decode("UTF-8"), } result = delete_role_socket(payload) result = loads(result) assert result["error"] == "Invalid credentials!"
def test_get_all_roles_invalid_key(client, database, cleanup): new_role = create_role(*admin_role) database.session.add(new_role) new_role = create_role(*owner_role) database.session.add(new_role) new_user = create_user(*user_2) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) payload = { "private-key": "siohfigdadANDVBSIAWE0WI21Y8OR1082ORHFEDNSLCSADIJOKA", "token": token.decode("UTF-8"), } result = get_all_roles_socket(payload) result = loads(result) assert result["error"] == "Invalid credentials!"
def test_delete_role_success(client, database, cleanup): new_role = create_role(*admin_role) database.session.add(new_role) new_role = create_role(*owner_role) database.session.add(new_role) new_user = create_user(*user_2) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) headers = { "private_key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb", "token": token.decode("UTF-8"), } result = client.delete("/roles/1", headers=headers, content_type="application/json") assert result.status_code == 200 assert database.session.query(Role).get(1) is None
def test_get_all_roles_invalid_token(client, database, cleanup): new_role = create_role(*admin_role) database.session.add(new_role) new_role = create_role(*owner_role) database.session.add(new_role) new_user = create_user(*user_2) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, "totally a secret, trust me") payload = { "private-key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb", "token": token.decode("UTF-8"), } result = get_all_roles_socket(payload) result = loads(result) assert result["error"] == "Invalid credentials!"
def test_put_role_invalid_token(client, database, cleanup): new_role = create_role(*owner_role) new_user = create_user(*user_1) database.session.add(new_role) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, "1029382trytdfsvcbxz") payload = { "id": 1, "role": role, "private-key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb", "token": token.decode("UTF-8"), } result = put_role_socket(payload) result = loads(result) assert result["error"] == "Invalid credentials!"
def test_get_all_roles_invalid_key(client, database, cleanup): new_role = create_role(*admin_role) database.session.add(new_role) new_role = create_role(*owner_role) database.session.add(new_role) new_user = create_user(*user_2) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) headers = { "private_key": "siohfigdadANDVBSIAWE0WI21Y8OR1082ORHFEDNSLCSADIJOKA", "token": token.decode("UTF-8"), } result = client.get( "/roles", data=dumps(payload), headers=headers, content_type="application/json" ) assert result.status_code == 403 assert result.get_json()["error"] == "Invalid credentials!"
def test_put_role_invalid_token(client, database, cleanup): new_role = create_role(*owner_role) new_user = create_user(*user_1) database.session.add(new_role) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, "1029382trytdfsvcbxz") headers = { "private_key": "3c777d6e1cece1e78aa9c26ae7fa2ecf33a6d3fb1db7c1313e7b79ef3ee884eb", "token": token.decode("UTF-8"), } result = client.put( "/roles/1", data=dumps(payload), headers=headers, content_type="application/json", ) assert result.status_code == 403 assert result.get_json()["error"] == "Invalid credentials!"
def test_put_role_invalid_key(client, database, cleanup): new_role = create_role(*owner_role) new_user = create_user(*user_1) database.session.add(new_role) database.session.add(new_user) database.session.commit() token = jwt.encode({"id": 1}, app.config["SECRET_KEY"]) headers = { "private_key": "dsapksasdp12-04290u83t5r752tyvdwhbsacnxz", "token": token.decode("UTF-8"), } result = client.put( "/roles/1", data=dumps(payload), headers=headers, content_type="application/json", ) assert result.status_code == 403 assert result.get_json()["error"] == "Invalid credentials!"