|
2 | 2 | (:require [clojure.test :refer :all]
|
3 | 3 | [clj-libssh2.libssh2 :as libssh2]
|
4 | 4 | [clj-libssh2.libssh2.userauth :as libssh2-userauth]
|
5 |
| - [clj-libssh2.session :as session] |
6 | 5 | [clj-libssh2.test-utils :as test])
|
7 | 6 | (:use clj-libssh2.authentication))
|
8 | 7 |
|
9 | 8 | (test/fixtures)
|
10 | 9 |
|
11 |
| -(defn auth |
12 |
| - [creds] |
13 |
| - (is (= 0 (count @session/sessions))) |
14 |
| - (let [session (session/open test/ssh-host test/ssh-port creds {})] |
15 |
| - (is (= 1 (count @session/sessions))) |
16 |
| - (session/close session)) |
17 |
| - (is (= 0 (count @session/sessions)))) |
18 |
| - |
19 | 10 | ; This is more fully tested in clj-libssh2.test-agent
|
20 | 11 | (deftest agent-authentication-works
|
21 |
| - (is (auth (->AgentCredentials (test/ssh-user))))) |
| 12 | + (is (test/auth {:credentials (->AgentCredentials (test/ssh-user))}))) |
22 | 13 |
|
23 | 14 | (deftest key-authentication-works
|
24 | 15 | (let [user (test/ssh-user)
|
|
41 | 32 | bad-pubkey (->KeyCredentials user "" "/bad" (pubkey ""))]
|
42 | 33 | (testing "A passphrase-less key works"
|
43 | 34 | (is (valid? no-passphrase))
|
44 |
| - (is (auth no-passphrase))) |
| 35 | + (is (test/auth {:credentials no-passphrase}))) |
45 | 36 | (testing "A key with a passphrase works"
|
46 | 37 | (is (valid? with-passphrase))
|
47 |
| - (is (auth with-passphrase))) |
| 38 | + (is (test/auth {:credentials with-passphrase}))) |
48 | 39 | (testing "A valid but unauthorized key does not work"
|
49 | 40 | (is (valid? unauthorized))
|
50 |
| - (is (thrown? Exception (auth unauthorized)))) |
| 41 | + (is (thrown? Exception (test/auth {:credentials unauthorized})))) |
51 | 42 | (testing "It fails if the private key file doesn't exist"
|
52 | 43 | (is (valid? bad-privkey))
|
53 |
| - (is (thrown? Exception (auth bad-privkey)))) |
| 44 | + (is (thrown? Exception (test/auth {:credentials bad-privkey})))) |
54 | 45 | (testing "It fails if the public key file doesn't exist"
|
55 | 46 | (is (valid? bad-pubkey))
|
56 |
| - (is (thrown? Exception (auth bad-pubkey)))) |
| 47 | + (is (thrown? Exception (test/auth {:credentials bad-pubkey})))) |
57 | 48 | (testing "It fails if the passphrase is incorrect"
|
58 | 49 | (is (valid? with-wrong-passphrase))
|
59 |
| - (is (thrown? Exception (auth with-wrong-passphrase)))))) |
| 50 | + (is (thrown? Exception (test/auth {:credentials with-wrong-passphrase})))))) |
60 | 51 |
|
61 | 52 | ; We can't test this all the way without knowing a password on the local
|
62 | 53 | ; machine. We can test with libssh2_userauth_password stubbed and some error
|
|
68 | 59 | (->PasswordCredentials (test/ssh-user) password))]
|
69 | 60 | (testing "A successful authentication returns true"
|
70 | 61 | (with-redefs [libssh2-userauth/password (constantly 0)]
|
71 |
| - (is (auth (password-creds "doesn't matter"))))) |
| 62 | + (is (test/auth {:credentials (password-creds "doesn't matter")})))) |
72 | 63 | (testing "It fails to authenticate with the wrong password"
|
73 |
| - (is (thrown? Exception (auth (password-creds "the wrong password"))))) |
| 64 | + (is (thrown? Exception (test/auth {:credentials (password-creds "the wrong password")})))) |
74 | 65 | (testing "A library error does not result in a crash"
|
75 | 66 | (with-redefs [libssh2-userauth/password (constantly libssh2/ERROR_ALLOC)]
|
76 |
| - (is (thrown? Exception (auth (password-creds "doesn't matter")))))))) |
| 67 | + (is (thrown? Exception (test/auth {:credentials (password-creds "doesn't matter")}))))))) |
77 | 68 |
|
78 | 69 | (deftest authenticating-with-a-map-fails-if-there's-no-equivalent-record
|
79 | 70 | (is (thrown-with-msg?
|
80 | 71 | Exception
|
81 | 72 | #"Failed to determine credentials type"
|
82 |
| - (auth {:password "foo"})))) |
| 73 | + (test/auth {:credentials {:username nil |
| 74 | + :password "foo"}})))) |
0 commit comments