cohost

Unofficial Common Lisp client library for Cohost
git clone https://todayiwilllaunchmyinfantsonintoorbit.com/cohost.git
Log | Files | Refs | LICENSE

main.lisp (7949B)


      1 (defpackage cohost/tests/main
      2   (:use :cl
      3         :cohost
      4         :cohost.util
      5         :rove))
      6 (in-package :cohost/tests/main)
      7 
      8 ;; NOTE: To run this test file, execute
      9 ;;   (asdf:test-system :cohost)
     10 ;; in your Lisp.
     11 
     12 (defparameter +test-email+ "test@test")
     13 (defparameter +test-password+ "password")
     14 (defparameter +test-salt+ "Ms7QhEwzfpoyQmLcUue-kA")
     15 (defparameter +test-client-hash+ "taufN1mhG3w5kD4mT/RzKlMjI8llHXmus2lK9xTw2SRYi24K3In4nXyv8YtQ17sA1NWqdXxo5yky2kYFSe9UVBgQMftTJcUpmKkDd5cFCDAUkwxIZ16M6ZNxcmFPIOMhoAjbmH4v1mS8d67ALzvFpi1/pNQvR/vbAfw/YBK1suc=")
     16 (defparameter +test-user-id+ 1337)
     17 (defparameter +test-batch-input+ '(("projects.listEditedProjects" . NIL) ("login.loggedIn" . NIL) ("bookmarks.tags.list" . NIL) ("users.displayPrefs" . NIL) ("posts.isLiked" . 12345)))
     18 
     19 (defvar *type* :unit)
     20 (defvar *client*)
     21 
     22 (defun http-request-mock (uri &key method parameters &allow-other-keys)
     23   (cond
     24     ;; login/salt
     25     ((and (string= uri "https://cohost.org/api/v1/login/salt")
     26           (string= (assoc-val "email" parameters) +test-email+)
     27           (eql method :get))
     28      (json:encode-json-plist-to-string `(:salt ,+test-salt+)))
     29     ;; login
     30     ((and (string= uri "https://cohost.org/api/v1/login")
     31           (string= (assoc-val "email" parameters) +test-email+)
     32           (string= (assoc-val "clientHash" parameters) +test-client-hash+)
     33           (eql method :post))
     34      (progn
     35        (setf (drakma:cookie-jar-cookies (cohost.client:cookie-jar *client*))
     36              (list (make-instance 'drakma:cookie
     37                                   :name "connect.sid"
     38                                   :value "test cookie"
     39                                   :expires (+ (get-universal-time) 86400)
     40                                   :domain "cohost.org"
     41                                   :securep t
     42                                   :http-only-p t)))
     43        (values (json:encode-json-plist-to-string `(:user-id ,+test-user-id+ :email ,+test-email+)) 200)))
     44     ;; login state
     45     ((and (string= uri "https://cohost.org/api/v1/trpc/login.loggedIn")
     46           (string= (assoc-val "batch" parameters) "1")
     47           (eql method :get))
     48      (json:encode-json-to-string '(((:result (:data (:logged-in . t)))))))
     49     ;; Failed login
     50     ((and (string= uri "https://cohost.org/api/v1/login")
     51           (eql method :post))
     52      (json:encode-json-plist-to-string `(:status 422 :message "Login Failed")))
     53     ;; Batch input
     54     ((and (string= uri "https://cohost.org/api/v1/trpc/projects.listEditedProjects,login.loggedIn,bookmarks.tags.list,users.displayPrefs,posts.isLiked")
     55           (string= (assoc-val "input" parameters) "{\"4\":12345}")
     56           (string= (assoc-val "batch" parameters) "1")
     57           (eql method :get))
     58      "{\"foo\":1}")))
     59 
     60 (defmacro with-test-client ((client) &rest body)
     61   `(let* ((,client (if (eql *type* :integration)
     62                        (cohost.client:init-client :v1)
     63                        (make-instance 'cohost.client-v1-impl::cohost-client-v1 :http-request #'http-request-mock :api-path "api/v1/" :cookie-jar (make-instance 'drakma:cookie-jar))))
     64           (cohost/tests/main::*client* ,client))
     65      ,@body))
     66 
     67 (deftest cohost-primitive-tests
     68   (with-test-client (client)
     69     (testing "Can get client base URI"
     70       (ok (cohost:base-uri client)))
     71     (testing "Can perform basic RPC call (login/salt)"
     72       (ok (multiple-value-bind (rpc)
     73               (cohost.rpc:cohost-rpc client "login/salt" :get :email +test-email+)
     74             (string= (assoc-val :salt rpc) +test-salt+))))
     75     (testing "Can perform tRPC batch call"
     76       (ok (multiple-value-bind (rpc)
     77               (cohost.rpc:cohost-trpc-batch client +test-batch-input+)
     78             rpc)))))
     79 
     80 (cohost.rpc:define-trpc-request "test.zeroParams" ())
     81 (cohost.rpc:define-trpc-request "test.atomicParam" (:atom))
     82 (cohost.rpc:define-trpc-request "test.structuredParams" ("firstParam" "secondParam"))
     83 
     84 (deftest cohost-trpc-tests
     85   (testing "Validate tRPC function definitions"
     86     (ok (let ((result (test.zero-params)))
     87           (and (string= (car result) "test.zeroParams")
     88                (null (cdr result)))))
     89     (ok (let ((result (test.atomic-param 42)))
     90           (and (string= (car result) "test.atomicParam")
     91                (eq (cdr result) 42))))
     92     (ok (let* ((result (test.structured-params "foo" "bar"))
     93                (params (cdr result)))
     94           (and (string= (car result) "test.structuredParams")
     95                (string= (assoc-val "firstParam" params) "foo")
     96                (string= (assoc-val "secondParam" params) "bar"))))))
     97 
     98 (deftest cohost-api-tests
     99   (with-test-client (client)
    100     (testing "Can call login successfully"
    101       (ok (cohost.client:login client +test-email+ +test-password+)))
    102     ;(testing "Can fail login"
    103     ;  (ok (let ((response (cohost.client:login client +test-email+ "foo")))
    104     ;        (and (eql (assoc-val :status response) 422)
    105     ;             (string= (assoc-val :message response) "Login Failed")))))
    106     ))
    107 
    108 (deftest cohost-creation-tests
    109   (with-test-client (client)
    110     (testing "Can create new post object with no parameters"
    111       (ok (let* ((project "TestProject")
    112                  (chost (cohost.client:new-post client project)))
    113             (and (string= (cohost.client:project chost) project)
    114                  (null (cohost.client:draft chost))
    115                  (null (cohost.client:adult-content chost))
    116                  (null (cohost.client:content-blocks chost))
    117                  (null (cohost.client:content-warnings chost))
    118                  (null (cohost.client:headline chost))
    119                  (null (cohost.client:tags chost))))))
    120     (testing "Can create new post object with a full set of parameters"
    121       (ok (let* ((content-block "test")
    122                  (cw "test-cw")
    123                  (headline "test")
    124                  (tag "test-tag")
    125                  (project "TestProject")
    126                  (chost (cohost.client:new-post client project
    127                                                        :draft t
    128                                                        :adult-content t
    129                                                        :blocks (list content-block)
    130                                                        :content-warnings (list cw)
    131                                                        :headline headline
    132                                                        :tags (list tag))))
    133             (and (string= (cohost.client:project chost) project)
    134                  (cohost.client:draft chost)
    135                  (cohost.client:adult-content chost)
    136                  (eql (length (cohost.client:content-blocks chost)) 1)
    137                  (string= (car (cohost.client:content-blocks chost)) content-block)
    138                  (eql (length (cohost.client:content-warnings chost)) 1)
    139                  (string= (car (cohost.client:content-warnings chost)) cw)
    140                  (string= (cohost.client:headline chost) headline)
    141                  (eql (length (cohost.client:tags chost)) 1)
    142                  (string= (car (cohost.client:tags chost)) tag)))))
    143     (testing "Can create markdown block object"
    144       (ok (let* ((content "test content")
    145                  (markdown (cohost.client:new-markdown-block client content)))
    146             (string= (cohost.client:content markdown) content))))
    147     (testing "Can create attachment block object with and without alt-text"
    148       (ok (let* ((attachment-id 12345)
    149                  (alt-text "foo")
    150                  (attachment-with-alt (cohost.client:new-attachment client attachment-id :alt-text alt-text))
    151                  (attachment-without-alt (cohost.client:new-attachment client attachment-id)))
    152             (and (eql (cohost.client:attachment-id attachment-with-alt) attachment-id)
    153                  (eql (cohost.client:attachment-id attachment-without-alt) attachment-id)
    154                  (string= (cohost.client:alt-text attachment-with-alt) alt-text)
    155                  (not (cohost.client:alt-text attachment-without-alt))))))))