dmin login Module Using Base64 in REST Assured- Notes By ShariqSP

Working with Admin API Methods Using RestAssured and TestNG

This guide explains how to work with the following API methods in a Spring Boot application using RestAssured and TestNG:

  • Add Admin
  • Admin Login
  • Delete Admin
  • Get All Admins

RestAssured is used for testing REST APIs by sending HTTP requests and verifying responses. TestNG helps to organize test cases and assertions.

1. Adding an Admin

The POST /api/admin endpoint is used to add a new admin. This requires a valid JSON payload representing the admin details.

                    @Test
                    public void testAddAdmin() {
                           JSONObject bodyContent = new JSONObject();

                           bodyContent.put("id", "123");
                           bodyContent.put("username", "admin");
                           bodyContent.put("password", "password");

                           given()
                           .header("Content-Type","application/JSON")
                           .body(bodyContent.toString())
                           .post("http://apitesting.shariqsp.com:8080/api/admin/add")
                           .then()
                           .assertThat()
                           .statusCode(201)
                           .log();
                    }
                

2. Admin Login

The POST /api/admin/login endpoint allows an admin to log in. The credentials must be encoded in Base64 and sent in the Authorization header.

                    @Test
                    public void testLoginAdmin() {

                        String originalInput = "admin:password";
                        String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
                        System.out.println("Base64 Encoded: " + encodedString);

                        given()
                        .header("Authorization", "Basic " + encodedString)
                           .post("http://apitesting.shariqsp.com:8080/api/admin/login")
                           .then()
                           .assertThat()
                           .statusCode(200)
                           .log()
                           .all();
                    }
                

3. Deleting an Admin

The DELETE /api/admin/{id} endpoint is used to delete an admin by their ID. This requires valid admin credentials.

                    @Test
                    public void deleteAdmin()
                    {
                        given()
                        .delete("http://apitesting.shariqsp.com:8080/api/admin/2")
                        .then()
                        .log()
                        .all();
                    }

                

4. Retrieving All Admins

The GET /api/admin endpoint retrieves a list of all admins. Admin credentials are required for authentication.

                    @Test
                    public void displayAdmins()
                    {
                        given()
                        .get("http://apitesting.shariqsp.com:8080/api/admin/all")
                        .then()
                        .log()
                        .all();
                    }

                

Key Points

  • Use Base64 encoding for login credentials.
  • Always pass the Authorization header for authenticated endpoints.
  • Use TestNG assertions to validate the HTTP response codes and content.
  • Ensure the Spring Boot application is running before executing the tests.

Following this approach ensures comprehensive test coverage for the Admin module and helps verify the API's behavior using RestAssured and TestNG.