branch: elpa/mastodon
commit a1e525dacca4f8b540c6496ea0c647c47188514b
Author: marty hiatt <martianhia...@disroot.org>
Commit: marty hiatt <martianhia...@disroot.org>

    refactor mastodon-auth--plstore-access-token-member + test it. #671
---
 lisp/mastodon-auth.el       | 37 +++++++++++++++++++++----------------
 test/mastodon-auth-tests.el | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 16 deletions(-)

diff --git a/lisp/mastodon-auth.el b/lisp/mastodon-auth.el
index c4d6c3bce8..d79eb60e6a 100644
--- a/lisp/mastodon-auth.el
+++ b/lisp/mastodon-auth.el
@@ -186,7 +186,7 @@ When ASK is absent return nil."
       (json-read-from-string json-string))))
 
 (defun mastodon-auth--plstore-token-check (&optional auth-source)
-  "Return non-nil if plstore contains unencrypted access-token.
+  "Signal an error if plstore contains unencrypted access-token.
 If AUTH-SOURCE, and if `mastodon-auth-use-auth-source' is non-nil,
 return non-nil if it contains any access token.
 Used to help users switch to the new encrypted auth token flow."
@@ -194,28 +194,33 @@ Used to help users switch to the new encrypted auth token 
flow."
   ;; e.g. inside of `mastodon-client--active-user'? the issue is that
   ;; ideally we want to test "user-" entry, even if fetching "active-user"
   ;; entry, so we would have to re-do the plstore read functions.
-  (let* ((plstore (plstore-open (mastodon-client--token-file)))
-         (name (concat "user-" (mastodon-client--form-user-from-vars)))
-         ;; get alist like plstore.el does, so that keys will display with
-         ;; ":secret-" prefix if encrypted:
-         (alist (assoc name (plstore--get-merged-alist plstore))))
-    ;; if auth source, we should have no access token at all:
-    (if (and auth-source mastodon-auth-use-auth-source)
-        (if (or (member :access_token alist)
-                (member :secret-access_token alist))
-            (user-error "Auth source storage of tokens is enabled,\
+  (when
+      (mastodon-auth--plstore-access-token-member auth-source)
+    (if auth-source
+        (user-error "Auth source storage of tokens is enabled,\
  but there is also an access token in your plstore.\
  If you're seeing this message after updating,\
  call `mastodon-forget-all-logins', and try again.
  If you don't want to use auth sources,\
  also set `mastodon-auth-use-auth-source' to nil.\
- If this message is in error, contact us on the mastodon.el repo"))
-      ;; else we just want to check if we have an unencrypted token:
-      (if (member :access_token alist)
-          (user-error "Unencrypted access token in your plstore.\
+ If this message is in error, contact us on the mastodon.el repo")
+      (user-error "Unencrypted access token in your plstore.\
  If you're seeing this message after updating,\
  call `mastodon-forget-all-logins', and log in again.
- If this message is in error, contact us on the mastodon.el repo")))))
+ If this message is in error, contact us on the mastodon.el repo"))))
+
+(defun mastodon-auth--plstore-access-token-member (&optional auth-source)
+  "Return non-nil if the user entry of the plstore contains :access_token.
+If AUTH-SOURCE, also check if it contains :secret-access_token."
+  (let* ((plstore (plstore-open (mastodon-client--token-file)))
+         (name (concat "user-" (mastodon-client--form-user-from-vars)))
+         ;; get alist like plstore.el does, so that keys will display with
+         ;; ":secret-" prefix if encrypted:
+         (alist (assoc name (plstore--get-merged-alist plstore))))
+    (if (and auth-source mastodon-auth-use-auth-source)
+        (or (member :access_token alist)
+            (member :secret-access_token alist))
+      (member :access_token alist))))
 
 (defun mastodon-auth--access-token ()
   "Return the access token to use with `mastodon-instance-url'.
diff --git a/test/mastodon-auth-tests.el b/test/mastodon-auth-tests.el
index af410364cb..5ce9910534 100644
--- a/test/mastodon-auth-tests.el
+++ b/test/mastodon-auth-tests.el
@@ -75,3 +75,39 @@
     (with-mock
       (mock (mastodon-client--active-user))
       (should-error (mastodon-auth--access-token)))))
+
+(ert-deftest mastodon-auth-plstore-token-check ()
+  (let ((mastodon-instance-url "https://mastodon.example";)
+        (mastodon-active-user "test8000")
+        (user-details ;; order changed for new encrypted auth flow:
+         '( :client_id "id" :client_secret "secret"
+            :access_token "token"
+            :username "test8000@mastodon.example"
+            :instance "https://mastodon.example";))
+        ;; save token to plstore encrypted:
+        (mastodon-auth-use-auth-source nil)) ;; FIXME: test auth source
+    ;; setup plstore: store access token
+    (with-mock
+      (mock (mastodon-client) => '(:client_id "id" :client_secret "secret"))
+      (mock (mastodon-client--token-file) => "stubfile.plstore")
+      (should
+       (equal (mastodon-client--store-access-token "token")
+              user-details))
+      ;; should non-nil if we check with auth-source:
+      ;; because we saved with non auth-source:
+      (should
+       (equal
+        (let ((mastodon-auth-use-auth-source t))
+          (mastodon-auth--plstore-access-token-member :auth-source))
+        '(:secret-access_token t :username "test8000@mastodon.example"
+                               :instance "https://mastodon.example";)))
+      ;; should nil if we don't check with auth source:
+      (should
+       (equal
+        (mastodon-auth--plstore-access-token-member)
+        nil)))
+    ;; FIXME: ideally we would also mock up a non-encrypted plstore and
+    ;; test against it too, as that's the work we really want
+    ;; `mastodon-auth--plstore-access-token-member' to do
+    ;; but we don't currently have a way to mock one up.
+    (delete-file "stubfile.plstore")))

Reply via email to