diff options
| author | Craig Jennings <c@cjennings.net> | 2026-05-03 19:40:21 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-05-03 19:40:21 -0500 |
| commit | ac1d0edabc8e30b3781ea7e491030e5bf02d2cae (patch) | |
| tree | 90e379b71293b68af973edfb5b0b2df91485f100 /tests/test-auth-config-oauth2-auto-plstore-read-fixed.el | |
| parent | e646995e940281e0ef575abc71532ac17f1b4fa9 (diff) | |
| download | dotemacs-ac1d0edabc8e30b3781ea7e491030e5bf02d2cae.tar.gz dotemacs-ac1d0edabc8e30b3781ea7e491030e5bf02d2cae.zip | |
test: cover auth-config helpers and oauth2 cache fix
I added 13 new tests across 5 files, covering the auth-config functions that lacked tests. Categories follow Normal / Boundary / Error where applicable.
`cj/toggle-auth-source-debug` flips state once and back through two toggles. `cj/oauth2-auto--plstore-read-fixed` gets three tests: cache miss reads then caches, cache hit skips `plstore-open` entirely, and the `unwind-protect` runs `plstore-close` even when `plstore-get` signals.
`cj/reset-auth-cache` covers no-prefix (skip `shell-command`), with-prefix success, and with-prefix shell failure (Emacs caches still clear). `cj/kill-gpg-agent` covers shell exit 0 and non-zero. `cj/clear-oauth2-auto-cache` covers bound-with-entries, bound-but-empty, and unbound. The unbound test restores the binding via `unwind-protect` so other tests in the same Emacs session don't void-variable.
I stubbed every boundary via `cl-letf` (`plstore-open`, `plstore-get`, `plstore-close`, `oauth2-auto--compute-id`, `auth-source-forget-all-cached`, `epa-file-clear-cache`, `shell-command`, `message`, `call-process`) and didn't stub any internal helpers.
15 auth-config tests pass together: 2 existing plus 13 new.
Diffstat (limited to 'tests/test-auth-config-oauth2-auto-plstore-read-fixed.el')
| -rw-r--r-- | tests/test-auth-config-oauth2-auto-plstore-read-fixed.el | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/tests/test-auth-config-oauth2-auto-plstore-read-fixed.el b/tests/test-auth-config-oauth2-auto-plstore-read-fixed.el new file mode 100644 index 000000000..25446dec5 --- /dev/null +++ b/tests/test-auth-config-oauth2-auto-plstore-read-fixed.el @@ -0,0 +1,86 @@ +;;; test-auth-config-oauth2-auto-plstore-read-fixed.el --- Tests for oauth2 cache fix -*- lexical-binding: t; -*- + +;;; Commentary: +;; Confirms the oauth2-auto plstore-read advice caches reads on miss, +;; skips plstore-open on hit, and runs plstore-close even when +;; plstore-get signals. + +;;; Code: + +(require 'cl-lib) +(require 'ert) +(require 'auth-source) + +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) + +(defvar oauth2-auto--plstore-cache nil + "Stub for oauth2-auto's in-memory token cache.") + +(defvar oauth2-auto-plstore "/tmp/test-oauth2-stub.plist" + "Stub for oauth2-auto plstore path.") + +(defun test-auth-config-oauth2--load () + "Load auth-config with external process calls stubbed." + (cl-letf (((symbol-function 'call-process) + (lambda (&rest _args) 0))) + (load (expand-file-name "modules/auth-config.el" user-emacs-directory) + nil t)) + (setq oauth2-auto--plstore-cache (make-hash-table :test 'equal))) + +(ert-deftest test-auth-config-oauth2-plstore-read-normal-cache-miss-reads-and-caches () + "Normal: cache miss opens plstore, reads value, caches, returns value." + (test-auth-config-oauth2--load) + (let ((opens 0) + (closes 0)) + (cl-letf (((symbol-function 'require) (lambda (&rest _) t)) + ((symbol-function 'oauth2-auto--compute-id) + (lambda (_u _p) "stable-id")) + ((symbol-function 'plstore-open) + (lambda (_) (cl-incf opens) 'fake-plstore)) + ((symbol-function 'plstore-get) + (lambda (_p id) (cons id "secret-value"))) + ((symbol-function 'plstore-close) + (lambda (_) (cl-incf closes)))) + (let ((result (cj/oauth2-auto--plstore-read-fixed "u" "p"))) + (should (equal result "secret-value")) + (should (= opens 1)) + (should (= closes 1)) + (should (equal (gethash "stable-id" oauth2-auto--plstore-cache) + "secret-value")))))) + +(ert-deftest test-auth-config-oauth2-plstore-read-boundary-cache-hit-skips-plstore () + "Boundary: cache hit returns cached value without opening plstore." + (test-auth-config-oauth2--load) + (puthash "stable-id" "cached-value" oauth2-auto--plstore-cache) + (let ((opens 0)) + (cl-letf (((symbol-function 'require) (lambda (&rest _) t)) + ((symbol-function 'oauth2-auto--compute-id) + (lambda (_u _p) "stable-id")) + ((symbol-function 'plstore-open) + (lambda (_) (cl-incf opens) 'fake-plstore)) + ((symbol-function 'plstore-get) + (lambda (_p _id) (error "plstore-get should not be called"))) + ((symbol-function 'plstore-close) + (lambda (_) nil))) + (let ((result (cj/oauth2-auto--plstore-read-fixed "u" "p"))) + (should (equal result "cached-value")) + (should (zerop opens)))))) + +(ert-deftest test-auth-config-oauth2-plstore-read-error-plstore-close-runs-after-get-fails () + "Error: plstore-close runs even when plstore-get signals an error." + (test-auth-config-oauth2--load) + (let ((closes 0)) + (cl-letf (((symbol-function 'require) (lambda (&rest _) t)) + ((symbol-function 'oauth2-auto--compute-id) + (lambda (_u _p) "stable-id")) + ((symbol-function 'plstore-open) + (lambda (_) 'fake-plstore)) + ((symbol-function 'plstore-get) + (lambda (_p _id) (error "boom"))) + ((symbol-function 'plstore-close) + (lambda (_) (cl-incf closes)))) + (should-error (cj/oauth2-auto--plstore-read-fixed "u" "p")) + (should (= closes 1))))) + +(provide 'test-auth-config-oauth2-auto-plstore-read-fixed) +;;; test-auth-config-oauth2-auto-plstore-read-fixed.el ends here |
