63 lines
1.5 KiB
Python
63 lines
1.5 KiB
Python
import requests
|
|
import json
|
|
import sys
|
|
from time import sleep
|
|
|
|
BASEURL = "http://127.0.0.1:3000"
|
|
USERNAME="bradley"
|
|
PASSWORD="BadPassword"
|
|
|
|
def display_error_and_die(msg, response, exit_code=1):
|
|
print(msg)
|
|
print("Status Code: {status}".format(status=response.status_code))
|
|
print(json.dumps(response.json(), indent = 4))
|
|
sys.exit(exit_code)
|
|
|
|
# Put a new user
|
|
register_result = requests.post(
|
|
"{base}/register".format(base = BASEURL),
|
|
data = json.dumps({
|
|
"username": USERNAME,
|
|
"password": PASSWORD
|
|
}),
|
|
headers={
|
|
"Content-Type": "application/json"
|
|
}
|
|
)
|
|
|
|
if (register_result.status_code != 201):
|
|
display_error_and_die("Failed to create dummy user", register_result)
|
|
|
|
login_result = requests.post(
|
|
"{base}/login".format(base = BASEURL),
|
|
data = json.dumps({
|
|
"username": USERNAME,
|
|
"password": PASSWORD
|
|
}),
|
|
headers={
|
|
"Content-Type": "application/json"
|
|
}
|
|
)
|
|
|
|
if (login_result.status_code != 200):
|
|
display_error_and_die("Failed to login", login_result)
|
|
|
|
session = requests.Session()
|
|
session.headers = {
|
|
"X-JWT-EXAMPLE-TOKEN": login_result.json()["token"],
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
|
|
protected_response = session.get(
|
|
"{base}/protected".format(base = BASEURL)
|
|
)
|
|
|
|
if (protected_response.status_code != 200):
|
|
display_error_and_die("Failed to get protected information", protected_response)
|
|
|
|
print("Protected information: {protected}".format(
|
|
protected = protected_response.json()["message"]
|
|
))
|
|
|