aboutsummaryrefslogtreecommitdiff
path: root/test/mastodon-auth-tests.el
blob: fda04eb063b3d4c7aaf6898564f3fcc07b84fdfb (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(require 'el-mock)

(ert-deftest generate-token--no-storing-credentials ()
  "Should make `mastdon-http--post' request to generate auth token."
  (with-mock
    (let ((mastodon-auth-source-file "")
	  (mastodon-instance-url "https://instance.url"))
      (mock (mastodon-client) => '(:client_id "id" :client_secret "secret"))
      (mock (read-string "Email: " user-mail-address) => "foo@bar.com")
      (mock (read-passwd "Password: ") => "password")
      (mock (mastodon-http--post "https://instance.url/oauth/token"
                                 '(("client_id" . "id")
                                   ("client_secret" . "secret")
                                   ("grant_type" . "password")
                                   ("username" . "foo@bar.com")
                                   ("password" . "password")
                                   ("scope" . "read write follow"))
                                 nil
                                 :unauthenticated))
      (mastodon-auth--generate-token))))

(ert-deftest generate-token--storing-credentials ()
  "Should make `mastdon-http--post' request to generate auth token."
  (with-mock
    (let ((mastodon-auth-source-file "~/.authinfo")
	  (mastodon-instance-url "https://instance.url"))
      (mock (mastodon-client) => '(:client_id "id" :client_secret "secret"))
      (mock (auth-source-search :create t
                                :host "https://instance.url"
                                :port 443
                                :require '(:user :secret))
            => '((:user "foo@bar.com" :secret (lambda () "password"))))
      (mock (mastodon-http--post "https://instance.url/oauth/token"
                                 '(("client_id" . "id")
                                   ("client_secret" . "secret")
                                   ("grant_type" . "password")
                                   ("username" . "foo@bar.com")
                                   ("password" . "password")
                                   ("scope" . "read write follow"))
                                 nil
				 :unauthenticated))
      (mastodon-auth--generate-token))))

(ert-deftest get-token ()
  "Should generate token and return JSON response."
  (with-temp-buffer
    (with-mock
      (mock (mastodon-auth--generate-token) => (progn
                                                 (insert "\n\n{\"access_token\":\"abcdefg\"}")
                                                 (current-buffer)))
      (should (equal (mastodon-auth--get-token) '(:access_token "abcdefg"))))))

(ert-deftest access-token-found ()
  "Should return value in `mastodon-auth--token-alist' if found."
  (let ((mastodon-instance-url "https://instance.url")
        (mastodon-auth--token-alist '(("https://instance.url" . "foobar")) ))
    (should (string= (mastodon-auth--access-token) "foobar"))))

(ert-deftest access-token-2 ()
  "Should set and return `mastodon-auth--token' if nil."
  (let ((mastodon-instance-url "https://instance.url")
        (mastodon-auth--token nil))
    (with-mock
      (mock (mastodon-auth--get-token) => '(:access_token "foobaz"))
      (should (string= (mastodon-auth--access-token) "foobaz"))
      (should (equal mastodon-auth--token-alist '(("https://instance.url" . "foobaz")))))))