1 ;;; auth-source-pass-tests.el --- Tests for auth-source-pass.el -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2013, 2017 Free Software Foundation, Inc.
5 ;; Author: Damien Cassou <damien.cassou@gmail.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs 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.
14 ;; GNU Emacs 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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;; Tests for auth-source-pass.el
30 (require 'auth-source-pass
)
32 (eval-when-compile (require 'cl-lib
))
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
)
38 ("key2" .
"val2"))))))
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
)
44 ("key2" .
"val2"))))))
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
)
50 ("key2" .
"val2"))))))
52 (defvar auth-source-pass--debug-log nil
53 "Contains a list of all messages passed to `auth-source-do-debug`.")
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
)))
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
))
66 (defmacro auth-source-pass--with-store
(store &rest body
)
67 "Use STORE as password-store while executing BODY."
69 `(cl-letf (((symbol-function 'auth-source-pass-parse-entry
) (lambda (entry) (cdr (cl-find entry
,store
:key
#'car
:test
#'string
=))) )
70 ((symbol-function 'auth-source-pass-entries
) (lambda () (mapcar #'car
,store
)))
71 ((symbol-function 'auth-source-pass--entry-valid-p
) (lambda (_entry) t
)))
72 (let ((auth-source-debug #'auth-source-pass--debug
)
73 (auth-source-pass--debug-log nil
))
76 (ert-deftest auth-source-pass-find-match-matching-at-entry-name
()
77 (auth-source-pass--with-store '(("foo"))
78 (should (equal (auth-source-pass--find-match "foo" nil
)
81 (ert-deftest auth-source-pass-find-match-matching-at-entry-name-part
()
82 (auth-source-pass--with-store '(("foo"))
83 (should (equal (auth-source-pass--find-match "https://foo" nil
)
86 (ert-deftest auth-source-pass-find-match-matching-at-entry-name-ignoring-user
()
87 (auth-source-pass--with-store '(("foo"))
88 (should (equal (auth-source-pass--find-match "https://SomeUser@foo" nil
)
91 (ert-deftest auth-source-pass-find-match-matching-at-entry-name-with-user
()
92 (auth-source-pass--with-store '(("SomeUser@foo"))
93 (should (equal (auth-source-pass--find-match "https://SomeUser@foo" nil
)
96 (ert-deftest auth-source-pass-find-match-matching-at-entry-name-prefer-full
()
97 (auth-source-pass--with-store '(("SomeUser@foo") ("foo"))
98 (should (equal (auth-source-pass--find-match "https://SomeUser@foo" nil
)
101 (ert-deftest auth-source-pass-find-match-matching-at-entry-name-prefer-full-reversed
()
102 (auth-source-pass--with-store '(("foo") ("SomeUser@foo"))
103 (should (equal (auth-source-pass--find-match "https://SomeUser@foo" nil
)
106 (ert-deftest auth-source-pass-find-match-matching-at-entry-name-without-subdomain
()
107 (auth-source-pass--with-store '(("bar.com"))
108 (should (equal (auth-source-pass--find-match "foo.bar.com" nil
)
111 (ert-deftest auth-source-pass-find-match-matching-at-entry-name-without-subdomain-with-user
()
112 (auth-source-pass--with-store '(("someone@bar.com"))
113 (should (equal (auth-source-pass--find-match "foo.bar.com" "someone")
114 "someone@bar.com"))))
116 (ert-deftest auth-source-pass-find-match-matching-at-entry-name-without-subdomain-with-bad-user
()
117 (auth-source-pass--with-store '(("someoneelse@bar.com"))
118 (should (equal (auth-source-pass--find-match "foo.bar.com" "someone")
121 (ert-deftest auth-source-pass-find-match-matching-at-entry-name-without-subdomain-prefer-full
()
122 (auth-source-pass--with-store '(("bar.com") ("foo.bar.com"))
123 (should (equal (auth-source-pass--find-match "foo.bar.com" nil
)
126 (ert-deftest auth-source-pass-dont-match-at-folder-name
()
127 (auth-source-pass--with-store '(("foo.bar.com/foo"))
128 (should (equal (auth-source-pass--find-match "foo.bar.com" nil
)
131 (ert-deftest auth-source-pass-search-with-user-first
()
132 (auth-source-pass--with-store '(("foo") ("user@foo"))
133 (should (equal (auth-source-pass--find-match "foo" "user")
135 (auth-source-pass--should-have-message-containing "Found 1 match")))
137 (ert-deftest auth-source-pass-give-priority-to-desired-user
()
138 (auth-source-pass--with-store '(("foo") ("subdir/foo" ("user" .
"someone")))
139 (should (equal (auth-source-pass--find-match "foo" "someone")
141 (auth-source-pass--should-have-message-containing "Found 2 matches")
142 (auth-source-pass--should-have-message-containing "matching user field")))
144 (ert-deftest auth-source-pass-give-priority-to-desired-user-reversed
()
145 (auth-source-pass--with-store '(("foo" ("user" .
"someone")) ("subdir/foo"))
146 (should (equal (auth-source-pass--find-match "foo" "someone")
148 (auth-source-pass--should-have-message-containing "Found 2 matches")
149 (auth-source-pass--should-have-message-containing "matching user field")))
151 (ert-deftest auth-source-pass-return-first-when-several-matches
()
152 (auth-source-pass--with-store '(("foo") ("subdir/foo"))
153 (should (equal (auth-source-pass--find-match "foo" nil
)
155 (auth-source-pass--should-have-message-containing "Found 2 matches")
156 (auth-source-pass--should-have-message-containing "the first one")))
158 (ert-deftest auth-source-pass-make-divansantana-happy
()
159 (auth-source-pass--with-store '(("host.com"))
160 (should (equal (auth-source-pass--find-match "smtp.host.com" "myusername@host.co.za")
163 (ert-deftest auth-source-pass-hostname
()
164 (should (equal (auth-source-pass--hostname "https://foo.bar") "foo.bar"))
165 (should (equal (auth-source-pass--hostname "http://foo.bar") "foo.bar"))
166 (should (equal (auth-source-pass--hostname "https://SomeUser@foo.bar") "foo.bar")))
168 (ert-deftest auth-source-pass-hostname-with-user
()
169 (should (equal (auth-source-pass--hostname-with-user "https://foo.bar") "foo.bar"))
170 (should (equal (auth-source-pass--hostname-with-user "http://foo.bar") "foo.bar"))
171 (should (equal (auth-source-pass--hostname-with-user "https://SomeUser@foo.bar") "SomeUser@foo.bar")))
173 (defmacro auth-source-pass--with-store-find-foo
(store &rest body
)
174 "Use STORE while executing BODY. \"foo\" is the matched entry."
176 `(auth-source-pass--with-store ,store
177 (cl-letf (((symbol-function 'auth-source-pass-find-match
)
178 (lambda (_host _user
)
182 (ert-deftest auth-source-pass-build-result-return-parameters
()
183 (auth-source-pass--with-store-find-foo '(("foo"))
184 (let ((result (auth-source-pass--build-result "foo" 512 "user")))
185 (should (equal (plist-get result
:port
) 512))
186 (should (equal (plist-get result
:user
) "user")))))
188 (ert-deftest auth-source-pass-build-result-return-entry-values
()
189 (auth-source-pass--with-store-find-foo '(("foo" ("port" .
512) ("user" .
"anuser")))
190 (let ((result (auth-source-pass--build-result "foo" nil nil
)))
191 (should (equal (plist-get result
:port
) 512))
192 (should (equal (plist-get result
:user
) "anuser")))))
194 (ert-deftest auth-source-pass-build-result-entry-takes-precedence
()
195 (auth-source-pass--with-store-find-foo '(("foo" ("port" .
512) ("user" .
"anuser")))
196 (let ((result (auth-source-pass--build-result "foo" 1024 "anotheruser")))
197 (should (equal (plist-get result
:port
) 512))
198 (should (equal (plist-get result
:user
) "anuser")))))
200 (ert-deftest auth-source-pass-only-return-entries-that-can-be-open
()
201 (cl-letf (((symbol-function 'auth-source-pass-entries
)
202 (lambda () '("foo.site.com" "bar.site.com"
203 "mail/baz.site.com/scott")))
204 ((symbol-function 'auth-source-pass--entry-valid-p
)
205 ;; only foo.site.com and "mail/baz.site.com/scott" are valid
206 (lambda (entry) (member entry
'("foo.site.com"
207 "mail/baz.site.com/scott")))))
208 (should (equal (auth-source-pass--find-all-by-entry-name "foo.site.com" "someuser")
210 (should (equal (auth-source-pass--find-all-by-entry-name "bar.site.com" "someuser")
212 (should (equal (auth-source-pass--find-all-by-entry-name "baz.site.com" "scott")
213 '("mail/baz.site.com/scott")))))
215 (ert-deftest auth-source-pass-entry-is-not-valid-when-unreadable
()
216 (cl-letf (((symbol-function 'auth-source-pass--read-entry
)
218 ;; only foo is a valid entry
219 (if (string-equal entry
"foo")
222 (should (auth-source-pass--entry-valid-p "foo"))
223 (should-not (auth-source-pass--entry-valid-p "bar"))))
225 (provide 'auth-source-pass-tests
)
227 ;;; auth-source-pass-tests.el ends here