File: test_core.clj

package info (click to toggle)
rbac-client-clojure 1.1.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 288 kB
  • sloc: sh: 61; makefile: 27; xml: 11
file content (56 lines) | stat: -rw-r--r-- 2,889 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
(ns puppetlabs.rbac-client.test-core
  (:require [clojure.test :refer [deftest testing is]]
            [puppetlabs.rbac-client.core :as core]
            [puppetlabs.trapperkeeper.testutils.webserver :refer [with-test-webserver]]
            [puppetlabs.trapperkeeper.testutils.logging :refer [with-test-logging]]
            [puppetlabs.http.client.sync :refer [create-client]]
            [puppetlabs.rbac-client.test-server :as test-server]
            [slingshot.test]))

(deftest test-api-caller
  (with-test-logging
    (let [client (create-client {})
          app (constantly {:status 200 :body "ok"})
          port 38924]
      (with-test-webserver app port
        (is (= "ok"
               (:body (core/api-caller client (format "http://localhost:%s/" port) :get ""))))))

    (let [client (create-client {})
          app (constantly {:status 500 :body "server error"})
          port 38924]
      (with-test-webserver app port
        (is (= "server error" (:body (core/api-caller client (format "http://localhost:%s/" port) :get ""))))
        (is (thrown+? [:kind :puppetlabs.rbac-client/status-error]
                      (:body (core/api-caller client (format "http://localhost:%s/" port) :get "" {:status-errors true}))))))

    (let [client (create-client {})
          app (constantly {:status 500 :body "ok"})
          port 38924]
      (is (thrown+? [:kind :puppetlabs.rbac-client/connection-failure]
                    (:body (core/api-caller client (format "http://localhost:%s/" port) :get "")))))))

(deftest test-json-api-caller
  (with-test-logging
    (let [client (create-client {})
          app (test-server/make-json-handler {:status 200
                                              :body {:foo 1 :bar {:baz 2}}})
          port 38924]
      (with-test-webserver app port
        (let [response (core/json-api-caller client (format "http://localhost:%s/foo" port) :get "/bar?p=1")]
          (is (= 200 (:status response)))
          (is (= 1 (get-in response [:body :foo])))
          (is (= "application/json" (get-in response [:body :_request :headers :accept])))
          (is (= "/foo/bar" (get-in response [:body :_request :uri])))
          (is (= "1" (get-in response [:body :_request :params :p]))))))

    (let [client (create-client {})
          app (test-server/make-json-handler {:status 400
                                              :body {:kind :invalid
                                                     :msg "oops"}})
          port 38924]
      (with-test-webserver app port
        (is (thrown+? [:kind :invalid]
                      (core/json-api-caller client (format "http://localhost:%s/" port) :get "" {:throw-body true})))
        (is (thrown+? [:kind :puppetlabs.rbac-client/status-error]
                      (core/json-api-caller client (format "http://localhost:%s/" port) :get "" {:status-errors true})))))))