Skip to content

Instantly share code, notes, and snippets.

@chgad
Created June 13, 2019 16:12
Show Gist options
  • Save chgad/9cb2af666964eda2abc9bfdb3e687c4a to your computer and use it in GitHub Desktop.
Save chgad/9cb2af666964eda2abc9bfdb3e687c4a to your computer and use it in GitHub Desktop.
Simple script to check wether or not the serializer baseclass works
import requests
import json
import datetime
from pytz import utc
def get_auth_key(user_mail, user_pw):
user_response = requests.post(
url="http://localhost:8000/auth/jwt/create",
data={"email": user_mail, "password": user_pw},
)
return json.loads(user_response.content)["access"]
def valid_contract_json(user_uuid):
"""
This fixture provides a valid (according to the ContractSerializer) JSON dictionary.
:param user_object:
:param user_object:
:return: Dict
"""
name = "Test Contract"
hours = 20.0
start_date = datetime.date(2019, 1, 1).isoformat()
end_date = datetime.date(2019, 1, 31).isoformat()
user = user_uuid # "4680c636-4886-44ab-a774-61592fc0e4bc"
created_at = datetime.datetime(2018, 12, 31, hour=10).isoformat()
modified_at = created_at
data = {
"name": name,
"hours": hours,
"start_date": start_date,
"end_date": end_date,
"user": user,
"created_by": user,
"modified_by": user,
"created_at": created_at,
"modified_at": modified_at,
}
return data
def valid_shift_json(user_uuid, contract_uuid):
"""
This fixture provides a valid (according to the ShiftSerializer) JSON dictionary for a shift
which is created manually (for the past) or was 'gestochen'.
:param user_object:
:param contract_object:
:return: Dict
"""
started = datetime.datetime(2019, 1, 29, 14, tzinfo=utc).isoformat()
stopped = datetime.datetime(2019, 1, 29, 16, tzinfo=utc).isoformat()
created_at = datetime.datetime(2019, 1, 29, 16, tzinfo=utc).isoformat()
modified_at = created_at
user = user_uuid
contract = contract_uuid
_type = "st"
note = "something was strange"
tags = json.dumps(["tag1", "tag2"])
was_reviewed = True
data = {
"started": started,
"stopped": stopped,
"contract": contract,
"type": _type,
"note": note,
"tags": tags,
"user": user,
"created_by": user,
"modified_by": user,
"created_at": created_at,
"modified_at": modified_at,
"was_reviewed": was_reviewed,
}
return data
def try_post(url, data, user_mail, user_pw):
user_response = requests.post(
url=url,
data=data,
headers={"AUTHORIZATION": "Bearer {}".format(get_auth_key(user_mail=user_mail, user_pw=user_pw))}
)
print(user_response.status_code)
print(json.loads(user_response.content))
try_post(
"http://localhost:8000/api/contracts/",
valid_contract_json(user_uuid="<tes_user_uuid>"),
user_mail="<test_user_mail>", user_pw="<test_user_pw>"
)
try_post("http://localhost:8000/api/shifts/",
valid_shift_json(
user_uuid="<tes_user_uuid>",
contract_uuid="<tes_contract_uuid>"
),
user_mail="<test_user_mail>", user_pw="<test_user_pw>"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment