From d542e355e576684ac3a0ba00b9bc0481e8053b2c Mon Sep 17 00:00:00 2001 From: Arne Brasseur Date: Tue, 15 Aug 2017 17:05:41 +0200 Subject: [PATCH 1/2] Create the test system configuration --- repl/sparkledriver.clj | 12 +++++++++++- src/clj/booklog/application.clj | 4 ++-- test/clj/booklog/test_helper.clj | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/repl/sparkledriver.clj b/repl/sparkledriver.clj index 84500da..a6de7dc 100644 --- a/repl/sparkledriver.clj +++ b/repl/sparkledriver.clj @@ -1,2 +1,12 @@ (ns repl.sparkledriver - (:require [sparkledriver.core :as sd])) + (:require [sparkledriver.core :as sd] + [clojure.java.browse :refer [browse-url]])) + +(def browser (sd/make-browser)) +(sd/fetch! browser "http://localhost:1234") + +(sd/click! (sd/find-by-css browser "a.log-in")) +(sd/current-url browser) ;;=> "http://localhost:1234/login" + +(sd/click! (sd/find-by-xpath browser "//a[text()='Log in']")) +(sd/current-url browser) ;;=> "http://localhost:1234/login" diff --git a/src/clj/booklog/application.clj b/src/clj/booklog/application.clj index 58ff034..6f91532 100644 --- a/src/clj/booklog/application.clj +++ b/src/clj/booklog/application.clj @@ -1,9 +1,9 @@ (ns booklog.application (:gen-class) - (:require [booklog.components.spicerack :refer [new-spicerack]] + (:require [com.stuartsierra.component :as component] + [booklog.components.spicerack :refer [new-spicerack]] [booklog.middleware.render :refer [wrap-render-views]] [booklog.routes :refer [app-routes]] - [com.stuartsierra.component :as component] [environ.core :refer [env]] [prone.middleware :as prone] [ring.middleware.defaults :refer [site-defaults wrap-defaults]] diff --git a/test/clj/booklog/test_helper.clj b/test/clj/booklog/test_helper.clj index 081e84b..ea6fd48 100644 --- a/test/clj/booklog/test_helper.clj +++ b/test/clj/booklog/test_helper.clj @@ -6,3 +6,18 @@ [clojure.java.io :as io] [lambdaisland.uri :as uri] [booklog.components.spicerack :as sc])) + +(def test-http-port 59800) + +(defn test-config [] + {:http-port test-http-port + :db-path (util/temp-file-name "test" "db")}) + +(defn wrap-test-system + "A fixture function which sets up the system before tests and tears it down afterwards." + [tests] + (let [config (test-config) + system (-> config app/app-system component/start)] + (tests) + (component/stop system) + (io/delete-file (:db-path config)))) From 882da07cfd66aca1d2082dec7d6df3565ea7fdf6 Mon Sep 17 00:00:00 2001 From: Arne Brasseur Date: Wed, 16 Aug 2017 15:43:41 +0200 Subject: [PATCH 2/2] Finish two integration tests --- test/clj/booklog/acceptance_test.clj | 33 ++++++++++++++- test/clj/booklog/test_helper.clj | 62 +++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/test/clj/booklog/acceptance_test.clj b/test/clj/booklog/acceptance_test.clj index 987f84e..894f043 100644 --- a/test/clj/booklog/acceptance_test.clj +++ b/test/clj/booklog/acceptance_test.clj @@ -1,4 +1,35 @@ (ns booklog.acceptance-test (:require [clojure.test :refer :all] [booklog.test-helper :refer :all] - [sparkledriver.core :as sd :refer [click! send-text!]])) + [sparkledriver.core :as sd :refer + [click! send-text!]])) + +(use-fixtures :once wrap-test-system wrap-browser) +(use-fixtures :each wrap-clear-db wrap-clear-cookies) + +(deftest sign-up-test + (fetch! (app-url "/")) + (click! (find-by-css "a.log-in")) + (is (= (current-path) "/login")) + + (click! (find-by-xpath "//a[text()='Register instead']")) + (is (= (current-path) "/register")) + + (send-text! (find-by-css "#username") "Arne") + (send-text! (find-by-css "#password") "sekrit") + (click! (find-by-css "input[type=submit]")) + + (is (re-find #"Thanks for registering! Please log in" (page-text)))) + +(deftest login-test + (load-db! {"users" {"Arne" "bcrypt+sha512$e0cf0035cd4f616d180119d2c3cd4c12$12$a841ed436714247677f0342268ede7aa9e681b71d6060b46"}}) + + (fetch! (app-url "/login")) + (send-text! (find-by-css "#username") "Arne") + (send-text! (find-by-css "#password") "sekrit") + (click! (find-by-css "input[type=submit]")) + + (is (re-find #"Login successful, welcome back!" (page-text)))) + +(comment + (run-tests)) diff --git a/test/clj/booklog/test_helper.clj b/test/clj/booklog/test_helper.clj index ea6fd48..f831084 100644 --- a/test/clj/booklog/test_helper.clj +++ b/test/clj/booklog/test_helper.clj @@ -9,6 +9,10 @@ (def test-http-port 59800) +(def ^:dynamic *browser* nil) + +(def ^:dynamic *system* nil) + (defn test-config [] {:http-port test-http-port :db-path (util/temp-file-name "test" "db")}) @@ -18,6 +22,62 @@ [tests] (let [config (test-config) system (-> config app/app-system component/start)] - (tests) + (binding [*system* system] + (tests)) (component/stop system) (io/delete-file (:db-path config)))) + +(defn wrap-clear-db + "Test fixture which clears the database before each test." + [test] + (sc/clear-db! (:spicerack *system*)) + (test)) + +(defn wrap-browser + "A fixture function which binds *browser* to a new browser instance." + [tests] + (sd/with-browser [browser (sd/make-browser)] + (binding [*browser* browser] + (tests)))) + +(defn wrap-clear-cookies [test] + (sd/delete-all-cookies! *browser*) + (test)) + +(defn fetch! + ([url] + (sd/fetch! *browser* url)) + ([browser url] + (sd/fetch! browser url))) + +(defn find-by-css + ([css] + (sd/find-by-css *browser* css)) + ([browser css] + (sd/find-by-css browser css))) + +(defn find-by-xpath + ([xpath] + (sd/find-by-xpath *browser* xpath)) + ([browser xpath] + (sd/find-by-xpath browser xpath))) + +(defn page-text + ([] + (sd/page-text *browser*)) + ([browser] + (sd/page-text))) + +(defn app-url [path] + (assoc (uri/uri "http://localhost") + :path path + :port test-http-port)) + +(defn current-path [] + (:path (uri/uri (sd/current-url *browser*)))) + +(defn dump-db [] + (sc/dump-db (:spicerack *system*))) + +(defn load-db! [data] + (sc/load-db! (:spicerack *system*) data))