API Testing : Check auth types

Verify that the API correctly handles various types of authentication, such as basic authentication, token authentication, and OAuth.

Language: Java

Framework: Rest assured

copy
1/​/​Assumptions: 2/​/​1. API Endpoint URL is already defined3/​/​2. Credentials for Basic Authentication, Token Authentication, and OAuth are provided and valid45import org.junit.jupiter.api.Test;6import static io.restassured.RestAssured.*;7import static io.restassured.matcher.RestAssuredMatchers.*;8import static org.hamcrest.Matchers.*;910public class AuthenticationTest {1112 @Test13 public void testBasicAuthentication() {14 given()15 .auth()16 .basic("username", "password")17 .when()18 .get("apiEndpointURL")19 .then()20 .statusCode(200);21 }22 23 @Test24 public void testTokenAuthentication() {25 given()26 .auth()27 .oauth2("accessToken")28 .when()29 .get("apiEndpointURL")30 .then()31 .statusCode(200);32 }33 34 @Test35 public void testOAuthAuthentication() {36 given()37 .auth()38 .oauth("consumerKey", "consumerSecret", "accessToken", "accessSecret")39 .when()40 .get("apiEndpointURL")41 .then()42 .statusCode(200);43 }44 45 /​/​Code to connect to remote client with desired capabilities46 /​*47 DesiredCapabilities desiredCapabilities = new DesiredCapabilities();48 desiredCapabilities.setCapability(CapabilityType.PLATFORM_NAME, "android");49 desiredCapabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");50 desiredCapabilities.setCapability(CapabilityType.VERSION, "4.4.2");51 URL remoteURL = new URL("http:/​/​remoteUrl:port/​wd/​hub");52 WebDriver driver = new RemoteWebDriver(remoteURL, desiredCapabilities);53 */​54}

Language: Javascript

copy
1/​/​Assuming that the API is hosted on http:/​/​localhost:8080/​23const axios = require('axios');45describe('API Testing', () => {6 it('should handle various types of authentication', async () => {7 /​/​Assuming that basic authentication is used for /​login endpoint8 const basicAuth = {9 username: 'user123',10 password: 'pass123'11 };12 const basicAuthRes = await axios.get('http:/​/​localhost:8080/​login', {13 auth: basicAuth14 });15 expect(basicAuthRes.status).toEqual(200);1617 /​/​Assuming that token authentication is used for /​data endpoint18 const tokenAuth = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';19 const tokenAuthRes = await axios.get('http:/​/​localhost:8080/​data', {20 headers: {21 Authorization: `Bearer ${tokenAuth}`22 }23 });24 expect(tokenAuthRes.status).toEqual(200);2526 /​/​Assuming that OAuth authentication is used for /​account endpoint27 const oauthAuth = {28 client_id: 'abc123',29 client_secret: 'def456',30 token: 'xyz789'31 };32 const oauthAuthRes = await axios.get('http:/​/​localhost:8080/​account', {33 params: oauthAuth34 });35 expect(oauthAuthRes.status).toEqual(200);36 });3738 /​/​Uncomment the code below to connect to a remote client with desired capabilities39 /​*40 const { Builder } = require('selenium-webdriver');41 const { DesiredCapabilities } = require('selenium-webdriver/​lib/​capabilities');4243 it('should connect to a remote client with desired capabilities', async () => {44 const capabilities = DesiredCapabilities.chrome();45 capabilities.set('platform', 'macOS 10.15');46 capabilities.set('version', 'latest');4748 const driver = await new Builder()49 .usingServer('http:/​/​localhost:4444/​wd/​hub')50 .withCapabilities(capabilities)51 .build();5253 await driver.get('http:/​/​localhost:8080/​login');54 expect(await driver.getTitle()).toEqual('Login Page');5556 await driver.quit();57 });58 */​59});

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