Commits

Martin Czygan  committed e1724b3

added load_fixtures

  • Participants
  • Parent commits 88ab47d

Comments (0)

Files changed (4)

File audrid/manage.py

 from audrid import (app, db, models)
 from flask.ext.script import Manager, prompt_bool
 import pyes
+import base64
 import sys
+import json
 
 manager = Manager(app)
 
         db.session.rollback()
         print(exc, file=sys.stderr)
 
+@manager.command
+def load_fixtures():
+    """ Create sample business data.
+    """
+    test_app = app.test_client()
+    def open_with_auth(url, method='GET', username='admin', password='admin', data=None):
+        headers = {
+            'Authorization' : 'Basic %s' % (
+            base64.b64encode("%s:%s" % (username, password)))
+        }
+        print('%s\t%s\t%s' % (url, method, data))
+        return test_app.open(url, method=method, headers=headers, data=data)
+
+    open_with_auth('/api/v1/pools', method='POST', data=json.dumps({'id' : u'123456', 'tasks' : []}))
+    open_with_auth('/api/v1/exams', method='POST', data=json.dumps({"pool_id" : u'123456', 'name' : u'This is a test pool'}))
+    open_with_auth('/api/v1/audits', method='POST', data=json.dumps({"exam_id" : 1, "user_id" : 1}))
+
 if __name__ == "__main__":
     manager.run()

File audrid/models.py

         return '<Document %s, type=%s, length=%s, deleted=%s>' % (
             self.id, self.type, len(self.body), self.deleted)
 
+    def as_dict(self):
+        return {
+            "id" : self.id,
+            "body" : json.loads(self.body),
+            "type" : self.type,
+            "deleted" : self.deleted,
+        }
+
     def to_python(self):
         """ Return the python representation (dictionary) of this document.
         """

File audrid/views/api.py

         return f(*args, **kwargs)
     return decorated_function
 
-@app.route('/api/v1/info', methods=['GET'])
-@authorization_required
+@app.route('/api/v1', methods=['GET'])
 def info(user=None):
-    return jsonify(info='Audrid API', version="1")
+    return jsonify(info='Audrid API', version="1", urls=[ [r.rule] + list(r.methods) for r in app.url_map.iter_rules() ])
+
+@app.route('/api/v1/documents', methods=['GET'])
+def documents_get_all():
+    try:
+        docs = Document.query.filter_by(deleted=False).all()
+        return jsonify(docs=[ d.as_dict() for d in docs ])
+    except Exception as exc:
+        return jsonify(message='storage error: %s' % exc), 500
 
 @app.route('/api/v1/pools', methods=['GET'])
 @authorization_required
         audit_dict = V.audit(json.loads(request.data))
     except Exception as exc:
         return jsonify(message='error during validation: %s' % exc), 400
+
     try:
         exam = Exam.query.filter_by(id=audit_dict['exam_id']).first_or_404()
         pool = Document.query.filter_by(id=exam.pool_id).first_or_404()
         app.logger.error(exc)
         return jsonify(message='storage error: %s' % exc), 400
     return jsonify(message='ok')
+

File tests/test_api.py

         rv = self.open_with_auth('/api/v1/audits', method='POST', data=json.dumps({"exam_id" : Exam.query.first().id, "user_id" : 1}))
         rv = self.open_with_auth('/api/v1/audits', method='DELETE')
 
-        self.assertEqual(2, Document.query.count())
         self.assertEqual(3, Log.query.count())
+        self.assertEqual(2, Document.query.count())
         self.assertEqual(1, Exam.query.count())
         self.assertEqual(0, Audit.query.count())