0

*New to Rest Assured

Question: How do I pass values provided from endpoint 'A' to endpoint 'B'?

My scenario: (using a UI api service that provides curls)

  1. [Endpoint 1] I provide a username in [GET] user/signin.
  2. I then get the follow response

{
  "session": "Need to pass this session Id which changes everytime for every signin",
  "challengeName": "CUSTOM_CHALLENGE",
  "challengeSentTo": "example@EMAIL.com",
  "username": "This id never changes"
}
  1. [Endpoint 2] I need to pass the session Id and username Id to the second endpoint which is a [GET] /user/verifyCode. This endpoint requires the following:
    1.The session id (string from endpoint 1)
    2.username id (string from endpoint 1)
    3.An api key

Curl for endpoint 2:
curl -X GET "https://example.com/apiservice/m1/users/verifyCode" -H "accept: application/json" -H "session: id from GET user/signin" -H "username: id from GET user/signin" -H "X-API-KEY: api key needed"

My Code for GET user/sign in (works for GET user/signin). Need to get this to work for Step 3

    public void getInitialAuthSignIn() {
            .given()
            .header("X-API-KEY", "apiKey in here")
            .queryParam("challengeMethod", "EMAIL")
            .header("alias", "example@EMAIL.com")
            .when()
            .log().all()
            .get(baseUri + basePath + "/users/signin");

}
foragerEngineer
  • 149
  • 1
  • 2
  • 13

1 Answers1

3

You can run the initial sign in API call first and set the sessionId, username from the response as class variables. Then use them in all other API calls you want.

import io.restassured.path.json.JsonPath;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;

public class AuthResponseTest {

    private static final String baseUri = "https://ExampleApiService.com";
    private static final String basePath = "/apiservice/m1";
    private static final String verifyPath = "/users/verifyCode";
    private static final String usenameId = "fixed_id_never_changes";

    private String sessionId;
    private String userName;

    @BeforeMethod // Use the appropriate TestNG annotation here.
    public void getInitialAuthSignIn() {
        JsonPath jsonPath = given()
                .contentType("application/json")
                .header("X-API-KEY", "apiKey in here")
                .queryParam("challengeMethod", "EMAIL")
                .header("alias", "example@EMAIL.com")
                .when()
                .log().all()
                .get(baseUri + basePath + "/users/signin")
                .then()
                .extract()
                .jsonPath();

        this.sessionId = jsonPath.get("session");
        this.userName = jsonPath.get("username");
    }

    @Test
    public void testVerifyCode() {

        given()
                .header("X-API-KEY", "apiKey in here")
                .header("session", this.sessionId)
                .header("username", this.userName)
                .when()
                .log().all()
                .get(baseUri + basePath + "/users/verifyCode")
                .then()
                .extract()
                .response()
                .prettyPrint();
    }

}

I have used the TestNG annotations here.

kaweesha
  • 803
  • 6
  • 16
  • Hello, I ran the code however I'm getting an error saying "No tests were found". – foragerEngineer Jun 21 '20 at 20:25
  • Nevermind, I got it to work. Thank you so much. I will look up the syntax I'm unfamiliar with which you provided to me so I can understand what you did. From my understanding, you made two class variables so that when `.extract().response.jsonpath()` is executed, we can store the captured response inside of the class variables, then call on them accordingly. – foragerEngineer Jun 21 '20 at 20:42
  • One other last question. I would like to return my `getInitialAuthSignIn` so that I can toss it into a test annotation. But I'm having difficulty doing that for some reason. I updated my code above to show what I'm doing. – foragerEngineer Jun 21 '20 at 20:52
  • Ok I think I fixed my problem with trying to return the `initialAuthSignIn`. Thank you very much for your help with this. – foragerEngineer Jun 21 '20 at 23:44
  • Im not very clear what you meant by "I would like to return my getInitialAuthSignIn so that I can toss it into a test annotation. ". Please refer TestNG tutorials to understand more on TestNG. – kaweesha Jun 22 '20 at 05:07
  • 2
    Please don't change the original question to a different question after receiving answers. When others (who come looking for answers for the same question) open this post, they will see different title, different question and different answers, different tags which don't have any connection between them. So please add a new question for your new problem. – kaweesha Jun 22 '20 at 05:16
  • 1
    Got it, my apologies. I have re-editted my code to the original state so that other's can learn from the original issue. Thank you! – foragerEngineer Jun 22 '20 at 05:22
  • 1
    Refer the part, "When should I edit posts?" in [edit questions and answers](https://stackoverflow.com/help/privileges/edit) – kaweesha Jun 22 '20 at 05:26