def test_require_csrf_token(mocker): mock_fn = mocker.Mock() mock_fn.__name__ = 'mock_fn' mock_fn.return_value = 'unittestval' wrapped = authnz.require_csrf_token(mock_fn) mocker.patch('confidant.authnz.settings.USE_AUTH', False) assert wrapped() == 'unittestval' mocker.patch('confidant.authnz.settings.USE_AUTH', True) g_mock = mocker.patch('confidant.authnz.g') g_mock.auth_type = 'kms' assert wrapped() == 'unittestval' g_mock = mocker.patch('confidant.authnz.g') g_mock.auth_type = 'google oauth' u_mock = mocker.patch('confidant.authnz.user_mod') u_mock.check_csrf_token = mocker.Mock(return_value=True) assert wrapped() == 'unittestval' g_mock = mocker.patch('confidant.authnz.g') g_mock.auth_type = 'google oauth' u_mock = mocker.patch('confidant.authnz.user_mod') u_mock.check_csrf_token = mocker.Mock(return_value=False) with pytest.raises(Unauthorized): wrapped()
def test_require_csrf_token(self): mock_fn = Mock() mock_fn.__name__ = 'mock_fn' mock_fn.return_value = 'unittestval' wrapped = authnz.require_csrf_token(mock_fn) app.config['USE_AUTH'] = False self.assertEqual(wrapped(), 'unittestval') app.config['USE_AUTH'] = True with patch('confidant.authnz.g') as g_mock: g_mock.auth_type = 'kms' self.assertEqual(wrapped(), 'unittestval') with patch('confidant.authnz.g') as g_mock: g_mock.auth_type = 'google oauth' with patch('confidant.authnz.user_mod') as u_mock: u_mock.check_csrf_token = Mock(return_value=True) self.assertEqual(wrapped(), 'unittestval') with patch('confidant.authnz.g') as g_mock: g_mock.auth_type = 'google oauth' with patch('confidant.authnz.user_mod') as u_mock: u_mock.check_csrf_token = Mock(return_value=False) self.assertRaises(Unauthorized, wrapped)