def test_valid_authentication(self): is_valid_mock = mock.MagicMock(return_value=True) builder = EnvironBuilder() request = Request(builder.get_environ()) request.authorization = {'username': '******', 'password': '******'} dummy = DummyAuthentication(valid_auth=True) dummy_auth_mock = mock.MagicMock(return_value=True) dummy.authenticate = dummy_auth_mock auth = And(BasicAuth(is_valid_user=is_valid_mock), dummy) result = auth.authenticate(request) self.assertEqual(result, None) is_valid_mock.assert_called_once_with('john', 'xxx') dummy_auth_mock.assert_called_once_with(request)
def test_not_valid_authentication_second_auth_not_invoked(self): "Should raise an Unthorized exception and avoid invoking other auth strategies" is_valid_mock = mock.MagicMock(return_value=False) builder = EnvironBuilder() request = Request(builder.get_environ()) request.authorization = {'username': '******', 'password': '******'} dummy = DummyAuthentication(valid_auth=True) dummy_auth_mock = mock.MagicMock(return_value=True) dummy.authenticate = dummy_auth_mock auth = And(BasicAuth(is_valid_user=is_valid_mock), dummy) with self.assertRaises(Unauthorized): auth.authenticate(request) is_valid_mock.assert_called_once_with('john', 'xxx') self.assertEqual(dummy_auth_mock.call_count, 0)
def test_valid_authentication(self): is_valid_mock = mock.MagicMock(return_value=True) builder = EnvironBuilder() request = Request(builder.get_environ()) request.authorization = {'username': '******', 'password': '******'} dummy = DummyAuthentication(valid_auth=True) dummy_auth_mock = mock.MagicMock(return_value=True) dummy.authenticate = dummy_auth_mock auth = And( BasicAuth(is_valid_user=is_valid_mock), dummy ) result = auth.authenticate(request) self.assertEqual(result, None) is_valid_mock.assert_called_once_with('john', 'xxx') dummy_auth_mock.assert_called_once_with(request)
def test_not_valid_authentication_second_auth_not_invoked(self): "Should raise an Unthorized exception and avoid invoking other auth strategies" is_valid_mock = mock.MagicMock(return_value=False) builder = EnvironBuilder() request = Request(builder.get_environ()) request.authorization = {'username': '******', 'password': '******'} dummy = DummyAuthentication(valid_auth=True) dummy_auth_mock = mock.MagicMock(return_value=True) dummy.authenticate = dummy_auth_mock auth = And( BasicAuth(is_valid_user=is_valid_mock), dummy ) with self.assertRaises(Unauthorized): auth.authenticate(request) is_valid_mock.assert_called_once_with('john', 'xxx') self.assertEqual(dummy_auth_mock.call_count, 0)