From f80eb9e400dfc3162ef452ff72a7d2403f03b526 Mon Sep 17 00:00:00 2001 From: Sergey Reshetnyak Date: Sat, 10 Oct 2015 21:11:42 +0300 Subject: [PATCH] Add unit tests for AuthValidator middleware Change-Id: I75874de59c75d41715bea6c31e2438b029565d2c --- sahara/tests/unit/api/middleware/__init__.py | 0 .../unit/api/middleware/test_auth_valid.py | 63 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 sahara/tests/unit/api/middleware/__init__.py create mode 100644 sahara/tests/unit/api/middleware/test_auth_valid.py diff --git a/sahara/tests/unit/api/middleware/__init__.py b/sahara/tests/unit/api/middleware/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/sahara/tests/unit/api/middleware/test_auth_valid.py b/sahara/tests/unit/api/middleware/test_auth_valid.py new file mode 100644 index 00000000..ae8276f1 --- /dev/null +++ b/sahara/tests/unit/api/middleware/test_auth_valid.py @@ -0,0 +1,63 @@ +# Copyright (c) 2015 Mirantis Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import webob.dec + +from sahara.api.middleware import auth_valid +from sahara.tests.unit import base as test_base + + +class AuthValidatorTest(test_base.SaharaTestCase): + + @staticmethod + @webob.dec.wsgify + def application(req): + return "Banana" + + def setUp(self): + super(AuthValidatorTest, self).setUp() + self.app = auth_valid.AuthValidator(self.application) + + def test_auth_ok(self): + req = webob.Request.blank("/v1.1/tid/clusters", accept="text/plain", + method="GET", + environ={"HTTP_X_TENANT_ID": "tid"}) + res = req.get_response(self.app) + self.assertEqual(200, res.status_code) + + def test_auth_ok_without_path(self): + req = webob.Request.blank("/", accept="text/plain", method="GET", + environ={"HTTP_X_TENANT_ID": "tid"}) + res = req.get_response(self.app) + self.assertEqual(200, res.status_code) + + def test_auth_without_header(self): + req = webob.Request.blank("/v1.1/tid/clusters", accept="text/plain", + method="GET") + res = req.get_response(self.app) + self.assertEqual(503, res.status_code) + + def test_auth_with_wrong_url(self): + req = webob.Request.blank("/v1.1", accept="text/plain", method="GET", + environ={"HTTP_X_TENANT_ID": "tid"}) + res = req.get_response(self.app) + self.assertEqual(404, res.status_code) + + def test_auth_different_tenant(self): + req = webob.Request.blank("/v1.1/tid1/clusters", accept="text/plain", + method="GET", + environ={"HTTP_X_TENANT_ID": "tid2"}) + res = req.get_response(self.app) + self.assertEqual(401, res.status_code)