aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohnson Denen <johnson.denen@gmail.com>2017-04-12 12:59:11 -0400
committerJohnson Denen <johnson.denen@gmail.com>2017-04-12 18:11:48 -0400
commit525235938369dd035c3e2bd073186266c7815af9 (patch)
tree226a9d62bd63a26d7227e9db1e3c983775773864
parent40a54819cfcba889a812c721d4cc0743ce5ae544 (diff)
Add mastodon--get-access-token test
Abstract Email and Password prompts to `mastodon-auth--user-and-passwd' function
-rw-r--r--lisp/mastodon-auth.el25
-rw-r--r--test/mastodon-auth-tests.el16
2 files changed, 33 insertions, 8 deletions
diff --git a/lisp/mastodon-auth.el b/lisp/mastodon-auth.el
index 9ded694..1f4adaf 100644
--- a/lisp/mastodon-auth.el
+++ b/lisp/mastodon-auth.el
@@ -113,18 +113,27 @@ STATUS is passed by `url-retrieve'."
(mastodon--http-response-triage status
'mastodon-auth--get-token-success))
+(defun mastodon-auth--user-and-passwd ()
+ "Prompt for user email and password."
+ (let ((email (read-string "Email: "))
+ (passwd (read-string "Password: ")))
+ (cons email passwd)))
+
(defun mastodon--get-access-token ()
"Retrieve access token from instance.
Authenticates with email address and password. Neither are not stored."
- (mastodon--http-post (concat mastodon-instance-url "/oauth/token")
- 'mastodon--get-access-token-triage
- `(("client_id" . ,(plist-get (mastodon--client-app) :client_id))
- ("client_secret" . ,(plist-get (mastodon--client-app) :client_secret))
- ("grant_type" . "password")
- ("username" . ,(read-string "Email: "))
- ("password" . ,(read-passwd "Password: "))
- ("scope" . "read write follow"))))
+ (let* ((creds (mastodon-auth--user-and-passwd))
+ (email (car creds))
+ (passwd (cdr creds)))
+ (mastodon--http-post (concat mastodon-instance-url "/oauth/token")
+ 'mastodon--get-access-token-triage
+ `(("client_id" . ,(plist-get (mastodon--client-app) :client_id))
+ ("client_secret" . ,(plist-get (mastodon--client-app) :client_secret))
+ ("grant_type" . "password")
+ ("username" . ,email)
+ ("password" . ,passwd)
+ ("scope" . "read write follow")))))
(defun mastodon--access-token ()
"Return `mastodon--api-token-string'.
diff --git a/test/mastodon-auth-tests.el b/test/mastodon-auth-tests.el
index 4560e27..6e8b10c 100644
--- a/test/mastodon-auth-tests.el
+++ b/test/mastodon-auth-tests.el
@@ -101,3 +101,19 @@
(with-mock
(mock (mastodon--http-response-triage "status" 'mastodon-auth--get-token-success))
(mastodon--get-access-token-triage "status")))
+
+(ert-deftest mastodon-auth:get-access-token ()
+ "Should POST auth data to retreive access token."
+ (let ((client-app '(:client_id "id" :client_secret "secret")))
+ (with-mock
+ (mock (mastodon-auth--user-and-passwd) => (cons "email" "password"))
+ (mock (mastodon--client-app) => client-app)
+ (mock (mastodon--http-post "https://mastodon.social/oauth/token"
+ 'mastodon--get-access-token-triage
+ '(("client_id" . "id")
+ ("client_secret" . "secret")
+ ("grant_type" . "password")
+ ("username" . "email")
+ ("password" . "password")
+ ("scope" . "read write follow"))))
+ (mastodon--get-access-token))))