frisby icon indicating copy to clipboard operation
frisby copied to clipboard

Could not generate token using frisby v2

Open G4gan opened this issue 4 years ago • 10 comments

i am using frisby for apis automation and Jasmine for test. I am not able to generate simple token generation using basic Auth

this is how my curl call look like for token generation and same I am trying with Frisby.js. $ curl -k https://URL -u username:password -H "header:value"

The output brings token like some value TDIKhjglj7698ssjgljuypubvbi

Same I am doing with frisby to generate token so in this case I am not getting error and my spec is getting passed everytime. Even for negative expects conditions. here is my code

var frisby =require('frisby'); frisby.globalSetup({ request: { headers: {

'Authorization': 'Basic ' + Buffer.from("username:password").toString('base64'), 'Content-Type': 'application/json+scim', 'header':'value' }} });

//do setup for Authorization of the token for REST call it ('uses globalSetup for every test after it is called', function () {

`return frisby
 .get('URL')`
  ` .inspectResponse()`
  .expect('status', 400),`
  { strictSSL: false }`
  });`

can anyone just point out what exactly am I missing in code. I have tested with response 400 still test is getting passed and token not getting generated. I am using link Frisby Documentation

Also running test using -

#jasmine test_spec.js Randomized with seed 54594 Started

1 spec, 0 failures Finished in 0.049 seconds Randomized with seed 54594 (jasmine --random=true --seed=54594)

G4gan avatar May 21 '20 07:05 G4gan

Try using inspectRequest() to ensure that the header values are getting passed correctly, and are the values that you expect.

vlucas avatar May 21 '20 14:05 vlucas

Thanks @vlucas. If you have read my comment. My test is getting passed even when expect checks for status 200 and provided is 400. And my token is not getting generated in either case. I tried inspectBody(),inspectJson() also inspectrequest(). None worked. If you can tell is there anything else I need to change in my code. I am following this link: https://docs.frisbyjs.com/api-and-usage/setup

G4gan avatar May 21 '20 14:05 G4gan

Your curl command disables HTTPS cert checking with the -k flag. So maybe you need to do the same in the frisby test. Just guessing but maybe you should add the node-fetch options to disable HTTPS cert checking. Try this.

var frisby = require('frisby');
frisby.globalSetup({
    request: {
        headers: {
            'Authorization': 'Basic ' + Buffer.from("username:password").toString('base64'),
            'header':'value'
        },
        // It's one of these
        strictSSL : false,
        rejectUnauthorized: false
    } 
});

//do setup for Authorization of the token for REST call
it ('uses globalSetup for every test after it is called', function () {
    return frisby.get('URL')
        .inspectResponse()
        .expect('status', 400);
});

LarryBattleWork avatar May 21 '20 23:05 LarryBattleWork

@LarryBattleWork Getting error when passing either of strictSSL : false, rejectUnauthorized: false in globalSetup() Failures: FetchError: request to http://URL/login failed, reason: socket hang up Stack: error properties: null({ type: 'system', errno: 'ECONNRESET', code: 'ECONNRESET', constructor: Function }) at <Jasmine> at ClientRequest. (dir\node_modules\node-fetch\lib\index.js:1455:11) at ClientRequest.emit (events.js:310:20)

G4gan avatar May 22 '20 08:05 G4gan

Thanks. It worked adding above globaSetup(). process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

G4gan avatar May 22 '20 11:05 G4gan

So your solution looks like this?

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

frisby.globalSetup({
    request: {
        headers: {
            'Authorization': 'Basic ' + Buffer.from("username:password").toString('base64'),
            'header':'value'
        }
    } 
});

NOTE: Don't use NODE_TLS_REJECT_UNAUTHORIZED in production code. Doc link: https://nodejs.org/api/cli.html#cli_node_tls_reject_unauthorized_value

LarryBattleWork avatar May 22 '20 16:05 LarryBattleWork

@LarryBattleWork thanks for response. Can you suggest other property other than this which will work. I tried strictSSL : false, rejectUnauthorized: false Both seems to not work.

Also how can I get the response in variable. I tried .then(function (res) { let postId = res.json; Console.log(postId) } But this seems to give error. Any suggestions.

G4gan avatar May 24 '20 07:05 G4gan

If curl -k option is removed, does it give an error like frisby ?

H1Gdev avatar May 24 '20 13:05 H1Gdev

@H1Gdev -k in curl is for verifying server legitimacy. so in that case it error me with ssl error.

G4gan avatar May 25 '20 11:05 G4gan

@vlucas Can you please suggest which reporting goes with Jasmine-Frisby project. Or if any links yo can refer me too.

Thanks in advance.

G4gan avatar Jun 07 '20 13:06 G4gan