Skip to content

Commit

Permalink
SQL-215 added basic test
Browse files Browse the repository at this point in the history
  • Loading branch information
milt committed Nov 15, 2023
1 parent b4b6f8a commit 6374f17
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
com.yetanalytics/lrs
{;; :mvn/version "1.2.15"
:git/url "https://github.com/yetanalytics/lrs.git"
:git/sha "ea6922af2cba3a34a67c40aa8b056298b59b0b35"
:git/sha "8ebd384df6ebdff397ba5622385513c5b5cdfbb6"
:exclusions [org.clojure/clojure
org.clojure/clojurescript
com.yetanalytics/xapi-schema]}
Expand Down
45 changes: 45 additions & 0 deletions dev-resources/clamav/attachment_body.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--105423a5219f5a63362a375ba7a64a8f234da19c7d01e56800c3c64b26bb2fa0
Content-Type:application/json

{
"actor": {
"mbox": "mailto:sample.agent@example.com",
"name": "Sample Agent",
"objectType": "Agent"
},
"verb": {
"id": "http://adlnet.gov/expapi/verbs/answered",
"display": {
"en-US": "answered"
}
},
"object": {
"id": "http://www.example.com/tincan/activities/multipart",
"objectType": "Activity",
"definition": {
"name": {
"en-US": "Multi Part Activity"
},
"description": {
"en-US": "Multi Part Activity Description"
}
}
},
"attachments": [
{
"usageType": "http://example.com/attachment-usage/test",
"display": { "en-US": "A test attachment" },
"description": { "en-US": "A test attachment (description)" },
"contentType": "text/plain; charset=ascii",
"length": 27,
"sha2": "495395e777cd98da653df9615d09c0fd6bb2f8d4788394cd53c56a3bfdcd848a"
}
]
}
--105423a5219f5a63362a375ba7a64a8f234da19c7d01e56800c3c64b26bb2fa0
Content-Type:text/plain
Content-Transfer-Encoding:binary
X-Experience-API-Hash:495395e777cd98da653df9615d09c0fd6bb2f8d4788394cd53c56a3bfdcd848a

here is a simple attachment
--105423a5219f5a63362a375ba7a64a8f234da19c7d01e56800c3c64b26bb2fa0--
93 changes: 93 additions & 0 deletions src/test/lrsql/scan_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
(ns lrsql.scan-test
(:require [clojure.test :refer [deftest testing is use-fixtures]]
[babashka.curl :as curl]
[com.stuartsierra.component :as component]
[lrsql.test-support :as support]
[lrsql.init.clamav :as clam]))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Init
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(support/instrument-lrsql)

(use-fixtures :each support/fresh-db-fixture)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Tests
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(def attachment-post-params
{:basic-auth ["username" "password"]
:headers
{"X-Experience-API-Version" "1.0.3"
"Content-Type"
"multipart/mixed; boundary=105423a5219f5a63362a375ba7a64a8f234da19c7d01e56800c3c64b26bb2fa0"}
:body (slurp "dev-resources/clamav/attachment_body.txt")
:throw false})

(def doc-post-params
{:basic-auth ["username" "password"]
:headers {"X-Experience-API-Version" "1.0.3"}
:query-params
{"activityId" "http://www.example.com/activityId/hashset"
"agent"
"{\"objectType\":\"Agent\",\"account\":{\"homePage\":\"http://www.example.com/agentId/1\",\"name\":\"Rick James\"}}"
"stateId" "f8128f68-74e2-4951-8c5f-ef7cce73b4ff"}
:body "I'm a little teapot"
:throw false})

(deftest scan-test
(testing "File/Document Scanning"
;; Stub out a scanner that always fails
(testing "Failure"
(with-redefs [clam/init-file-scanner (fn [_]
(fn [in]
(slurp in)
{:message "Scan Fail!"}))]
(let [sys (support/test-system
:conf-overrides {[:webserver :enable-clamav] true})
sys' (component/start sys)
pre (-> sys' :webserver :config :url-prefix)]
(try
(testing "Attachment"
(is (= {:status 400
:body "{\"error\":{\"message\":\"Attachment scan failed, Errors: Scan Fail!\"}}"}
(select-keys
(curl/post
(format "http://localhost:8080%s/statements" pre)
attachment-post-params)
[:body :status]))))
(testing "Document"
(is (= {:status 400
:body "{\"error\":{\"message\":\"Document scan failed, Error: Scan Fail!\"}}"}
(select-keys
(curl/post
(format "http://localhost:8080%s/activities/state" pre)
doc-post-params)
[:body :status]))))
(finally (component/stop sys'))))))
;; And one that always succeeds
(testing "Success"
(with-redefs [clam/init-file-scanner (fn [_]
(fn [in]
(slurp in)
nil))]
(let [sys (support/test-system
:conf-overrides {[:webserver :enable-clamav] true})
sys' (component/start sys)
pre (-> sys' :webserver :config :url-prefix)]
(try
(testing "Attachment"
(is (= 200
(:status
(curl/post
(format "http://localhost:8080%s/statements" pre)
attachment-post-params)))))
(testing "Document"
(is (= 204
(:status
(curl/post
(format "http://localhost:8080%s/activities/state" pre)
doc-post-params)))))
(finally (component/stop sys'))))))))

0 comments on commit 6374f17

Please sign in to comment.