Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #14 from Kinto/fix-heartbeat-response
Browse files Browse the repository at this point in the history
Fix heartbeat behaviour
  • Loading branch information
leplatrem authored Nov 3, 2016
2 parents 27b51b1 + a260f3c commit db514b0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ This document describes changes between each past release.
0.3.0 (unreleased)
------------------

- Nothing changed yet.
**Bug fixes**

- Fix heartbeat that would always return False


0.2.0 (2016-11-02)
Expand Down
2 changes: 2 additions & 0 deletions kinto_ldap/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def ldap_ping(request):
try:
with cm.connection('mail=mail@test,o=com,dc=test', 'password'):
ldap = True
except INVALID_CREDENTIALS:
ldap = True
except Exception:
logger.exception("Heartbeat Failure")
ldap = False
Expand Down
17 changes: 0 additions & 17 deletions kinto_ldap/tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,3 @@ def test_forget_uses_realm(self):
headers = policy.forget(self.request)
self.assertEqual(headers[0],
('WWW-Authenticate', 'Basic realm="Who"'))


class LDAPPingTest(unittest.TestCase):
def setUp(self):
self.request = DummyRequest()
self.request.registry.settings = DEFAULT_SETTINGS

def test_returns_true_if_ok(self):
mocked = mock.MagicMock()
self.request.registry.ldap_cm.connection \
.return_value.__enter__.return_value = mocked
self.assertTrue(authentication.ldap_ping(self.request))

def test_returns_false_if_ko(self):
self.request.registry.ldap_cm.connection \
.return_value.__enter__.side_effect = ldap.INVALID_CREDENTIALS
self.assertFalse(authentication.ldap_ping(self.request))
25 changes: 22 additions & 3 deletions kinto_ldap/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import unittest

import mock

import kinto.core
import webtest
from kinto.core.utils import random_bytes_hex
from pyramid.config import Configurator
from ldap import INVALID_CREDENTIALS

from kinto_ldap import __version__ as ldap_version

Expand Down Expand Up @@ -81,10 +84,26 @@ class HeartbeatTest(BaseWebTest, unittest.TestCase):

def get_app_settings(self, extras=None):
settings = super(HeartbeatTest, self).get_app_settings(extras)
settings['ldap.endpoint'] = 'ldap://ldap.with.unreachable.server.com'
settings['ldap.pool_timeout'] = '1'
return settings

def test_heartbeat_returns_false(self):
resp = self.app.get('/__heartbeat__', status=503)
def test_heartbeat_returns_false_if_unreachable(self):
unreachable = 'ldap://ldap.with.unreachable.server.com'
app = self._get_test_app(settings={'ldap.endpoint': unreachable})
resp = app.get('/__heartbeat__', status=503)
heartbeat = resp.json['ldap']
self.assertFalse(heartbeat)

def test_heartbeat_returns_true_if_test_credentials_are_valid(self):
self.app.app.registry.ldap_cm = mock.MagicMock()
resp = self.app.get('/__heartbeat__')
heartbeat = resp.json['ldap']
self.assertTrue(heartbeat)

def test_heartbeat_returns_true_if_credentials_are_invalid(self):
self.app.app.registry.ldap_cm = mock.MagicMock()
self.app.app.registry.ldap_cm.connection \
.return_value.__enter__.side_effect = INVALID_CREDENTIALS
resp = self.app.get('/__heartbeat__')
heartbeat = resp.json['ldap']
self.assertTrue(heartbeat)

0 comments on commit db514b0

Please sign in to comment.