API Testing : Check data encryption-decryption

Verify that the API correctly handles encryption and decryption of data.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API endpoint URL to be tested23import org.junit.Assert;4import org.junit.Test;5import io.restassured.RestAssured;6import io.restassured.response.Response;78public class EncryptionDecryptionTest {910 @Test11 public void testEncryptionDecryption() {12 /​/​Assuming sample data to be encrypted and decrypted13 String data = "Hello, World!";14 15 /​/​Assuming encryption is done using AES algorithm with secret key16 String key = "1234abcd5678efgh";17 String encryptedData = null;1819 /​/​Assuming API endpoint to return encrypted data in response body20 Response response = RestAssured21 .given()22 .body(data)23 .when()24 .post("/​encrypt") /​/​Assuming endpoint to be /​encrypt25 .then()26 .extract().response();27 28 if(response.getStatusCode() == 200) { /​/​Assuming status code for success is 20029 encryptedData = response.getBody().asString();30 }31 32 /​/​Assuming API endpoint to return decrypted data in response body33 String decryptedData = null;34 response = RestAssured35 .given()36 .body(encryptedData)37 .when()38 .post("/​decrypt") /​/​Assuming endpoint to be /​decrypt39 .then()40 .extract().response();41 42 if(response.getStatusCode() == 200) { /​/​Assuming status code for success is 20043 decryptedData = response.getBody().asString();44 }45 46 Assert.assertEquals(data, decryptedData);47 }48 49 /​/​Uncomment to connect to remote client with desired capabilities50 /​*51 @Test52 public void testEncryptionDecryptionOnRemoteServer() {53 DesiredCapabilities capabilities = new DesiredCapabilities();54 capabilities.setCapability(CapabilityType.BROWSER_NAME, "chrome");55 capabilities.setCapability(CapabilityType.VERSION, "80.0");56 capabilities.setCapability("enableVNC", true);57 58 RemoteWebDriver driver = null;59 try {60 driver = new RemoteWebDriver(61 new URL("http:/​/​localhost:4444/​wd/​hub"), 62 capabilities63 );64 65 driver.get("https:/​/​example.com");66 /​/​Add test logic here67 } catch (MalformedURLException e) {68 e.printStackTrace();69 } finally {70 if(driver != null) {71 driver.quit();72 }73 }74 }75 */​76}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assumptions:4/​/​1. The API endpoint for data encryption/​decryption is: https:/​/​example.com/​encryptdecrypt5/​/​2. The encryption key is provided in the headers of the API request.6/​/​3. The API responds with a status code of 200 if encryption/​decryption was successful.78const request = require('request');9const expect = require('chai').expect;1011describe('API Testing - Data Encryption/​Decryption', function() {12 /​/​Initialize the API endpoint URL and encryption key.13 const apiUrl = 'https:/​/​example.com/​encryptdecrypt';14 const encryptionKey = 'myEncryptionKey';1516 it('Should encrypt and decrypt data successfully', function(done) {17 /​/​Assume the data to be encrypted is a JSON object with the following properties.18 const dataToEncrypt = {19 name: 'John Doe',20 email: 'johndoe@example.com',21 password: 'password123'22 };23 /​/​Convert the JSON object to a string.24 const dataToEncryptString = JSON.stringify(dataToEncrypt);2526 /​/​Assume the headers to be sent with the API request.27 const headers = {28 'content-type': 'application/​json',29 'encryption-key': encryptionKey30 };3132 /​/​Send an API request to encrypt the data.33 request.post({34 url: apiUrl,35 headers: headers,36 body: dataToEncryptString37 }, function(error, response, body) {38 /​/​Verify that the API response has a status code of 200.39 expect(response.statusCode).to.equal(200);4041 /​/​Assume that the API response is a JSON object with the encrypted data and status.42 const apiResponse = JSON.parse(body);4344 /​/​Assume that the encrypted data is returned in the 'data' property of the API response.45 const encryptedData = apiResponse.data;4647 /​/​Assume that the decryption key is the same as the encryption key.48 const decryptionKey = encryptionKey;4950 /​/​Assume that the headers to be sent with the API request to decrypt the data are the same as the headers for encryption.51 const decryptionHeaders = headers;5253 /​/​Send an API request to decrypt the data.54 request.post({55 url: apiUrl,56 headers: decryptionHeaders,57 body: encryptedData58 }, function(error, response, body) {59 /​/​Verify that the API response has a status code of 200.60 expect(response.statusCode).to.equal(200);6162 /​/​Assume that the API response is a JSON object with the decrypted data and status.63 const apiResponse = JSON.parse(body);6465 /​/​Assume that the decrypted data is returned in the 'data' property of the API response.66 const decryptedData = apiResponse.data;6768 /​/​Verify that the decrypted data is the same as the original data.69 expect(decryptedData).to.equal(dataToEncryptString);7071 /​/​Complete the test.72 done();73 });74 });75 });76});7778/​/​Uncomment the following code to connect to a remote client with desired capabilities.79/​*80const webdriver = require('selenium-webdriver');81const {Capabilities} = require('selenium-webdriver/​lib/​capabilities');82const desiredCapabilities = Capabilities.chrome();83const remoteUrl = 'http:/​/​localhost:4444/​wd/​hub';84const driver = new webdriver.Builder()85 .usingServer(remoteUrl)86 .withCapabilities(desiredCapabilities)87 .build();88*/​

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