 (defn ring-app []
   (ltu/make-ring-app (ltu/concat-routes [(routes/get-main-routes)])))
 
+(defn strip-unwanted-attrs [m]
+  (let [unwanted #{:id :resourceURI :acl :operations
+                   :created :updated :name :description}]
+    (into {} (remove #(unwanted (first %)) m))))
+
 (deftest lifecycle
   (let [session-admin (-> (session (ring-app))
                           (content-type "application/json")
                           (header authn-info-header "root ADMIN USER ANON"))
-        session-user (-> (session (ring-app))
+        session-jane (-> (session (ring-app))
                          (content-type "application/json")
                          (header authn-info-header "jane USER ANON"))
         session-anon (-> (session (ring-app))


    ;; create a vm as a normal user
    (let [timestamp "1964-08-25T10:00:00.0Z"
          create-req {:id           (str vm/resource-url "/uuid")
                      :resourceURI  vm/resource-uri
                      :created      timestamp
                      :updated      timestamp

                      :name         "short name"
                      :description  "short description",
                      :properties   {:a "one",
                                     :b "two"}

                      :instanceID   "aaa-bbb-111"
                      :state        "Running"
                      :ip           "127.0.0.1"


                      :cloud        {:href  "connector/0123-4567-8912",
                                     :roles ["realm:cern", "realm:my-accounting-group"]}


                      :run          {:href "run/aaa-bbb-ccc",
                                     :user {:href "user/test"}}

                      :serviceOffer {:href                  "service-offer/e3db10f4-ad81-4b3e-8c04-4994450da9e3"
                                     :resource:vcpu         1
                                     :resource:ram          4096
                                     :resource:disk         10
                                     :resource:instanceType "Large"}}

          create-priv-req (assoc create-req :run {:href "run/444-555-666"
                                                  :user {:href "user/jane"}})

          resp (-> session-admin
                   (request base-uri
                            :request-method :post
                            :body (json/write-str create-req))
                   (ltu/body->edn)
                   (ltu/is-status 201))

          resp-priv (-> session-admin
                        (request base-uri
                                 :request-method :post
                                 :body (json/write-str create-priv-req))
                        (ltu/body->edn)
                        (ltu/is-status 201))

          id (get-in resp [:response :body :resource-id])
          id-priv (get-in resp-priv [:response :body :resource-id])
          id-other (get-in resp-priv [:response :body :resource-id])
          uri (-> resp
                  (ltu/location))
          uri-priv (-> resp-priv
                       (ltu/location))

          jane-uri (str p/service-context (u/de-camelcase uri))
          janet-uri (str p/service-context (u/de-camelcase uri-priv))]

      ;; admin should be able to see everyone's records
      (-> session-admin
          (request jane-uri)
          (ltu/body->edn)
          (ltu/is-status 200)
          (ltu/is-operation-present "delete")
          (ltu/is-operation-present "edit"))

      (-> session-admin
          (request janet-uri)
          (ltu/body->edn)
          (ltu/is-status 200)
          (ltu/is-operation-present "delete")
          (ltu/is-operation-present "edit"))

      (-> session-user
          (request janet-uri)
          (ltu/body->edn)
          (ltu/is-status 403)
          (ltu/is-operation-absent "delete")
          (ltu/is-operation-absent "edit"))

      ;;edit
      (-> session-admin
          (request base-uri
                   :request-method :put
                   :body (json/write-str create-req))
          (ltu/body->edn)
          (ltu/is-status 200))

      (-> session-admin
          (request jane-uri
                   :request-method :put
                   :body (json/write-str (assoc create-req :id id)))
          (ltu/body->edn)
          (ltu/is-status 200))

      (-> session-user
          (request jane-uri
                   :request-method :put
                   :body (json/write-str (assoc create-req :id id)))
          (ltu/body->edn)
          (ltu/is-status 403))


      (-> session-user
          (request jane-uri
                   :request-method :put
                   :body (json/write-str create-req))
          (ltu/body->edn)
          (ltu/is-status 403))


      (-> session-anon
          (request base-uri
                   :request-method :put
                   :body (json/write-str create-req))
          (ltu/body->edn)
          (ltu/is-status 403))

      (-> session-admin
          (request base-uri
                   :request-method :put
                   :body (json/write-str create-req))
          (ltu/body->edn)
          (ltu/is-count 2)
          (ltu/is-status 200))

      ;;delete
      (-> session-user
          (request jane-uri
                   :request-method :delete)
          (ltu/body->edn)
          (ltu/is-status 403))

      (-> session-admin
          (request jane-uri
                   :request-method :delete)
          (ltu/body->edn)
          (ltu/is-status 200))

      (-> session-admin
          (request janet-uri
                   :request-method :delete)
          (ltu/body->edn)
          (ltu/is-status 200))

      ;;record should be deleted
      (-> session-admin
          (request jane-uri
                   :request-method :delete)
          (ltu/body->edn)
          (ltu/is-status 404)))))

        (ltu/is-operation-absent "edit"))

    ;; normal user collection query should succeed but be empty (no vm created yet)
    (-> session-jane
        (request base-uri)
        (ltu/body->edn)
        (ltu/is-status 200)
