import json
from django.http import Http404

import pytest

from django_healthchecks import views


def test_index_view(rf, settings):
    settings.HEALTH_CHECKS = {
        'database': 'django_healthchecks.contrib.check_dummy_true',
        'redis': 'django_healthchecks.contrib.check_dummy_false',
    }

    request = rf.get('/')
    view = views.HealthCheckView()
    result = view.dispatch(request)

    data = json.loads(result.content.decode(result.charset))
    assert data == {
        'database': True,
        'redis': False,
    }


def test_service_view(rf, settings):
    settings.HEALTH_CHECKS = {
        'database': 'django_healthchecks.contrib.check_dummy_true'
    }

    request = rf.get('/')
    view = views.HealthCheckServiceView()
    result = view.dispatch(request, service='database')

    assert result.status_code == 200
    assert result.content == b'true'


def test_service_view_err(rf, settings):
    settings.HEALTH_CHECKS = {
        'database': 'django_healthchecks.contrib.check_dummy_false'
    }

    request = rf.get('/')
    view = views.HealthCheckServiceView()

    result = view.dispatch(request, service='database')
    assert result.status_code == 200
    assert result.content == b'false'


def test_service_view_err_custom_code(rf, settings):
    settings.HEALTH_CHECKS_ERROR_CODE = 500
    settings.HEALTH_CHECKS = {
        'database': 'django_healthchecks.contrib.check_dummy_false'
    }

    request = rf.get('/')
    view = views.HealthCheckServiceView()

    result = view.dispatch(request, service='database')
    assert result.status_code == 500
    assert result.content == b'false'


def test_service_view_404(rf):
    request = rf.get('/')
    view = views.HealthCheckServiceView()

    with pytest.raises(Http404):
        view.dispatch(request, service='database')


def test_service_require_auth(rf, settings):
    settings.HEALTH_CHECKS = {
        'database': 'django_healthchecks.contrib.check_dummy_true'
    }
    settings.HEALTH_CHECKS_BASIC_AUTH = {
        '*': [('user', 'password')],
    }

    request = rf.get('/')
    view = views.HealthCheckServiceView()

    result = view.dispatch(request, service='database')
    assert result.status_code == 401

@@ -6,24 +6,75 @@
 t json%0Afrom 
+collections import OrderedDict%0A%0Aimport pytest%0Afrom 
 django.http 
@@ -87,31 +87,16 @@
  Http404
-%0A%0Aimport pytest
 %0A%0Afrom d
@@ -636,34 +636,115 @@
 TH_CHECKS = 
-%7B%0A        
+OrderedDict(%5B%0A        ('redis', 'django_healthchecks.contrib.check_dummy_false'),%0A        (
 'database': 
@@ -733,33 +733,33 @@
      ('database'
-:
+,
  'django_healthc
@@ -789,22 +789,25 @@
 my_true'
+),
 %0A    
-%7D
+%5D)
 %0A%0A    re

Add test for #4 (healthcheck detail)