@@ -18,13 +18,24 @@ def unique_user_params
18
18
{ user : { login : generate ( :login ) , password : "secret" } }
19
19
end
20
20
21
+ def unique_admin_params
22
+ { admin : { login : generate ( :login ) , password : "secret" } }
23
+ end
24
+
21
25
it "test utility returns valid parameters for successful user login attempts" do
22
26
params = unique_user_params
23
27
create ( :user , login : params [ :user ] [ :login ] , password : params [ :user ] [ :password ] )
24
28
post user_session_path , params : params . to_query
25
29
expect ( response ) . to have_http_status ( :redirect )
26
30
end
27
31
32
+ it "test utility returns valid parameters for successful admin login attempts" do
33
+ params = unique_admin_params
34
+ create ( :admin , login : params [ :admin ] [ :login ] , password : params [ :admin ] [ :password ] )
35
+ post admin_session_path , params : params . to_query
36
+ expect ( response ) . to have_http_status ( :redirect )
37
+ end
38
+
28
39
it "successful response does not include retry-after header" do
29
40
get root_path , env : { "REMOTE_ADDR" => Faker ::Internet . unique . public_ip_v4_address }
30
41
expect ( response ) . to have_http_status ( :ok )
@@ -113,4 +124,70 @@ def unique_user_params
113
124
expect ( response ) . to have_http_status ( :ok )
114
125
end
115
126
end
116
- end
127
+
128
+ context "when there have been max admin login attempts from an IP address" do
129
+ let ( :ip ) { Faker ::Internet . unique . public_ip_v4_address }
130
+
131
+ before do
132
+ 10 . times do
133
+ post admin_session_path , params : unique_admin_params . to_query , env : { "REMOTE_ADDR" => ip }
134
+ end
135
+ end
136
+
137
+ it "response to the next attempt from the same IP includes retry-after header" do
138
+ post admin_session_path , params : unique_admin_params . to_query , env : { "REMOTE_ADDR" => ip }
139
+ expect ( response ) . to have_http_status ( :too_many_requests )
140
+ expect ( response . headers [ "Retry-After" ] . to_i ) . to be > 0
141
+ expect ( response . headers [ "Retry-After" ] . to_i ) . to be <= 5 . minutes
142
+ end
143
+
144
+ it "throttles the next attempt from the same IP" do
145
+ post admin_session_path , params : unique_admin_params . to_query , env : { "REMOTE_ADDR" => ip }
146
+ expect ( response ) . to have_http_status ( :too_many_requests )
147
+ end
148
+
149
+ it "does not throttle an attempt from a different IP" do
150
+ post admin_session_path , params : unique_admin_params . to_query , env : unique_ip_env
151
+ expect ( response ) . to have_http_status ( :ok )
152
+ end
153
+
154
+ it "does not throttle the next attempt from the same IP after some time" do
155
+ travel 5 . minutes
156
+ post admin_session_path , params : unique_admin_params . to_query , env : { "REMOTE_ADDR" => ip }
157
+ expect ( response ) . to have_http_status ( :ok )
158
+ end
159
+ end
160
+
161
+ context "when there have been max admin login attempts for a username" do
162
+ let ( :params ) { unique_admin_params . to_query }
163
+
164
+ before do
165
+ 10 . times do
166
+ post admin_session_path , params : params , env : unique_ip_env
167
+ end
168
+ end
169
+
170
+ it "response to the next attempt for the same username includes retry-after header" do
171
+ post admin_session_path , params : params , env : unique_ip_env
172
+ expect ( response ) . to have_http_status ( :too_many_requests )
173
+ expect ( response . headers [ "Retry-After" ] . to_i ) . to be > 0
174
+ expect ( response . headers [ "Retry-After" ] . to_i ) . to be <= 5 . minutes
175
+ end
176
+
177
+ it "throttles the next attempt for the same username" do
178
+ post admin_session_path , params : params , env : unique_ip_env
179
+ expect ( response ) . to have_http_status ( :too_many_requests )
180
+ end
181
+
182
+ it "does not throttle an attempt for a different username" do
183
+ post admin_session_path , params : unique_admin_params . to_query , env : unique_ip_env
184
+ expect ( response ) . to have_http_status ( :ok )
185
+ end
186
+
187
+ it "does not throttle the next attempt for the same username after some time" do
188
+ travel 5 . minutes
189
+ post admin_session_path , params : params , env : unique_ip_env
190
+ expect ( response ) . to have_http_status ( :ok )
191
+ end
192
+ end
193
+ end
0 commit comments