How to pass API request test case

i am trying to add negative test cases for login api request , where request status code 409 and that is as expected and design

at robot framework i have designed below code but during execution test case status fail and that is not correct .

what i am need is away to make these test cases pass as long as status code 409

*** Settings ***
Library RequestsLibrary
Test Template Login

*** Test Cases *** Username Password
Login with invalid username should fail invalid ${VALID_PASSWORD}
Login with invalid password should fail ${VALID_USER} invalid
Login with invalid username and password should fail invalid invalid
Login with empty username should fail ${EMPTY} ${VALID_PASSWORD}
Login with empty password should fail ${VALID_USER} ${EMPTY}
Login with empty username and password should fail ${EMPTY} ${EMPTY}

*** Keywords ***

Login
[Arguments] ${USERNAME} ${PASSWORD}
${data}= Evaluate {‘Username’: (None, ‘${USERNAME}’), ‘Password’: (None, ‘${PASSWORD}’)}
${response}= POST ${Server_url}${Login} files=${data}

Log To Console ${response.content}

assertions

Should Be Equal As Strings ${response.status_code} 409
${json_dict}= Evaluate json.loads(‘’‘${response.content}’‘’) modules=json
Should not Be Equal As Strings None ${json_dict[‘errorCode’]}
Should not Be Equal As Strings None ${json_dict[‘errorMessage’]}
Should not Be Equal As Strings Authentication failed ${json_dict[‘errorMessage’]}
Should Be Equal As Strings failed ${json_dict[‘status’]}
Should Be Equal As Strings None ${json_dict[‘data’]}

below is execution error , you will notice that execution stopped at post line and keywords after that not run which contain assertions

TEST Login with invalid username should fail

Full Name: AP Login with invalid username should fail
Start / End / Elapsed: 20230410 11:28:49.213 / 20230410 11:28:49.294 / 00:00:00.081
Status: FAIL
Message: HTTPError: 409 Client Error: Conflict for url: {serverurl}

00:00:00.074KEYWORD Login invalid, ${INVALID_PASSWORD}

Start / End / Elapsed: 20230410 11:28:49.219 / 20230410 11:28:49.293 / 00:00:00.074

00:00:00.000KEYWORD ${data} = BuiltIn . Evaluate {‘Username’: (None, ‘${USERNAME}’), ‘Password’: (None, ‘${PASSWORD}’)}

00:00:00.071KEYWORD ${response} = RequestsLibrary . POST ${Server_url}${AdminLogin}, files=${data}

Documentation: Sends a POST request.
Start / End / Elapsed: 20230410 11:28:49.220 / 20230410 11:28:49.291 / 00:00:00.071

11:28:49.286 INFO POST Request : url={serverurl} path_url={login endpoint} headers={‘User-Agent’: ‘python-requests/2.28.2’, ‘Accept-Encoding’: ‘gzip, deflate’, ‘Accept’: ‘/’, ‘Connection’: ‘keep-alive’, ‘Content-Length’: ‘230’, ‘Content-Type’: ‘multipart/form-data; boundary=e16badfce03cd6e03ba679560f63acc5’} body=b’–e16badfce03cd6e03ba679560f63acc5\r\nContent-Disposition: form-data; name=“Username”\r\n\r\ninvalid\r\n–e16badfce03cd6e03ba679560f63acc5\r\nContent-Disposition: form-data; name=“Password”\r\n\r\valid\r\n–e16badfce03cd6e03ba679560f63acc5–\r\n’

11:28:49.286 INFO POST Response : url={server}/{endpoint} status=409, reason=Conflict headers={‘Cache-Control’: ‘no-cache’, ‘Pragma’: ‘no-cache’, ‘Content-Length’: ‘151’, ‘Content-Type’: ‘application/json; charset=utf-8’, ‘Expires’: ‘-1’, ‘Server’: ‘Microsoft-IIS/8.5’, ‘X-AspNet-Version’: ‘4.0.30319’, ‘X-Powered-By’: ‘ASP.NET’, ‘Access-Control-Allow-Origin’: ‘', ‘Access-Control-Allow-Headers’: '’, ‘Access-Control-Allow-Methods’: ‘GET, POST, PUT, DELETE, OPTIONS’, ‘Date’: ‘Mon, 10 Apr 2023 08:29:50 GMT’} body={“data”:null,“status”:“failed”,“errorCode”:“GWUM_UL_001”,“errorMessage”:{“EN”:“Failed to find the user. Authentication failed”,“AR”:“”,“Details”:null}}

FAIL HTTPError: 409 Client Error: Conflict for url: {server}/{endpoint}
KEYWORD BuiltIn . Should Be Equal As Strings ${response.status_code}, 409

00:00:00.000KEYWORD ${json_dict} = BuiltIn . Evaluate json.loads(‘’‘${response.content}’‘’), modules=json

00:00:00.000KEYWORD BuiltIn . Should Not Be Equal As Strings None, ${json_dict[‘errorCode’]}

00:00:00.000KEYWORD BuiltIn . Should Not Be Equal As Strings None, ${json_dict[‘errorMessage’]}

00:00:00.000KEYWORD BuiltIn . Should Be Equal As Strings failed, ${json_dict[‘status’]}

00:00:00.000KEYWORD BuiltIn . Should Be Equal As Strings None, ${json_dict[‘data’]}

Hi Khalid,

In the documentation for POST, you’ll see expected_status perhaps this is what you’re after?

There’s more detail about expected_status in the documentation for Status Should Be

Pay particular attention to this paragraph:

New requests keywords like GET or GET On Session (starting from 0.8 version) already have an implicit assert mechanism that, by default, verifies the response status code. Status Should Be keyword can be useful when you disable implicit assert using expected_status=anything.

It caught me out at first when version 0.8 came out.

Dave.