summaryrefslogtreecommitdiff
path: root/tests/test-restclient-config-inject-skyfi-key.el
blob: d471b9138b1a26ebd11c22d85f228a09259759a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
;;; test-restclient-config-inject-skyfi-key.el --- Tests for cj/restclient--inject-skyfi-key -*- lexical-binding: t; -*-

;;; Commentary:
;; Unit tests for cj/restclient--inject-skyfi-key function.
;; Replaces the :skyfi-key placeholder in a restclient buffer with the real
;; API key from authinfo. Tests cover Normal, Boundary, and Error cases.

;;; Code:

(when noninteractive
  (package-initialize))

(require 'ert)
(require 'restclient-config)

;; ---------------------------------------------------------------------------
;; Test Helpers
;; ---------------------------------------------------------------------------

(defvar test-inject--fake-key "sk_test_fake_key_12345"
  "Fake API key used in tests.")

(defmacro test-inject--with-skyfi-buffer (content &rest body)
  "Create a temp buffer with CONTENT simulating skyfi-api.rest, then run BODY.
Sets buffer-file-name to skyfi-api.rest and activates restclient-mode.
Binds `cj/skyfi-api-key' to return `test-inject--fake-key'."
  (declare (indent 1))
  `(with-temp-buffer
     (insert ,content)
     (setq buffer-file-name (expand-file-name "data/skyfi-api.rest" user-emacs-directory))
     (restclient-mode)
     (cl-letf (((symbol-function 'cj/skyfi-api-key)
                (lambda () test-inject--fake-key)))
       ,@body)))

;; ---------------------------------------------------------------------------
;;; Normal Cases
;; ---------------------------------------------------------------------------

(ert-deftest test-inject-skyfi-key-replaces-placeholder ()
  "Replaces :skyfi-key = PLACEHOLDER with real key."
  (test-inject--with-skyfi-buffer
      ":skyfi-key = PLACEHOLDER\n#\nGET https://example.com\n"
    (cj/restclient--inject-skyfi-key)
    (goto-char (point-min))
    (should (string-match-p (format ":skyfi-key = %s" test-inject--fake-key)
                            (buffer-string)))))

(ert-deftest test-inject-skyfi-key-preserves-other-content ()
  "Rest of buffer content unchanged after injection."
  (let ((rest-content "# SkyFi API\nGET https://app.skyfi.com/platform-api/\nAPIKey: :skyfi-key\n"))
    (test-inject--with-skyfi-buffer
        (concat ":skyfi-key = PLACEHOLDER\n" rest-content)
      (cj/restclient--inject-skyfi-key)
      (should (string-match-p "# SkyFi API" (buffer-string)))
      (should (string-match-p "APIKey: :skyfi-key" (buffer-string))))))

(ert-deftest test-inject-skyfi-key-only-replaces-skyfi-key-line ()
  "Does not modify other restclient variable lines."
  (test-inject--with-skyfi-buffer
      ":skyfi-key = PLACEHOLDER\n:other-var = keep-me\n"
    (cj/restclient--inject-skyfi-key)
    (should (string-match-p ":other-var = keep-me" (buffer-string)))))

;; ---------------------------------------------------------------------------
;;; Boundary Cases
;; ---------------------------------------------------------------------------

(ert-deftest test-inject-skyfi-key-no-key-line-no-error ()
  "Buffer with no :skyfi-key line — no change, no error."
  (test-inject--with-skyfi-buffer
      "# Just comments\nGET https://example.com\n"
    (let ((before (buffer-string)))
      (cj/restclient--inject-skyfi-key)
      (should (string= before (buffer-string))))))

(ert-deftest test-inject-skyfi-key-already-has-value ()
  "Buffer where :skyfi-key already has a real value — still replaces (idempotent)."
  (test-inject--with-skyfi-buffer
      ":skyfi-key = old_real_key_abc\n"
    (cj/restclient--inject-skyfi-key)
    (should (string-match-p (format ":skyfi-key = %s" test-inject--fake-key)
                            (buffer-string)))))

(ert-deftest test-inject-skyfi-key-empty-buffer ()
  "Empty buffer — no error."
  (test-inject--with-skyfi-buffer ""
    (cj/restclient--inject-skyfi-key)
    (should (string= "" (buffer-string)))))

(ert-deftest test-inject-skyfi-key-only-first-occurrence ()
  "Multiple :skyfi-key lines — only first replaced."
  (test-inject--with-skyfi-buffer
      ":skyfi-key = PLACEHOLDER\n:skyfi-key = SECOND\n"
    (cj/restclient--inject-skyfi-key)
    (let ((content (buffer-string)))
      (should (string-match-p (format ":skyfi-key = %s" test-inject--fake-key) content))
      (should (string-match-p ":skyfi-key = SECOND" content)))))

;; ---------------------------------------------------------------------------
;;; Error Cases
;; ---------------------------------------------------------------------------

(ert-deftest test-inject-skyfi-key-wrong-mode-no-replacement ()
  "Wrong major mode — no replacement happens."
  (with-temp-buffer
    (insert ":skyfi-key = PLACEHOLDER\n")
    (setq buffer-file-name (expand-file-name "data/skyfi-api.rest" user-emacs-directory))
    (fundamental-mode)
    (let ((before (buffer-string)))
      (cj/restclient--inject-skyfi-key)
      (should (string= before (buffer-string))))))

(ert-deftest test-inject-skyfi-key-wrong-filename-no-replacement ()
  "Wrong filename — no replacement happens."
  (with-temp-buffer
    (insert ":skyfi-key = PLACEHOLDER\n")
    (setq buffer-file-name "/tmp/other-file.rest")
    (restclient-mode)
    (let ((before (buffer-string)))
      (cj/restclient--inject-skyfi-key)
      (should (string= before (buffer-string))))))

(ert-deftest test-inject-skyfi-key-no-filename-no-replacement ()
  "No filename (scratch buffer) — no replacement happens."
  (with-temp-buffer
    (insert ":skyfi-key = PLACEHOLDER\n")
    (restclient-mode)
    (setq buffer-file-name nil)
    (let ((before (buffer-string)))
      (cj/restclient--inject-skyfi-key)
      (should (string= before (buffer-string))))))

(ert-deftest test-inject-skyfi-key-auth-returns-nil-no-error ()
  "Auth-source returns nil — no error, no replacement."
  (with-temp-buffer
    (insert ":skyfi-key = PLACEHOLDER\n")
    (setq buffer-file-name (expand-file-name "data/skyfi-api.rest" user-emacs-directory))
    (restclient-mode)
    (cl-letf (((symbol-function 'cj/skyfi-api-key)
               (lambda () nil)))
      (let ((before (buffer-string)))
        (cj/restclient--inject-skyfi-key)
        (should (string= before (buffer-string)))))))

(provide 'test-restclient-config-inject-skyfi-key)
;;; test-restclient-config-inject-skyfi-key.el ends here