Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #54 from duck1123/feature/route-testing
Convert all route tests to match the same pattern
  • Loading branch information
duck1123 committed Apr 15, 2016
2 parents ea4de72 + fa55eaa commit d4a9af9
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 130 deletions.
2 changes: 2 additions & 0 deletions src/jiksnu/modules/web/routes/client_routes.clj
Expand Up @@ -79,6 +79,8 @@
;; :post! (fn [ctx] {:data (some-> ctx :request :params actions.client/register)})
:handle-created :data)

;; =============================================================================

(defgroup jiksnu oauth
:name "OAuth API"
:url "/oauth")
Expand Down
11 changes: 11 additions & 0 deletions src/jiksnu/modules/web/routes/home_routes.clj
Expand Up @@ -41,6 +41,13 @@
:available-media-types ["text/html"]
:exists? (fn [ctx] {:data (str @resources )}))

(defresource root :rsd
:name "Really Simple Discovery"
:url "/rsd.xml"
:mixins [item-resource]
:available-media-types ["application/xml"]
:exists? (fn [ctx] {:data (site/rsd)}))

(defresource root :settings
:url "/main/settings"
:name "Settings Page"
Expand All @@ -49,6 +56,10 @@
:doc {:get {:nickname "settings-page"
:summary "Settings Page"}})

(defresource root :oembed
:name "OEmbed"
:url "/main/oembed")

;; =============================================================================

(defgroup jiksnu api
Expand Down
6 changes: 6 additions & 0 deletions src/jiksnu/modules/web/routes/subscription_routes.clj
Expand Up @@ -47,3 +47,9 @@
(let [id (-> ctx :request :route-params :_id)]
(when-let [subscription (model.subscription/fetch-by-id id)]
{:data subscription}))))

(defgroup jiksnu ostatus
:url "/main"
:name "OStatus")

(defresource ostatus :sub)
38 changes: 38 additions & 0 deletions test/jiksnu/modules/core/views/activity_views_test.clj
@@ -0,0 +1,38 @@
(ns jiksnu.modules.web.routes.activity-routes-test
(:require [ciste.sections.default :refer [full-uri]]
[clj-factory.core :refer [fseq]]
[clojure.data.json :as json]
[clojurewerkz.support.http.statuses :as status]
[jiksnu.mock :as mock]
[jiksnu.model.activity :as model.activity]
jiksnu.modules.web.routes.activity-routes
[jiksnu.routes-helper :refer [as-user json-response response-for]]
[jiksnu.test-helper :as th]
[jiksnu.util :as util]
[midje.sweet :refer :all]
[ring.mock.request :as req]
[jiksnu.actions.activity-actions :as actions.activity]))

(future-fact "apply-view #'actions.activity/oembed [:http :json]"
(let [action #'actions.activity/oembed]
(with-context [:http :json]
(let [activity (mock/there-is-an-activity)
request {:params {:url (:id activity)}
:action action}
response (filter-action action request)]
(apply-view request response) =>
(contains {:status status/success?
:body (contains {:title (:title activity)})})))))

(future-fact "apply-view #'actions.activity/oembed [:http :xml]"
(let [action #'actions.activity/oembed]
(with-context [:http :xml]
(let [activity (mock/there-is-an-activity)
request {:params {:url (:id activity)}
:action action}
item {} #_(filter-action action request)]
(let [response (apply-view request item)]
(let [body (:body response)]
response => map?
(:status response) => status/success?
body =not=> string?))))))
49 changes: 1 addition & 48 deletions test/jiksnu/modules/web/routes/activity_routes_test.clj
Expand Up @@ -16,7 +16,7 @@
(th/module-test ["jiksnu.modules.core"
"jiksnu.modules.web"])

(facts "route: activities-api/item :delete"
(fact "route: activities-api/item :delete"
(fact "when authenticated"
(let [user (mock/a-user-exists)
activity (mock/there-is-an-activity {:user user})
Expand All @@ -32,50 +32,3 @@
request (req/request :delete url)]
(response-for request) => (contains {:status 401})
(model.activity/fetch-by-id (:_id activity)) =not=> nil)))

(fact "route: activity/update"
(fact "when the user is authenticated"
(let [author (mock/a-user-exists)
content (fseq :content)
data (json/json-str
{:content content})]
data => string?)))

(future-fact "apply-view #'actions.activity/oembed [:http :json]"
(let [action #'actions.activity/oembed]
(with-context [:http :json]
(let [activity (mock/there-is-an-activity)
request {:params {:url (:id activity)}
:action action}
response (filter-action action request)]
(apply-view request response) =>
(contains {:status status/success?
:body (contains {:title (:title activity)})})))))

(future-fact "apply-view #'actions.activity/oembed [:http :xml]"
(let [action #'actions.activity/oembed]
(with-context [:http :xml]
(let [activity (mock/there-is-an-activity)
request {:params {:url (:id activity)}
:action action}
item {} #_(filter-action action request)]
(let [response (apply-view request item)]
(let [body (:body response)]
response => map?
(:status response) => status/success?
body =not=> string?))))))

(future-fact "oembed"
(fact "when the format is json"
(let [activity (mock/there-is-an-activity)
url (str "/main/oembed?format=json&url=" (:url activity))]
(response-for (req/request :get url)) =>
(contains {:status status/redirect?
:body string?})))

(fact "when the format is xml"
(let [activity (mock/there-is-an-activity)
url (str "/main/oembed?format=xml&url=" (:url activity))]
(response-for (req/request :get url)) =>
(contains {:status status/success?
:body string?}))))
12 changes: 12 additions & 0 deletions test/jiksnu/modules/web/routes/client_routes_test.clj
Expand Up @@ -139,3 +139,15 @@
(let [id (get-in response [:body "oauth_token"])
secret (get-in response [:body "oauth_token_secret"])]
(model.access-token/fetch-by-id id) => (contains {:secret secret})))))

(future-fact "route: oauth/authorize :get"
(fact "when authenticated"
(let [actor (mock/a-user-exists)]

(fact "when given a valid request token"
(let [request-token (mock/a-request-token-exists)
url (format "/oauth/authorize?oauth_token=%s" (:_id request-token))]
(let [response (-> (req/request :get url)
(as-user actor)
response-for)]
(:status response) => status/success?))))))
2 changes: 1 addition & 1 deletion test/jiksnu/modules/web/routes/domain_routes_test.clj
Expand Up @@ -19,7 +19,7 @@
(th/module-test ["jiksnu.modules.core"
"jiksnu.modules.web"])

(facts "Requesting the host meta"
(facts "route: well-known/host-meta :get"
(fact "host meta json"
(let [domain (actions.domain/current-domain)
url "/.well-known/host-meta"
Expand Down
64 changes: 64 additions & 0 deletions test/jiksnu/modules/web/routes/home_routes_test.clj
@@ -0,0 +1,64 @@
(ns jiksnu.modules.web.routes.home-routes-test
(:require [ciste.formats :refer [format-as]]
[ciste.model :as cm]
[clojurewerkz.support.http.statuses :as status]
[jiksnu.db :as db]
[jiksnu.mock :as mock]
[jiksnu.test-helper :as th]
[jiksnu.routes-helper :refer [as-user response-for]]
[midje.sweet :refer :all]
[ring.mock.request :as req]))

(th/module-test ["jiksnu.modules.core"
"jiksnu.modules.web"])

(fact "route: root/home :get"
(fact "when there are no activities"
(db/drop-all!)


(-> (req/request :get "/")
response-for) =>
(contains {:status status/success?}))

(fact "when there are activities"
(let [user (mock/a-user-exists)]
(dotimes [n 10]
(mock/there-is-an-activity {:user user}))

(fact "when the user is not authenticated"
(-> (req/request :get "/")
response-for) =>
(contains {:status status/success?
:body string?}))

(fact "when the user is authenticated"
(-> (req/request :get "/")
as-user response-for) =>
(contains {:status status/success?
:body string?})))))

(future-fact "route: root/oembed :get"
(fact "when the format is json"
(let [activity (mock/there-is-an-activity)
url (str "/main/oembed?format=json&url=" (:url activity))]
(response-for (req/request :get url)) =>
(contains {:status status/redirect?
:body string?})))

(fact "when the format is xml"
(let [activity (mock/there-is-an-activity)
url (str "/main/oembed?format=xml&url=" (:url activity))]
(response-for (req/request :get url)) =>
(contains {:status status/success?
:body string?}))))

(future-fact "route: root/rsd :get"
(let [response (-> (req/request :get "/rsd.xml") response-for)]
response => map?
(:status response) => status/success?
(let [body (cm/string->document (:body response))
root (.getRootElement body)
attr {"rsd" "http://archipelago.phrasewise.com/rsd"}
nodes (cm/query root "//rsd:rsd" attr)]
(count nodes) => 1)))
2 changes: 1 addition & 1 deletion test/jiksnu/modules/web/routes/like_routes_test.clj
Expand Up @@ -10,7 +10,7 @@
(th/module-test ["jiksnu.modules.core"
"jiksnu.modules.web"])

(future-fact "delete html"
(future-fact "route: likes-api :delete"
(let [like (model.like/create (factory :like))
url (format "/likes/%s/delete" (:_id like))]
(-> (req/request :post url)
Expand Down
2 changes: 1 addition & 1 deletion test/jiksnu/modules/web/routes/pubsub_routes_test.clj
Expand Up @@ -12,7 +12,7 @@
(th/module-test ["jiksnu.modules.core"
"jiksnu.modules.web"])

(future-fact "subscription request"
(future-fact "route: subscriptions-api/item :post"
(let [domain (mock/a-domain-exists)
source (mock/a-feed-source-exists
{:domain (actions.domain/current-domain)})
Expand Down
22 changes: 0 additions & 22 deletions test/jiksnu/modules/web/routes/request_token_routes_test.clj

This file was deleted.

21 changes: 0 additions & 21 deletions test/jiksnu/modules/web/routes/site_routes_test.clj

This file was deleted.

26 changes: 0 additions & 26 deletions test/jiksnu/modules/web/routes/stream_routes_test.clj
Expand Up @@ -52,29 +52,3 @@
response => (contains {:status 200})
(let [body (json/read-json (:body response))]
body => (contains {:totalItems 1})))))

(fact "public-timeline-http-route"
(fact "when there are no activities"
(db/drop-all!)


(-> (req/request :get "/")
response-for) =>
(contains {:status status/success?}))

(fact "when there are activities"
(let [user (mock/a-user-exists)]
(dotimes [n 10]
(mock/there-is-an-activity {:user user}))

(fact "when the user is not authenticated"
(-> (req/request :get "/")
response-for) =>
(contains {:status status/success?
:body string?}))

(fact "when the user is authenticated"
(-> (req/request :get "/")
as-user response-for) =>
(contains {:status status/success?
:body string?})))))
11 changes: 1 addition & 10 deletions test/jiksnu/modules/web/routes/subscription_routes_test.clj
Expand Up @@ -14,7 +14,7 @@
(th/module-test ["jiksnu.modules.core"
"jiksnu.modules.web"])

(future-fact "ostatus submit"
(future-fact "route: ostatus/sub :post"
(let [username (fseq :username)
domain-name (fseq :domain)
uri (format "acct:%s@%s" username domain-name)
Expand All @@ -39,12 +39,3 @@
(provided
(ops/get-discovered anything) => (d/success-deferred
(model/map->Domain {:_id domain-name}))))))))

(future-fact "get-subscriptions"
(let [user (mock/a-user-exists)
subscription (mock/a-subscription-exists {:from user})
path (str "/users/" (:_id user) "/subscriptions")]
(-> (req/request :get path)
response-for) =>
(contains {:status status/success?
:body #(enlive/select (th/hiccup->doc %) [:.subscriptions])})))
9 changes: 9 additions & 0 deletions test/jiksnu/modules/web/routes/user_routes_test.clj
Expand Up @@ -47,3 +47,12 @@
(let [parsed-body (some-> response :body json/read-str)]
parsed-body => (contains {"items" (has every? string?)
"totalItems" m}))))))

(future-fact "route: users-api/subscriptions :get"
(let [user (mock/a-user-exists)
subscription (mock/a-subscription-exists {:from user})
path (str "/users/" (:_id user) "/subscriptions")]
(-> (req/request :get path)
response-for) =>
(contains {:status status/success?
:body #(enlive/select (th/hiccup->doc %) [:.subscriptions])})))

0 comments on commit d4a9af9

Please sign in to comment.