Browse Source

#156 Remove node-api dependency on decode-app

Jordi Coscolla 1 year ago
parent
commit
3be1178d3e

+ 0 - 34
apps/petitions/decode-app/data/petitions.json

@@ -23,39 +23,5 @@
23 23
     }],
24 24
     "isOpen": true,
25 25
     "outcome": {}
26
-  },
27
-  {
28
-    "title": "Universal basic income",
29
-    "summary": "White paper blockchain technology node research and develop. Cryptographic modelling. Availability fairbnb cryptographic modelling data ontology pilot",
30
-    "description": "White paper blockchain technology node research and develop. cryptographic modelling data ontology pilots. Availability fairbnb cryptographic modelling data ontology pilots. Availability fairbnb cryptographic modelling data ontology pilots. Availability fairbnb cryptographic modelling data ontology pilots.",
31
-    "submittedTo": "Jill Doe, Company",
32
-    "createdBy": "Ina",
33
-    "closingDate": "2018-10-28",
34
-    "id": "59f888c8ce33c76884e8cf17",
35
-    "cs_id": null,
36
-    "isEthereum": "true",
37
-    "images": {
38
-      "square": "images/petition2-square.jpg",
39
-      "wide": "images/petition2-wide.jpg"
40
-    },
41
-    "isOpen": true,
42
-    "outcome": {}
43
-  },
44
-  {
45
-    "title": "Additional seating in the park",
46
-    "summary": "Amsterdam blockchain collaboration smart attribute based encryption data  collection privacy data sovereignity. Proprietary gateway rich services.",
47
-    "description": "Rich services data management sharing economy privacy. cryptographic modelling data ontology pilots. Availability fairbnb cryptographic modelling data ontology pilots. Availability fairbnb cryptographic modelling data ontology pilots. Availability fairbnb cryptographic modelling data ontology pilots.",
48
-    "submittedTo": "Bob Douglas, Council",
49
-    "createdBy": "Camilla",
50
-    "closingDate": "2017-12-20",
51
-    "id": "59f888c8ce33c76884e8cf18",
52
-    "cs_id": null,
53
-    "isEthereum": "true",
54
-    "images": {
55
-      "square": "images/petition3-square.jpg",
56
-      "wide": "images/petition3-wide.jpg"
57
-    },
58
-    "isOpen": true,
59
-    "outcome": {}
60 26
   }
61 27
 ]

+ 0 - 9
apps/petitions/decode-app/src/decode_app/handler.clj

@@ -67,15 +67,6 @@
67 67
                                                                               (contents/sign-petition email current-url petition)))
68 68
                                                      (redirect (redirect-auth-url current-url)))))
69 69
 
70
-           (POST "/sign/petitions/:id" request (let [current-url (:uri request)
71
-                                                     petition-id (get-in request [:params :id])]
72
-                                                    (if (authenticated? request)
73
-                                                      (do
74
-                                                        (let [signatory (users/get-name (get-in request [:session :identity]))]
75
-                                                             (petitions/add-signature petition-id signatory)
76
-                                                             (redirect (format "/sign/petitions/%s/confirmation" petition-id))))
77
-                                                      (redirect (redirect-auth-url current-url)))))
78
-
79 70
            (GET "/sign/petitions/:id/confirmation" request (let [current-url (:uri request)
80 71
                                                                  petition-id (get-in request [:params :id])]
81 72
                                                                 (if (authenticated? request)

+ 0 - 27
apps/petitions/decode-app/src/decode_app/nodeApi.clj

@@ -1,27 +0,0 @@
1
-(ns decode-app.nodeApi
2
-  (:require [clj-http.client :as client]
3
-            [clojure.data.json :as json]
4
-            [decode-app.config :as config]))
5
-
6
-
7
-(defn get-petition-url [petition-id path]
8
-  (let [config (config/get-config)
9
-        host (:node-api-host config)
10
-        port (:node-api-port config)]
11
-    (format "%s:%s/dapp/petitions/%s/%s" host port petition-id path)))
12
-
13
-(defn sign [petition-id signatory]
14
-  (client/post 
15
-   {:body (format "{\"signatory\":\"%s\"}" signatory)
16
-    :content-type :json}))
17
-
18
-(defn getSignatureCount [id]
19
-  (let [dappResponse (client/get (get-petition-url id "signature-count") {:content-type :json})
20
-        responseContent (get dappResponse :body)]
21
-    (json/read-str responseContent :key-fn keyword)))
22
-
23
-(defn getSignatories [id]
24
-  (let [dappResponse (client/get (get-petition-url id "signatories") {:content-type :json})
25
-        responseContent (get dappResponse :body)]
26
-    (json/read-str responseContent :key-fn keyword)))
27
-

+ 4 - 25
apps/petitions/decode-app/src/decode_app/petitions.clj

@@ -1,34 +1,13 @@
1 1
 (ns decode-app.petitions
2
-    (:require [decode-app.database :as db])
3
-    (:require [decode-app.nodeApi :as nodeApi]))
2
+    (:require [decode-app.database :as db]))
4 3
 
5
-(defn get-signature-count [id] (nodeApi/getSignatureCount id))
6
-
7
-(defn transformPetitions [petition] (let [is-ethereum? (boolean (Boolean/valueOf (:isEthereum petition)))]
8
-                                    (if is-ethereum?
9
-                                      (merge (get-signature-count (get petition :id)) petition)
10
-                                      (merge {:signatureCount 0} petition)
11
-                                      )))
12
-
13
-(defn get-petitions [] (let [petitions (db/find-all "petitions")]
14
-                            (map transformPetitions petitions)))
15
-
16
-(defn get-petition-data-eth [petition-details]
17
-      (let [id (:id petition-details)
18
-            signatories (nodeApi/getSignatories id)
19
-            signature-count (count (get signatories :signatories))]
20
-          (merge petition-details signatories {:signatureCount signature-count})))
4
+(defn get-petitions [] (db/find-all "petitions"))
21 5
 
22 6
 (defn get-petition-data-cs [petition-details]
23 7
       (merge petition-details {:signatories ["Pavlin x9"] :signatureCount 9}))
24 8
 
25
-(defn get-petition [id] (let [petition-details (db/find-by-id "petitions" id)
26
-                              is-ethereum? (boolean (Boolean/valueOf (:isEthereum petition-details)))]
27
-                             (if is-ethereum?
28
-                               (get-petition-data-eth petition-details)
29
-                               (get-petition-data-cs petition-details))))
30
-
31
-(defn add-signature [id signatory] (nodeApi/sign id signatory))
9
+(defn get-petition [id] (let [petition-details (db/find-by-id "petitions" id)]
10
+                             (get-petition-data-cs petition-details)))
32 11
 
33 12
 (defn add-petition [petition-object-id title description]
34 13
       (db/create-petition petition-object-id title description))

+ 0 - 36
apps/petitions/decode-app/test/decode_app/handler_test.clj

@@ -36,12 +36,6 @@
36 36
       (is (= (:status response) 302))
37 37
       (is (= ((:headers response) "Location") "http://localhost/auth/in?next=%2Fsign%2Fpetitions%2F23"))))
38 38
 
39
-  ;  (testing "POST /sign/petitions/:id route with exception"
40
-  ;    (with-redefs [petitions/add-signature (fn [id] (throw (Exception. "Failed to increment by id")))]
41
-  ;      (let [response (app (mock/request :post "/sign/petitions/23"))]
42
-  ;        (is (= 500 (:status response)))
43
-  ;        (is (= "Internal Server Error" (:body response))))))
44
-
45 39
   (testing "GET /sign/petitions/:id/confirmation route (when authenticated)"
46 40
     (with-redefs [authenticated? (fn [request] true)
47 41
                   petitions/get-petition (fn [id] {:title "title"})
@@ -77,33 +71,3 @@
77 71
   (testing "not-found route"
78 72
     (let [response (app (mock/request :get "/invalid"))]
79 73
       (is (= (:status response) 404)))))
80
-
81
-(facts "about handlers"
82
-  (fact "POST /sign/petitions/:id route (when authenticated)"
83
-    (:status (app (mock/request :post "/sign/petitions/23"))) => 302
84
-    ((:headers (app (mock/request :post "/sign/petitions/23"))) "Location") => "http://localhost/sign/petitions/23/confirmation"
85
-    (against-background
86
-      (authenticated? anything) => true,
87
-      (users/get-name anything) => "Bob",
88
-      (petitions/add-signature "23" "Bob") => {}))
89
-
90
-  (fact "POST /sign/petitions/:id route (when not authenticated)"
91
-    (:status (app (mock/request :post "/sign/petitions/23"))) => 302
92
-    ((:headers (app (mock/request :post "/sign/petitions/23"))) "Location") => "http://localhost/auth/in?next=%2Fsign%2Fpetitions%2F23"
93
-    (against-background
94
-      (authenticated? anything) => false,
95
-      (users/get-name anything) => "Bob",
96
-      (petitions/add-signature "23" anything) => {}))
97
-
98
-  (fact "POST /sign/petitions/:id route with exception"
99
-    (:status (app (mock/request :post "/sign/petitions/23"))) => 500
100
-    (provided
101
-      (users/get-name anything) => "Bob",
102
-      (petitions/add-signature anything anything) =throws=> (Exception. "Failed to increment by id"),
103
-      (authenticated? anything) => true)
104
-
105
-    (:body (app (mock/request :post "/sign/petitions/23"))) => "Internal Server Error"
106
-    (provided
107
-      (users/get-name anything) => "Bob",
108
-      (petitions/add-signature anything anything) =throws=> (Exception. "Failed to increment by id"),
109
-      (authenticated? anything) => true)))

+ 0 - 31
apps/petitions/decode-app/test/decode_app/nodeapi_test.clj

@@ -1,31 +0,0 @@
1
-(ns decode-app.nodeapi-test
2
-    (:require [midje.sweet :refer :all]
3
-      [clj-http.client :as client]
4
-      [decode-app.nodeApi :as nodeApi])
5
-    (:import org.bson.types.ObjectId))
6
-
7
-(future-facts "about node api"
8
-       (fact "sign returns response from node api sign"
9
-             (let [petition-id (str (ObjectId.))
10
-                   signatory (str (ObjectId.))
11
-                   post-resource (format "http://127.0.0.1:3000/dapp/petitions/%s/sign" petition-id)
12
-                   request-body (format "{\"signatory\":\"%s\"}" signatory)]
13
-                  (nodeApi/sign petition-id signatory) => {}
14
-                  (provided (client/post post-resource
15
-                                         {:body         request-body,
16
-                                          :content-type :json}) => {})))
17
-
18
-       (fact "get signatories returns response from node api signatories"
19
-             (let [petition-id (str (ObjectId.))
20
-                   post-resource (format "http://127.0.0.1:3000/dapp/petitions/%s/signatories" petition-id)]
21
-                  (nodeApi/getSignatories petition-id) => {:signatories "5"}
22
-                  (provided (client/get post-resource
23
-                                        {:content-type :json}) => {:body "{\"signatories\":\"5\"}"})))
24
-
25
-       (fact "get signature count returns response from node api signature-count"
26
-             (let [petition-id (str (ObjectId.))
27
-                   post-resource (format "http://127.0.0.1:3000/dapp/petitions/%s/signature-count" petition-id)]
28
-                  (nodeApi/getSignatureCount petition-id) => {:signatory "Alissa Evonne"}
29
-                  (provided (client/get post-resource
30
-                                        {:content-type :json}) => {:body "{\"signatory\":\"Alissa Evonne\"}"})))
31
-       )

+ 0 - 47
apps/petitions/decode-app/test/decode_app/petitions_test.clj

@@ -1,47 +0,0 @@
1
-(ns decode-app.petitions-test
2
-    (:require [midje.sweet :refer :all]
3
-      [decode-app.petitions :refer :all]
4
-      [decode-app.nodeApi :as nodeApi]
5
-      [decode-app.database :as db])
6
-    (:import org.bson.types.ObjectId))
7
-
8
-(future-facts "about petitions"
9
-       (fact "get-petitions returns the petitions as a map with swapped ids"
10
-             (let [id1 (str (ObjectId.)) id2 (str (ObjectId.)) id3 (str (ObjectId.))]
11
-                  (get-petitions) => [{:id (str id1) :title "title1" :isEthereum "true" :signatureCount 1}
12
-                                      {:id (str id2) :title "title2" :isEthereum "true" :signatureCount 10}
13
-                                      {:id (str id3) :title "title3" :isEthereum "false" :signatureCount 0}]
14
-                  (provided (db/find-all "petitions") => [{:id id1 :title "title1" :isEthereum "true"}
15
-                                                          {:id id2 :title "title2" :isEthereum "true"}
16
-                                                          {:id id3 :title "title3" :isEthereum "false"}])
17
-                  (provided (nodeApi/getSignatureCount id1) => {:signatureCount 1})
18
-                  (provided (nodeApi/getSignatureCount id2) => {:signatureCount 10})))
19
-
20
-       (fact "get-petition returns the petition as a map"
21
-             (let [id (str (ObjectId.))]
22
-                  (get-petition id) => {:id id :title "title" :signatureCount 2 :signatories ["Ted", "Jane"] :isEthereum "true"}
23
-                  (provided (db/find-by-id "petitions" id) => {:id id :title "title" :isEthereum "true"}
24
-                            (provided (nodeApi/getSignatories id) => {:signatories ["Ted", "Jane"]}))
25
-                  ))
26
-
27
-
28
-       (fact "add-signature adds a signature and the signatory to a petition and returns the updated petition with swapped id"
29
-             (let [id (str (ObjectId.))]
30
-                  (add-signature id "Bob") => {}
31
-                  (provided
32
-                    (nodeApi/sign id "Bob") => {})))
33
-
34
-       (fact "add-signature returns an error if database returns an error"
35
-             (let [id "invalid"]
36
-                  (add-signature id "Bob") => (throws Exception "Failed to increment by id")
37
-                  (provided
38
-                    (nodeApi/sign id "Bob") =throws=> (Exception. "Failed to increment by id")))))
39
-
40
-(facts "integration with node-api"
41
-       (future-fact "end to end test for signing petitions with node-api and mocked Mongo"
42
-             (let [petition-id "UUID0"
43
-                   signature-id "Bob"]
44
-                  (add-signature petition-id signature-id) => (contains {:body "Accepted"})
45
-                  (get-petition petition-id) => {:id petition-id :title "title" :signatureCount 1 :signatories [signature-id]}
46
-                  (provided (db/find-by-id "petitions" petition-id) => {:id petition-id :title "title"}))
47
-             ))