<commit_before>(ns org.akvo.flow-api.end-to-end
  (:require [clojure.test :refer [use-fixtures deftest testing is]]
            [clj-http.client]
            [org.akvo.flow-api.fixtures :as fixtures])
  (:import clojure.lang.ExceptionInfo))

(use-fixtures :once (fn [f]
                      (fixtures/check-servers-up)
                      (f)))

(deftest huge-headers
  (testing "Keycloak Bearer token header is bigger than 8k"
    (is (= 200
          (:status (clj-http.client/get "http://localhost:3000/"
                     {:as :json
                      :headers {"huge" (apply str (repeat 30000 "x"))}
                      :content-type :json}))))))

(deftest answer-stats
  (testing "Random email trying to get a statistic"
    (is (thrown-with-msg? ExceptionInfo #"status 403"
        (try
          (clj-http.client/get "http://localhost:3000/orgs/akvoflowsandbox/stats"
                               {:as :json
                                :headers {"x-akvo-email" "<EMAIL>"}
                                :query-params {:survey_id "148412306"
                                               :form_id "145492013"
                                               :question_id "147432013"}
                                :content-type :json})
          (catch ExceptionInfo e
            (is (= (-> e ex-data :status) 403))
            (is (.contains ^String (-> e ex-data :body) "User does not exist"))
            (throw e))))))
  (testing "Survey Not Found"
    (is (thrown-with-msg? ExceptionInfo #"status 404"
                          (try
                            (clj-http.client/get "http://localhost:3000/orgs/akvoflowsandbox/stats"
                                                 {:as :json
                                                  :headers {"x-akvo-email" "<EMAIL>"}
                                                  :query-params {:survey_id "999"
                                                                 :form_id "145492013"
                                                                 :question_id "147432013"}
                                                  :content-type :json})
                            (catch ExceptionInfo e
                              (is (= (-> e ex-data :status) 404))
                              (is (.contains ^String (-> e ex-data :body) "Survey not found"))
                              (throw e))))))
  (testing "Question Not Found"
    (is (thrown-with-msg? ExceptionInfo #"status 404"
                          (try
                            (clj-http.client/get "http://localhost:3000/orgs/akvoflowsandbox/stats"
                                                 {:as :json
                                                  :headers {"x-akvo-email" "<EMAIL>"}
                                                  :query-params {:survey_id "148412306"
                                                                 :form_id "145492013"
                                                                 :question_id "999"}
                                                  :content-type :json})
                            (catch ExceptionInfo e
                              (is (= (-> e ex-data :status) 404))
                              (is (.contains ^String (-> e ex-data :body) "Question not found"))
                              (throw e))))))
  (testing "Question is not an Option Question"
    (is (thrown-with-msg? ExceptionInfo #"status 400"
                          (try
                            (clj-http.client/get "http://localhost:3000/orgs/akvoflowsandbox/stats"
                                                 {:as :json
                                                  :headers {"x-akvo-email" "<EMAIL>"}
                                                  :query-params {:survey_id "148412306"
                                                                 :form_id "145492013"
                                                                 :question_id "148442013"}
                                                  :content-type :json})
                            (catch ExceptionInfo e
                              (is (= (-> e ex-data :status) 400))
                              (prn (-> e ex-data :body))
                              (is (.contains ^String (-> e ex-data :body) "Not an OPTION question"))
                              (throw e))))))
  (testing "Answer summary of an option type of question"
    (let [response (clj-http.client/get "http://mainnetwork:3000/orgs/akvoflowsandbox/stats"
                     {:as :json
                      :headers {"x-akvo-email" "<EMAIL>"}
                      :query-params {:survey_id "148412306"
                                     :form_id "145492013"
                                     :question_id "147432013"}
                      :content-type :json})]
      (is (= 200 (:status response)))
      (is (= 2 (-> response :body :Kpalbe))))))

(deftest form-instances
  (testing "Submission date filter is optional"
    (let [response (clj-http.client/get "http://mainnetwork:3000/orgs/akvoflowsandbox/form_instances"
                     {:as :json
                      :headers {"x-akvo-email" "<EMAIL>"}
                      :query-params {:survey_id "152342023"
                                     :form_id "146532016"
                                     :page_size 2}
                      :content-type :json})]
      (is (= 200 (:status response)))
      (is (= 2 (-> response :body :formInstances count))))))

(deftest data-point
  (testing "Datapoint"
    (let [response (clj-http.client/get "http://mainnetwork:3000/orgs/akvoflowsandbox/data_points"
                     {:as :json
                      :headers {"x-akvo-email" "<EMAIL>"}
                      :query-params {:survey_id "152342023"<commit_msg>[#244] Extract code to function<commit_after>(ns org.akvo.flow-api.end-to-end
  (:require [cheshire.core :as json]
            [clj-http.client]
            [clojure.test :refer [use-fixtures deftest testing is]]
            [org.akvo.flow-api.fixtures :as fixtures])
  (:import clojure.lang.ExceptionInfo))

(use-fixtures :once (fn [f]
                      (fixtures/check-servers-up)
                      (f)))

(deftest huge-headers
  (testing "Keycloak Bearer token header is bigger than 8k"
    (is (= 200
          (:status (clj-http.client/get "http://localhost:3000/"
                     {:as :json
                      :headers {"huge" (apply str (repeat 30000 "x"))}
                      :content-type :json}))))))

(defn http-result [^ExceptionInfo e]
  (-> e
      ex-data
      (select-keys [:status :body])
      (update :body #(json/parse-string % true))))

(defn test-error [expected-status expected-message http-url http-opts]
  (is (thrown-with-msg?
       ExceptionInfo (re-pattern (str "status " expected-status))
       (try
         (clj-http.client/get http-url
                              (merge {:as :json
                                      :content-type :json}
                                     http-opts))
         (catch ExceptionInfo e
           (let [{{message :message} :body
                  :keys [status]} (http-result e)]
             (is (= expected-status status))
             (is (= expected-message message)))
           (throw e))))))

(deftest answer-stats
  (testing "Random email trying to get a statistic"
    (test-error 403
                "User does not exist"
                "http://localhost:3000/orgs/akvoflowsandbox/stats"
                {:headers {"x-akvo-email" "<EMAIL>"}
                 :query-params {:survey_id "148412306"
                                :form_id "145492013"
                                :question_id "147432013"}}))
  (testing "Survey Not Found"
    (test-error 404
                "Survey not found"
                "http://localhost:3000/orgs/akvoflowsandbox/stats"
                {:headers {"x-akvo-email" "<EMAIL>"}
                 :query-params {:survey_id "999"
                                :form_id "145492013"
                                :question_id "147432013"}}))
  (testing "Question Not Found"
    (test-error 404
                "Question not found"
                "http://localhost:3000/orgs/akvoflowsandbox/stats"
                {:headers {"x-akvo-email" "<EMAIL>"}
                 :query-params {:survey_id "148412306"
                                :form_id "145492013"
                                :question_id "999"}}))
  (testing "Question is not an Option Question"
    (test-error 400
                "Not an OPTION question"
                "http://localhost:3000/orgs/akvoflowsandbox/stats"
                {:headers {"x-akvo-email" "<EMAIL>"}
                 :query-params {:survey_id "148412306"
                                :form_id "145492013"
                                :question_id "148442013"}}))
  (testing "Answer summary of an option type of question"
    (let [response (clj-http.client/get "http://mainnetwork:3000/orgs/akvoflowsandbox/stats"
                     {:as :json
                      :headers {"x-akvo-email" "<EMAIL>"}
                      :query-params {:survey_id "148412306"
                                     :form_id "145492013"
                                     :question_id "147432013"}
                      :content-type :json})]
      (is (= 200 (:status response)))
      (is (= 2 (-> response :body :Kpalbe))))))

(deftest form-instances
  (testing "Submission date filter is optional"
    (let [response (clj-http.client/get "http://mainnetwork:3000/orgs/akvoflowsandbox/form_instances"
                     {:as :json
                      :headers {"x-akvo-email" "<EMAIL>"}
                      :query-params {:survey_id "152342023"
                                     :form_id "146532016"
                                     :page_size 2}
                      :content-type :json})]
      (is (= 200 (:status response)))
      (is (= 2 (-> response :body :formInstances count))))))

(deftest data-point
  (testing "Datapoint"
    (let [response (clj-http.client/get "http://mainnetwork:3000/orgs/akvoflowsandbox/data_points"
                     {:as :json
                      :headers {"x-akvo-email" "<EMAIL>"}
                      :query-params {:survey_id "152342023"
{'fraction_non_alphanumeric': 0.11867534233271827, 'fraction_numerical': 0.04263377682820239, 'mean_word_length': 1.181779661016949, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 56, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2552468', 'n_tokens_mistral': 3108, 'n_tokens_neox': 2668, 'n_words': 618}