-
Notifications
You must be signed in to change notification settings - Fork 12
/
auth_test.clj
213 lines (210 loc) · 11.3 KB
/
auth_test.clj
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
(ns meuse.interceptor.auth-test
(:require [meuse.auth.frontend :as auth-frontend]
[meuse.auth.token :as auth-token]
[meuse.helpers.fixtures :as fixtures]
[meuse.interceptor.auth :as auth]
[meuse.mocks.db :as mocks]
[clj-time.core :as t]
[crypto.password.bcrypt :as bcrypt]
[clojure.test :refer :all])
(:import java.util.UUID))
(def user-id (UUID/randomUUID))
(def token-clear (auth-token/generate-token))
(def token (bcrypt/encrypt token-clear))
(deftest auth-request-test
(testing ":meuse.api.crate.http: skip auth"
(let [itc (auth/auth-request nil nil nil nil)]
(is (= {:request {:subsystem :meuse.api.crate.http
:action :search}}
((:enter itc) {:request {:subsystem :meuse.api.crate.http
:action :search}})))
(is (= {:request {:subsystem :meuse.api.crate.http
:action :download}}
((:enter itc) {:request {:subsystem :meuse.api.crate.http
:action :download}})))))
(testing ":meuse.api.crate.http: valid auth"
(let [token-mock (mocks/token-mock {:get-token-user-role
{:users/name "foo"
:users/active true
:roles/name "tech"
:tokens/token token
:tokens/expired_at
(t/plus (t/now)
(t/months 1))}})
itc (auth/auth-request token-mock nil nil nil)]
(is (= {:request {:subsystem :meuse.api.crate.http
:action :foo
:headers {"authorization" token-clear}
:auth {:role-name "tech", :user-name "foo"}}}
((:enter itc) {:request {:subsystem :meuse.api.crate.http
:action :foo
:headers {"authorization" token-clear}}})))))
(testing ":meuse.api.crate.http: user not active"
(let [token-mock (mocks/token-mock {:get-token-user-role
{:users/name "foo"
:users/active false
:roles/name "tech"
:tokens/token token
:tokens/expired_at
(t/plus (t/now)
(t/months 1))}})
itc (auth/auth-request token-mock nil nil nil)]
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"is not active"
((:enter itc) {:request {:subsystem :meuse.api.crate.http
:action :foo
:headers {"authorization" token-clear}}})))))
(testing ":meuse.api.crate.http: token expired"
(let [token-mock (mocks/token-mock {:get-token-user-role
{:users/name "foo"
:users/active true
:roles/name "tech"
:tokens/token token
:tokens/expired_at
(t/minus (t/now)
(t/months 1))}})
itc (auth/auth-request token-mock nil nil nil)]
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"invalid token"
((:enter itc) {:request {:subsystem :meuse.api.crate.http
:action :foo
:headers {"authorization" token-clear}}})))))
(testing ":meuse.api.crate.http: wrong token"
(let [token-mock (mocks/token-mock {:get-token-user-role
{:users/name "foo"
:users/active true
:roles/name "tech"
:tokens/token "foobar"
:tokens/expired_at
(t/plus (t/now)
(t/months 1))}})
itc (auth/auth-request token-mock nil nil nil)]
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"invalid token"
((:enter itc) {:request {:subsystem :meuse.api.crate.http
:action :foo
:headers {"authorization" token-clear}}})))))
(testing ":meuse.api.meuse.http: skip auth"
(let [itc (auth/auth-request nil nil nil nil)]
(is (= {:request {:subsystem :meuse.api.meuse.http
:action :create-token}}
((:enter itc) {:request {:subsystem :meuse.api.meuse.http
:action :create-token}})))))
(testing ":meuse.api.meuse.http: valid auth"
(let [token-mock (mocks/token-mock {:get-token-user-role
{:users/name "foo"
:users/active true
:roles/name "tech"
:tokens/token token
:tokens/expired_at
(t/plus (t/now)
(t/months 1))}})
itc (auth/auth-request token-mock nil nil nil)]
(is (= {:request {:subsystem :meuse.api.meuse.http
:action :foo
:headers {"authorization" token-clear}
:auth {:role-name "tech", :user-name "foo"}}}
((:enter itc) {:request {:subsystem :meuse.api.meuse.http
:action :foo
:headers {"authorization" token-clear}}})))))
(testing ":meuse.api.meuse.http: user not active"
(let [token-mock (mocks/token-mock {:get-token-user-role
{:users/name "foo"
:users/active false
:roles/name "tech"
:tokens/token token
:tokens/expired_at
(t/plus (t/now)
(t/months 1))}})
itc (auth/auth-request token-mock nil nil nil)]
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"is not active"
((:enter itc) {:request {:subsystem :meuse.api.meuse.http
:action :foo
:headers {"authorization" token-clear}}})))))
(testing ":meuse.api.meuse.http: token expired"
(let [token-mock (mocks/token-mock {:get-token-user-role
{:users/name "foo"
:users/active true
:roles/name "tech"
:tokens/token token
:tokens/expired_at
(t/minus (t/now)
(t/months 1))}})
itc (auth/auth-request token-mock nil nil nil)]
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"invalid token"
((:enter itc) {:request {:subsystem :meuse.api.meuse.http
:action :foo
:headers {"authorization" token-clear}}})))))
(testing ":meuse.api.meuse.http: wrong token"
(let [token-mock (mocks/token-mock {:get-token-user-role
{:users/name "foo"
:users/active true
:roles/name "tech"
:tokens/token "foobar"
:tokens/expired_at
(t/plus (t/now)
(t/months 1))}})
itc (auth/auth-request token-mock nil nil nil)]
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"invalid token"
((:enter itc) {:request {:subsystem :meuse.api.meuse.http
:action :foo
:headers {"authorization" token-clear}}})))))
(testing ":meuse.api.mirror.http: skip auth"
(let [itc (auth/auth-request nil nil nil nil)]
(is (= {:request {:subsystem :meuse.api.mirror.http
:action :download}}
((:enter itc) {:request {:subsystem :meuse.api.mirror.http
:action :download}})))))
(testing ":meise.api.mirror.http: auth needed"
(let [itc (auth/auth-request nil nil nil nil)]
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"token missing in the header"
((:enter itc) {:request {:subsystem :meuse.api.mirror.http
:action :cache}})))))
(testing ":meuse.api.public.http"
(let [itc (auth/auth-request nil nil nil nil)]
(is (= {:request {:subsystem :meuse.api.public.http}}
((:enter itc) {:request {:subsystem :meuse.api.public.http}})))))
(testing "meuse.front.http: skip auth"
(doseq [action auth/front-actions-no-cookie]
(let [itc (auth/auth-request nil nil nil nil)
request {:subsystem :meuse.front.http
:action action
:cookies {}}]
(is (= {:request request} ((:enter itc) {:request request}))))))
(testing "meuse.front.http: invalid cookie"
(let [user-mock (mocks/user-mock {:by-id #:users{:id user-id}})
itc (auth/auth-request nil user-mock fixtures/default-key-spec nil)
request {:subsystem :meuse.front.http
:cookies {"session-token"
{:value "foaazeeaaeaeaeaaaeazazo"}}}]
(is (thrown? Exception
((:enter itc) {:request request})))))
(testing "meuse.front.http: valid cookie"
(let [user-mock (mocks/user-mock {:by-id #:users{:id user-id :name "foo"}})
itc (auth/auth-request nil user-mock fixtures/default-key-spec nil)
request {:subsystem :meuse.front.http
:auth {:user-name "foo"
:user-id user-id}
:cookies {"session-token"
{:value (auth-frontend/generate-token
user-id
fixtures/default-key-spec)}}}]
(Thread/sleep 100)
(is (= {:request request} ((:enter itc) {:request request})))))
(testing "meuse.front.http: auth disabled for the frontend"
(let [user-mock (mocks/user-mock {:by-id #:users{:id user-id :name "foo"}})
itc (auth/auth-request nil user-mock fixtures/default-key-spec true)
request {:subsystem :meuse.front.http}]
(Thread/sleep 100)
(is (= {:request request} ((:enter itc) {:request request}))))))