# Path: habitat/views/utils.py
# def read_json_schema(schemaname):
# def must_be_admin(user,
#                   msg="Only server administrators may edit this document."):
# def _validate_timestr(data):
# def _validate_base64(data):
# def _validate_timezone(data):
# def _validate_formats(data, schema):
# def validate_doc(data, schema):
# def only_validates(doc_type):
#     def decorator(func):
#         def wrapped(new, old, userctx, secobj):


import copy
from nose.tools import assert_raises
from couch_named_python import UnauthorizedError, ForbiddenError
from ...views import utils
    utils.must_be_admin(alsoadmin)

def test_validate_doc():
    schema = {
        "type": "object",
        "additionalProperties": False,
        "required": True,
        "properties": {
            "test": {
                "type": "string",
                "required": True
            },
            "opt": {
                "type": "number"
            }
        }
    }

    ok = {"test": "hello"}
    wopt = {"test": "hi", "opt": 123}
    bad = {"not test": "hello"}
    badopt = {"test": "hey", "opt": "oh no a string"}
    multibad = {"not test": "hello", "opt": "not a number"}
    extras = {"test": "heya", "opt": 123, "extra": "not allowed!"}

    utils.validate_doc(ok, schema)
    utils.validate_doc(wopt, schema)
    assert_raises(ForbiddenError, utils.validate_doc, bad, schema)
    assert_raises(ForbiddenError, utils.validate_doc, badopt, schema)
    assert_raises(ForbiddenError, utils.validate_doc, multibad, schema)

# Path: habitat/views/utils.py
# def read_json_schema(schemaname):
# def must_be_admin(user,
#                   msg="Only server administrators may edit this document."):
# def _validate_timestr(data):
# def _validate_base64(data):
# def _validate_timezone(data):
# def _validate_formats(data, schema):
# def validate_doc(data, schema):
# def only_validates(doc_type):
#     def decorator(func):
#         def wrapped(new, old, userctx, secobj):


# Path: habitat/tests/test_views/test_utils.py
import copy
from nose.tools import assert_raises
from couch_named_python import UnauthorizedError, ForbiddenError
from ...views import utils

    utils.must_be_admin(alsoadmin)

def test_validate_doc():
    schema = {
        "type": "object",
        "additionalProperties": False,
        "required": True,
        "properties": {
            "test": {
                "type": "string",
                "required": True
            },
            "opt": {
                "type": "number"
            }
        }
    }

    ok = {"test": "hello"}
    wopt = {"test": "hi", "opt": 123}
    bad = {"not test": "hello"}
    badopt = {"test": "hey", "opt": "oh no a string"}
    multibad = {"not test": "hello", "opt": "not a number"}
    extras = {"test": "heya", "opt": 123, "extra": "not allowed!"}

    utils.validate_doc(ok, schema)
    utils.validate_doc(wopt, schema)
    assert_raises(ForbiddenError, utils.validate_doc, bad, schema)
    assert_raises(ForbiddenError, utils.validate_doc, badopt, schema)
    assert_raises(ForbiddenError, utils.validate_doc, multibad, schema)

    assert_raises(ForbiddenError, utils.validate_doc, extras, schema)