Repository: marcus-crane/netbox-plugin-azuread (language: python)

Issue:
Set up e2e testing
Now that I have a proper Microsoft 365 Developer account set up, I can actually test this plugin locally.

Prior to now, I didn't actually have access to try out different settings.

As a result, it should be possible to set up a pipeline dedicated to running e2e login using the plugin.

With this, we can identify regressions and that errors are surfaced where expected.


Discussion / hints:

Fix patch:
diff --git a/Makefile b/Makefile
index 0be2438..149dd6a 100644
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,8 @@ NETBOX_VER=3.0.0
 
 COMPOSE_FILE=./develop/docker-compose.yml
 BUILD_NAME=netbox_plugin_azuread
+TESTCAFE_FOLDER=e2e
+TESTCAFE_ARGS=firefox "/tests/*.js"
 
 up:
 	@echo "Starting Netbox"
@@ -13,3 +15,8 @@ down:
 
 build:
 	docker-compose -f ${COMPOSE_FILE} -p ${BUILD_NAME} build --build-arg NETBOX_VER=${NETBOX_VER}
+
+test:
+	docker pull testcafe/testcafe
+	# We don't use -it here as this also runs within the CI pipeline (which is not a TTY)
+	docker run -i --net=host --mount type=bind,source=$(shell pwd)/${TESTCAFE_FOLDER},target=/tests testcafe/testcafe ${TESTCAFE_ARGS}
\ No newline at end of file
diff --git a/develop/docker-compose.yml b/develop/docker-compose.yml
index 4a58293..fa78d38 100644
--- a/develop/docker-compose.yml
+++ b/develop/docker-compose.yml
@@ -1,5 +1,14 @@
 version: '3.6'
 services:
+  nginx:
+    image: nginx
+    depends_on:
+      - netbox
+    ports:
+      - '8000:8000'
+    volumes:
+      - ./nginx.conf:/etc/nginx/nginx.conf:ro
+    command: [nginx-debug, '-g', 'daemon off;']
   netbox:
     build:
       context: ../
diff --git a/develop/nginx.conf b/develop/nginx.conf
new file mode 100644
index 0000000..89844e4
--- /dev/null
+++ b/develop/nginx.conf
@@ -0,0 +1,39 @@
+worker_processes 1;
+pid /tmp/nginx.pid;
+
+error_log /dev/stderr info;
+
+events {
+    worker_connections 1024;
+}
+
+http {
+    include              /etc/nginx/mime.types;
+    default_type         application/octet-stream;
+    server_tokens        off;
+    client_max_body_size 25m;
+    access_log           /dev/stdout;
+
+    server {
+        listen      8000;
+        server_name _;
+        access_log  off;
+
+        location / {
+            proxy_pass http://netbox:8080/;
+        }
+
+        location /login/ {
+            proxy_pass http://netbox:8080/plugins/azuread/login/;
+        }
+
+        location /complete/ {
+            proxy_pass http://netbox:8080/plugins/azuread/complete/;
+        }
+
+        location /ping {
+            default_type text/plain;
+            return 200 "alive\n";
+        }
+    }
+}
\ No newline at end of file


Test patch:
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
new file mode 100644
index 0000000..449c6c3
--- /dev/null
+++ b/.github/workflows/test.yml
@@ -0,0 +1,35 @@
+name: End to end testing
+
+on:
+  push:
+    branches:
+      - main
+  pull_request:
+    branches:
+      - main
+
+jobs:
+  docker:
+    timeout-minutes: 10
+    runs-on: ubuntu-latest
+
+    steps:
+    - name: Checkout
+      uses: actions/checkout@v1
+
+    - name: Build plugin container
+      run: make build
+
+    - name: Start containers
+      run: make up
+
+    - name: Wait for a minute for Netbox to boot
+      run: sleep 120s
+      shell: bash
+
+    - name: Run Testcafe tests
+      run: make test
+
+    - name: Stop containers
+      if: always()
+      run: make down
diff --git a/e2e/01_home.js b/e2e/01_home.js
new file mode 100644
index 0000000..2490d9a
--- /dev/null
+++ b/e2e/01_home.js
@@ -0,0 +1,9 @@
+import { Selector } from 'testcafe'
+
+fixture`Homepage exists`
+  .page`http://localhost:8000`
+
+test('Access the homepage', async t => {
+  await t
+    .expect(Selector('a').withAttribute('href', '/login/').innerText).contains('Log In')
+})