<commit_before>(ns objective8.api.tokens-integration-tests
  (:require [midje.sweet :refer :all]
            [peridot.core :as p]
            [cheshire.core :as json]
            [objective8.integration-helpers :as helpers]
            [objective8.storage.storage :as storage]
            [objective8.storage.database :as db]
            [objective8.bearer-tokens :as bearer-tokens]))

(defn db-connection [] (db/connect! db/postgres-spec))

(def app (helpers/test-context))

(facts "Bearer token tests" :integration
       (against-background [(before :contents (db-connection)) (after :facts (helpers/truncate-tables))]
       (fact "api is protected by bearer-tokens"
             ; Temporarily using stub-token-provider so stored tokens not required.  Will be fixed during #47.
             (let [the-token "some-secure-token"
                   the-bearer "objective8.dev"] 
               (storage/pg-store! {:entity :bearer-token :bearer-name the-bearer :bearer-token the-token})
               (p/request app "/api/v1/users"
                          :request-method :post
                          :content-type "application/json"
                          :headers {"api-bearer-token" "some-wrong-token"
                                    "api-bearer-name" "objective8.dev"}
                          :body (json/generate-string {:twitter-id "Twitter_ID"})) 
               => (contains {:response (contains  {:status 401})}))))) <commit_msg>DM + CM #47 - Test that protected api resources are accessible with correct credentials<commit_after>(ns objective8.api.tokens-integration-tests
  (:require [midje.sweet :refer :all]
            [peridot.core :as p]
            [cheshire.core :as json]
            [objective8.integration-helpers :as helpers]
            [objective8.storage.storage :as storage]
            [objective8.storage.database :as db]
            [objective8.bearer-tokens :as bearer-tokens]))

(defn db-connection [] (db/connect! db/postgres-spec))

(def app (helpers/test-context))

(facts "Bearer token tests" :integration
       (against-background [(before :contents (db-connection)) (after :facts (helpers/truncate-tables))]
                           (fact "can't access protected api resource without valid bearer-token"
                                 ; Temporarily using stub-token-provider so stored tokens not required.  Will be fixed during #47.
                                 (let [the-token "some-<PASSWORD>"
                                       the-bearer "<PASSWORD>"]
                                   (storage/pg-store! {:entity :bearer-token :bearer-name the-bearer :bearer-token the-token})
                                   (p/request app "/api/v1/users"
                                              :request-method :post
                                              :content-type "application/json"
                                              :headers {"api-bearer-token" "some-wrong-token"
                                                        "api-bearer-name" "<PASSWORD>"}
                                              :body (json/generate-string {:twitter-id "Twitter_ID"}))
                                   => (contains {:response (contains  {:status 401})})))
                           (fact "can access protected api resource with valid bearer-token"
                                 ; Temporarily using stub-token-provider so stored tokens not required.  Will be fixed during #47.
                                 (let [the-token "some-secure-token"
                                       the-bearer "<PASSWORD>"]
                                   (storage/pg-store! {:entity :bearer-token :bearer-name the-bearer :bearer-token the-token})
                                   (p/request app "/api/v1/users"
                                              :request-method :post
                                              :content-type "application/json"
                                              :headers {"api-bearer-token" "token"
                                                        "api-bearer-name" "bearer"}
                                              :body (json/generate-string {:twitter-id "Twitter_ID"}))
                                   => (contains {:response (contains  {:status 201})})))))
{'fraction_non_alphanumeric': 0.10904007455731593, 'fraction_numerical': 0.007455731593662628, 'mean_word_length': 1.4050420168067226, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 33, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3830480', 'n_tokens_mistral': 1087, 'n_tokens_neox': 1032, 'n_words': 260}