Esempio n. 1
0
    def put(self):
        try:
            uuid.UUID(self.password_id)
        except ValueError:
            return invalid_password_id()

        password = self._get_password()
        if password is None:
            return password_not_found()

        else:
            cleaned_data, errors = validate_password(self.request.body,
                                                     self.request.charset)

            if errors:
                result = {'message': ','.join(errors)}
                return HTTPBadRequest(body=json.dumps(result),
                                      charset='utf8',
                                      content_type='application/json')

            password.secret = cleaned_data['secret']
            password.service = cleaned_data['service']
            password.account = cleaned_data['account']
            password.expiration = cleaned_data['expiration']
            password.notes = cleaned_data['notes']
            password.tags = cleaned_data['tags']

            Session.add(password)

            return {'password': password.as_dict()}
Esempio n. 2
0
    def put(self):
        try:
            uuid.UUID(self.password_id)
        except ValueError:
            return invalid_password_id()

        password = self._get_password()
        if password is None:
            return password_not_found()

        else:
            cleaned_data, errors = validate_password(self.request.body,
                                                     self.request.charset)

            if errors:
                result = {'message': ','.join(errors)}
                return HTTPBadRequest(body=json.dumps(result),
                                      charset='utf8',
                                      content_type='application/json')

            password.secret = cleaned_data['secret']
            password.service = cleaned_data['service']
            password.account = cleaned_data['account']
            password.expiration = cleaned_data['expiration']
            password.notes = cleaned_data['notes']
            password.tags = cleaned_data['tags']

            Session.add(password)

            return {'password': password.as_dict()}
 def test_everything_fine(self):
     password, errors = validate_password(b'{"password": {"secret": "s3cr3t", "service": "myservice"}}')
     self.assertEqual(errors, [])
     self.assertEqual(password, {
         'secret': 's3cr3t',
         'service': 'myservice',
         'account': '',
         'expiration': None,
         'notes': '',
         'tags': [],
     })
Esempio n. 4
0
    def post(self):
        user = authorize_user(self.request)
        password, errors = validate_password(self.request.body,
                                             self.request.charset)

        if errors:
            result = {'message': ','.join(errors)}
            return HTTPBadRequest(body=json.dumps(result),
                                  content_type='application/json')

        return self.passwords_manager.create(user, password)
Esempio n. 5
0
    def post(self):
        password, errors = validate_password(self.request.body,
                                             self.request.charset)

        if errors:
            result = {'message': ','.join(errors)}
            return HTTPBadRequest(body=json.dumps(result),
                                  content_type='application/json')

        result = self.passwords_manager.create(self.request.user, password)
        result['id'] = result['_id']
        return {'password': result}
 def test_everything_fine(self):
     password, errors = validate_password(
         b'{"password": {"secret": "s3cr3t", "service": "myservice"}}')
     self.assertEqual(errors, [])
     self.assertEqual(
         password, {
             'secret': 's3cr3t',
             'service': 'myservice',
             'account': '',
             'expiration': None,
             'notes': '',
             'tags': [],
         })
Esempio n. 7
0
    def post(self):
        cleaned_data, errors = validate_password(self.request.body,
                                                 self.request.charset)

        if errors:
            result = {'message': ','.join(errors)}
            return HTTPBadRequest(body=json.dumps(result),
                                  content_type='application/json')

        password = Password(**cleaned_data)
        self.request.user.passwords.append(password)
        Session.add(password)
        Session.flush()

        return {'password': password.as_dict()}
    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')
        self.assertEqual(errors, ['Secret is required',
                                  'Service is required'])

        # service is missing
        password, errors = validate_password(b'{"_id": "1", "secret": "s3cr3t"}', _id='1')
        self.assertEqual(errors, ['Service is required'])

        # everything is fine
        password, errors = validate_password(b'{"_id": "1", "secret": "s3cr3t", "service": "myservice"}', _id='1')
        self.assertEqual(errors, [])
        self.assertEqual(password, {
                '_id': '1',
                'secret': 's3cr3t',
                'service': 'myservice',
                'account': None,
                'expiration': None,
                'notes': None,
                'tags': None,
                'creation': None,
                'last_modification': None,
                })
Esempio n. 9
0
    def put(self):
        user = authorize_user(self.request)
        try:
            _id = bson.ObjectId(self.password_id)
        except bson.errors.InvalidId:
            return invalid_password_id()

        password, errors = validate_password(self.request.body,
                                             self.request.charset,
                                             _id)

        if errors:
            result = {'message': ','.join(errors)}
            return HTTPBadRequest(body=json.dumps(result),
                                  content_type='application/json')

        result = self.passwords_manager.update(user, _id, password)
        if result is None:
            return password_not_found()
        else:
            return result
Esempio n. 10
0
    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'])

        # no password
        password, errors = validate_password(b'{"foo": "bar"}')
        self.assertEqual(password, {})
        self.assertEqual(errors, ['There must be only one toplevel element called "password"'])

        # secret is missing
        password, errors = validate_password(b'{"password": {}}', _id='1')
        self.assertEqual(errors, ['Secret is required',
                                  'Service is required'])

        # service is missing
        password, errors = validate_password(b'{"password": {"secret": "s3cr3t"}}', _id='1')
        self.assertEqual(errors, ['Service is required'])

        # everything is fine
        password, errors = validate_password(b'{"password": {"secret": "s3cr3t", "service": "myservice"}}', _id='1')
        self.assertEqual(errors, [])
        self.assertEqual(password, {
            'secret': 's3cr3t',
            'service': 'myservice',
            'account': None,
            'expiration': None,
            'notes': None,
            'tags': None,
            'creation': None,
            'last_modification': None,
        })
 def test_service_missing(self):
     password, errors = validate_password(
         b'{"password": {"secret": "s3cr3t"}}')
     self.assertEqual(errors, ['Service is required'])
 def test_service_missing(self):
     password, errors = validate_password(b'{"password": {"secret": "s3cr3t"}}')
     self.assertEqual(errors, ['Service is required'])
 def test_secret_missing(self):
     password, errors = validate_password(b'{"password": {}}')
     self.assertEqual(errors, ['Secret is required',
                               'Service is required'])
 def test_no_password(self):
     password, errors = validate_password(b'{"foo": "bar"}')
     self.assertEqual(password, {})
     self.assertEqual(errors, ['There must be only one toplevel element called "password"'])
 def test_bad_json(self):
     password, errors = validate_password(b'[1')
     self.assertEqual(password, {})
     self.assertEqual(errors, ['No JSON object could be decoded'])
 def test_bad_json(self):
     password, errors = validate_password(b'[1')
     self.assertEqual(password, {})
     self.assertEqual(errors, ['No JSON object could be decoded'])
 def test_secret_missing(self):
     password, errors = validate_password(b'{"password": {}}')
     self.assertEqual(errors, ['Secret is required', 'Service is required'])
 def test_no_password(self):
     password, errors = validate_password(b'{"foo": "bar"}')
     self.assertEqual(password, {})
     self.assertEqual(
         errors,
         ['There must be only one toplevel element called "password"'])