def post(self, sharing_secret):

        self.__log.info('SharingSpaceActionHandler - Received action for sharing space %s' % sharing_secret)

        action_json = self.get_argument('action')
        file = None
        if len(self.request.files) > 0:
            file = self.request.files['file'][0]
        sharing_action = \
            SharingActionFactory.from_json_and_file(action_json, file)
        if sharing_action is None:
            self.set_status(400)
            self.finish()
        else:

            all_actions = \
                yield gen.Task(SharingActionFactory.create_related_sharing_actions,
                    sharing_secret, sharing_action)

            if all_actions is None:
                self.set_status(404)
                self.finish()
            else:
                sharing_storage = SharingSpaceStorage.get_instance()
                sharing_space = sharing_storage.get_sharing_space(sharing_secret)

                self.__log.info('SharingSpaceActionHandler - adding %s action to sharing space %s from action initiated by user %s' % (str(len(all_actions)), sharing_secret, sharing_action.get_user_id()))

                for action in all_actions:
                    sharing_space.add_action(action)

                #as the user is concerned this call is finished
                self.set_status(200)
                self.finish()
    def test_deserialize_json_for_delete_note_invalid_json(self):

        invalid_str = "invalid_str"
        dict = {SharingEvent.DELETE_NOTE: invalid_str}
        json_str = json.dumps(dict)

        sharing_action = SharingActionFactory.from_json_and_file(json_str, None)

        self.assertTrue(sharing_action is None)
    def test_deserialize_json_for_update_note_invalid_json(self):

        note_file = open("../test_resources/XooML.xml")

        invalid_str = "invalid_str"
        dict = {SharingEvent.UPDATE_NOTE: invalid_str}
        json_str = json.dumps(dict)

        sharing_action = SharingActionFactory.from_json_and_file(json_str, note_file)
        self.assertTrue(sharing_action is None)
    def test_deserialize_json_for_update_manifest_invalid_json(self):

        invalid_str = "invalid_str"
        dict = {SharingEvent.UPDATE_MANIFEST: invalid_str}
        json_str = json.dumps(dict)

        manifest = open("../test_resources/XooML.xml")
        sharing_action = SharingActionFactory.from_json_and_file(json_str, manifest)

        self.assertTrue(sharing_action is None)
    def test_deserialize_json_for_update_thumbnail_missing_file(self):

        user_id = "userID"
        collection_name = "coll_name"
        details = {"user_id": user_id, "collection_name": collection_name}
        dict = {SharingEvent.UPDATE_THUMBNAIL: details}
        json_str = json.dumps(dict)
        sharing_action = SharingActionFactory.from_json_and_file(json_str, None)

        self.assertTrue(sharing_action is None)
    def test_deserialize_json_for_update_manifest_missing_file(self):

        user_id = "userID"
        collection_name = "collection_name"
        details = {"user_id": user_id, "collection_name": collection_name}
        dict = {SharingEvent.UPDATE_MANIFEST: details}
        json_str = json.dumps(dict)
        manifest = None
        sharing_action = SharingActionFactory.from_json_and_file(json_str, manifest)

        self.assertTrue(sharing_action is None)
    def test_deserialize_json_for_update_manifest(self):
        user_id = "userID"
        collection_name = "collection_name"
        details = {"user_id": user_id, "collection_name": collection_name}
        dict = {SharingEvent.UPDATE_MANIFEST: details}
        json_str = json.dumps(dict)
        manifest = open("../test_resources/XooML.xml")
        sharing_action = SharingActionFactory.from_json_and_file(json_str, manifest)

        self.assertEqual(user_id, sharing_action.get_user_id())
        self.assertEqual(collection_name, sharing_action.get_collection_name())
        self.assertTrue(sharing_action.get_associated_file() is not None)
        self.assertEqual(SharingEvent.UPDATE_MANIFEST, sharing_action.get_action_type())
    def test_deserialize_json_for_delete_note(self):

        user_id = "userID"
        collection_name = "collection_name"
        note_name = "note_name"

        details = {"user_id": user_id, "collection_name": collection_name, "note_name": note_name}
        dict = {SharingEvent.DELETE_NOTE: details}
        json_str = json.dumps(dict)
        sharing_action = SharingActionFactory.from_json_and_file(json_str, None)

        self.assertEqual(user_id, sharing_action.get_user_id())
        self.assertEqual(collection_name, sharing_action.get_collection_name())
        self.assertEqual(note_name, sharing_action.get_note_name())
        self.assertTrue(sharing_action.get_associated_file() is None)
        self.assertEqual(SharingEvent.DELETE_NOTE, sharing_action.get_action_type())
    def test_deserialize_json_for_update_note_img(self):

        user_id = "userID"
        collection_name = "collection_name"
        note_name = "note_name"
        note_img_file = open("../test_resources/workfile.jpg")

        details = {"user_id": user_id, "collection_name": collection_name, "note_name": note_name}
        dict = {SharingEvent.UPDATE_NOTE_IMG: details}
        json_str = json.dumps(dict)
        sharing_action = SharingActionFactory.from_json_and_file(json_str, note_img_file)

        self.assertEqual(user_id, sharing_action.get_user_id())
        self.assertEqual(collection_name, sharing_action.get_collection_name())
        self.assertEqual(note_name, sharing_action.get_note_name())
        self.assertTrue(sharing_action.get_associated_file() is not None)
        self.assertEqual(SharingEvent.UPDATE_NOTE_IMG, sharing_action.get_action_type())