<commit_before>(ns freecoin.test.handlers.participant-query
  (:require [midje.sweet :refer :all]
            [net.cgrand.enlive-html :as html]
            [ring.mock.request :as rmr]
            [freecoin.db.uuid :as uuid]
            [freecoin.db.mongo :as fm]
            [freecoin.db.wallet :as w]
            [freecoin.blockchain :as fb]
            [freecoin.test.test-helper :as th]
            [freecoin.handlers.participants :as fp]))

(defn create-wallet [wallet-store blockchain wallet-data]
  (let [{:keys [sso-id name email]} wallet-data]
    (:wallet (w/new-empty-wallet! wallet-store blockchain uuid/uuid sso-id name email))))

(defn index->wallet-data [index]
  (let [sso-id (str "sso-id-" index)
        name (str "name-" index)
        email (str "wallet-" index "@email.com")]
    {:sso-id sso-id :name name :email email}))

(facts "about the account page"
       (fact "displays the signed-in participant's balance"
             (let [wallet-store (fm/create-memory-store)
                   blockchain (fb/create-in-memory-blockchain :bk)
                   wallet (:wallet (w/new-empty-wallet! wallet-store blockchain uuid/uuid
                                                        "stonecutter-user-id" "name" "<EMAIL>"))
                   account-page-handler (fp/account wallet-store blockchain)
                   response (account-page-handler (-> (rmr/request :get "/account/")
                                                      (assoc :params {:uid (:uid wallet)})
                                                      (assoc :session {:signed-in-uid (:uid wallet)})))]
               (:status response) => 200
               (:body response) => (contains #"Balance:"))))

;;        (fact "can not be accessed when user is not signed in"))

(facts "about the participant query form"
       (fact "can be accessed by signed-in users"
             (let [wallet-store (fm/create-memory-store)
                   blockchain (fb/create-in-memory-blockchain :bk)
                   wallet (:wallet (w/new-empty-wallet! wallet-store blockchain uuid/uuid
                                                        "stonecutter-user-id" "name" "<EMAIL>"))
                   query-form-handler (fp/query-form wallet-store)
                   response (-> (th/create-request
                                 :get "/participants-query"
                                 {} (th/authenticated-session (:uid wallet)))
                                query-form-handler)]
               (:status response) => 200))

       (fact "can not be accessed when user is not signed in"
             (let [wallet-store (fm/create-memory-store)
                   query-form-handler (fp/query-form wallet-store)
                   response (-> (th/create-request :get "/participants-query" {})
                                query-form-handler)]
               (:status response) => 401)))

(facts "about the participants query handler"
       (fact "can not be accessed when user is not signed in"
             (let [wallet-store (fm/create-memory-store)
                   participants-handler (fp/participants wallet-store)
                   response (-> (th/create-request :get "/participants" {})
                                participants-handler)]
               (:status response) => 401))
       
       (fact "without any query parameters, lists all participants"
             (let [wallet-store (fm/create-memory-store)
                   blockchain (fb/create-in-memory-blockchain :bk)
                   wallets (doall (->> (range 5)
                                       (map index->wallet-data)
                                       (map (partial create-wallet wallet-store blockchain))))
                   participants-handler (fp/participants wallet-store)
                   response (-> (th/create-request
                                 :get "/participants"
                                 {} (th/authenticated-session (:uid (first wallets))))
                                participants-handler)]
               (:status response) => 200
               (-> (:body response) html/html-snippet) => (th/element-count [:.clj--participant__item] 5)))

       (tabular
        
        (fact "can query by name or email"
              (let [wallet-store (fm/create-memory-store)
                    blockchain (fb/create-in-memory-blockchain :bk)
                    wallet-data [{:name "<NAME>" :email "<EMAIL>" :sso-id "sso-id-1"}
                                 {:name "<NAME>" :email "<EMAIL>" :sso-id "sso-id-2"}
                                 {:name "<NAME>" :email "<EMAIL>" :sso-id "sso-id-3"}]
                    wallets (doall (map (partial create-wallet wallet-store blockchain) wallet-data))
                    participants-handler (fp/participants wallet-store)
                    query-m {:field ?field :value ?value}
                    response (-> (th/create-request
                                  :get "/participants"
                                  query-m (th/authenticated-session (:uid (first wallets))))
                                 participants-handler)]
                (-> (:body response) html/html-snippet)
                => (th/element-count [:.clj--participant__item] ?expected-count)))
        
        ?field   ?value                  ?expected-count
        "name"   "<NAME>"           2
        "email"  "<EMAIL>"       1
        "fake"   "whatever"              0))

(future-facts "Query parameters are validated")<commit_msg>fixed test to check translated balance string<commit_after>(ns freecoin.test.handlers.participant-query
  (:require [midje.sweet :refer :all]
            [net.cgrand.enlive-html :as html]
            [ring.mock.request :as rmr]
            [freecoin.db.uuid :as uuid]
            [freecoin.db.mongo :as fm]
            [freecoin.db.wallet :as w]
            [freecoin.blockchain :as fb]
            [freecoin.translation :as t]
            [freecoin.test.test-helper :as th]
            [freecoin.handlers.participants :as fp]))

(defn create-wallet [wallet-store blockchain wallet-data]
  (let [{:keys [sso-id name email]} wallet-data]
    (:wallet (w/new-empty-wallet! wallet-store blockchain uuid/uuid sso-id name email))))

(defn index->wallet-data [index]
  (let [sso-id (str "sso-id-" index)
        name (str "name-" index)
        email (str "wallet-" index "@email.com")]
    {:sso-id sso-id :name name :email email}))

(facts "about the account page"
       (fact "displays the signed-in participant's balance"
             (let [wallet-store (fm/create-memory-store)
                   blockchain (fb/create-in-memory-blockchain :bk)
                   wallet (:wallet (w/new-empty-wallet! wallet-store blockchain uuid/uuid
                                                        "stonecutter-user-id" "name" "<EMAIL>"))
                   account-page-handler (fp/account wallet-store blockchain)
                   response (account-page-handler (-> (rmr/request :get "/account/")
                                                      (assoc :params {:uid (:uid wallet)})
                                                      (assoc :session {:signed-in-uid (:uid wallet)})))]
               (:status response) => 200
               (:body response) => (contains (t/locale [:wallet :balance]))
               )))

;;        (fact "can not be accessed when user is not signed in"))

(facts "about the participant query form"
       (fact "can be accessed by signed-in users"
             (let [wallet-store (fm/create-memory-store)
                   blockchain (fb/create-in-memory-blockchain :bk)
                   wallet (:wallet (w/new-empty-wallet! wallet-store blockchain uuid/uuid
                                                        "stonecutter-user-id" "name" "<EMAIL>"))
                   query-form-handler (fp/query-form wallet-store)
                   response (-> (th/create-request
                                 :get "/participants-query"
                                 {} (th/authenticated-session (:uid wallet)))
                                query-form-handler)]
               (:status response) => 200))

       (fact "can not be accessed when user is not signed in"
             (let [wallet-store (fm/create-memory-store)
                   query-form-handler (fp/query-form wallet-store)
                   response (-> (th/create-request :get "/participants-query" {})
                                query-form-handler)]
               (:status response) => 401)))

(facts "about the participants query handler"
       (fact "can not be accessed when user is not signed in"
             (let [wallet-store (fm/create-memory-store)
                   participants-handler (fp/participants wallet-store)
                   response (-> (th/create-request :get "/participants" {})
                                participants-handler)]
               (:status response) => 401))

       (fact "without any query parameters, lists all participants"
             (let [wallet-store (fm/create-memory-store)
                   blockchain (fb/create-in-memory-blockchain :bk)
                   wallets (doall (->> (range 5)
                                       (map index->wallet-data)
                                       (map (partial create-wallet wallet-store blockchain))))
                   participants-handler (fp/participants wallet-store)
                   response (-> (th/create-request
                                 :get "/participants"
                                 {} (th/authenticated-session (:uid (first wallets))))
                                participants-handler)]
               (:status response) => 200
               (-> (:body response) html/html-snippet) => (th/element-count [:.clj--participant__item] 5)))

       (tabular

        (fact "can query by name or email"
              (let [wallet-store (fm/create-memory-store)
                    blockchain (fb/create-in-memory-blockchain :bk)
                    wallet-data [{:name "<NAME>" :email "<EMAIL>" :sso-id "sso-id-1"}
                                 {:name "<NAME>" :email "<EMAIL>" :sso-id "sso-id-2"}
                                 {:name "<NAME>" :email "<EMAIL>" :sso-id "sso-id-3"}]
                    wallets (doall (map (partial create-wallet wallet-store blockchain) wallet-data))
                    participants-handler (fp/participants wallet-store)
                    query-m {:field ?field :value ?value}
                    response (-> (th/create-request
                                  :get "/participants"
                                  query-m (th/authenticated-session (:uid (first wallets))))
                                 participants-handler)]
                (-> (:body response) html/html-snippet)
                => (th/element-count [:.clj--participant__item] ?expected-count)))

        ?field   ?value                  ?expected-count
        "name"   "<NAME>"           2
        "email"  "<EMAIL>"       1
        "fake"   "whatever"              0))

(future-facts "Query parameters are validated")
{'fraction_non_alphanumeric': 0.12279279279279279, 'fraction_numerical': 0.004144144144144144, 'mean_word_length': 1.4521758338855755, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1685412', 'n_tokens_mistral': 2998, 'n_tokens_neox': 2842, 'n_words': 732}