Skip to content
Permalink
Browse files

Test improvements and couple of bug fixes

  • Loading branch information...
Alex Simkin authored and SimY4 committed Apr 12, 2017
1 parent 63c7b4a commit c02eace426234ff62bcce0c3511da9a8c360bbdd
Showing with 39 additions and 24 deletions.
  1. +2 −2 rt-repl-bot/src/rt_repl_bot/handler.clj
  2. +37 −22 rt-repl-bot/test/rt_repl_bot/handler_test.clj
@@ -39,8 +39,8 @@
"REPL bot application"
(->
app-routes
(wrap-defaults api-defaults)
(instrument)
(wrap-json-body)
(wrap-json-response)
(expose-metrics-as-json)))
(expose-metrics-as-json)
(wrap-defaults api-defaults)))
@@ -7,44 +7,59 @@
(deftest handler-test

(testing "Test GET /info"
(let [response (mock/request :get "/info" nil)]
(let [response (app (mock/request :get "/info"))]
(is (= 200 (:status response)))
(is (= "application/json; charset=utf8" (get-in (:headers response) "Content-Type")))
(is (= "application/json; charset=utf-8" (get-in response [:headers "Content-Type"])))
(is (str/starts-with? (:body response) "{\"author\":\"Alex 'SimY4' Simkin\",\"info\":\"Clojure REPL bot\",\"commands\":"))))

(testing "Test POST /event should skip malformed JSON requests with 400 code"
(let [request (-> (mock/request :post "/event" "random body")
(mock/content-type "application/json"))
response (app request)]
(is (= 400 (:status response)))))

(testing "Test POST /event should skip random messages with 417 code"
(let [response (mock/request :post "/event" "random body")]
(is (= 417 (:status response)))
(is (= "application/json; charset=utf8" (get-in (:headers response) "Content-Type"))))
(let [response (mock/request :post "/event" {"text" "randon text"})]
(let [request (-> (mock/request :post "/event" "{\"text\": \"randon text\"}")
(mock/content-type "application/json"))
response (app request)]
(is (= 417 (:status response)))
(is (= "application/json; charset=utf8" (get-in (:headers response) "Content-Type")))))
(is (= "application/json; charset=utf-8" (get-in response [:headers "Content-Type"])))))

(testing "Test POST /event should evaluate clojure commands and respond with 201 code"
(let [response (mock/request :post "/event" {"text" "clj> (+ 1 1)"})]
(let [request (-> (mock/request :post "/event" "{\"text\": \"clj> (+ 1 1)\"}")
(mock/content-type "application/json"))
response (app request)]
(is (= 201 (:status response)))
(is (= "application/json; charset=utf8" (get-in (:headers response) "Content-Type")))
(is (= "application/json; charset=utf-8" (get-in response [:headers "Content-Type"])))
(is (= "{\"text\":\"```\\n2\\n```\",\"bot\":\"REPL-bot\"}" (:body response))))
(let [response (mock/request :post "/event" {"text" "clj>"})]
(let [request (-> (mock/request :post "/event" "{\"text\": \"clj>\"}")
(mock/content-type "application/json"))
response (app request)]
(is (= 201 (:status response)))
(is (= "application/json; charset=utf8" (get-in (:headers response) "Content-Type")))
(is (= "application/json; charset=utf-8" (get-in response [:headers "Content-Type"])))
(is (= "{\"text\":\"```\\nEOF while reading\\n```\",\"bot\":\"REPL-bot\"}" (:body response))))
(let [response (mock/request :post "/event" {"text" "clj>(def var1 5)"})]
(let [request (-> (mock/request :post "/event" "{\"text\": \"clj>(def var1 5)\"}")
(mock/content-type "application/json"))
response (app request)]
(is (= 201 (:status response)))
(is (= "application/json; charset=utf8" (get-in (:headers response) "Content-Type")))
(is (= "application/json; charset=utf-8" (get-in response [:headers "Content-Type"])))
(is (re-matches #"\{\"text\":\"```\\n#'sandbox[0-9]{1,4}/var1\\n```\",\"bot\":\"REPL-bot\"\}" (:body response))))
(let [response (mock/request :post "/event" {"text" "clj> (while true)"})]
(let [request (-> (mock/request :post "/event" "{\"text\": \"clj> (while true)\"}")
(mock/content-type "application/json"))
response (app request)]
(is (= 201 (:status response)))
(is (= "application/json; charset=utf8" (get-in (:headers response) "Content-Type")))
(is (= "application/json; charset=utf-8" (get-in response [:headers "Content-Type"])))
(is (= "{\"text\":\"```\\nEvaluation timed out in 5 seconds\\n```\",\"bot\":\"REPL-bot\"}" (:body response))))
(let [response (mock/request :post "/event" {"text" "clj> (+ var1 5)"})]
(let [request (-> (mock/request :post "/event" "{\"text\": \"clj> (+ var1 5)\"}")
(mock/content-type "application/json"))
response (app request)]
(is (= 201 (:status response)))
(is (= "application/json; charset=utf8" (get-in (:headers response) "Content-Type")))
(is (= "application/json; charset=utf-8" (get-in response [:headers "Content-Type"])))
(is (= "{\"text\":\"```\\n10\\n```\",\"bot\":\"REPL-bot\"}" (:body response)))))

(testing "Test 404"
(is (= 404 (:status (mock/request :post "/info" nil))))
(is (= 404 (:status (mock/request :post "/heathcheck" nil))))
(is (= 404 (:status (mock/request :get "/event" {"text" "clj>(+1 1)"}))))
(is (= 404 (:status (mock/request :get "/" nil))))
(is (= 404 (:status (mock/request :get "/random" nil))))))
(is (= 404 (:status (app (mock/request :post "/info")))))
(is (= 404 (:status (app (mock/request :post "/heathcheck")))))
(is (= 404 (:status (app (mock/request :get "/event" "{\"text\": \"clj> (+1 1)\"}")))))
(is (= 404 (:status (app (mock/request :get "/")))))
(is (= 404 (:status (app (mock/request :get "/random")))))))

0 comments on commit c02eace

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.