     :dataset_query          {}
     :visualization_settings {}))
 
-(defn new-pulse []
-  (let [cards    [(new-card), (new-card)]
+(defn new-pulse [& {:keys [name cards channels]
+                    :or   {name     (random-name)
+                           cards    nil
+                           channels nil}}]
+  (let [cards    (or cards [(new-card), (new-card)])
         card-ids (filter identity (map :id cards))]
-    (pulse/create-pulse (random-name) (user->id :crowberto) card-ids [])))
-
-;; ## GET /api/emailreport/form_input
-;; Test that we can get the form input options for the Test Org
-;(expect-let [_ @test-db ; force lazy loading of Test Data / Metabase DB
-;             _ (cascade-delete Database :name [not= "Test Database"])] ; Delete all Databases that aren't the Test DB
-;  {:users #{{:id (user->id :rasta),     :name "Rasta Toucan"}
-;            {:id (user->id :crowberto), :name "Crowberto Corv"}
-;            {:id (user->id :lucky),     :name "Lucky Pigeon"}
-;            {:id (user->id :trashbird), :name "Trash Bird"}}
-;   :databases [{:id (:id @test-db)
-;                :name "Test Database"}],
-;   :timezones ["GMT"
-;               "UTC"
-;               "US/Alaska"
-;               "US/Arizona"
-;               "US/Central"
-;               "US/Eastern"
-;               "US/Hawaii"
-;               "US/Mountain"
-;               "US/Pacific"
-;               "America/Costa_Rica"]
-;   :times_of_day [{:id "morning",   :realhour 8,  :name "Morning"}
-;                  {:id "midday",    :realhour 12, :name "Midday"}
-;                  {:id "afternoon", :realhour 16, :name "Afternoon"}
-;                  {:id "evening",   :realhour 20, :name "Evening"}
-;                  {:id "midnight",  :realhour 0,  :name "Midnight"}]
-;   :days_of_week [{:id "sun", :name "Sun"}
-;                  {:id "mon", :name "Mon"}
-;                  {:id "tue", :name "Tue"}
-;                  {:id "wed", :name "Wed"}
-;                  {:id "thu", :name "Thu"}
-;                  {:id "fri", :name "Fri"}
-;                  {:id "sat", :name "Sat"}]
-;   :modes [{:name "Active", :id 1}
-;           {:name "Disabled", :id 2}]
-;   :permissions [{:name "None",         :id 0}
-;                 {:name "Read Only",    :id 1}
-;                 {:name "Read & Write", :id 2}]}
-;  (-> ((user->client :rasta) :get 200 "emailreport/form_input" :org @org-id) ; convert to a set so test doesn't fail if order differs
-;        (update-in [:users] set)))
-
-;; ## /api/pulse/* AUTHENTICATION Tests
-;; We assume that all endpoints for a given context are enforced by the same middleware, so we don't run the same
-;; authentication test on every single individual endpoint
-
-(expect (get middleware/response-unauthentic :body) (http/client :get 401 "pulse"))
-(expect (get middleware/response-unauthentic :body) (http/client :put 401 "pulse/13"))
+    (pulse/create-pulse name (user->id :crowberto) card-ids [])))
+
+(defn delete-existing-pulses []
+  (->> (db/sel :many :field [Pulse :id])
+       (mapv #(db/cascade-delete Pulse :id %))))
 
 (defn user-details [user]
   (match-$ user

     :created_at   $
     :updated_at   $
     :creator_id   $
     :creator      (user-details @(:creator pulse))
     :cards        (mapv pulse-card-details @(:cards pulse))
     :channels     (mapv pulse-channel-details @(:channels pulse))}))

(defn pulse-response [{:keys [created_at updated_at] :as pulse}]
  (-> pulse

  {:name         "A Pulse"
   :public_perms common/perms-readwrite
   :creator_id   (user->id :rasta)
   :creator      (user-details (fetch-user :rasta))
   :created_at   true
   :updated_at   true
   :cards        (mapv pulse-card-details [card1 card2])
   :channels     [{:channel_type  "email"
                   :schedule_type "daily"
                   :schedule_hour 12
                   :schedule_day  nil
                   :details       {}
                   :recipients    []}]}
  (-> (pulse-response ((user->client :rasta) :post 200 "pulse" {:name     "A Pulse"
                                                                :cards    [{:id (:id card1)} {:id (:id card2)}]
                                                                :channels [{:channel_type  "email"
                                                                            :schedule_type "daily"
                                                                            :schedule_hour 12
                                                                            :schedule_day  nil
                                                                            :recipients    []}]}))
      (update :channels (fn [chans]
                          (mapv #(dissoc % :id :pulse_id :created_at :updated_at) chans)))))

;; ## GET /api/pulse

(expect-let [_      (delete-existing-pulses)
             pulse1 (new-pulse :name "ABC")
             pulse2 (new-pulse :name "DEF")]
  [(pulse-details pulse1)
   (pulse-details pulse2)]
  ((user->client :rasta) :get 200 "pulse"))


;; ## GET /api/pulse/:id

(expect-let [pulse1 (new-pulse)]
  (pulse-details pulse1)
  ((user->client :rasta) :get 200 (format "pulse/%d" (:id pulse1))))

;(expect-eval-actual-first
;    (match-$ (sel :one EmailReport (order :id :DESC))
