Skip to content

Commit

Permalink
Move testing macro to fixture
Browse files Browse the repository at this point in the history
This doesn't need to be a macro we wrap each test with.
  • Loading branch information
tobias committed Feb 17, 2024
1 parent 6a8c802 commit d4129f1
Showing 1 changed file with 103 additions and 105 deletions.
208 changes: 103 additions & 105 deletions test/clojars/unit/admin_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,40 @@
[clojars.storage :as storage]
[clojars.test-helper :as help]
[clojure.java.io :as io]
[clojure.test :refer [are deftest is join-fixtures testing use-fixtures]]
[clojure.test :refer [are deftest is testing use-fixtures]]
[matcher-combinators.test]))

(def ^:dynamic *search-removals* nil)

(defn with-repo-setup2
[f]
(let [jar (io/file (io/resource "fake.jar"))]
(binding [*search-removals* (atom #{})
admin/*db* help/*db*
admin/*search* (reify search/Search
(delete! [_ group#]
(swap! *search-removals* conj group#))
(delete! [_ group# artifact#]
(swap! *search-removals* conj (format "%s/%s" group# artifact#))))
admin/*storage* (storage/fs-storage (:repo (config)))]
(help/add-verified-group "testuser" "org.ham")
(db/add-jar admin/*db* "testuser" {:group "org.ham" :name "biscuit" :version "1" :description "delete me"})
(db/add-jar admin/*db* "testuser" {:group "org.ham" :name "biscuit" :version "2" :description ""})
(db/add-jar admin/*db* "testuser" {:group "org.ham" :name "sandwich" :version "1" :description ""})
(storage/write-artifact admin/*storage* "org/ham/biscuit/1/biscuit-1.jar" jar)
(storage/write-artifact admin/*storage* "org/ham/biscuit/1/biscuit-1.pom" jar)
(storage/write-artifact admin/*storage* "org/ham/biscuit/2/biscuit-2.jar" jar)
(storage/write-artifact admin/*storage* "org/ham/biscuit/2/biscuit-2.pom" jar)
(storage/write-artifact admin/*storage* "org/ham/sandwich/1/sandwich-1.jar" jar)
(storage/write-artifact admin/*storage* "org/ham/sandwich/1/sandwich-1.pom" jar)

(with-redefs [admin/current-date-str (constantly "20160827")]
(f)))))

(use-fixtures :each
(join-fixtures
[help/default-fixture
help/with-clean-database]))
help/default-fixture
help/with-clean-database
with-repo-setup2)

(deftest segments->path-should-work
(are [exp given] (= exp (admin/segments->path given))
Expand All @@ -32,115 +59,86 @@
(defn in-backup-somewhere? [name]
(some #{name} (map (memfn getName) (file-seq (io/file (:deletion-backup-dir (config)))))))

(def ^:dynamic *search-removals* nil)

(defmacro with-repo-setup [& body]
`(let [jar# (io/file (io/resource "fake.jar"))]
(binding [*search-removals* (atom #{})
admin/*db* help/*db*
admin/*search* (reify search/Search
(delete! [_ group#]
(swap! *search-removals* conj group#))
(delete! [_ group# artifact#]
(swap! *search-removals* conj (format "%s/%s" group# artifact#))))
admin/*storage* (storage/fs-storage (:repo (config)))]
(help/add-verified-group "testuser" "org.ham")
(db/add-jar admin/*db* "testuser" {:group "org.ham" :name "biscuit" :version "1" :description "delete me"})
(db/add-jar admin/*db* "testuser" {:group "org.ham" :name "biscuit" :version "2" :description ""})
(db/add-jar admin/*db* "testuser" {:group "org.ham" :name "sandwich" :version "1" :description ""})
(storage/write-artifact admin/*storage* "org/ham/biscuit/1/biscuit-1.jar" jar#)
(storage/write-artifact admin/*storage* "org/ham/biscuit/1/biscuit-1.pom" jar#)
(storage/write-artifact admin/*storage* "org/ham/biscuit/2/biscuit-2.jar" jar#)
(storage/write-artifact admin/*storage* "org/ham/biscuit/2/biscuit-2.pom" jar#)
(storage/write-artifact admin/*storage* "org/ham/sandwich/1/sandwich-1.jar" jar#)
(storage/write-artifact admin/*storage* "org/ham/sandwich/1/sandwich-1.pom" jar#)

(with-redefs [admin/current-date-str (constantly "20160827")]
~@body))))

(deftest delete-group-should-work
(with-repo-setup
(with-out-str
((admin/delete-group "org.ham")))
(with-out-str
((admin/delete-group "org.ham")))

(is (backup-exists? "org/ham" "biscuit/1/biscuit-1.jar"))
(is (backup-exists? "org/ham" "biscuit/1/biscuit-1.pom"))
(is (backup-exists? "org/ham" "biscuit/2/biscuit-2.jar"))
(is (backup-exists? "org/ham" "biscuit/2/biscuit-2.pom"))
(is (backup-exists? "org/ham" "sandwich/1/sandwich-1.jar"))
(is (backup-exists? "org/ham" "sandwich/1/sandwich-1.pom"))
(is (backup-exists? "org/ham" "biscuit/1/biscuit-1.jar"))
(is (backup-exists? "org/ham" "biscuit/1/biscuit-1.pom"))
(is (backup-exists? "org/ham" "biscuit/2/biscuit-2.jar"))
(is (backup-exists? "org/ham" "biscuit/2/biscuit-2.pom"))
(is (backup-exists? "org/ham" "sandwich/1/sandwich-1.jar"))
(is (backup-exists? "org/ham" "sandwich/1/sandwich-1.pom"))

(is (not (.exists (io/file (:repo (config)) "org/ham"))))
(is (not (.exists (io/file (:repo (config)) "org/ham"))))

(is (not (db/find-jar admin/*db* "org.ham" "biscuit")))
(is (not (db/find-jar admin/*db* "org.ham" "sandwich")))
(is (empty? (db/group-activenames admin/*db* "org.ham")))
(is (= #{"org.ham"} @*search-removals*))))
(is (not (db/find-jar admin/*db* "org.ham" "biscuit")))
(is (not (db/find-jar admin/*db* "org.ham" "sandwich")))
(is (empty? (db/group-activenames admin/*db* "org.ham")))
(is (= #{"org.ham"} @*search-removals*)))

(deftest delete-jar-without-version-should-work
(with-repo-setup
(with-out-str
((admin/delete-jars "org.ham" "biscuit")))

(is (backup-exists? "org/ham/biscuit" "1/biscuit-1.jar"))
(is (backup-exists? "org/ham/biscuit" "1/biscuit-1.pom"))
(is (backup-exists? "org/ham/biscuit" "2/biscuit-2.jar"))
(is (backup-exists? "org/ham/biscuit" "2/biscuit-2.pom"))
(is (not (in-backup-somewhere? "sandwich-1.jar")))
(is (not (in-backup-somewhere? "sandwich-1.pom")))

(is (not (.exists (io/file (:repo (config)) "org/ham/biscuit/1/biscuit-1.jar"))))
(is (not (.exists (io/file (:repo (config)) "org/ham/biscuit/1/biscuit-1.pom"))))
(is (not (.exists (io/file (:repo (config)) "org/ham/biscuit/2/biscuit-2.jar"))))
(is (not (.exists (io/file (:repo (config)) "org/ham/biscuit/2/biscuit-2.pom"))))
(is (.exists (io/file (:repo (config)) "org/ham/sandwich/1/sandwich-1.jar")))
(is (.exists (io/file (:repo (config)) "org/ham/sandwich/1/sandwich-1.pom")))

(is (not (db/find-jar admin/*db* "org.ham" "biscuit")))
(is (db/find-jar admin/*db* "org.ham" "sandwich"))
(is (seq (db/group-activenames admin/*db* "org.ham")))
(is (= #{"org.ham/biscuit"} @*search-removals*))))
(with-out-str
((admin/delete-jars "org.ham" "biscuit")))

(is (backup-exists? "org/ham/biscuit" "1/biscuit-1.jar"))
(is (backup-exists? "org/ham/biscuit" "1/biscuit-1.pom"))
(is (backup-exists? "org/ham/biscuit" "2/biscuit-2.jar"))
(is (backup-exists? "org/ham/biscuit" "2/biscuit-2.pom"))
(is (not (in-backup-somewhere? "sandwich-1.jar")))
(is (not (in-backup-somewhere? "sandwich-1.pom")))

(is (not (.exists (io/file (:repo (config)) "org/ham/biscuit/1/biscuit-1.jar"))))
(is (not (.exists (io/file (:repo (config)) "org/ham/biscuit/1/biscuit-1.pom"))))
(is (not (.exists (io/file (:repo (config)) "org/ham/biscuit/2/biscuit-2.jar"))))
(is (not (.exists (io/file (:repo (config)) "org/ham/biscuit/2/biscuit-2.pom"))))
(is (.exists (io/file (:repo (config)) "org/ham/sandwich/1/sandwich-1.jar")))
(is (.exists (io/file (:repo (config)) "org/ham/sandwich/1/sandwich-1.pom")))

(is (not (db/find-jar admin/*db* "org.ham" "biscuit")))
(is (db/find-jar admin/*db* "org.ham" "sandwich"))
(is (seq (db/group-activenames admin/*db* "org.ham")))
(is (= #{"org.ham/biscuit"} @*search-removals*)))

(deftest delete-jar-with-version-should-work
(with-repo-setup
(with-out-str
((admin/delete-jars "org.ham" "biscuit" "1")))

(is (backup-exists? "org/ham/biscuit/1" "biscuit-1.jar"))
(is (backup-exists? "org/ham/biscuit/1" "biscuit-1.pom"))
(is (not (in-backup-somewhere? "biscuit-2.jar")))
(is (not (in-backup-somewhere? "biscuit-2.pom")))
(is (not (in-backup-somewhere? "sandwich-1.jar")))
(is (not (in-backup-somewhere? "sandwich-1.pom")))

(is (not (.exists (io/file (:repo (config)) "org/ham/biscuit/1/biscuit-1.jar"))))
(is (not (.exists (io/file (:repo (config)) "org/ham/biscuit/1/biscuit-1.pom"))))
(is (.exists (io/file (:repo (config)) "org/ham/biscuit/2/biscuit-2.jar")))
(is (.exists (io/file (:repo (config)) "org/ham/biscuit/2/biscuit-2.pom")))
(is (.exists (io/file (:repo (config)) "org/ham/sandwich/1/sandwich-1.jar")))
(is (.exists (io/file (:repo (config)) "org/ham/sandwich/1/sandwich-1.pom")))

(is (not (db/find-jar admin/*db* "org.ham" "biscuit" "1")))
(is (db/find-jar admin/*db* "org.ham" "biscuit" "2"))
(is (db/find-jar admin/*db* "org.ham" "sandwich"))
(is (seq (db/group-activenames admin/*db* "org.ham")))
(is (empty? @*search-removals*))))
(with-out-str
((admin/delete-jars "org.ham" "biscuit" "1")))

(is (backup-exists? "org/ham/biscuit/1" "biscuit-1.jar"))
(is (backup-exists? "org/ham/biscuit/1" "biscuit-1.pom"))
(is (not (in-backup-somewhere? "biscuit-2.jar")))
(is (not (in-backup-somewhere? "biscuit-2.pom")))
(is (not (in-backup-somewhere? "sandwich-1.jar")))
(is (not (in-backup-somewhere? "sandwich-1.pom")))

(is (not (.exists (io/file (:repo (config)) "org/ham/biscuit/1/biscuit-1.jar"))))
(is (not (.exists (io/file (:repo (config)) "org/ham/biscuit/1/biscuit-1.pom"))))
(is (.exists (io/file (:repo (config)) "org/ham/biscuit/2/biscuit-2.jar")))
(is (.exists (io/file (:repo (config)) "org/ham/biscuit/2/biscuit-2.pom")))
(is (.exists (io/file (:repo (config)) "org/ham/sandwich/1/sandwich-1.jar")))
(is (.exists (io/file (:repo (config)) "org/ham/sandwich/1/sandwich-1.pom")))

(is (not (db/find-jar admin/*db* "org.ham" "biscuit" "1")))
(is (db/find-jar admin/*db* "org.ham" "biscuit" "2"))
(is (db/find-jar admin/*db* "org.ham" "sandwich"))
(is (seq (db/group-activenames admin/*db* "org.ham")))
(is (empty? @*search-removals*)))

(deftest verify-group!-works
(with-repo-setup
(is (= "'testuser2' doesn't have access to the 'org.ham' group"
(admin/verify-group! "testuser2" "org.ham")))
(is (= "'about' is a reserved name"
(admin/verify-group! "testuser2" "about")))
(is (= "'abcd' isn't a reverse domain name"
(admin/verify-group! "testuser2" "abcd")))
(is (match? {:group_name "org.ham"
(is (= "'testuser2' doesn't have access to the 'org.ham' group"
(admin/verify-group! "testuser2" "org.ham")))
(is (= "'about' is a reserved name"
(admin/verify-group! "testuser2" "about")))
(is (= "'abcd' isn't a reverse domain name"
(admin/verify-group! "testuser2" "abcd")))
(is (match? {:group_name "org.ham"
:verified_by "testuser"}
(admin/verify-group! "testuser" "org.ham"))
"Can verify an existing group")

(testing "Can add and verify a new group"
(is (match? {:group_name "org.hambiscuit"
:verified_by "testuser"}
(admin/verify-group! "testuser" "org.ham"))
"Can verify an existing group")

(testing "Can add and verify a new group"
(is (match? {:group_name "org.hambiscuit"
:verified_by "testuser"}
(admin/verify-group! "testuser" "org.hambiscuit")))
(is (some #{"testuser"} (db/group-activenames help/*db* "org.hambiscuit"))))))
(admin/verify-group! "testuser" "org.hambiscuit")))
(is (some #{"testuser"} (db/group-activenames help/*db* "org.hambiscuit")))))

0 comments on commit d4129f1

Please sign in to comment.