Skip to content
Snippets Groups Projects
Commit 316de789 authored by Cam Saul's avatar Cam Saul
Browse files

test fixes + ring adapter

parent 6a4ed4ed
Branches
Tags
No related merge requests found
......@@ -22,7 +22,7 @@
[com.cemerick/friend "0.2.1"] ; auth library
[com.h2database/h2 "1.3.170"] ; embedded SQL database
[com.mattbertolini/liquibase-slf4j "1.2.1"]
[compojure "1.3.1"] ; HTTP Routing library built on Ring
[compojure "1.3.2"] ; HTTP Routing library built on Ring
[environ "0.5.0"] ; easy environment management
[korma "0.4.0"] ; SQL lib
[log4j/log4j "1.2.17"
......@@ -36,6 +36,7 @@
[org.slf4j/slf4j-log4j12 "1.7.1"]
[org.yaml/snakeyaml "1.14"] ; YAML parser (required by liquibase)
[postgresql "9.1-901.jdbc4"] ; Postgres driver
[ring/ring-jetty-adapter "1.3.2"] ; Ring adapter using Jetty webserver (used to run a Ring server for unit tests)
[ring/ring-json "0.3.1"] ; Ring middleware for reading/writing JSON automatically
[swiss-arrows "1.0.0"] ; 'Magic wand' macro -<>, etc.
]
......
......@@ -2,6 +2,7 @@
(:gen-class)
(:require [clojure.tools.logging :as log]
[clojure.java.jdbc :as jdbc]
[ring.adapter.jetty :as ring]
(ring.middleware [cookies :refer [wrap-cookies]]
[json :refer [wrap-json-response
wrap-json-body]]
......@@ -39,3 +40,17 @@
wrap-cookies ; Parses cookies in the request map and assocs as :cookies
wrap-session ; reads in current HTTP session and sets :session/key
))
;; ## Jetty Adapter (for unit tests)
(def ^:private jetty-server
"`org.eclipse.jetty.server.Server` instance."
(delay (try (ring/run-jetty app {:port 3000
:join? false})
(catch java.net.BindException e ; assume server is already running if port's already bound
:already-running))))
(defn start-jetty-server-if-needed
"Manually start the Ring server, e.g. when running unit tests."
[]
@jetty-server)
......@@ -13,7 +13,7 @@
(-> @korma-entity
(select (aggregate (count :*) :count))
first
count))
:count))
(defn update-table-row-count
"Update the `:rows` column for TABLE with the count from `get-table-row-count`."
......
......@@ -92,8 +92,11 @@
(defn- authenticate [{:keys [email password] :as credentials}]
{:pre [(string? email)
(string? password)]}
(-> (client :post 200 "session" credentials)
:id))
(try
(-> (client :post 200 "session" credentials)
:id)
(catch Exception e
(println "Failed to authenticate with email:" email "and password:" password ". Does user exist?"))))
(defn- build-url [url url-param-kwargs]
{:pre [(string? url)
......
......@@ -2,6 +2,7 @@
"Functions relating to using the test data, Database, Organization, and Users."
(:require [cemerick.friend.credentials :as creds]
[medley.core :as medley]
[metabase.core :refer [start-jetty-server-if-needed]]
[metabase.db :refer :all]
[metabase.http-client :as http]
(metabase.models [field :refer [Field]]
......@@ -78,7 +79,8 @@
(def test-org
"The test Organization."
(delay (load/test-org)))
(delay (migrate :up)
(load/test-org)))
(def org-id
"The ID of the test Organization."
......@@ -88,7 +90,8 @@
;; ## Test Users
;;
;; These users have permissions for the Test Org. Three test users are defined:
;; These users have permissions for the Test Org. They are lazily created as needed.
;; Three test users are defined:
;; * rasta - an admin
;; * lucky
;; * trashbird
......@@ -121,10 +124,14 @@
(defn user->client
"Returns a `metabase.http-client/client` partially bound with the credentials for User with USERNAME.
In addition, it forces lazy creation of the User if needed and starts a Jetty web server if one is
not already running.
((user->client) :get 200 \"meta/table\")"
[username]
{:pre [(contains? usernames username)]}
(start-jetty-server-if-needed)
(user->id username) ; call a function that will force User to created if need be
(partial http/client (user->credentials username)))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment