def events(unsafe_event_id): """ The only way to create new Events on the server. For now, we just create the blob resources separately. Maybe someday they can be created inline too. """ event_id = uuid.UUID(unsafe_event_id) archive = memdam.server.web.utils.get_archive(flask.request.authorization.username) if flask.request.method == 'GET': event = archive.get(event_id) event_json = event.to_json_dict() return flask.jsonify(event_json) elif flask.request.method == 'DELETE': archive.delete(event_id) return '', 204 else: if not flask.request.json: raise memdam.server.web.errors.BadRequest("Must send JSON for events.") assert 'id__id' not in flask.request.json or flask.request.json['id__id'] == event_id.hex, \ "id__id field must be undefined or equal to the id in the event" flask.request.json['id__id'] = event_id.hex #TODO: run more validation on event json event = memdam.common.event.Event.from_json_dict(flask.request.json) archive.save([event]) return '', 204
def test_put_event(): ''' Check that events can be saved to the server ''' app.config['DATABASE_FOLDER'] = ':memory:' app.config['TESTING'] = True username = u'hello' password = u'world' client = app.test_client() #TODO: ew. flask.g = FakeFlaskGlobal() memdam.server.admin.create_archive(username, password) event = memdam.common.event.new(NAMESPACE, cpu__number__percent=0.567) event_json = json.dumps(event.to_json_dict()) result = client.put(u'/api/v1/events/' + event.id__id.hex, data=event_json, headers={ u'Content-Type': u'application/json', u'Authorization': u'Basic ' + base64.b64encode(username + u':' + password) }) assert result.status_code == 204
def test_serialization(): """Check that converting to and from a json dict gives the same object""" event = memdam.common.event.new( u"some.data.type", cpu__number__percent=0.567, a1_2__string__rfc123=u"Didnt+Look+Up+This+Data+Format", b__text=u"string for searching", c__enum__country=u"USA", d__bool=True, e__time=memdam.common.timeutils.now(), f__id=uuid.uuid4(), g__long=184467440737095516L, h__file=memdam.common.blob.BlobReference(uuid.uuid4(), u"txt"), i__namespace=u"some.thing", j__raw=buffer(uuid.uuid4().bytes) ) serialized_json_dict = event.to_json_dict() json_string = json.dumps(serialized_json_dict) deserialized_json_dict = json.loads(json_string) new_event = memdam.common.event.Event.from_json_dict(deserialized_json_dict) nose.tools.eq_(new_event, event)
import base64 import json import nose.tools import memdam.common.event import memdam.common.timeutils import memdam.server.web.utils import memdam.server.web.events import tests.unit.server.web event = memdam.common.event.new(u"whatever", cpu__number__percent=0.567) event_json = json.dumps(event.to_json_dict()) class CreateTest(tests.unit.server.web.FlaskResourceTestCase): def runTest(self): """PUTting an Event succeeds""" with self.context("/api/v1/events/" + event.id__id.hex, method="PUT", data=event_json, headers=self.headers): assert memdam.server.web.events.events(event.id__id.hex) == ("", 204) assert memdam.server.web.utils.get_archive(self.username).get(event.id__id) == event class NoJsonErrorTest(tests.unit.server.web.FlaskResourceTestCase): @nose.tools.raises(memdam.server.web.errors.BadRequest) def runTest(self): """PUTting an Event fails without correct Content Type""" new_headers = dict(Authorization=self.headers["Authorization"]) with self.context("/api/v1/events/" + event.id__id.hex, method="PUT", data=event_json, headers=new_headers): memdam.server.web.events.events(event.id__id.hex)