aboutsummaryrefslogtreecommitdiff
path: root/test/mastodon-auth-tests.el
blob: 371abcc49378ce38afcf8629fabe67146173abef (plain) (blame)
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
(require 'el-mock)
(load-file "../lisp/mastodon-auth.el")

(ert-deftest mastodon-auth:registration-success ()
  "Should set `mastodon--client-app-plist' on succesful registration."
  (let ((hash (make-hash-table :test 'equal))
        (client-plist '(:client_id "client-id" :client_secret "client-secret")))
    (puthash "client_id" "client-id" hash)
    (puthash "client_secret" "client-secret" hash)
    (with-mock
      (stub mastodon--json-hash-table => hash)
      (mastodon-auth--registration-success)
      (should (equal client-plist mastodon--client-app-plist)))))

(ert-deftest mastodon-auth:register-client-app ()
  "Should POST client data to /apps endpoint and return client plist."
  (let ((app-plist '("id" "id-val" "secret" "secret-val")))
    (with-mock
      (mock (mastodon--api-for "apps") => "https://instance/api/v1/apps")
      (mock (mastodon--register-client-app-triage "status") => app-plist)
      (mock (mastodon--http-post "https://instance/api/v1/apps"
                                 'mastodon--register-client-app-triage
                                 '(("client_name" . "mastodon.el")
                                   ("redirect_uris" . "urn:ietf:wg:oauth:2.0:oob")
                                   ("scopes" . "read write follow")
                                   ("website" . "https://github.com/jdenen/mastodon.el")))
            => (funcall 'mastodon--register-client-app-triage "status"))
      (should (eq app-plist (mastodon--register-client-app))))))

(ert-deftest mastodon-auth:register-client-app-triage ()
  "Should wrap `mastodon--http-response-triage' call and return client plist."
  (let ((app-plist '("id" "id-val" "secret" "secret-val")))
    (with-mock
      (mock (mastodon-auth--registration-success) => app-plist)
      (mock (mastodon--http-response-triage "status" 'mastodon-auth--registration-success)
            => (funcall 'mastodon-auth--registration-success))
      (should (eq app-plist (mastodon--register-client-app-triage "status"))))))

(ert-deftest mastodon-auth:register-and-return-client-app ()
  "Should return a plist of client_id and client_secret after registration."
  (let ((app-plist '("id" "id-val" "secret" "secret-val")))
    (with-mock
      (mock (mastodon--register-client-app) => app-plist)
      (should (equal app-plist (mastodon--register-and-return-client-app))))))