<commit_before>(ns stonecutter.integration.kerodon
  (:require [midje.sweet :refer :all]
            [kerodon.core :as k]
            [stonecutter.handler :as h]
            [stonecutter.storage :as s]
            [stonecutter.logging :as l]
            [stonecutter.view.register :refer [registration-form]] 
            [net.cgrand.enlive-html :as html]))

(l/init-logger!)

(defn page-title [state]
  (-> state :enlive (html/select [:title]) first html/text))

(defn page-title-is [state title]
  (fact {:midje/name "Checking page title:"}
       (page-title state) => title)
  state)

(defn page-uri-is [state uri]
  (fact {:midje/name "Checking page uri:"}
        (-> state :request :uri) => uri)
  state)

(defn selector-has-content [state selector content]
  (fact {:midje/name "Check content of element"}
        (-> state :enlive (html/select selector) first html/text) => content)
  state)

(defn print-enlive [state]
  (prn (-> state :enlive))
  state)

(s/setup-in-memory-stores!)

(facts "Home url redirects to sign-in page if user is not signed in"
       (-> (k/session h/app)
           (k/visit "/")
           (k/follow-redirect)
           (page-title-is "Sign in")
           (page-uri-is "/sign-in")))

(facts "User is returned to same page when email is invalid"
       (-> (k/session h/app)
           (k/visit "/register")
           (k/fill-in "Email address" "invalid-email")
           (k/press :.func--create-profile__button)
           (page-uri-is "/register")
           (selector-has-content [:.clj--registration-email__validation] "Enter a valid email address")))

(facts "User is taken to success page when user is successfully created"
       (-> (k/session h/app)
           (k/visit "/register")
           (k/fill-in :.func--email__input "<EMAIL>")
           (k/fill-in :.func--password__input "<PASSWORD>")
           (k/fill-in :.func--confirm-password__input "<PASSWORD>")
           (k/press :.func--create-profile__button)
           (page-uri-is "/register")
           (selector-has-content [:body] "You saved the user")))

(facts "User is redirected to sign-in page when accessing profile page not signed in"
       (-> (k/session h/app)
           (k/visit "/profile")
           (k/follow-redirect)
           (page-uri-is "/sign-in")))

(facts "User can sign in"
       (-> (k/session h/app)
           (k/visit "/login")
           (k/fill-in :.func--email__input "<EMAIL>")
           (k/fill-in :.func--password__input "<PASSWORD>")
           (k/press :.func--sign-in__button)
           (k/follow-redirect)
           (page-uri-is "/profile")
           (selector-has-content [:body] "You are signed in as <EMAIL>")))

(facts "Home url redirects to profile page if user is signed in"
       (-> (k/session h/app)
           (k/visit "/sign-in")
           (k/fill-in :.func--email__input "<EMAIL>")
           (k/fill-in :.func--password__input "<PASSWORD>")
           (k/press :.func--sign-in__button)
           (k/visit "/")
           (k/follow-redirect)
           (page-uri-is "/profile")))

(facts "Not found page is shown for unknown url"<commit_msg>AB & RS | Change /sign-in to /login in kerodon tests<commit_after>(ns stonecutter.integration.kerodon
  (:require [midje.sweet :refer :all]
            [kerodon.core :as k]
            [stonecutter.handler :as h]
            [stonecutter.storage :as s]
            [stonecutter.logging :as l]
            [stonecutter.view.register :refer [registration-form]] 
            [net.cgrand.enlive-html :as html]))

(l/init-logger!)

(defn page-title [state]
  (-> state :enlive (html/select [:title]) first html/text))

(defn page-title-is [state title]
  (fact {:midje/name "Checking page title:"}
       (page-title state) => title)
  state)

(defn page-uri-is [state uri]
  (fact {:midje/name "Checking page uri:"}
        (-> state :request :uri) => uri)
  state)

(defn selector-has-content [state selector content]
  (fact {:midje/name "Check content of element"}
        (-> state :enlive (html/select selector) first html/text) => content)
  state)

(defn print-enlive [state]
  (prn (-> state :enlive))
  state)

(s/setup-in-memory-stores!)

(facts "Home url redirects to sign-in page if user is not signed in"
       (-> (k/session h/app)
           (k/visit "/")
           (k/follow-redirect)
           (page-title-is "Sign in")
           (page-uri-is "/login")))

(facts "User is returned to same page when email is invalid"
       (-> (k/session h/app)
           (k/visit "/register")
           (k/fill-in "Email address" "invalid-email")
           (k/press :.func--create-profile__button)
           (page-uri-is "/register")
           (selector-has-content [:.clj--registration-email__validation] "Enter a valid email address")))

(facts "User is taken to success page when user is successfully created"
       (-> (k/session h/app)
           (k/visit "/register")
           (k/fill-in :.func--email__input "<EMAIL>")
           (k/fill-in :.func--password__input "<PASSWORD>")
           (k/fill-in :.func--confirm-password__input "<PASSWORD>")
           (k/press :.func--create-profile__button)
           (page-uri-is "/register")
           (selector-has-content [:body] "You saved the user")))

(facts "User is redirected to sign-in page when accessing profile page not signed in"
       (-> (k/session h/app)
           (k/visit "/profile")
           (k/follow-redirect)
           (page-uri-is "/login")))

(facts "User can sign in"
       (-> (k/session h/app)
           (k/visit "/login")
           (k/fill-in :.func--email__input "<EMAIL>")
           (k/fill-in :.func--password__input "<PASSWORD>")
           (k/press :.func--sign-in__button)
           (k/follow-redirect)
           (page-uri-is "/profile")
           (selector-has-content [:body] "You are signed in as <EMAIL>")))

(facts "Home url redirects to profile page if user is signed in"
       (-> (k/session h/app)
           (k/visit "/login")
           (k/fill-in :.func--email__input "<EMAIL>")
           (k/fill-in :.func--password__input "<PASSWORD>")
           (k/press :.func--sign-in__button)
           (k/visit "/")
           (k/follow-redirect)
           (page-uri-is "/profile")))

(facts "Not found page is shown for unknown url"
{'fraction_non_alphanumeric': 0.1596678907871627, 'fraction_numerical': 0.0, 'mean_word_length': 2.1684370257966616, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5513504', 'n_tokens_mistral': 2056, 'n_tokens_neox': 2029, 'n_words': 529}