bjowes / cypress-ntlm-auth

Windows authentication plugin for Cypress
MIT License
54 stars 10 forks source link

Cannot make it work with environment variable NODE_TLS_REJECT_UNAUTHORIZED='0' #106

Closed nknysh closed 4 years ago

nknysh commented 4 years ago

I try to use the way close to what is recommended in readme: "scripts": { "ntlm-proxy": "start /min \"ntlm-proxy\" cmd /c node_modules\\.bin\\ntlm-proxy", "cypress-ntlm": "npm run-script env NODE_TLS_REJECT_UNAUTHORIZED='0' ntlm-proxy && (cypress-ntlm open & ntlm-proxy-exit)" }

Then, when I run with npm run cypress-ntlm, I get an error saying

ntlm-proxy must be started before this command

Readme says

use the standard Node workaround by setting the environment variable NODE_TLS_REJECT_UNAUTHORIZED=0 before starting ntlm-proxy

Can you please example how I should pass the env variable to ntlm-proxy? And sorry if the question is stupid - I'm new to node/npm.

Thanks!

bjowes commented 4 years ago

Hi @nknysh - it seems the env command in npm doesn't work that well on windows. You should be able to get it working using:

"scripts": { 
  "ntlm-proxy": "start /min \"ntlm-proxy\" cmd /c node_modules\\.bin\\ntlm-proxy", 
  "cypress-ntlm": "set NODE_TLS_REJECT_UNAUTHORIZED=0 && npm run ntlm-proxy && (cypress-ntlm open & ntlm-proxy-exit)" 
}
nknysh commented 4 years ago

Thanks @bjowes! This is one of the things I tried. But then, in Cypress, process.env does NOT contain this variable.

Can it be not set in Cypress, but already set for ntlm-proxy, which is ran right after the setcommand?

bjowes commented 4 years ago

It's actually not needed by cypress itself, only by ntlm-proxy since cypress already ignores cert errors. There is no easy way to see if it is set for ntlm-proxy though, but I guess you can just try it?

You can compare the results if you manually do set NODE_TLS_REJECT_UNAUTHORIZED=0 in the command prompt before you do npm run cypress-ntlm, this will set it for sure for both cypress and ntlm-proxy.

nknysh commented 4 years ago

@bjowes you're right - I use Windows (and powershell). It didn't work for me from within package.json, but I succeeded with this powershell script:

$scriptBlock = {
    $Env:NODE_TLS_REJECT_UNAUTHORIZED='0'
    npm run cypress-ntlm
}
Invoke-Command -ScriptBlock $scriptBlock

Thanks for your help, and for the awesome plugin you give to the world! :)

bjowes commented 4 years ago

I'm glad it worked out and that you enjoy the plugin :) I have noticed that I frequently receive questions similar to this one. Setting environment variables in Windows from package.json seems a bit tricky, what works for some doesn't work for others. I'll consider adding something about this to the docs.

MSIH commented 4 years ago

On windows, got environment variable to work with no space between &&

"ntlm-proxy": "set NODE_TLS_REJECT_UNAUTHORIZED=0&&start /min \"ntlm-proxy\" cmd /c node_modules\\.bin\\ntlm-proxy",