Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to add login test #1

Merged
merged 6 commits into from Feb 11, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 40 additions & 8 deletions test/accounts/endpoint/login_test.clj
@@ -1,18 +1,21 @@
(ns accounts.endpoint.login-test
(:require [accounts.component.mailer :refer [stub-mailer]]
(:require [accounts
[system :refer [base-config]]
[users :refer [add]]]
[accounts.component.mailer :refer [stub-mailer]]
[accounts.endpoint.login :refer :all]
[accounts.system :refer [base-config]]
[clojure.test :refer :all]
[meta-merge.core :refer [meta-merge]]
[com.stuartsierra.component :as component]
[duct.component
[endpoint :refer [endpoint-component]]
[handler :refer [handler-component]]
[hikaricp :refer [hikaricp]]
[ragtime :refer [migrate ragtime]]]
[kerodon
[core :refer :all]
[test :refer :all]]
[clojure.pprint :refer [pprint]]))
[meta-merge.core :refer [meta-merge]]
[clojure.core.async :refer [<!! >!!]]))

(def config {:db {:uri "jdbc:sqlite::memory:"}})

Expand All @@ -22,14 +25,16 @@
:ragtime (ragtime (:ragtime config))
:login (endpoint-component login-endpoint)
:db (hikaricp (:db config))
:mailer (stub-mailer))
:mailer (stub-mailer)
:app (handler-component (:app config)))
(component/system-using
{:login [:db :mailer]
:app [:login]
:ragtime [:db]}))))

(deftest smoke-test
(let [system (component/start (test-system config))
handler (some-> system :login :routes)]
handler (some-> system :app :handler)]
(try
(migrate (:ragtime system))

Expand All @@ -43,7 +48,34 @@
(visit "/login")
(fill-in "Email:" "notvalid@example.com")
(press "submit")
(has (some-text? "User not found")
"Couldn't find non-existing user. Phew!")))
(within [:h1]
(has (text? "User not found")))))

(testing "login success"
;; We don't support registering yet, so manually add a test user
;; before the login attempt.
(let [email (str (gensym) "@example.com")
db-spec (-> system :db :spec)
channel (-> system :mailer :channel)
user-id (add db-spec {:email email :moniker email})]
(-> (session handler)
(visit "/login")
(fill-in "Email:" email)
(press "submit")
(within [:h1]
(has (text? "Login token on its way!"))))

(let [m (<!! channel)]
(is (= email (:to m)))
(is (= "One-time login URL" (:subject m)))
;;; TODO check link in body
)

;; We want to check there's no more messages on the channel, however
;; <!! blocks if there's no message, so write a sentinel and make
;; sure that's what we get back.
(let [sentinel (keyword (gensym))]
(is (= sentinel (do (>!! channel sentinel)
(<!! channel)))))))

(finally (component/stop system)))))