API Testing : Check authentication handling

Verify that the API correctly handles authentication based on user account or API key.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API endpoint requires authentication based on user account or API key23import org.junit.Test;4import static io.restassured.RestAssured.*;5import static org.hamcrest.Matchers.*;67public class AuthenticationTest {89 /​/​Local Driver10 @Test11 public void testAuthenticationWithUser() {12 13 given()14 .auth()15 .basic("user","password")16 .when()17 .get("https:/​/​example.com/​api/​endpoint")18 .then()19 .statusCode(200);20 }21 22 /​/​Remote Client with desired capabilities23 /​/​@Test24 public void testAuthenticationWithApiKey() {25 26 given()27 .auth()28 .oauth2("API Key")29 .when()30 .get("https:/​/​example.com/​api/​endpoint")31 .then()32 .statusCode(200);33 34 /​/​Desired Capabilities code to connect to remote client35 /​*DesiredCapabilities capabilities = new DesiredCapabilities();36 capabilities.setCapability("browserName", "Chrome");37 capabilities.setCapability("platformName", "Windows 10");38 WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);*/​39 }4041}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​ Assumptions: 4/​/​ 1. We have access to the API endpoint for testing. 5/​/​ 2. The API requires authentication through user account or API key. 6/​/​ 3. We have valid credentials to test with. 78describe('API Authentication Testing', function() {9 it('should handle authentication based on user account', function() {10 /​/​ Connecting to the API endpoint using local driver11 let url = 'http:/​/​localhost:8080/​api/​v1/​users'; /​/​ Update URL with actual API endpoint12 let driver = require('request');13 let options = {14 url: url,15 headers: {16 'user': 'testuser', /​/​ Replace with actual test user account17 'password': 'testpassword' /​/​ Replace with actual test user account password18 }19 };20 /​/​ Making the API request with authentication through user account21 driver.get(options, function(error, response, body) {22 assert.equal(response.statusCode, 200); /​/​ Asserting that response code is 200 OK23 assert.include(JSON.parse(body), {'authenticated': true}); /​/​ Asserting that authentication is successful24 });25 });2627 it('should handle authentication based on API key', function() {28 /​/​ Connecting to the API endpoint using local driver29 let url = 'http:/​/​localhost:8080/​api/​v1/​users'; /​/​ Update URL with actual API endpoint30 let driver = require('request');31 let options = {32 url: url,33 headers: {34 'x-api-key': 'testapikey' /​/​ Replace with actual test API key35 }36 };37 /​/​ Making the API request with authentication through API key38 driver.get(options, function(error, response, body) {39 assert.equal(response.statusCode, 200); /​/​ Asserting that response code is 200 OK40 assert.include(JSON.parse(body), {'authenticated': true}); /​/​ Asserting that authentication is successful41 });42 });43}); 4445/​/​ Commented code to connect to a remote client with desired capabilities 46/​*47let webdriver = require('selenium-webdriver');48let capabilities = webdriver.Capabilities.chrome();49capabilities.set('chromeOptions', {50 'args': ['--disable-extensions','--disable-infobars']51});52let driver = new webdriver.Builder()53 .withCapabilities(capabilities)54 .usingServer('http:/​/​localhost:4444/​wd/​hub')55 .build();56*/​

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