# Path: yithlibraryserver/password/validation.py
# def validate_password(rawdata, encoding='utf-8', _id=None):
#     errors = []
# 
#     try:
#         data = json.loads(rawdata.decode(encoding))
#     except ValueError:
#         errors.append('No JSON object could be decoded')
# 
#     # if we have errors here, we can't proceed
#     if errors:
#         return {}, errors
# 
#     password = {}
# 
#     # check the password id is the same as in the URL
#     if _id is not None:
#         if '_id' not in data:
#             errors.append('The password id must be in the body')
#         else:
#             if data['_id'] != str(_id):
#                 errors.append('The password id does not match the URL')
#             else:
#                 password['_id'] = _id
# 
#     # white list submission attributes ignoring anything else
#     # first required attributes
#     try:
#         password['secret'] = data['secret']
#     except KeyError:
#         errors.append('Secret is required')
# 
#     try:
#         password['service'] = data['service']
#     except KeyError:
#         errors.append('Service is required')
# 
#     # then optional attributes
#     password['account'] = data.get('account')
#     password['expiration'] = data.get('expiration')
#     password['notes'] = data.get('notes')
#     password['tags'] = data.get('tags')
#     password['last_modification'] = data.get('last_modification')
#     password['creation'] = data.get('creation')
# 
#     return password, errors


import unittest
from yithlibraryserver.password.validation import validate_password




class UtilsTests(unittest.TestCase):

    def test_validate_password(self):
        # empty json
        password, errors = validate_password(b'')
        self.assertEqual(password, {})
        self.assertEqual(errors, ['No JSON object could be decoded'])

        # bad json
        password, errors = validate_password(b'[1')
        self.assertEqual(password, {})
        self.assertEqual(errors, ['No JSON object could be decoded'])

        # id not in the URL
        password, errors = validate_password(b'{}', _id='1')
        self.assertEqual(errors, ['The password id must be in the body',
                                  'Secret is required',
                                  'Service is required'])

        # id doesn't match URL's id
        password, errors = validate_password(b'{"_id": "1"}', _id='2')
        self.assertEqual(errors, ['The password id does not match the URL',
                                  'Secret is required',
                                  'Service is required'])

        # secret is missing

# Path: yithlibraryserver/password/validation.py
# def validate_password(rawdata, encoding='utf-8', _id=None):
#     errors = []
# 
#     try:
#         data = json.loads(rawdata.decode(encoding))
#     except ValueError:
#         errors.append('No JSON object could be decoded')
# 
#     # if we have errors here, we can't proceed
#     if errors:
#         return {}, errors
# 
#     password = {}
# 
#     # check the password id is the same as in the URL
#     if _id is not None:
#         if '_id' not in data:
#             errors.append('The password id must be in the body')
#         else:
#             if data['_id'] != str(_id):
#                 errors.append('The password id does not match the URL')
#             else:
#                 password['_id'] = _id
# 
#     # white list submission attributes ignoring anything else
#     # first required attributes
#     try:
#         password['secret'] = data['secret']
#     except KeyError:
#         errors.append('Secret is required')
# 
#     try:
#         password['service'] = data['service']
#     except KeyError:
#         errors.append('Service is required')
# 
#     # then optional attributes
#     password['account'] = data.get('account')
#     password['expiration'] = data.get('expiration')
#     password['notes'] = data.get('notes')
#     password['tags'] = data.get('tags')
#     password['last_modification'] = data.get('last_modification')
#     password['creation'] = data.get('creation')
# 
#     return password, errors


# Path: yithlibraryserver/password/tests/test_validation.py
import unittest
from yithlibraryserver.password.validation import validate_password





class UtilsTests(unittest.TestCase):

    def test_validate_password(self):
        # empty json
        password, errors = validate_password(b'')
        self.assertEqual(password, {})
        self.assertEqual(errors, ['No JSON object could be decoded'])

        # bad json
        password, errors = validate_password(b'[1')
        self.assertEqual(password, {})
        self.assertEqual(errors, ['No JSON object could be decoded'])

        # id not in the URL
        password, errors = validate_password(b'{}', _id='1')
        self.assertEqual(errors, ['The password id must be in the body',
                                  'Secret is required',
                                  'Service is required'])

        # id doesn't match URL's id
        password, errors = validate_password(b'{"_id": "1"}', _id='2')
        self.assertEqual(errors, ['The password id does not match the URL',
                                  'Secret is required',
                                  'Service is required'])

        # secret is missing

        password, errors = validate_password(b'{"_id": "1"}', _id='1')