summaryrefslogtreecommitdiff
path: root/test/functional/api/token_auth_test.rb
diff options
context:
space:
mode:
authorNavaL <ayoyo@thoughtworks.com>2016-07-14 15:44:07 +0200
committerNavaL <ayoyo@thoughtworks.com>2016-07-14 15:44:07 +0200
commite3c2cb91dfef5c39c608b967e702e9de977d1bd2 (patch)
tree154dc28dd986bd6e0a48e933c5da46994ffaa0cb /test/functional/api/token_auth_test.rb
parente2f19bcfb6dbce77746c2d61715340525b29a592 (diff)
parentf09e6ec1337962ab279f021a6a6d0ff30479ebe0 (diff)
Merge branch 'develop' of https://github.com/leapcode/leap_web into feature/expose_admin_in_api
Diffstat (limited to 'test/functional/api/token_auth_test.rb')
-rw-r--r--test/functional/api/token_auth_test.rb40
1 files changed, 40 insertions, 0 deletions
diff --git a/test/functional/api/token_auth_test.rb b/test/functional/api/token_auth_test.rb
new file mode 100644
index 0000000..c7f91c7
--- /dev/null
+++ b/test/functional/api/token_auth_test.rb
@@ -0,0 +1,40 @@
+#
+# tests for authenticating an admin or monitor user
+# via static configured tokens.
+#
+
+require 'test_helper'
+
+class Api::TokenAuthTest < ApiControllerTest
+ tests Api::ConfigsController
+
+ def test_login_via_api_token
+ with_config(:allow_anonymous_certs => false) do
+ monitor_auth do
+ api_get :index
+ assert assigns(:token), 'should have authenticated via api token'
+ assert assigns(:token).is_a? ApiToken
+ assert @controller.send(:current_user).is_a? ApiMonitorUser
+ end
+ end
+ end
+
+ def test_fail_api_auth_when_ip_not_allowed
+ with_config(:allow_anonymous_certs => false) do
+ allowed = "99.99.99.99"
+ new_config = {api_tokens: APP_CONFIG["api_tokens"].merge(allowed_ips: [allowed])}
+ with_config(new_config) do
+ monitor_auth do
+ request.env['REMOTE_ADDR'] = "1.1.1.1"
+ api_get :index
+ assert_nil assigns(:token), "should not be able to auth with api token when ip restriction doesn't allow it"
+ request.env['REMOTE_ADDR'] = allowed
+ api_get :index
+ assert assigns(:token), "should have authenticated via api token"
+ end
+ end
+ end
+ end
+
+end
+