 
 (deftest rename-congregation-test
   (let [session (login! app)
-        response (-> (request :post "/api/congregations")
-                     (json-body {:name "Old Name"})
-                     (merge session)
-                     app
-                     (assert-response ok?))
-        cong-id (UUID/fromString (:id (:body response)))]
+        cong-id (create-congregation! session "Old Name")]
 
     (testing "rename congregation"
-      (let [response (-> (request :post (str "/api/congregation/" cong-id "/rename"))
-                         (json-body {:name "New Name"})
-                         (merge session)
-                         app)]
+      (let [response (try-rename-congregation! session cong-id "New Name")]
         (is (ok? response)))
       (let [response (-> (request :get (str "/api/congregation/" cong-id))
                          (merge session)

      app
      (assert-response ok?)))


(deftest format-for-api-test
  (is (= {} (api/format-for-api {})))


(deftest add-user-test
  (let [session (login! app)
        cong-id (create-congregation! session "Congregation")
        new-user-id (db/with-db [conn {}]
                      (user/save-user! conn "user1" {:name "User 1"}))]

    (testing "add user"
      (let [response (try-add-user! session cong-id new-user-id)]
        (is (ok? response))
        ;; TODO: check the result through the API
        (let [users (congregation/get-users (projections/cached-state) cong-id)]
          (is (contains? (set users) new-user-id)))))

    (testing "invalid user"
      (let [response (try-add-user! session cong-id (UUID. 0 1))]
        (is (bad-request? response))
        (is (= {:errors [["no-such-user" "00000000-0000-0000-0000-000000000001"]]}
               (:body response)))))

    (testing "no access"
      (revoke-access-from-all! cong-id)
      (let [response (try-add-user! session cong-id new-user-id)]
        (is (forbidden? response))))))

(deftest set-user-permissions-test
  (let [session (login! app)
        cong-id (create-congregation! session "Congregation")
        user-id (db/with-db [conn {}]
                  (user/save-user! conn "user1" {:name "User 1"}))]
    (is (ok? (try-add-user! session cong-id user-id)))

    ;; TODO: test changing permissions (after new users no more get full admin access)?

