authorjapps / zerocode-hello-world

Zerocode YAML and JSON based declarative steps hello world rest api testing example - soap, database
https://github.com/authorjapps/zerocode
MIT License
18 stars 40 forks source link

Passing access token received from previous call to my GET request #12

Closed authorjapps closed 5 years ago

authorjapps commented 5 years ago

Hi,

I was exploring zerocode framework and found very easy automating rest api tests.

I having a scenario where i need to pass accesstoken received from previous call to my GET request. This GET request takes token as x-col-jwt key in header. When i am using it i am not able to authenticate the request and it gjve me 401.

Its working fine in postman and custom java code.

Please help me to resolve this

Thanks

Get Outlook for Android

santhoshTpixler commented 5 years ago

I'll check weather header variables are replaced properly or not. But for further debugging we need the sample code which threw the error.

gsolaich commented 5 years ago

This is the payload I used.

{
        "scenarioName": "Login as user and get ME details",
        "steps": [
            {
                "name": "validate_login",
                "url": "/auth/local",
                "operation": "POST",
                "request": {
                    "header": {
                        "Content-Type":"application/x-www-form-urlencoded"
                    },
                    "body": {
                        "username": "pari1@yopmail.com",
                        "password": "Welcome@123"
                    }
                },
                "assertions": {
                    "status": 200
                }
            },
            {
                "name": "get_emp_details",
                "url": "/me",
                "operation": "GET",
                "request": {
                    "header": {
                        "x-col-jwt":"${$.validate_login.response.rawBody}",
                        "Content-Type":"application/json"
                    }
                },
                "assertions": {
                    "status": 200

                }
            }

        ]
    }
authorjapps commented 5 years ago

Thanks for the steps. Is the token an xml content? Just checking... (It should not matter anyway)

1)

Assuming $.validate_login.response.rawBody holds the token, do you have a sample response which you can stick to this ticket?

You can get the sample response from your IDE console after you run the test - just look for the Response after the step "name": "validate_login", in the JUnit console.

This will help us to see what exactly passed to the next step.

2)

Also the screen shot of PostMan where you are sending the header.

3)

Also the console output of the GET step get_emp_details here, just to see what's going in the headers "x-col-jwt":"${$.validate_login.response.rawBody}",

gsolaich commented 5 years ago

This is the entire json.

2019-02-21 15:17:25,641 [main] INFO  org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl - 
-------------------------- BDD: Scenario:Login as user and get ME details -------------------------

2019-02-21 15:17:25,662 [main] INFO  org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl - 
### Executing Scenario -->> Count No: 0
2019-02-21 15:17:25,663 [main] INFO  org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl - 
### Executing Step -->> Count No: 0
2019-02-21 15:17:25,760 [main] INFO  org.jsmart.zerocode.core.httpclient.ssl.SslTrustHttpClient - ###Used SSL Enabled Http Client for http/https/TLS connections
Feb 21, 2019 3:17:28 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Invalid cookie header: "Set-Cookie: AWSALB=HcL60G4FGvuKVXO9M8v15RU1vKXo26FqHdLfOtBsnXpl/oNksnl1DEkaeqgEAjHWOTwc2heBUZ4lZtVTbU9O/Hv5LeS6oYhvjL2ogMlz9RE81NCWQRx5Ewq+/Jc0; Expires=Thu, 28 Feb 2019 09:47:27 GMT; Path=/". Invalid 'expires' attribute: Thu, 28 Feb 2019 09:47:27 GMT
Feb 21, 2019 3:17:28 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Invalid cookie header: "Set-Cookie: col:jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImNqcng0MXEzbjAwM3IwbG54aTdrcDh5NzQiLCJhY2NvdW50X3R5cGUiOjAsImFjY291bnRfaWQiOiJwYXJpMUB5b3BtYWlsLmNvbSIsImFjY291bnRfdG9rZW4iOiIkMmEkMTAkUE5wSG0wL2g0T2NHZnVxTWVZS0VvZVREaW9uN0dQSTJndUNDMnRZSEpnYUR3aGx5alJDSHUiLCJlbWFpbCI6InBhcmkxQHlvcG1haWwuY29tIiwiZmlyc3RfbmFtZSI6IlBhcmkiLCJsYXN0X25hbWUiOiJLIiwicHJvZmlsZV9pbWFnZSI6bnVsbCwibGFzdF9sb2dpbiI6IjIwMTktMDItMjFUMDk6NDc6MjguMDAwWiIsImxhc3RfbG9nb3V0IjoiMjAxOS0wMi0xOVQwNTozMzowMC4wMDBaIiwiaWF0IjoxNTUwNzQyNDQ4LCJleHAiOjE1NTEzNDcyNDh9.sOHjUJ5HBqdl_-IOAy605RAET9Mkuf4Zo8pELqnIhcs; path=/; expires=Thu, 28 Feb 2019 09:47:28 GMT; httponly". Invalid 'expires' attribute: Thu, 28 Feb 2019 09:47:28 GMT
2019-02-21 15:17:28,480 [main] WARN  org.jsmart.zerocode.core.engine.executor.JsonServiceExecutorImpl - 
---------------------------------------------

                         * Warning *  

Output was not a valid JSON body. It was treated as a simple rawBody. If it was intentional, you can ignore this warning. 
 -OR- Update your assertions block with "rawBody" instead of "body" 
 e.g. "rawBody" : "an expected string "

---------------------------------------------
2019-02-21 15:17:28,491 [main] INFO  org.jsmart.zerocode.core.runner.StepNotificationHandler - 
***Step PASSED:Login as user and get ME details->validate_login
2019-02-21 15:17:28,493 [main] INFO  org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl - 
--------- TEST-STEP-CORRELATION-ID: c2893c91-2514-4b41-ac4a-733a987744e5 ---------
*requestTimeStamp:2019-02-21T15:17:25.704
step:validate_login
url:/api/auth/local
method:POST
request:
{
  "header" : {
    "Content-Type" : "application/x-www-form-urlencoded"
  },
  "body" : {
    "username" : "pari1@yopmail.com",
    "password" : "Welcome@123"
  }
} 
--------- TEST-STEP-CORRELATION-ID: c2893c91-2514-4b41-ac4a-733a987744e5 ---------
Response:
{
  "status" : 200,
  "headers" : {
    "Date" : [ "Thu, 21 Feb 2019 09:47:28 GMT" ],
    "Content-Length" : [ "565" ],
    "Set-Cookie" : [ "AWSALB=HcL60G4FGvuKVXO9M8v15RU1vKXo26FqHdLfOtBsnXpl/oNksnl1DEkaeqgEAjHWOTwc2heBUZ4lZtVTbU9O/Hv5LeS6oYhvjL2ogMlz9RE81NCWQRx5Ewq+/Jc0; Expires=Thu, 28 Feb 2019 09:47:27 GMT; Path=/", "col:jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImNqcng0MXEzbjAwM3IwbG54aTdrcDh5NzQiLCJhY2NvdW50X3R5cGUiOjAsImFjY291bnRfaWQiOiJwYXJpMUB5b3BtYWlsLmNvbSIsImFjY291bnRfdG9rZW4iOiIkMmEkMTAkUE5wSG0wL2g0T2NHZnVxTWVZS0VvZVREaW9uN0dQSTJndUNDMnRZSEpnYUR3aGx5alJDSHUiLCJlbWFpbCI6InBhcmkxQHlvcG1haWwuY29tIiwiZmlyc3RfbmFtZSI6IlBhcmkiLCJsYXN0X25hbWUiOiJLIiwicHJvZmlsZV9pbWFnZSI6bnVsbCwibGFzdF9sb2dpbiI6IjIwMTktMDItMjFUMDk6NDc6MjguMDAwWiIsImxhc3RfbG9nb3V0IjoiMjAxOS0wMi0xOVQwNTozMzowMC4wMDBaIiwiaWF0IjoxNTUwNzQyNDQ4LCJleHAiOjE1NTEzNDcyNDh9.sOHjUJ5HBqdl_-IOAy605RAET9Mkuf4Zo8pELqnIhcs; path=/; expires=Thu, 28 Feb 2019 09:47:28 GMT; httponly" ],
    "Server" : [ "openresty" ],
    "Vary" : [ "Origin" ],
    "Content-Type" : [ "text/plain; charset=utf-8" ],
    "Connection" : [ "keep-alive" ],
    "X-Cache" : [ "MISS" ],
    "Age" : [ "1" ],
    "Via" : [ "http/1.1 api-umbrella (ApacheTrafficServer [cMsSf ])" ]
  },
  "rawBody" : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.pZCI6ImNqcng0MXEzbjAwM3IwbG54aTdrcDh5NzQiLCJhY2NvdW50X3R5cGUiOjAsImFjY291bnRfaWQiOiJwYXJpMUB5b3BtYWlsLmNvbSIsImFjY291bnRfdG9rZW4iOiIkMmEkMTAkUE5wSG0wL2g0T2NHZnVxTWVZS0VvZVREaW9uN0dQSTJndUNDMnRZSEpnYUR3aGx5alJDSHUiLCJlbWFpbCI6InBhcmkxQHlvcG1haWwuY29tIiwiZmlyc3RfbmFtZSI6IlBhcmkiLCJsYXN0X25hbWUiOiJLIiwicHJvZmlsZV9pbWFnZSI6bnVsbCwibGFzdF9sb2dpbiI6IjIwMTktMDItMjFUMDk6NDc6MjguMDAwWiIsImxhc3RfbG9nb3V0IjoiMjAxOS0wMi0xOVQwNTozMzowMC4wMDBaIiwiaWF0IjoxNTUwNzQyNDQ4LCJleHAiOjE1NTEzNDcyNDh9.sOHjUJ5HBqdl_-IOAy605RAET9Mkuf4Zo8pELqnIhcs"
}
*responseTimeStamp:2019-02-21T15:17:28.486 
*Response delay:2782.0 milli-secs 
---------> Assertion: <----------
{
  "status" : 200
} 
-done-

2019-02-21 15:17:28,494 [main] INFO  org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl - 
### Executing Step -->> Count No: 0
2019-02-21 15:17:28,500 [main] INFO  org.jsmart.zerocode.core.httpclient.ssl.SslTrustHttpClient - ###Used SSL Enabled Http Client for http/https/TLS connections

2019-02-21 15:17:29,663 [main] WARN  org.jsmart.zerocode.core.engine.executor.JsonServiceExecutorImpl - 
---------------------------------------------

                         * Warning *  

Output was not a valid JSON body. It was treated as a simple rawBody. If it was intentional, you can ignore this warning. 
 -OR- Update your assertions block with "rawBody" instead of "body" 
 e.g. "rawBody" : "an expected string "

---------------------------------------------Feb 21, 2019 3:17:29 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Invalid cookie header: "Set-Cookie: AWSALB=RJ3wXUpz9vavPnXJePnilN4DsP6kRMUTNmF9Fn0uGCYtcJgpBUZniJ6qVHFIV8EpIwu8FrSFyAVwsD3rit4HpZqtqQ/c7oWNU5QehMRFFQpcz/oilG6Wd/TE1G3o; Expires=Thu, 28 Feb 2019 09:47:29 GMT; Path=/". Invalid 'expires' attribute: Thu, 28 Feb 2019 09:47:29 GMT
Feb 21, 2019 3:17:29 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Invalid cookie header: "Set-Cookie: col:jwt=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; httponly". Invalid 'expires' attribute: Thu, 01 Jan 1970 00:00:00 GMT
2019-02-21 15:17:29,672 [main] ERROR org.jsmart.zerocode.core.runner.StepNotificationHandler - Failed assertion during Scenario:Login as user and get ME details, --> Step:get_emp_details, Details: Assertion path '$.status' with actual value '401' did not match the expected value '200'

2019-02-21 15:17:29,675 [main] ERROR org.jsmart.zerocode.core.runner.StepNotificationHandler - Assertion failed for :- 

[Login as user and get ME details] 
    |
    |
    +---Step --> [get_emp_details] 

Failures:
--------- 
Assertion path '$.status' with actual value '401' did not match the expected value '200'
(See below 'Actual Vs Expected' to learn why this step failed) 

2019-02-21 15:17:29,680 [main] INFO  org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl - 
--------- TEST-STEP-CORRELATION-ID: 0f6fe487-3c66-462a-b133-2d648bb1ec80 ---------
*requestTimeStamp:2019-02-21T15:17:28.499
step:get_emp_details
url:/api/me
method:GET
request:
{
  "header" : {
    "x-col-jwt" : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.pZCI6ImNqcng0MXEzbjAwM3IwbG54aTdrcDh5NzQiLCJhY2NvdW50X3R5cGUiOjAsImFjY291bnRfaWQiOiJwYXJpMUB5b3BtYWlsLmNvbSIsImFjY291bnRfdG9rZW4iOiIkMmEkMTAkUE5wSG0wL2g0T2NHZnVxTWVZS0VvZVREaW9uN0dQSTJndUNDMnRZSEpnYUR3aGx5alJDSHUiLCJlbWFpbCI6InBhcmkxQHlvcG1haWwuY29tIiwiZmlyc3RfbmFtZSI6IlBhcmkiLCJsYXN0X25hbWUiOiJLIiwicHJvZmlsZV9pbWFnZSI6bnVsbCwibGFzdF9sb2dpbiI6IjIwMTktMDItMjFUMDk6NDc6MjguMDAwWiIsImxhc3RfbG9nb3V0IjoiMjAxOS0wMi0xOVQwNTozMzowMC4wMDBaIiwiaWF0IjoxNTUwNzQyNDQ4LCJleHAiOjE1NTEzNDcyNDh9.sOHjUJ5HBqdl_-IOAy605RAET9Mkuf4Zo8pELqnIhcs",
    "Content-Type" : "application/json"
  }
} 
--------- TEST-STEP-CORRELATION-ID: 0f6fe487-3c66-462a-b133-2d648bb1ec80 ---------
Response:
{
  "status" : 401,
  "headers" : {
    "Date" : [ "Thu, 21 Feb 2019 09:47:29 GMT" ],
    "Content-Length" : [ "12" ],
    "Set-Cookie" : [ "AWSALB=RJ3wXUpz9vavPnXJePnilN4DsP6kRMUTNmF9Fn0uGCYtcJgpBUZniJ6qVHFIV8EpIwu8FrSFyAVwsD3rit4HpZqtqQ/c7oWNU5QehMRFFQpcz/oilG6Wd/TE1G3o; Expires=Thu, 28 Feb 2019 09:47:29 GMT; Path=/", "col:jwt=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; httponly" ],
    "Server" : [ "openresty" ],
    "Vary" : [ "Origin" ],
    "Content-Type" : [ "text/plain; charset=utf-8" ],
    "Connection" : [ "keep-alive" ],
    "X-Cache" : [ "MISS" ],
    "Age" : [ "0" ],
    "Via" : [ "http/1.1 api-umbrella (ApacheTrafficServer [cMsSf ])" ]
  },
  "rawBody" : "Unauthorized"
}
*responseTimeStamp:2019-02-21T15:17:29.664 
*Response delay:1165.0 milli-secs 
---------> Assertion: <----------
{
  "status" : 200
} 
-done-

java.lang.RuntimeException: Assertion failed for :- 

[Login as user and get ME details] 
    |
    |
    +---Step --> [get_emp_details] 

Failures:
--------- 
Assertion path '$.status' with actual value '401' did not match the expected value '200'

    at org.jsmart.zerocode.core.runner.StepNotificationHandler.handleAssertionFailed(StepNotificationHandler.java:37)
    at org.jsmart.zerocode.core.runner.StepNotificationHandler.handleAssertion(StepNotificationHandler.java:71)
    at org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl.runScenario(ZeroCodeMultiStepsScenarioRunnerImpl.java:267)
    at org.jsmart.zerocode.core.runner.ZeroCodeUnitRunner.runLeafJsonTest(ZeroCodeUnitRunner.java:198)
    at org.jsmart.zerocode.core.runner.ZeroCodeUnitRunner.runChild(ZeroCodeUnitRunner.java:118)
    at org.jsmart.zerocode.core.runner.ZeroCodeUnitRunner.runChild(ZeroCodeUnitRunner.java:45)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.jsmart.zerocode.core.runner.ZeroCodeUnitRunner.run(ZeroCodeUnitRunner.java:101)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
BeTheCodeWithYou commented 5 years ago

@gsolaich Hi Mate - looking at your logs, it all looks ok but just an observation that you are getting token with key as col:jwt in your set-cookie with your POST response but in your GET call, you are sending your token value in x-col-jwt header. Could this be an issue?

1)

Also, it would be helpful for us if you could send screen shot of your Postman request headers and response headers, for your GET call, so we can double check and compare. thanks a lot.

"Set-Cookie" : [ "AWSALB=HcL60G4FGvuKVXO9M8v15RU1vKXo26FqHdLfOtBsnXpl/oNksnl1DEkaeqgEAjHWOTwc2heBUZ4lZtVTbU9O/Hv5LeS6oYhvjL2ogMlz9RE81NCWQRx5Ewq+/Jc0; Expires=Thu, 28 Feb 2019 09:47:27 GMT; Path=/", " col:jwt =eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImNqcng0MXEzbjAwM3IwbG54aTdrcDh5NzQiLCJhY2NvdW50X3R5cGUiOjAsImFjY291bnRfaWQiOiJwYXJpMUB5b3BtYWlsLmNvbSIsImFjY291bnRfdG9rZW4iOiIkMmEkMTAkUE5wSG0wL2g0T2NHZnVxTWVZS0VvZVREaW9uN0dQSTJndUNDMnRZSEpnYUR3aGx5alJDSHUiLCJlbWFpbCI6InBhcmkxQHlvcG1haWwuY29tIiwiZmlyc3RfbmFtZSI6IlBhcmkiLCJsYXN0X25hbWUiOiJLIiwicHJvZmlsZV9pbWFnZSI6bnVsbCwibGFzdF9sb2dpbiI6IjIwMTktMDItMjFUMDk6NDc6MjguMDAwWiIsImxhc3RfbG9nb3V0IjoiMjAxOS0wMi0xOVQwNTozMzowMC4wMDBaIiwiaWF0IjoxNTUwNzQyNDQ4LCJleHAiOjE1NTEzNDcyNDh9.sOHjUJ5HBqdl_-IOAy605RAET9Mkuf4Zo8pELqnIhcs; path=/; expires=Thu, 28 Feb 2019 09:47:28 GMT; httponly" ],

authorjapps commented 5 years ago

Another Observation

2)

It seems like there are two parts in the above JWT token separated by a dot, Part1.Part2 Part1: ey*** Part2: ey***** Full token: col:jwt=ey***.ey*****

The token which is in the "Set-Cookie" against the key col:jwt differs from "rawBody" in the Part2. "Set-Cookie" : ey*.ey*** "rawBody" : ey*.*** <------- There is no ey here.

Could this be a problem ? (Not sure, you can have a look at the server implementation code)

The server clearly says unauthorized for this token(wo the 2nd ey) ey***.*****.

The Postman screenshot might help here to figure out whether it works with the earlier body token or the col:jwt token

3 (Suggestion)

Add this to the headers in the GET call and re-run.

"Cookie":"${$.validate_login.response.headers.Set-Cookie[0]}",

Your server might be expecting the above.

{
                "name": "get_emp_details",
                "url": "/me",
                "operation": "GET",
                "request": {
                    "header": {
                        "Cookie":"${$.validate_login.response.headers.Set-Cookie[0]}",
                        "x-col-jwt":"${$.validate_login.response.rawBody}",
                        "Content-Type":"application/json"
                    }
                },
                "assertions": {
                    "status": 200

                }
            }