From 3e4bc13f293c02ece65ee590dc193c4e5ba10892 Mon Sep 17 00:00:00 2001
From: Erwin Kooi <erwin.kooi@empire.ca>
Date: Sat, 10 Nov 2018 19:49:18 -0500
Subject: [PATCH] Cypress

---
 django-poll-project/kube101/cypress.json      |  1 +
 .../kube101/cypress/integration/flow.js       | 16 +++++++++
 .../kube101/cypress/plugins/index.js          | 17 ++++++++++
 django-poll-project/kube101/cypress/run.sh    | 20 +++++++++++
 .../kube101/cypress/support/commands.js       | 25 ++++++++++++++
 .../kube101/cypress/support/index.js          | 20 +++++++++++
 .../kube101/polls/tests/__init__.py           |  0
 .../kube101/polls/tests/poll.json             | 33 ++++++++++++++++++
 .../kube101/polls/tests/test_e2e.py           | 34 +++++++++++++++++++
 9 files changed, 166 insertions(+)
 create mode 100644 django-poll-project/kube101/cypress.json
 create mode 100644 django-poll-project/kube101/cypress/integration/flow.js
 create mode 100644 django-poll-project/kube101/cypress/plugins/index.js
 create mode 100755 django-poll-project/kube101/cypress/run.sh
 create mode 100644 django-poll-project/kube101/cypress/support/commands.js
 create mode 100644 django-poll-project/kube101/cypress/support/index.js
 create mode 100644 django-poll-project/kube101/polls/tests/__init__.py
 create mode 100644 django-poll-project/kube101/polls/tests/poll.json
 create mode 100644 django-poll-project/kube101/polls/tests/test_e2e.py

diff --git a/django-poll-project/kube101/cypress.json b/django-poll-project/kube101/cypress.json
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/django-poll-project/kube101/cypress.json
@@ -0,0 +1 @@
+{}
diff --git a/django-poll-project/kube101/cypress/integration/flow.js b/django-poll-project/kube101/cypress/integration/flow.js
new file mode 100644
index 0000000..0f4a7f6
--- /dev/null
+++ b/django-poll-project/kube101/cypress/integration/flow.js
@@ -0,0 +1,16 @@
+/* global describe it cy expect */
+
+describe('Poll Test', function() {
+    it('Tests the poll system flow', function() {
+        cy.visit('/polls/');
+
+        cy.get('ul>li>a:first').click();
+        cy.location().should(function(loc) { expect(loc.pathname).to.eq('/polls/1/'); });
+
+        cy.get('form>label[for=choice3]').click();
+        cy.get('form>input[type=submit]').click();
+        cy.location().should(function(loc) { expect(loc.pathname).to.eq('/polls/1/results/'); });
+
+        cy.get('ul > :nth-child(3)').contains('Yes, I love beer! -- 1 vote')
+    });
+});
\ No newline at end of file
diff --git a/django-poll-project/kube101/cypress/plugins/index.js b/django-poll-project/kube101/cypress/plugins/index.js
new file mode 100644
index 0000000..fd170fb
--- /dev/null
+++ b/django-poll-project/kube101/cypress/plugins/index.js
@@ -0,0 +1,17 @@
+// ***********************************************************
+// This example plugins/index.js can be used to load plugins
+//
+// You can change the location of this file or turn off loading
+// the plugins file with the 'pluginsFile' configuration option.
+//
+// You can read more here:
+// https://on.cypress.io/plugins-guide
+// ***********************************************************
+
+// This function is called when a project is opened or re-opened (e.g. due to
+// the project's config changing)
+
+module.exports = (on, config) => {
+  // `on` is used to hook into various events Cypress emits
+  // `config` is the resolved Cypress config
+}
diff --git a/django-poll-project/kube101/cypress/run.sh b/django-poll-project/kube101/cypress/run.sh
new file mode 100755
index 0000000..5e567c5
--- /dev/null
+++ b/django-poll-project/kube101/cypress/run.sh
@@ -0,0 +1,20 @@
+#!/bin/bash
+
+if [[ "$#" -ne 2 ]]; then
+    echo "Usage: run.sh base_url spec"
+    exit -1
+fi
+
+echo "base_url=$1"
+echo "spec=$2"
+
+export CYPRESS_BASE_URL="$1"
+
+# see https://docs.cypress.io/guides/guides/command-line.html#cypress-run-record-key-lt-record-key-gt
+# If you set the Record Key as the environment variable CYPRESS_RECORD_KEY, you can omit the --key flag.
+# You’d typically set this environment variable when running in Continuous Integration.
+if [[ -z "${CYPRESS_RECORD_KEY}" ]]; then
+  npx cypress run --spec ${2}
+else
+  npx cypress run --spec ${2} --record
+fi
diff --git a/django-poll-project/kube101/cypress/support/commands.js b/django-poll-project/kube101/cypress/support/commands.js
new file mode 100644
index 0000000..c1f5a77
--- /dev/null
+++ b/django-poll-project/kube101/cypress/support/commands.js
@@ -0,0 +1,25 @@
+// ***********************************************
+// This example commands.js shows you how to
+// create various custom commands and overwrite
+// existing commands.
+//
+// For more comprehensive examples of custom
+// commands please read more here:
+// https://on.cypress.io/custom-commands
+// ***********************************************
+//
+//
+// -- This is a parent command --
+// Cypress.Commands.add("login", (email, password) => { ... })
+//
+//
+// -- This is a child command --
+// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
+//
+//
+// -- This is a dual command --
+// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
+//
+//
+// -- This is will overwrite an existing command --
+// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
diff --git a/django-poll-project/kube101/cypress/support/index.js b/django-poll-project/kube101/cypress/support/index.js
new file mode 100644
index 0000000..d68db96
--- /dev/null
+++ b/django-poll-project/kube101/cypress/support/index.js
@@ -0,0 +1,20 @@
+// ***********************************************************
+// This example support/index.js is processed and
+// loaded automatically before your test files.
+//
+// This is a great place to put global configuration and
+// behavior that modifies Cypress.
+//
+// You can change the location of this file or turn off
+// automatically serving support files with the
+// 'supportFile' configuration option.
+//
+// You can read more here:
+// https://on.cypress.io/configuration
+// ***********************************************************
+
+// Import commands.js using ES2015 syntax:
+import './commands'
+
+// Alternatively you can use CommonJS syntax:
+// require('./commands')
diff --git a/django-poll-project/kube101/polls/tests/__init__.py b/django-poll-project/kube101/polls/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/django-poll-project/kube101/polls/tests/poll.json b/django-poll-project/kube101/polls/tests/poll.json
new file mode 100644
index 0000000..f4fd2cd
--- /dev/null
+++ b/django-poll-project/kube101/polls/tests/poll.json
@@ -0,0 +1,33 @@
+[
+  {
+    "model": "polls.Poll",
+    "pk": 1,
+    "fields": {
+      "question": "Do you like beer?"
+    }
+  },
+  {
+    "model": "polls.Choice",
+    "pk": 1,
+    "fields": {
+      "poll_id": 1,
+      "choice_text": "Yes, I love beer!"
+    }
+  },
+  {
+    "model": "polls.Choice",
+    "pk": 2,
+    "fields": {
+      "poll_id": 1,
+      "choice_text": "Yes!"
+    }
+  },
+  {
+    "model": "polls.Choice",
+    "pk": 3,
+    "fields": {
+      "poll_id": 1,
+      "choice_text": "Also yes"
+    }
+  }
+]
diff --git a/django-poll-project/kube101/polls/tests/test_e2e.py b/django-poll-project/kube101/polls/tests/test_e2e.py
new file mode 100644
index 0000000..0566632
--- /dev/null
+++ b/django-poll-project/kube101/polls/tests/test_e2e.py
@@ -0,0 +1,34 @@
+from django.contrib.staticfiles.testing import StaticLiveServerTestCase
+from django.core.servers.basehttp import WSGIServer, WSGIRequestHandler
+from django.test.testcases import LiveServerThread
+
+
+# @see https://stackoverflow.com/questions/45981634/how-to-make-django-liveservertestcase-log-requests
+from ..models import Choice
+
+
+class VerboseLiveServerThread(LiveServerThread):
+    def _create_server(self):
+        return WSGIServer((self.host, self.port), WSGIRequestHandler, allow_reuse_address=False)
+
+
+class PollTest(StaticLiveServerTestCase):
+    server_thread_class = VerboseLiveServerThread
+
+    fixtures = [
+        'polls/tests/poll.json',
+    ]
+
+    def test_flow(self):
+        self.assertEqual(0, Choice.objects.get(pk=1).votes)
+
+        from subprocess import call
+        exit_code = call([
+            './cypress/run.sh',
+            f'http://{self.server_thread.host}:{self.server_thread.port}',
+            'cypress/integration/flow.js'
+        ])
+
+        self.assertEqual(0, exit_code)
+
+        self.assertEqual(1, Choice.objects.get(pk=1).votes)