|
28 | 28 | :is_current true}]}) |
29 | 29 |
|
30 | 30 | (defn- build-breach-request |
31 | | - [token-value] |
32 | | - (let [payload [{:token token-value |
33 | | - :type "whatever" |
34 | | - :url "https://github.com/foo/bar"}] |
| 31 | + [& token-values] |
| 32 | + (let [payload (mapv (fn [token-value] |
| 33 | + {:token token-value |
| 34 | + :type "whatever" |
| 35 | + :url "https://github.com/foo/bar"}) |
| 36 | + token-values) |
35 | 37 | payload-str (json/encode payload) |
36 | 38 | sig (dsa/sign payload-str {:key privkey :alg :ecdsa+sha256}) |
37 | 39 | sig-b64 (String. (base64/encode sig))] |
|
59 | 61 | (with-redefs [client/get (constantly {:body github-response})] |
60 | 62 | (testing "when token is enabled" |
61 | 63 | (let [token (db/add-deploy-token help/*db* "ham" "a token" nil nil false nil) |
62 | | - res (app (build-breach-request (:token token))) |
| 64 | + token-str (:token token) |
| 65 | + res (app (build-breach-request token-str)) |
63 | 66 | db-token (find-token "ham" "a token") |
64 | 67 | _ (is (true? (email/wait-for-mock-emails))) |
65 | 68 | [to subject message] (first @email/mock-emails)] |
66 | 69 | (is (= 200 (:status res))) |
| 70 | + (is (= [{:token_raw token-str |
| 71 | + :token_type "whatever" |
| 72 | + :label "true_positive"}] |
| 73 | + (:body res))) |
67 | 74 | (is (:disabled db-token)) |
68 | 75 | (is (= "ham@biscuit.co" to)) |
69 | 76 | (is (= "Deploy token found on GitHub" subject)) |
|
73 | 80 |
|
74 | 81 | (testing "when token is disabled" |
75 | 82 | (let [token (db/add-deploy-token help/*db* "ham" "another token" nil nil false nil) |
| 83 | + token-str (:token token) |
76 | 84 | db-token (find-token "ham" "another token") |
77 | 85 | _ (db/disable-deploy-token help/*db* (:id db-token)) |
78 | 86 | _ (email/expect-mock-emails 1) |
79 | | - res (app (build-breach-request (:token token))) |
| 87 | + res (app (build-breach-request token-str)) |
80 | 88 | _ (is (true? (email/wait-for-mock-emails))) |
81 | 89 | [to subject message] (first @email/mock-emails)] |
82 | 90 | (is (= 200 (:status res))) |
| 91 | + (is (= [{:token_raw token-str |
| 92 | + :token_type "whatever" |
| 93 | + :label "true_positive"}] |
| 94 | + (:body res))) |
83 | 95 | (is (= "ham@biscuit.co" to)) |
84 | 96 | (is (= "Deploy token found on GitHub" subject)) |
85 | 97 | (is (re-find #"'another token'" message)) |
86 | 98 | (is (re-find #"https://github.com/foo/bar" message)) |
87 | | - (is (re-find #"was already disabled" message))))))) |
| 99 | + (is (re-find #"was already disabled" message)))) |
| 100 | + |
| 101 | + (testing "with existing and non-existent tokens" |
| 102 | + (let [token (db/add-deploy-token help/*db* "ham" "a token" nil nil false nil) |
| 103 | + token-str (:token token) |
| 104 | + res (app (build-breach-request token-str "non-existent-token")) |
| 105 | + db-token (find-token "ham" "a token")] |
| 106 | + (is (= 200 (:status res))) |
| 107 | + (is (= [{:token_raw token-str |
| 108 | + :token_type "whatever" |
| 109 | + :label "true_positive"} |
| 110 | + {:token_raw "non-existent-token" |
| 111 | + :token_type "whatever" |
| 112 | + :label "false_positive"}] |
| 113 | + (:body res))) |
| 114 | + (is (:disabled db-token))))))) |
0 commit comments