aboutsummaryrefslogtreecommitdiff
path: root/test/mastodon-client-tests.el
diff options
context:
space:
mode:
authorHolger Dürer <me@hdurer.net>2018-02-28 20:48:24 +0000
committerHolger Dürer <me@hdurer.net>2018-03-02 20:18:22 +0000
commitb774f9e5295341a68171d94765320114d0b5b407 (patch)
tree5e055a479aa6af15e524bca313ab557c8130684e /test/mastodon-client-tests.el
parent3e9bdbf2eeecafd7bb0673f2709f816a740c7d61 (diff)
Keep track of to which instance secrets in plstore belong.
While testing out issue 149 (https://github.com/jdenen/mastodon.el/issues/149) I had problems due to stale client information being cached. With this change we store various pieces of information (the client information in the plstore and the auth tokens) in alists keyed by the instance url (and the plstore key contains the instance url as well to allow us to store data per instance).
Diffstat (limited to 'test/mastodon-client-tests.el')
-rw-r--r--test/mastodon-client-tests.el72
1 files changed, 50 insertions, 22 deletions
diff --git a/test/mastodon-client-tests.el b/test/mastodon-client-tests.el
index c339efa..dfe175b 100644
--- a/test/mastodon-client-tests.el
+++ b/test/mastodon-client-tests.el
@@ -9,7 +9,8 @@
("redirect_uris" . "urn:ietf:wg:oauth:2.0:oob")
("scopes" . "read write follow")
("website" . "https://github.com/jdenen/mastodon.el"))
- nil))
+ nil
+ :unauthenticated))
(mastodon-client--register)))
(ert-deftest fetch ()
@@ -23,53 +24,80 @@
(ert-deftest store-1 ()
"Should return the client plist."
- (let ((plist '(:client_id "id" :client_secret "secret")))
+ (let ((mastodon-instance-url "http://mastodon.example")
+ (plist '(:client_id "id" :client_secret "secret")))
(with-mock
(mock (mastodon-client--token-file) => "stubfile.plstore")
(mock (mastodon-client--fetch) => '(:client_id "id" :client_secret "secret"))
(let* ((plstore (plstore-open "stubfile.plstore"))
- (client (delete "mastodon" (plstore-get plstore "mastodon"))))
- (should (equal (mastodon-client--store) plist))
- ))))
+ (client (cdr (plstore-get plstore "mastodon-http://mastodon.example"))))
+ (should (equal (mastodon-client--store) plist))))))
(ert-deftest store-2 ()
"Should store client in `mastodon-client--token-file'."
- (let* ((plstore (plstore-open "stubfile.plstore"))
- (client (delete "mastodon" (plstore-get plstore "mastodon"))))
+ (let* ((mastodon-instance-url "http://mastodon.example")
+ (plstore (plstore-open "stubfile.plstore"))
+ (client (cdr (plstore-get plstore "mastodon-http://mastodon.example"))))
(plstore-close plstore)
(should (string= (plist-get client :client_id) "id"))
(should (string= (plist-get client :client_secret) "secret"))))
-(ert-deftest read-1 ()
+(ert-deftest read-finds-match ()
"Should return mastodon client from `mastodon-token-file' if it exists."
- (with-mock
- (mock (mastodon-client--token-file) => "fixture/client.plstore")
- (should (equal (mastodon-client--read) '(:client_id "id" :client_secret "secret")))))
+ (let ((mastodon-instance-url "http://mastodon.example"))
+ (with-mock
+ (mock (mastodon-client--token-file) => "fixture/client.plstore")
+ (should (equal (mastodon-client--read)
+ '(:client_id "id2" :client_secret "secret2"))))))
+
+(ert-deftest read-finds-no-match ()
+ "Should return mastodon client from `mastodon-token-file' if it exists."
+ (let ((mastodon-instance-url "http://mastodon.social"))
+ (with-mock
+ (mock (mastodon-client--token-file) => "fixture/client.plstore")
+ (should (equal (mastodon-client--read) nil)))))
-(ert-deftest read-2 ()
+(ert-deftest read-empty-store ()
"Should return nil if mastodon client is not present in the plstore."
(with-mock
(mock (mastodon-client--token-file) => "fixture/empty.plstore")
(should (equal (mastodon-client--read) nil))))
-(ert-deftest client-1 ()
- "Should return `mastondon-client' if non-nil."
- (let ((mastodon-client--client-details t))
- (should (eq (mastodon-client) t))))
+(ert-deftest client-set-and-matching ()
+ "Should return `mastondon-client' if `mastodon-client--client-details-alist' is non-nil and instance url is included."
+ (let ((mastodon-instance-url "http://mastodon.example")
+ (mastodon-client--client-details-alist '(("https://other.example" . :no-match)
+ ("http://mastodon.example" . :matches))))
+ (should (eq (mastodon-client) :matches))))
+
+(ert-deftest client-set-but-not-matching ()
+ "Should read from `mastodon-token-file' if wrong data is cached."
+ (let ((mastodon-instance-url "http://mastodon.example")
+ (mastodon-client--client-details-alist '(("http://other.example" :wrong))))
+ (with-mock
+ (mock (mastodon-client--read) => '(:client_id "foo" :client_secret "bar"))
+ (should (equal (mastodon-client) '(:client_id "foo" :client_secret "bar")))
+ (should (equal mastodon-client--client-details-alist
+ '(("http://mastodon.example" :client_id "foo" :client_secret "bar")
+ ("http://other.example" :wrong)))))))
-(ert-deftest client-2 ()
+(ert-deftest client-unset ()
"Should read from `mastodon-token-file' if available."
- (let ((mastodon-client--client-details nil))
+ (let ((mastodon-instance-url "http://mastodon.example")
+ (mastodon-client--client-details-alist nil))
(with-mock
(mock (mastodon-client--read) => '(:client_id "foo" :client_secret "bar"))
(should (equal (mastodon-client) '(:client_id "foo" :client_secret "bar")))
- (should (equal mastodon-client--client-details '(:client_id "foo" :client_secret "bar"))))))
+ (should (equal mastodon-client--client-details-alist
+ '(("http://mastodon.example" :client_id "foo" :client_secret "bar")))))))
-(ert-deftest client-3 ()
+(ert-deftest client-unset-and-not-in-storage ()
"Should store client data in plstore if it can't be read."
- (let ((mastodon-client--client-details nil))
+ (let ((mastodon-instance-url "http://mastodon.example")
+ (mastodon-client--client-details-alist nil))
(with-mock
(mock (mastodon-client--read))
(mock (mastodon-client--store) => '(:client_id "foo" :client_secret "baz"))
(should (equal (mastodon-client) '(:client_id "foo" :client_secret "baz")))
- (should (equal mastodon-client--client-details '(:client_id "foo" :client_secret "baz"))))))
+ (should (equal mastodon-client--client-details-alist
+ '(("http://mastodon.example" :client_id "foo" :client_secret "baz")))))))