(ns auto-ap.functional.test
  (:require  [clojure.test :as t :refer :all]
             [etaoin.api :as e]
             [etaoin.keys :as k]
             [config.core :refer [env]]
             [clj-time.core :as time]
             [buddy.sign.jwt :as jwt]))

(def base-url "https://staging3.app.integreatconsult.com")
(defn login-admin [driver]
  (let [jwt-token (jwt/sign {:user "Automated Tests"
                             :exp (time/plus (time/now) (time/days 30))
                             :user/role "admin"
                             :user/name "Automated Tests"}
                            (:jwt-secret env)
                            {:alg :hs512})]
    (e/go driver (str base-url "/?jwt=" jwt-token))
    (e/wait-visible driver {:tag :h1
                            :class "title"})))

(deftest create-invoice
  (testing "Creating a new invoice"
    (e/with-wait-timeout 10
      (e/with-firefox {} driver 
        (login-admin driver)
        (e/click driver {:tag :a :fn/text "Invoices"})
        (e/wait-visible driver {:tag :h1
                                :class "title"
                                :fn/text "Unpaid Invoices"})))))


(deftest edit-client
  (testing "Editing a client"
    (e/with-wait-timeout 10
      (e/with-firefox {} driver 
        (login-admin driver)
        
        (e/click driver {:tag :a :class "navbar-link login"})
        (e/click driver {:tag :a :fn/text "Administration"})
        
        (e/wait-visible driver {:tag :h1
                                :class "title"
                                :fn/text "Admin"})
        (e/click driver {:tag :span :fn/text "Clients"})
        (e/wait-visible driver {:tag :h1
                                :class "title"
                                :fn/text "Clients"})
        (e/click driver {:tag :i :class "fa fa-pencil"})
        (e/wait-visible driver {:tag :button
                                :fn/text "Save"})
        (e/click driver {:tag :button :fn/text "Save"})
        (e/wait-invisible driver {:tag :form})))))
