API Testing : Check failed auth

Verify that the API returns an error message if the authentication fails.

Language: Java

Framework: Rest assured

copy
12/​/​ Import the required libraries3import io.restassured.RestAssured;4import io.restassured.response.Response;5import io.restassured.specification.RequestSpecification;67/​/​ Define the endpoint URL and set up the request object8RestAssured.baseURI = "https:/​/​your.api.endpoint.com/​";9RequestSpecification request = RestAssured.given();1011/​/​ Set up the request headers and body12request.header("Content-Type", "application/​json");13String jsonBody = "{ \"username\": \"testuser\", \"password\": \"incorrectpassword\" }";14request.body(jsonBody);1516/​/​ Send the request and capture the response17Response response = request.post("/​auth");1819/​/​ Assert that the response returns an error message20assert response.getStatusCode() == 401;21assert response.getBody().asString().contains("Authentication failed. Please check your credentials.");2223/​/​ OPTIONAL: Connect to a remote client with desired capabilities by adding the following commented code:24/​*25DesiredCapabilities capabilities = new DesiredCapabilities();26capabilities.setCapability("browserName", "chrome");27capabilities.setCapability("platformName", "Windows 10");28capabilities.setCapability("platformVersion", "10.0");29capabilities.setCapability("deviceName", "WindowsPC");30capabilities.setCapability("deviceOrientation", "landscape");31RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);32*/​

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​ Assumptions4/​/​ - API endpoint is already set up5/​/​ - Failed authentication occurs when incorrect credentials are sent6/​/​ - Error message is returned in JSON format78const assert = require('chai').assert;9const request = require('request');1011describe('API Testing - Failed Auth', function() {12 it('Should return error message when authentication fails', function(done) {13 const credentials = {14 username: 'incorrect_username',15 password: 'incorrect_password'16 };17 18 request.post({url:'https:/​/​api.example.com/​login', form:credentials }, function(error, response, body) {19 const result = JSON.parse(body);20 assert.equal(response.statusCode, 401, 'Expected response status code to be 401');21 assert.equal(result.errorMessage, 'Authentication failed', 'Expected error message to be "Authentication failed"');22 done();23 });24 25 /​/​ Uncomment this section to use remote driver with desired capabilities26 /​/​ const remoteDriver = new webdriver.Builder()27 /​/​ .usingServer('http:/​/​localhost:4444/​wd/​hub')28 /​/​ .withCapabilities(webdriver.Capabilities.chrome())29 /​/​ .build();30 });31}); 3233/​/​ Note: Replace 'https:/​/​api.example.com/​login' with the actual API endpoint.

Disclaimer: Following code snippets and related information have been sourced from GitHub and/or generated using AI code generation tools. LambdaTest takes no responsibility in the accuracy of the code and is not liable for any damages.

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Power Your Software Testing with AI and cloud

Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.

Start Free Testing