diff options
| author | Damien Cassou | 2017-02-04 08:51:32 +0100 |
|---|---|---|
| committer | Ted Zlatanov | 2017-04-27 17:37:58 -0400 |
| commit | 0e066efe3b84ee9ecfc0a075e0ce289311f6d160 (patch) | |
| tree | 9bfcb8bb21f7d37b3124a7f171d255ead8dc56ff /test | |
| parent | 9e43525077697e75244d1275ec0ba88a7c2ed7f9 (diff) | |
| download | emacs-0e066efe3b84ee9ecfc0a075e0ce289311f6d160.tar.gz emacs-0e066efe3b84ee9ecfc0a075e0ce289311f6d160.zip | |
Integrate auth-source with password-store
* lisp/auth-source-pass.el: auth-source backend for password-store.
* test/lisp/auth-source-pass-tests.el: Tests for auth-source-pass
behavior.
Diffstat (limited to 'test')
| -rw-r--r-- | test/lisp/auth-source-pass-tests.el | 234 |
1 files changed, 234 insertions, 0 deletions
diff --git a/test/lisp/auth-source-pass-tests.el b/test/lisp/auth-source-pass-tests.el new file mode 100644 index 00000000000..c3586d8058c --- /dev/null +++ b/test/lisp/auth-source-pass-tests.el | |||
| @@ -0,0 +1,234 @@ | |||
| 1 | ;;; auth-source-pass-tests.el --- Tests for auth-source-pass.el -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2013 Damien Cassou | ||
| 4 | |||
| 5 | ;; Author: Damien Cassou <damien.cassou@gmail.com> | ||
| 6 | |||
| 7 | ;; This file is not part of GNU Emacs. | ||
| 8 | |||
| 9 | ;; This program is free software: you can redistribute it and/or modify | ||
| 10 | ;; it under the terms of the GNU General Public License as published by | ||
| 11 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 12 | ;; (at your option) any later version. | ||
| 13 | |||
| 14 | ;; This program is distributed in the hope that it will be useful, | ||
| 15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 17 | ;; GNU General Public License for more details. | ||
| 18 | |||
| 19 | ;; You should have received a copy of the GNU General Public License | ||
| 20 | ;; along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 21 | |||
| 22 | ;;; Commentary: | ||
| 23 | |||
| 24 | ;; Tests for auth-source-pass.el | ||
| 25 | |||
| 26 | ;;; Code: | ||
| 27 | |||
| 28 | (require 'ert) | ||
| 29 | |||
| 30 | (require 'auth-source-pass) | ||
| 31 | |||
| 32 | (eval-when-compile (require 'cl-macs)) | ||
| 33 | |||
| 34 | (ert-deftest auth-source-pass-parse-simple () | ||
| 35 | (let ((content "pass\nkey1:val1\nkey2:val2\n")) | ||
| 36 | (should (equal (auth-source-pass--parse-data content) | ||
| 37 | '(("key1" . "val1") | ||
| 38 | ("key2" . "val2")))))) | ||
| 39 | |||
| 40 | (ert-deftest auth-source-pass-parse-with-dash-line () | ||
| 41 | (let ((content "pass\n--\nkey1:val1\nkey2:val2\n")) | ||
| 42 | (should (equal (auth-source-pass--parse-data content) | ||
| 43 | '(("key1" . "val1") | ||
| 44 | ("key2" . "val2")))))) | ||
| 45 | |||
| 46 | (ert-deftest auth-source-pass-parse-with-trailing-spaces () | ||
| 47 | (let ((content "pass\n--\nkey1 :val1 \nkey2: val2\n\n")) | ||
| 48 | (should (equal (auth-source-pass--parse-data content) | ||
| 49 | '(("key1" . "val1") | ||
| 50 | ("key2" . "val2")))))) | ||
| 51 | |||
| 52 | (defvar auth-source-pass--debug-log nil | ||
| 53 | "Contains a list of all messages passed to `auth-source-do-debug`.") | ||
| 54 | |||
| 55 | (defun auth-source-pass--should-have-message-containing (regexp) | ||
| 56 | "Assert that at least one `auth-source-do-debug` matched REGEXP." | ||
| 57 | (should (seq-find (lambda (message) | ||
| 58 | (string-match regexp message)) | ||
| 59 | auth-source-pass--debug-log))) | ||
| 60 | |||
| 61 | (defun auth-source-pass--debug (&rest msg) | ||
| 62 | "Format MSG and add that to `auth-source-pass--debug-log`. | ||
| 63 | This function is intended to be set to `auth-source-debug`." | ||
| 64 | (add-to-list 'auth-source-pass--debug-log (apply #'format msg) t)) | ||
| 65 | |||
| 66 | (defmacro auth-source-pass--deftest (name arglist store &rest body) | ||
| 67 | "Define a new ert-test NAME with ARGLIST using STORE as password-store. | ||
| 68 | BODY is a sequence of instructions that will be evaluated. | ||
| 69 | |||
| 70 | This macro overrides `auth-source-pass-parse-entry' and `auth-source-pass-entries' to | ||
| 71 | test code without touching the file system." | ||
| 72 | (declare (indent 3)) | ||
| 73 | `(ert-deftest ,name ,arglist | ||
| 74 | (cl-letf (((symbol-function 'auth-source-pass-parse-entry) (lambda (entry) (cdr (cl-find entry ,store :key #'car :test #'string=))) ) | ||
| 75 | ((symbol-function 'auth-source-pass-entries) (lambda () (mapcar #'car ,store))) | ||
| 76 | ((symbol-function 'auth-source-pass--entry-valid-p) (lambda (_entry) t))) | ||
| 77 | (let ((auth-source-debug #'auth-source-pass--debug) | ||
| 78 | (auth-source-pass--debug-log nil)) | ||
| 79 | ,@body)))) | ||
| 80 | |||
| 81 | (auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name () | ||
| 82 | '(("foo")) | ||
| 83 | (should (equal (auth-source-pass--find-match "foo" nil) | ||
| 84 | "foo"))) | ||
| 85 | |||
| 86 | (auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-part () | ||
| 87 | '(("foo")) | ||
| 88 | (should (equal (auth-source-pass--find-match "https://foo" nil) | ||
| 89 | "foo"))) | ||
| 90 | |||
| 91 | (auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-ignoring-user () | ||
| 92 | '(("foo")) | ||
| 93 | (should (equal (auth-source-pass--find-match "https://SomeUser@foo" nil) | ||
| 94 | "foo"))) | ||
| 95 | |||
| 96 | (auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-with-user () | ||
| 97 | '(("SomeUser@foo")) | ||
| 98 | (should (equal (auth-source-pass--find-match "https://SomeUser@foo" nil) | ||
| 99 | "SomeUser@foo"))) | ||
| 100 | |||
| 101 | (auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-prefer-full () | ||
| 102 | '(("SomeUser@foo") ("foo")) | ||
| 103 | (should (equal (auth-source-pass--find-match "https://SomeUser@foo" nil) | ||
| 104 | "SomeUser@foo"))) | ||
| 105 | |||
| 106 | ;; same as previous one except the store is in another order | ||
| 107 | (auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-prefer-full-reversed () | ||
| 108 | '(("foo") ("SomeUser@foo")) | ||
| 109 | (should (equal (auth-source-pass--find-match "https://SomeUser@foo" nil) | ||
| 110 | "SomeUser@foo"))) | ||
| 111 | |||
| 112 | (auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-without-subdomain () | ||
| 113 | '(("bar.com")) | ||
| 114 | (should (equal (auth-source-pass--find-match "foo.bar.com" nil) | ||
| 115 | "bar.com"))) | ||
| 116 | |||
| 117 | (auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-without-subdomain-with-user () | ||
| 118 | '(("someone@bar.com")) | ||
| 119 | (should (equal (auth-source-pass--find-match "foo.bar.com" "someone") | ||
| 120 | "someone@bar.com"))) | ||
| 121 | |||
| 122 | (auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-without-subdomain-with-bad-user () | ||
| 123 | '(("someoneelse@bar.com")) | ||
| 124 | (should (equal (auth-source-pass--find-match "foo.bar.com" "someone") | ||
| 125 | nil))) | ||
| 126 | |||
| 127 | (auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-without-subdomain-prefer-full () | ||
| 128 | '(("bar.com") ("foo.bar.com")) | ||
| 129 | (should (equal (auth-source-pass--find-match "foo.bar.com" nil) | ||
| 130 | "foo.bar.com"))) | ||
| 131 | |||
| 132 | (auth-source-pass--deftest auth-source-pass-dont-match-at-folder-name () | ||
| 133 | '(("foo.bar.com/foo")) | ||
| 134 | (should (equal (auth-source-pass--find-match "foo.bar.com" nil) | ||
| 135 | nil))) | ||
| 136 | |||
| 137 | (auth-source-pass--deftest auth-source-pass-search-with-user-first () | ||
| 138 | '(("foo") ("user@foo")) | ||
| 139 | (should (equal (auth-source-pass--find-match "foo" "user") | ||
| 140 | "user@foo")) | ||
| 141 | (auth-source-pass--should-have-message-containing "Found 1 match")) | ||
| 142 | |||
| 143 | (auth-source-pass--deftest auth-source-pass-give-priority-to-desired-user () | ||
| 144 | '(("foo") ("subdir/foo" ("user" . "someone"))) | ||
| 145 | (should (equal (auth-source-pass--find-match "foo" "someone") | ||
| 146 | "subdir/foo")) | ||
| 147 | (auth-source-pass--should-have-message-containing "Found 2 matches") | ||
| 148 | (auth-source-pass--should-have-message-containing "matching user field")) | ||
| 149 | |||
| 150 | (auth-source-pass--deftest auth-source-pass-give-priority-to-desired-user-reversed () | ||
| 151 | '(("foo" ("user" . "someone")) ("subdir/foo")) | ||
| 152 | (should (equal (auth-source-pass--find-match "foo" "someone") | ||
| 153 | "foo")) | ||
| 154 | (auth-source-pass--should-have-message-containing "Found 2 matches") | ||
| 155 | (auth-source-pass--should-have-message-containing "matching user field")) | ||
| 156 | |||
| 157 | (auth-source-pass--deftest auth-source-pass-return-first-when-several-matches () | ||
| 158 | '(("foo") ("subdir/foo")) | ||
| 159 | (should (equal (auth-source-pass--find-match "foo" nil) | ||
| 160 | "foo")) | ||
| 161 | (auth-source-pass--should-have-message-containing "Found 2 matches") | ||
| 162 | (auth-source-pass--should-have-message-containing "the first one")) | ||
| 163 | |||
| 164 | (auth-source-pass--deftest auth-source-pass-make-divansantana-happy () | ||
| 165 | '(("host.com")) | ||
| 166 | (should (equal (auth-source-pass--find-match "smtp.host.com" "myusername@host.co.za") | ||
| 167 | "host.com"))) | ||
| 168 | |||
| 169 | (ert-deftest auth-source-pass-hostname () | ||
| 170 | (should (equal (auth-source-pass--hostname "https://foo.bar") "foo.bar")) | ||
| 171 | (should (equal (auth-source-pass--hostname "http://foo.bar") "foo.bar")) | ||
| 172 | (should (equal (auth-source-pass--hostname "https://SomeUser@foo.bar") "foo.bar"))) | ||
| 173 | |||
| 174 | (ert-deftest auth-source-pass-hostname-with-user () | ||
| 175 | (should (equal (auth-source-pass--hostname-with-user "https://foo.bar") "foo.bar")) | ||
| 176 | (should (equal (auth-source-pass--hostname-with-user "http://foo.bar") "foo.bar")) | ||
| 177 | (should (equal (auth-source-pass--hostname-with-user "https://SomeUser@foo.bar") "SomeUser@foo.bar"))) | ||
| 178 | |||
| 179 | (defmacro auth-source-pass--deftest-build-result (name arglist store &rest body) | ||
| 180 | "Define a new ert-test NAME with ARGLIST using STORE as password-store. | ||
| 181 | BODY is a sequence of instructions that will be evaluated. | ||
| 182 | |||
| 183 | This macro overrides `auth-source-pass-parse-entry', | ||
| 184 | `auth-source-pass-entries', and `auth-source-pass--find-match' to | ||
| 185 | ease testing." | ||
| 186 | (declare (indent 3)) | ||
| 187 | `(auth-source-pass--deftest ,name ,arglist ,store | ||
| 188 | (cl-letf (((symbol-function 'auth-source-pass-find-match) | ||
| 189 | (lambda (_host _user) | ||
| 190 | "foo"))) | ||
| 191 | ,@body))) | ||
| 192 | |||
| 193 | (auth-source-pass--deftest-build-result auth-source-pass-build-result-return-parameters () | ||
| 194 | '(("foo")) | ||
| 195 | (let ((result (auth-source-pass--build-result "foo" 512 "user"))) | ||
| 196 | (should (equal (plist-get result :port) 512)) | ||
| 197 | (should (equal (plist-get result :user) "user")))) | ||
| 198 | |||
| 199 | (auth-source-pass--deftest-build-result auth-source-pass-build-result-return-entry-values () | ||
| 200 | '(("foo" ("port" . 512) ("user" . "anuser"))) | ||
| 201 | (let ((result (auth-source-pass--build-result "foo" nil nil))) | ||
| 202 | (should (equal (plist-get result :port) 512)) | ||
| 203 | (should (equal (plist-get result :user) "anuser")))) | ||
| 204 | |||
| 205 | (auth-source-pass--deftest-build-result auth-source-pass-build-result-entry-takes-precedence () | ||
| 206 | '(("foo" ("port" . 512) ("user" . "anuser"))) | ||
| 207 | (let ((result (auth-source-pass--build-result "foo" 1024 "anotheruser"))) | ||
| 208 | (should (equal (plist-get result :port) 512)) | ||
| 209 | (should (equal (plist-get result :user) "anuser")))) | ||
| 210 | |||
| 211 | (ert-deftest auth-source-pass-only-return-entries-that-can-be-open () | ||
| 212 | (cl-letf (((symbol-function 'auth-source-pass-entries) | ||
| 213 | (lambda () '("foo.site.com" "bar.site.com"))) | ||
| 214 | ((symbol-function 'auth-source-pass--entry-valid-p) | ||
| 215 | ;; only foo.site.com is valid | ||
| 216 | (lambda (entry) (string-equal entry "foo.site.com")))) | ||
| 217 | (should (equal (auth-source-pass--find-all-by-entry-name "foo.site.com") | ||
| 218 | '("foo.site.com"))) | ||
| 219 | (should (equal (auth-source-pass--find-all-by-entry-name "bar.site.com") | ||
| 220 | '())))) | ||
| 221 | |||
| 222 | (ert-deftest auth-source-pass-entry-is-not-valid-when-unreadable () | ||
| 223 | (cl-letf (((symbol-function 'auth-source-pass--read-entry) | ||
| 224 | (lambda (entry) | ||
| 225 | ;; only foo is a valid entry | ||
| 226 | (if (string-equal entry "foo") | ||
| 227 | "password" | ||
| 228 | nil)))) | ||
| 229 | (should (auth-source-pass--entry-valid-p "foo")) | ||
| 230 | (should-not (auth-source-pass--entry-valid-p "bar")))) | ||
| 231 | |||
| 232 | (provide 'auth-source-pass-tests) | ||
| 233 | |||
| 234 | ;;; auth-source-pass-tests.el ends here | ||