API Testing : Check API audit logs

Verify that the API correctly handles API audit logs and returns the correct resources for each API audit log.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the following details about the API:2/​/​Endpoint: https:/​/​example.com/​api/​audit-logs3/​/​Authorization header required: Yes4/​/​Resources returned: timestamp, action, user_id, resource_id56import org.junit.Test;7import static io.restassured.RestAssured.*;8import static org.hamcrest.Matchers.*;910public class APIAuditLogTest {11 12 @Test13 public void testAPIAuditLog() {14 15 /​/​Connecting to local driver16 given()17 .baseUri("https:/​/​example.com")18 .header("Authorization", "Bearer <access_token>")19 .when()20 .get("/​api/​audit-logs")21 .then()22 .assertThat()23 .statusCode(200)24 .body("timestamp", hasItems("2021-06-01T12:00:00Z", "2021-06-02T10:00:00Z"))25 .body("action", hasItems("create", "update"))26 .body("user_id", hasItems(1234, 5678))27 .body("resource_id", hasItems(100, 200));28 29 /​/​Code to connect to remote client with desired capabilities30 /​/​DesiredCapabilities capabilities = new DesiredCapabilities();31 /​/​capabilities.setBrowserName("chrome");32 /​/​RemoteWebDriver driver = new RemoteWebDriver(new URL("<remote_url>"), capabilities);33 }34} 3536/​/​Note: Replace <access_token> with a valid access token and uncomment the code to connect to remote client with desired capabilities. Also update the expected values for timestamp, action, user_id and resource_id as per the API documentation.

Language: Javascript

copy
1/​/​ Mocha + Chai. 234/​/​Assumptions: API endpoint is accessible with the correct credentials and contains audit logs for testing 56const chai = require("chai");7const expect = chai.expect;8const request = require("request");910describe("API Audit Log testing", function(){11 it("should return the correct resources for each API audit log", function(done){12 /​/​Assume API endpoint URL and auth is available13 let options = {14 url: 'API Endpoint URL',15 headers: {16 'Authorization': 'Bearer AUTH_TOKEN',17 'Content-Type': 'application/​json'18 }19 };2021 request(options, function(error, response, body){22 if(error) done(error); /​/​In case there is an error during the request23 24 let auditLogs = JSON.parse(body); /​/​Assuming logs are returned in JSON format25 26 auditLogs.forEach(function(log){ /​/​Loop through each log and check resources27 expect(log.resource).to.not.be.null; /​/​Resources should not be null28 expect(log.resource).to.be.a('string'); /​/​Resources should be of type string29 expect(log.resource).to.match(/​^[a-zA-Z0-9_\-]+$/​); /​/​Resources should only contain alphanumeric characters, underscores, and hyphens30 });31 done();32 });33 });34});3536/​/​Assuming there is a separate configuration file for remote client 37/​/​const webdriver = require('selenium-webdriver');38/​/​const remote = require('selenium-webdriver/​remote');3940/​/​ let capabilities = webdriver.Capabilities.firefox();41/​/​ capabilities.set('browserName', 'firefox');42/​/​ const driver = new webdriver.Builder()43/​/​ .withCapabilities(capabilities)44/​/​ .usingServer('http:/​/​REMOTE_SERVER:4444/​wd/​hub')45/​/​ .build()

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