bjowes / cypress-ntlm-auth

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

Docker image support (cypress-io/cypress-docker-images) #139

Closed hetsch closed 3 years ago

hetsch commented 3 years ago

Hi,

thank you for your nice package! I wonder if you have ever tried to use your module with a Docker image like cypress-io/cypress-docker-images (https://github.com/cypress-io/cypress-docker-images/blob/master/included/5.3.0/Dockerfile) or similar. I cannot get it working, because cypress is installed in the container as a global package and installing cypress-ntlm-auth as a global dependency errors out with something like the below:

$ which cypress
/usr/local/bin/cypress
$ cypress --version
Cypress package version: 5.3.0
Cypress binary version: **5.3.0**

$ which cypress-ntlm
/usr/local/bin/cypress-ntlm
# cypress-ntlm version: 3.0.0
$ cypress-ntlm open --project .
Cannot parse command line arguments
Error: Cannot parse command line arguments
    at Startup.getArgsAfterCypressNtlm (/usr/local/lib/node_modules/cypress-ntlm-auth/dist/startup/startup.js:48:19)
    at Startup.argumentsToCypressMode (/usr/local/lib/node_modules/cypress-ntlm-auth/dist/startup/startup.js:53:35)
    at Object.argumentsToCypressMode (/usr/local/lib/node_modules/cypress-ntlm-auth/dist/index.js:47:20)
    at /usr/local/lib/node_modules/cypress-ntlm-auth/dist/launchers/cypress.ntlm.js:17:34
    at Generator.next (<anonymous>)
    at /usr/local/lib/node_modules/cypress-ntlm-auth/dist/launchers/cypress.ntlm.js:9:71
    at new Promise (<anonymous>)
    at __awaiter (/usr/local/lib/node_modules/cypress-ntlm-auth/dist/launchers/cypress.ntlm.js:5:12)
    at execute (/usr/local/lib/node_modules/cypress-ntlm-auth/dist/launchers/cypress.ntlm.js:15:12)
    at Object.<anonymous> (/usr/local/lib/node_modules/cypress-ntlm-auth/dist/launchers/cypress.ntlm.js:40:1)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
    at internal/main/run_main_module.js:17:11

Looking at these lines https://github.com/bjowes/cypress-ntlm-auth/blob/a5aff3ace7fee670dfe62726af622925702dce8a/src/startup/startup.ts#L47-L52 gives me the impression that cypress-ntlm-auth should or could only be used as a local dependency.

Changing these lines to accept the /usr/local/bin prefix, starts cypress without any error messages:

const cypressNtlmIndex = args.findIndex((t) => t === "cypress-ntlm" ||
    t.endsWith("node_modules/.bin/cypress-ntlm") ||
    t.endsWith("cypress-ntlm-auth\\dist\\launchers\\cypress.ntlm.js") ||
    t === "/usr/local/bin/cypress-ntlm" 
);

Inside the project browser, clicking on a spec and opening the test in Electron provides me the following error message:

Error: Webpack Compilation Error
./cypress/support/index.js
Module not found: Error: Can't resolve 'cypress-ntlm-auth/dist/commands' in '/workspaces/Omnitracker/cypress/support'
resolve 'cypress-ntlm-auth/dist/commands' in '/workspaces/Omnitracker/cypress/support'
  Parsed request is a module
  using description file: /workspaces/Omnitracker/package.json (relative path: ./cypress/support)
    Field 'browser' doesn't contain a valid alias configuration
    Looked for and couldn't find the file at the following paths: or is not a directory
[/workspaces/Omnitracker/cypress/support/node_modules]
[/workspaces/Omnitracker/cypress/node_modules]
[/workspaces/Omnitracker/node_modules]
[/workspaces/node_modules]
[/node_modules]
 @ ./cypress/support/index.js 5:0-42

This seems to be related to Webpack not knowing anything about the cypress-ntlm-auth module. Any thoughts and ideas are more than welcome!

bjowes commented 3 years ago

Hi @hetsch - thanks for pointing out that the plugin won't run when installed globally. I'll consider if the launcher needs to be improved. However, I wonder if there is a need to install it globally? As you have shown, there are issues in locating the module when it is installed globally. If you would install it locally instead, both the launcher would work and the command file would be found.

I haven't tried out the plugin in docker, so maybe there is some reason to install it globally in that environment? I looked at some issues related to plugins and docker, and it seems the recommendation is to add them to the local package.json and do npm install, which means they will be installed locally.

hetsch commented 3 years ago

Hi @bjowes - the thing is, that cypress is installed globally in the Docker image, and installing cypress-ntlm-auth locally ends up in cypress-ntlm-auth not finding the globally installed cypress module. Another reason for globally installing your plugin is that one can create a custom Docker image where all the requirements are already built in (globally installed).

bjowes commented 3 years ago

Understood. I see that I need to make the imports (require) agnostic to local or global installs, while preferring local. I can look into it, but you are also welcome to make a PR if you have a solution in mind already.

hetsch commented 3 years ago

Thank you for working on this issue. Sure, i'll continue to play around until I find something useful. But I have little hope that I'm much of a help considering my experience with Nodejs.

bjowes commented 3 years ago

As I understand it, you have two options: 1) Install cypress-ntlm-auth both globally and locally. That way, you launch it with the global version, which then uses the global cypress install correctly. The custom commands (from cypress/support/index.js) are imported from the local install. 2) Install cypress-ntlm-auth globally and use a custom import path in cypress/support/index.js. This may sound simpler, and it is as long as you don't need to have a portable environment. The custom import path will be different with different OS variants, and also if you have multiple Node installations. But if all that stays the same (which I figure it should in docker) this is quite useful. So, what custom import path? You can find that out by executing npm root -g in your container. Prefix the import statement of cypress-ntlm-auth with this path. The typical path for a linux installation should be /usr/local/lib/node_modules, meaning that the import statement in cypress/support/index.js should be import "/usr/local/lib/node_modules/cypress-ntlm-auth/dist/commands";

Please try it out. I will prepare a patch for the launcher.

hetsch commented 3 years ago

Hi @bjowes, I tried point 2 and it works. What I needed to add was t === "/usr/local/bin/cypress-ntlm" as mentioned above. Thank you for the hints!