(ns auto-ap.integration.graphql.invoices (:require [auto-ap.datomic :refer [conn uri]] [auto-ap.datomic.migrate :as m] [auto-ap.time-reader] [auto-ap.graphql.invoices :as sut] [clj-time.core :as time] [clojure.test :as t :refer [deftest is testing use-fixtures]] [datomic.api :as d])) (defn wrap-setup [f] (with-redefs [auto-ap.datomic/uri "datomic:mem://datomic-transactor:4334/invoice"] (d/create-database uri) (with-redefs [auto-ap.datomic/conn (d/connect uri)] (m/migrate conn) (f) (d/release conn) (d/delete-database uri)))) #_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} (defn admin-token [] {:user "TEST ADMIN" :exp (time/plus (time/now) (time/days 1)) :user/role "admin" :user/name "TEST ADMIN"}) #_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} (defn user-token [client-id] {:user "TEST USER" :exp (time/plus (time/now) (time/days 1)) :user/role "user" :user/name "TEST USER" :user/clients [{:db/id client-id}]}) (use-fixtures :each wrap-setup) (deftest test-add-invoice (testing "It should Add an invoice" (let [{:strs [vendor-id client-id account-id]} (:tempids @(d/transact conn [{:client/code "ABC" :db/id "client-id" :client/locations ["DT"]} {:vendor/name "Vendy" :db/id "vendor-id" :vendor/default-account "account-id"} {:account/name "Account" :account/numeric-code 123 :account/invoice-allowance :allowance/allowed :db/id "account-id"}]))] (is (some? (sut/add-invoice {:id (admin-token)} {:invoice {:client_id client-id :vendor_id vendor-id :invoice_number "123" :date #clj-time/date-time "2022-01-01" :total 10.00 :expense_accounts [{:amount 10.0 :location "DT" :account_id account-id}]}} nil))) (testing "It should prevent an expense account that isn't allowed" (let [{:strs [denied-account-id]} (:tempids @(d/transact conn [ {:account/name "Account" :account/numeric-code 123 :account/invoice-allowance :allowance/denied :db/id "denied-account-id"}]))] (is (thrown? Exception (sut/add-invoice {:id (admin-token)} {:invoice {:client_id client-id :vendor_id vendor-id :invoice_number "789" :date #clj-time/date-time "2022-01-01" :total 10.00 :expense_accounts [{:amount 10.0 :location "DT" :account_id denied-account-id}]}} nil))))) (testing "It should allow an expense account that is valid for the vendor" (let [{:strs [vendor-account-id vendor-2]} (:tempids @(d/transact conn [ {:account/name "Account" :account/numeric-code 123 :account/invoice-allowance :allowance/denied :account/vendor-allowance :allowance/allowed :db/id "vendor-account-id"} {:vendor/name "Testy" :vendor/default-account "vendor-account-id" :db/id "vendor-2"}]))] (is (some? (sut/add-invoice {:id (admin-token)} {:invoice {:client_id client-id :vendor_id vendor-2 :invoice_number "456" :date #clj-time/date-time "2022-01-01" :total 10.00 :expense_accounts [{:amount 10.0 :location "DT" :account_id vendor-account-id}]}} nil))))))))