FastAPI Pytest why does client returns 422 - pytest

I'm trying to implement testing my post route. It works in my project. I have problems only with pytest.
main.py:
#app.post('/create_service', status_code=status.HTTP_201_CREATED)
async def post_service(
response: Response, service: CreateService, db: Session = Depends(get_db)
):
service_model = models.Service()
service_name = service.name
service_instance = db.query(models.Service).filter(
models.Service.name == service_name
).first()
if service_instance is None:
service_model.name = service_name
db.add(service_model)
db.commit()
serviceversion_model = models.ServiceVersion()
service_instance = db.query(models.Service).filter(
models.Service.name == service_name
).first()
serviceversion_instance = db.query(models.ServiceVersion).filter(
models.ServiceVersion.service_id == service_instance.id
).filter(models.ServiceVersion.version == service.version).first()
if serviceversion_instance:
raise HTTPException(
status_code=400, detail='Version of service already exists'
)
serviceversion_model.version = service.version
serviceversion_model.is_used = service.is_used
serviceversion_model.service_id = service_instance.id
db.add(serviceversion_model)
db.commit()
db.refresh(serviceversion_model)
service_dict = service.dict()
for key in list(service_dict):
if isinstance(service_dict[key], list):
sub_dicts = service_dict[key]
if not sub_dicts:
response.status_code = status.HTTP_400_BAD_REQUEST
return HTTPException(status_code=400, detail='No keys in config')
servicekey_models = []
for i in range(len(sub_dicts)):
servicekey_model = models.ServiceKey()
servicekey_models.append(servicekey_model)
servicekey_models[i].service_id = service_instance.id
servicekey_models[i].version_id = serviceversion_model.id
servicekey_models[i].service_key = sub_dicts[i].get('service_key')
servicekey_models[i].service_value = sub_dicts[i].get('service_value')
db.add(servicekey_models[i])
db.commit()
return 'created'
test_main.py:
def test_create_service(client, db: Session = Depends(get_db)):
key = Key(service_key='testkey1', service_value='testvalue1')
service = CreateService(
name="testname1",
version="testversion1",
is_used=True,
keys=[key, ]
)
response = client.post("/create_service", params={"service": service.dict()})
assert response.status_code == 200
I tried to post service as json, as CreateService instance and finally as params dictionary. I have no errors at response line only with the last one . But I got 422 response status code. What is wrong?
If it can help:
schemas.py
class Key(BaseModel):
service_key: str
service_value: str
class CreateService(BaseModel):
name: str
version: str
is_used: bool
keys: list[Key]
class Config:
orm_mode = True

Don't use params. Use json:
response = client.post("/create_service", json=service.dict())

Related

How do I make a HTTP PUT call from XLRelease to update data in Adobe Workfront?

I am attempting to make an HTTP PUT request from XLRelease to update data in Adobe Workfront. I have been able to successfully login using the API client and GET data. I have also been able to successfully update data using Postman as well as using a native Python script. I am using the HttpRequest library within XLR. I am receiving the same response back in XLR as I am when successfully updating when using Postman, however, the data is not updated when using XLR.
My code is as follows:
import json
WORKFRONT_API_HOST = releaseVariables['url']
WORKFRONT_API_VERSION = releaseVariables['wfApiVersion']
WORKFRONT_API_KEY = releaseVariables['apiKey']
WORKFRONT_USERNAME = releaseVariables['wfUsername']
FI_ID = releaseVariables['target_customer_id']
newPortfolioId = releaseVariables['target_portfolio_id']
WORKFRONT_API_URL = WORKFRONT_API_HOST + WORKFRONT_API_VERSION
def wfLogin():
sessionID = ""
login_endpoint = "/login"
login_request = HttpRequest({'url': WORKFRONT_API_URL})
login_response = login_request.get(login_endpoint + "?apiKey=" + str(WORKFRONT_API_KEY).replace("u'","'") + "&username=" + WORKFRONT_USERNAME, contentType='application/json')
if login_response.getStatus() != 200:
print('# Error logging into WF\n')
print(login_response.getStatus())
print(login_response.errorDump())
sys.exit(1)
else:
json_response = json.loads(login_response.getResponse())
print ("Logged in to WF")
sessionID = json_response['data']['sessionID']
return sessionID
def wfLogout(sessionID):
logout_endpoint = "/logout"
logout_request = HttpRequest({'url': WORKFRONT_API_URL})
logout_response = logout_request.get(logout_endpoint + "?sessionID=" + sessionID, contentType='application/json')
if logout_response.getStatus() != 200:
print('# Error logging out of WF\n')
print(logout_response.getStatus())
print(logout_response.errorDump())
sys.exit(1)
else:
json_response = json.loads(logout_response.getResponse())
print ("Logged out of WF")
result = []
session_id = wfLogin()
if session_id != "":
customer_request = HttpRequest({'url': WORKFRONT_API_URL})
endpoint = '/prgm/%s?sessionID=%s&portfolioID=%s&customerID=%s' % (FI_ID, session_id, newPortfolioId, FI_ID)
jsonObj = "{}"
payload = {}
customer_response = customer_request.put(endpoint, jsonObj, contentType='application/json')
if customer_response.getStatus() != 200:
print('# Error connecting to WF\n')
print(customer_response)
print(customer_response.getStatus())
print(customer_response.errorDump())
sys.exit(1)
else:
response_json = json.loads(customer_response.getResponse())
print ("response_json: ", response_json)
#log out of current session
wfLogout(session_id)
else:
print ("No sessionID is available")
sys.exit(1)

Getting Invalid Response from API . - Groovy Scripting

I am trying a POST method in groovy which basically creates a Issue in Jira . I am getting invalid response from the API . However in postman I could able to get the response . I have searched few questions in stackoverflow where they are recommending to set the Accept-Encoding to "" . I tried out different methods but it didnt work .
Code :
import javax.net.ssl.*;
import java.security.cert.*;
import groovy.json.JsonBuilder;
import groovy.json.JsonOutput;
import groovy.json.JsonSlurperClassic;
import groovy.json.StreamingJsonBuilder;
def jiraurl = "xxxxx";
def netProxyAddr = "xxxx";
def netProxyPort = 80;
try {
// NOTE: Adjust the following as needed
def uri = jiraurl + "/issue";
def jiraTicketMap = [:];
jiraTicketMap["fields"] = [:];
jiraTicketMap["fields"]["project"]= [:];
jiraTicketMap["fields"]["project"]["id"] = "12412"
jiraTicketMap["fields"]["summary"] = "Test ticket for API";
jiraTicketMap["fields"]["assignee"] = [:];
jiraTicketMap["fields"]["assignee"]["name"] = "*********";
jiraTicketMap["fields"]["customfield_10700"] = [:];
jiraTicketMap["fields"]["customfield_10700"]["value"] = "Normal";
jiraTicketMap["fields"]["customfield_16901"] = "nothing impacted";
jiraTicketMap["fields"]["customfield_16900"] = "testing ";
jiraTicketMap["fields"]["customfield_10710"] = "removing the URL" ;
jiraTicketMap["fields"]["customfield_10711"] = "********";
jiraTicketMap["fields"]["customfield_16902"] = "descriptiong";
jiraTicketMap["fields"]["customfield_10702"] = [:];
jiraTicketMap["fields"]["customfield_10702"]["value"] ="Low";
jiraTicketMap["fields"]["customfield_10900"] = [:];
jiraTicketMap["fields"]["customfield_10900"]["value"] = "Yes";
jiraTicketMap["fields"]["customfield_10703"] = [:];
jiraTicketMap["fields"]["customfield_10703"]["value"] = "Low";
jiraTicketMap["fields"]["customfield_10705"] = "2022-03-19T10:29:29.908+1100";
jiraTicketMap["fields"]["customfield_10706"] = "2022-03-20T10:29:29.908+1100";
jiraTicketMap["fields"]["customfield_10707"] = "testing done";
jiraTicketMap["fields"]["customfield_10708"] = "Implemtation test done for zscaler";
jiraTicketMap["fields"]["customfield_10709"] = "post implention done";
jiraTicketMap["fields"]["issuetype"] = [:];
jiraTicketMap["fields"]["issuetype"]["id"]= "10200";
jiraTicketMap["fields"]["labels"] = ["test"];
jiraTicketMap["fields"]["customfield_21400"] = "Test Environment is required.";
jiraTicketMap["fields"]["customfield_21500"] = "Back-Out Test Details is required";
DEBUG.println("DEBUG: jiraTicketMap is: ${jiraTicketMap}");
def payloadData = StringUtils.mapToJson(jiraTicketMap);
DEBUG.println("DEBUG: Payload is: ${payloadData}");
DEBUG.println("DEBUG: PayloadData is: ${payloadData}");
DEBUG.println("DEBUG: PayloadData type is:"+payloadData.getClass());
netProxyPort = netProxyPort.toInteger();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(netProxyAddr, netProxyPort));
def authString = "*********".getBytes().encodeBase64().toString()
def url = new URL(uri);
// NOTE: The following removes all checks against SSL certificates
disableSSL();
def conn = url.openConnection(proxy);
conn.doOutput = true;
// Build the Credentials and HTTP headers
conn.setRequestProperty("Authorization", "Basic ***********");
conn.setRequestProperty("Content-type", "application/JSON;charset=UTF-8");
conn.setRequestProperty("Accept","*/*");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
//BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF8"));
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(),"UTF-8")
writer?.write(payloadData);
writer?.flush();
writer?.close();
if(conn.getResponseCode() == 201 || conn.getResponseCode() == 200 ) {
InputStream responseStream = conn.getInputStream();
result = responseStream.getText("UTF-8");
DEBUG.println("DEBUG:>>>>>>>>>>>>> SUccessfully Posted Data to Jira");
// NOTE: If you need to get the response header, use below
//INPUTS.HEADERS = conn.getHeaderFields();
responseStream.close();
}
else {
INPUTS.RSEXCEPTION = "Response code was " + conn.getResponseCode();
// Attempt to parse error stream.
InputStream responseStream = conn.getErrorStream();
result = responseStream?.getText("UTF-8");
responseStream?.close();
}
}
catch (Exception e) {
//INPUTS.RSEXCEPTION = e.getMessage();
result = e.getMessage();
result += "\n" + e.getClass() + ": " + e.getMessage();
for (trace in e.getStackTrace())
{
result += "\n\t" + trace;
}
}
return result;
Response :
Response from Postman and local code editor
{
"id": "12212",
"key": "WCM-2211",
"self": "https://********/rest/api/2/issue/2496228"
}
Response from Vendor Tool:
�4̱#0��w��^BV��\z(M��J"��u1���?`4tPȶ.���S���7��0�%���q7A M�X����xv…qD�h�O��~��NCe
Response Headers
[X-AREQUESTID:[533x1208409x1], null:[HTTP/1.1 201 Created], Server:[Web Server], X-Content-Type-Options:[nosniff], Connection:[close], X-ANODEID:[node1], Date:[Thu, 12 May 2022 07:53:25 GMT], X-Frame-Options:[SAMEORIGIN], Referrer-Policy:[no-referrer-when-downgrade], Strict-Transport-Security:[max-age=15768000], Cache-Control:[no-cache, no-store, no-transform], Content-Security-Policy:[frame-ancestors 'self'], X-AUSERNAME:[SVC], Content-Encoding:[gzip], X-ASESSIONID:[1l2vszx], Set-Cookie:[akaalb_jira_dev_alb=~op=jira_dev_lb:jira-dev-ld6|~rv=95~m=jia-dev-ld6:0|~os=287df636055edb4e278283~id=de0d2567c76bf80374af7c583f; path=/; HttpOnly; Secure; SameSite=None, JESSIONID=2920944044.37151.0000; path=/; Httponly; Secure, atlassian.xsrf.token=BZXB-Z179-LJQD-6WAV_9b298f0387a7c68a8652e963b69e4_lin; Path=/; Secure, JSESSIONID=27C93900AA8B780AA44C2861F73; Path=/; Secure; HttpOnly], Feature-Policy:[midi 'none'], Vary:[Accept-Encoding, User-Agent], X-XSS-Protection:[1; mode=block], X-Seraph-LoginReason:[OK], X-Powered-By:[Web Server], Content-Type:[application/json;charset=UTF-8]]
Solution from #dagget :
// use the below code when reading it from input stream
accept-encoding : identity
responseStream = new java.util.zip.GZIPInputStream(responseStream)

How to make A Weather command? (Discord.py)

So I made this weather command down below, but for some reason, it only says the bot is typing, and there gives a error. This is the code:
import requests
#client.command()
async def weather(ctx, *, city: str):
city_name = city
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
response = requests.get(complete_url)
x = response.json()
channel = ctx.message.channel
if x["cod"] != "404":
async with channel.typing():
y = x["main"]
current_temperature = y["temp"]
current_temperature_celsiuis = str(round(current_temperature - 273.15))
current_pressure = y["pressure"]
current_humidity = y["humidity"]
z = x["weather"]
weather_description = z[0]["description"]
weather_description = z[0]["description"]
embed = discord.Embed(title=f"Weather in {city_name}",
color=ctx.guild.me.top_role.color,
timestamp=ctx.message.created_at,)
embed.add_field(name="Descripition", value=f"**{weather_description}**", inline=False)
embed.add_field(name="Temperature(C)", value=f"**{current_temperature_celsiuis}°C**", inline=False)
embed.add_field(name="Humidity(%)", value=f"**{current_humidity}%**", inline=False)
embed.add_field(name="Atmospheric Pressure(hPa)", value=f"**{current_pressure}hPa**", inline=False)
embed.set_thumbnail(url="https://i.ibb.co/CMrsxdX/weather.png")
embed.set_footer(text=f"Requested by {ctx.author.name}")
await channel.send(embed=embed)
else:
await channel.send("City not found.")
But the error I get from this is saying 'main' is a keyerror

Huobi REST API - signing requests - Verification failure

I have problem with Huobi (cryptoexchange) REST API request-signing. Rules are clear (e.g. here). Example for "v1/account/acounts" - GET with no params:
URL params:
AccessKeyId=dbye2sf5t7-d5829459-bf3aee27-67f62&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2020-11-11T12%3A17%3A57
Pre-signing text:
GET\napi.huobi.pro\n/v1/account/accounts\nAccessKeyId=dbye2sf5t7-d5829459-bf3aee27-67f62&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2020-11-11T12%3A17%3A57
Hmac SHA256-Signature of Pre-signing text with private key (verified here):
auZl70i2qsUb7+U9yYEEY1bwzLypWIM7qF1pBAJcvfc=
URL to GET: https://api.huobi.pro/v1/account/accounts?AccessKeyId=dbye2sf5t7-d5829459-bf3aee27-67f62&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2020-11-11T12%3A17%3A57&Signature=auZl70i2qsUb7%2BU9yYEEY1bwzLypWIM7qF1pBAJcvfc%3D
And the response is:
{"status":"error","err-code":"api-signature-not-valid","err-msg":"Signature not valid: Verification failure [校验失败]","data":null}
What I'm making wrong? I have searched thru many code-examples and no error found...
I had this problem with the same endpoint. I'm not sure what code you're using to sign your requests. Here's mine:
class HuobiAuth(requests.auth.AuthBase):
def __init__(self, api_key: str, secret_key: str) -> None:
self.api_key: str = api_key
self.secret_key: str = secret_key
#staticmethod
def create_sign(p_params: Dict, method: str, host_url: str, request_path: str, secret_key: str):
sorted_params = sorted(p_params.items(), key=lambda d: d[0], reverse=False)
# encode_params = urllib.urlencode(sorted_params)
encode_params = urllib.parse.urlencode(sorted_params)
payload = [method, host_url, request_path, encode_params]
payload = '\n'.join(payload)
payload = payload.encode(encoding='UTF8')
secret_key = secret_key.encode(encoding='UTF8')
digest = hmac.new(secret_key, payload, digestmod=hashlib.sha256).digest()
signature = base64.b64encode(digest)
signature = signature.decode()
return signature
def __call__(self, request: requests.PreparedRequest) -> requests.PreparedRequest:
timestamp = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')
params_to_sign = {'AccessKeyId': self.api_key,
'SignatureMethod': 'HmacSHA256',
'SignatureVersion': '2',
'Timestamp': timestamp}
host_name = urllib.parse.urlparse(request.url).hostname.lower()
params_to_sign['Signature'] = self.create_sign(params_to_sign, request.method, host_name, request.path_url, self.secret_key)
request.url += '?' + urllib.parse.urlencode(params_to_sign)
return request
My original problem was that instead of using request.method I had a hardcoded "POST", which I must have copied from the original source.
Example usage:
requests.post(url, json=your_data, auth=HuobiAuth(key, secret))

Momoko, Jinja2 and Tornado

there is something fundamentally wrong with my code. These are my
tornado handlers with basic authentication and jinja2 as template
engine. The following works without the momoko db parts.
class BaseHandler(tornado.web.RequestHandler):
#property
def db(self):
return self.application.db
def get_current_user(self):
return self.get_secure_cookie("user")
class TemplateHandler(BaseHandler):
"""Request handler for writing HTML templates."""
def render(self, template_name, **kwargs):
"""Renders a Jinja2 template."""
kwargs['options'] = options.as_dict()
template = templates.environment.get_template(template_name)
html = template.render(kwargs)
self.write(html)
class AuthLoginHandler(TemplateHandler):
def get(self):
try:
errormessage = self.get_argument("error")
except:
errormessage = ""
self.render("login.html", errormessage = errormessage)
def check_permission(self, password, username):
if username == "admin" and password == "admin":
return True
return False
def post(self):
username = self.get_argument("username", "")
password = self.get_argument("password", "")
auth = self.check_permission(password, username)
if auth:
self.set_current_user(username)
self.redirect(self.get_argument("next", u"/"))
else:
error_msg = u"?error=" + tornado.escape.url_escape("Login incorrect")
self.redirect(u"/auth/login/" + error_msg)
def set_current_user(self, user):
if user:
self.set_secure_cookie("user", tornado.escape.json_encode(user))
else:
self.clear_cookie("user")
class AuthLogoutHandler(TemplateHandler):
def get(self):
self.clear_cookie("user")
self.redirect(self.get_argument("next", "/"))
class MainHandler(TemplateHandler):
#gen.engine
def get(self):
username = tornado.escape.xhtml_escape(self.current_user)
try:
cursor = yield momoko.Op(self.db.execute, 'SELECT * FROM products;')
except Exception as error:
self.write(str(error))
res = 'Query results: '+''.join(str(cursor.fetchall()))
self.render("index.html", username = username, cip = self.request.remote_ip, res = res)
For the logged in client, this code should execute a basic query and then print the result to the defined location ( {{ res }} ) within the jinja template. When I try to start the server, I get this:
line 22, in render
kwargs['options'] = options.as_dict()
AttributeError: 'module' object has no attribute 'as_dict'
We'll need to see your "import" statements to know what's wrong for certain. I suspect you have:
from tornado import options
But you need:
from tornado.options import options