Use builtin-mock in Api MixedAuth

Change-Id: I25428a7eed5aa1596490f2b13a295c2f7a72a5bd
This commit is contained in:
Paul Millette 2017-01-11 11:11:55 -05:00
parent 2f6e86049b
commit ad1104de6e
2 changed files with 22 additions and 18 deletions

View File

@ -12,11 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from flexmock import flexmock
from hamcrest import assert_that
from hamcrest import calling
from hamcrest import equal_to
from hamcrest import raises
import mock
from almanach.api.auth import mixed_auth
from almanach.core import exception
@ -27,24 +23,34 @@ class TestMixedAuthentication(base.BaseTestCase):
def setUp(self):
super(TestMixedAuthentication, self).setUp()
self.auth_one = flexmock()
self.auth_two = flexmock()
self.auth_one = mock.Mock()
self.auth_two = mock.Mock()
self.auth_backend = mixed_auth.MixedAuthentication([self.auth_one, self.auth_two])
def tearDown(self):
super(TestMixedAuthentication, self).tearDown()
def test_with_token_valid_with_auth_one(self):
token = "my token"
self.auth_one.should_receive("validate").and_return(True)
assert_that(self.auth_backend.validate(token), equal_to(True))
self.auth_one.validate.return_value = True
self.assertEqual(self.auth_backend.validate(token), True)
self.auth_one.validate.assert_called_once()
def test_with_token_valid_with_auth_two(self):
token = "my token"
self.auth_one.should_receive("validate").and_raise(exception.AuthenticationFailureException)
self.auth_two.should_receive("validate").and_return(True)
assert_that(self.auth_backend.validate(token), equal_to(True))
self.auth_one.validate.side_effect = exception.AuthenticationFailureException
self.auth_two.validate.return_value = True
self.assertEqual(self.auth_backend.validate(token), True)
self.auth_one.validate.assert_called_once()
self.auth_two.validate.assert_called_once()
def test_with_token_valid_with_auth_twos(self):
token = "bad token"
self.auth_one.should_receive("validate").and_raise(exception.AuthenticationFailureException)
self.auth_two.should_receive("validate").and_raise(exception.AuthenticationFailureException)
assert_that(calling(self.auth_backend.validate)
.with_args(token), raises(exception.AuthenticationFailureException))
self.auth_one.validate.side_effect = exception.AuthenticationFailureException
self.auth_two.validate.side_effect = exception.AuthenticationFailureException
self.assertRaises(exception.AuthenticationFailureException, self.auth_backend.validate, token)
self.auth_one.validate.assert_called_once()
self.auth_two.validate.assert_called_once()

View File

@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from flexmock import flexmock_teardown
from oslo_config import fixture
import testtools
@ -28,4 +27,3 @@ class BaseTestCase(testtools.TestCase):
def tearDown(self):
super(BaseTestCase, self).tearDown()
flexmock_teardown()