def test_add_listener_missing_parameter(self):

        collection_name = 'share_api_col_name' + str(random.randint(0,100))
        owner_id = self.account_id
        subscribers = [self.subscriber_id]

        sharing_secret, subscriber_list =\
        self.__create_shared_collection(owner_id, subscribers,
            collection_name)

        #add primary and secondary listeners
        sharing_space_url = '/'.join(['', 'SharingSpace', sharing_secret, 'Listen'])
        details = {'liliuser_id': owner_id}
        json_str = json.dumps(details)

        SharingSpaceStorage.get_instance().stop_cleanup_service()
        headers, post_body = HTTPHelper.create_multipart_request_with_parameters({'details' : json_str})
        #owner listens as primary listener

        #owner listens as primary listener
        response1 = self.fetch(sharing_space_url,
            method='POST', headers=headers, body=post_body,
            request_timeout = 100, connect_timeout = 100)
        self.__wait(5)
        #owner listeners as backup listener
        response2 =  self.fetch(sharing_space_url,
            method='POST', headers=headers, body=post_body,
            request_timeout = 100, connect_timeout = 100)

        self.assertEqual(StorageResponse.BAD_REQUEST, response1.code)
        self.assertEqual(StorageResponse.BAD_REQUEST, response2.code)

        self.__cleanup(owner_id, collection_name, subscriber_list)
        SharingSpaceStorage.get_instance().stop_cleanup_service()
    def test_add_listener_invalid_sharing_secret(self):

        owner_id = self.account_id

        sharing_secret = 'XXXXXXXX'

        #add primary and secondary listeners
        sharing_space_url = '/'.join(['', 'SharingSpace', sharing_secret, 'Listen'])
        details = {'user_id': owner_id}
        json_str = json.dumps(details)
        headers, post_body = HTTPHelper.create_multipart_request_with_parameters({'details' : json_str})
        #owner listens as primary listener
        response1 = self.fetch(sharing_space_url,
            method='POST', headers=headers, body=post_body,
            request_timeout = 100, connect_timeout = 100)
        self.__wait(5)
        #owner listeners as backup listener
        response2 =  self.fetch(sharing_space_url,
            method='POST', headers=headers, body=post_body,
            request_timeout = 100, connect_timeout = 100)

        self.assertEqual(StorageResponse.NOT_FOUND, response1.code)
        self.assertEqual(StorageResponse.NOT_FOUND, response2.code)

        SharingSpaceStorage.get_instance().stop_cleanup_service()
    def test_add_action(self):
        collection_name = 'share_api_col_name' + str(random.randint(0,100))
        owner_id = self.account_id
        subscribers = [self.subscriber_id]

        sharing_secret, subscriber_list =\
            self.__create_shared_collection(owner_id, subscribers,
                collection_name)

        note_name = 'note' + str(random.randint(0,100))
        json_str = self.__make_update_not_action(self.subscriber_id,
            collection_name, note_name)

        note_file = open('../../test_resources/XooML.xml')
        header, post_body = HTTPHelper.create_multipart_request_with_file_and_params({'action' : json_str},
            'file', note_file)

        sharing_space_url = '/'.join(['', 'SharingSpace', sharing_secret])
        response = self.fetch(path=sharing_space_url, method = 'POST',
            headers=header, body=post_body)

        self.__wait(10)
        self.assertEqual(200, response.code)

        self.__cleanup(owner_id, collection_name, subscriber_list)
        SharingSpaceStorage.get_instance().stop_cleanup_service()
    def test_get_temp_img_invalid_secret(self):

        collection_name = 'share_api_col_name' + str(random.randint(0,100))
        owner_id = self.account_id
        subscribers = [self.subscriber_id]

        sharing_secret, subscriber_list =\
        self.__create_shared_collection(owner_id, subscribers,
            collection_name)

        #add primary and secondary listeners
        sharing_space_url = '/'.join(['', 'SharingSpace', sharing_secret, 'Listen'])
        sharing_space_url = self.get_url(sharing_space_url)
        details = {'user_id': owner_id}
        json_str = json.dumps(details)
        headers, post_body = HTTPHelper.create_multipart_request_with_parameters({'details' : json_str})
        #owner listens as primary listener
        self.http_client.fetch(sharing_space_url,
            method='POST', headers=headers, body=post_body, callback=self.primary_listener_returned,
            request_timeout = 100, connect_timeout = 100)
        self.__wait(5)
        #owner listeners as backup listener
        self.http_client.fetch(sharing_space_url,
            method='POST', headers=headers, body=post_body, callback=self.backup_listener_returned,
            request_timeout = 100, connect_timeout = 100)

        self.__primary_listener_returned = False
        self.__backup_listener_returned = False
        #subscriber adds an action
        note_name = 'name' + str(random.randint(0,100))
        note_img_file = open('../../test_resources/note_img.jpg')
        json_str = self.__make_update_note_img_action(self.subscriber_id, collection_name, note_name)
        header, post_body = HTTPHelper.create_multipart_request_with_file_and_params({'action' : json_str},
            'file', note_img_file)

        sharing_space_url = '/'.join(['', 'SharingSpace', sharing_secret])
        response = self.fetch(path=sharing_space_url, method = 'POST',
            headers=header, body=post_body, request_timeout = 100, connect_timeout = 100)
        self.assertEqual(200, response.code)

        self.__wait(15)

        self.assertTrue(self.__primary_listener_returned)
        details = self.__primary_listener_json_obj[SharingEvent.UPDATE_NOTE_IMG]
        for note_name in details:
            secret =  'dummy'+ str(random.randint(0,100))
            temp_url = sharing_space_url + '/'+ owner_id + '/' +\
                       collection_name + '/' + note_name + '/' + secret
            response = self.fetch(path=temp_url, method = 'GET')
            #we still should get the image because the secret is used to revive
            #the temp image from the cache and if its not there we discard
            #and use otheri nformation to get the image
            self.assertEqual(StorageResponse.OK, response.code)
            self.assertTrue(response.body is not None)

        self.__cleanup(owner_id, collection_name, subscriber_list)
        SharingSpaceStorage.get_instance().stop_cleanup_service()
    def test_get_temp_img_thumbnail(self):

        collection_name = 'share_api_col_name' + str(random.randint(0,100))
        owner_id = self.account_id
        subscribers = [self.subscriber_id]

        sharing_secret, subscriber_list =\
        self.__create_shared_collection(owner_id, subscribers,
            collection_name)

        #add primary and secondary listeners
        sharing_space_url = '/'.join(['', 'SharingSpace', sharing_secret, 'Listen'])
        sharing_space_url = self.get_url(sharing_space_url)
        details = {'user_id': owner_id}
        json_str = json.dumps(details)
        headers, post_body = HTTPHelper.create_multipart_request_with_parameters({'details' : json_str})
        #owner listens as primary listener
        self.http_client.fetch(sharing_space_url,
            method='POST', headers=headers, body=post_body, callback=self.primary_listener_returned,
            request_timeout = 100, connect_timeout = 100)
        self.__wait(5)
        #owner listeners as backup listener
        self.http_client.fetch(sharing_space_url,
            method='POST', headers=headers, body=post_body, callback=self.backup_listener_returned,
            request_timeout = 100, connect_timeout = 100)

        self.__primary_listener_returned = False
        self.__backup_listener_returned = False
        #subscriber adds an action
        thumbnail = open('../../test_resources/note_img.jpg')
        json_str = self.__make_update_thumbnail_action(self.subscriber_id, collection_name)
        header, post_body = HTTPHelper.create_multipart_request_with_file_and_params({'action' : json_str},
            'file', thumbnail)

        sharing_space_url = '/'.join(['', 'SharingSpace', sharing_secret])
        response = self.fetch(path=sharing_space_url, method = 'POST',
            headers=header, body=post_body)
        self.assertEqual(200, response.code)

        self.__wait(10)

        self.assertTrue(self.__primary_listener_returned)
        secret = self.__primary_listener_json_obj[SharingEvent.UPDATE_THUMBNAIL]
        temp_url = sharing_space_url + '/'+ owner_id + '/' +\
                   collection_name + '/Thumbnail/' + secret
        response = self.fetch(path=temp_url, method = 'GET')
        self.assertEqual(StorageResponse.OK, response.code)
        self.assertTrue(response.body is not None)

        self.__cleanup(owner_id, collection_name, subscriber_list)
        SharingSpaceStorage.get_instance().stop_cleanup_service()
 def get(self, args=None):
     self.write('Why So Serious ? ***')
     self.write('Load Balancer : ' + Properties.load_balancer_url +'***')
     sharing_space_count = \
         SharingSpaceStorage.get_instance().get_sharing_space_count()
     self.write('Number of Sharing Spaces on Server: ' + str(sharing_space_count))
     self.finish()
    def test_cleanup_unused_space(self):

        sharing_secret = 'XXXXXXX3'
        #for testing set the sweep period low
        SharingSpaceStorage.SWEEP_PERIOD = 5
        sharing_space_storage = SharingSpaceStorage.get_instance()
        sharing_space_storage.reset_cleanup_timer()
        sharing_space = sharing_space_storage.get_sharing_space(sharing_secret)
        self.assertEqual(1, sharing_space_storage.get_sharing_space_count())

        #wait for the cleanup service

        try:
            self.wait(timeout=5)
        except Exception:
            pass

        self.assertEqual(1, sharing_space_storage.get_sharing_space_count())

        try:
            self.wait(timeout=11)
        except Exception:
            pass

        self.assertEqual(0, sharing_space_storage.get_sharing_space_count())
        sharing_space_storage.stop_cleanup_service()
        sharing_space_storage.clear()
    def post(self, sharing_secret):

        sharing_storage = SharingSpaceStorage.get_instance()
        isValid = yield gen.Task(sharing_storage.validate_secret,
            sharing_secret)
        if isValid:
            sharing_space = sharing_storage.get_sharing_space(sharing_secret)
            if sharing_space is None:
                self.set_status(404)
                self.finish()
            else:
                try:
                    json_str = self.get_argument('details')
                    json_obj = json.loads(json_str)
                    if 'user_id' in json_obj:
                        user_id = json_obj['user_id']

                        self.__log.info('SharingSpaceListener - adding user %s as listener to sharing space %s' % (user_id, sharing_secret))

                        sharing_space.add_listener(user_id, request=self)
                    else:
                        self.set_status(400)
                        self.finish()
                except Exception:
                    self.set_status(400)
                    self.finish()
        else:
            self.set_status(404)
            self.finish()
    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_cleanup_candidate_is_being_processed(self):

        sharing_secret = 'XXXXXXX7'
        #for testing set the sweep period low
        SharingSpaceStorage.SWEEP_PERIOD = 2
        sharing_space_storage = SharingSpaceStorage.get_instance()
        sharing_space_storage.reset_cleanup_timer()
        sharing_space = sharing_space_storage.get_sharing_space(sharing_secret)
        #make the sharing_space busy
        self.__load_test(sharing_space, 20,10, 10, 0)

        self.assertEqual(1, sharing_space_storage.get_sharing_space_count())

        #wait for the cleanup service

        try:
            self.wait(timeout=3)
        except Exception:
            pass
        self.assertEqual(1, sharing_space_storage.get_sharing_space_count())

        try:
            self.wait(timeout=30)
        except Exception:
            pass
        self.assertEqual(0, sharing_space_storage.get_sharing_space_count())
        sharing_space_storage.stop_cleanup_service()
        sharing_space_storage.clear()
    def test_delete_listener(self):

        collection_name = 'share_api_col_name' + str(random.randint(0,100))
        owner_id = self.account_id
        subscribers = [self.subscriber_id]

        sharing_secret, subscriber_list =\
        self.__create_shared_collection(owner_id, subscribers,
            collection_name)

        #add primary and secondary listeners
        sharing_space_url = '/'.join(['', 'SharingSpace', sharing_secret, 'Listen'])
        sharing_space_url = self.get_url(sharing_space_url)
        details = {'user_id': owner_id}
        json_str = json.dumps(details)
        headers, post_body = HTTPHelper.create_multipart_request_with_parameters({'details' : json_str})
        #owner listens as primary listener
        self.http_client.fetch(sharing_space_url,
            method='POST', headers=headers, body=post_body, callback=self.primary_listener_returned,
            request_timeout = 100, connect_timeout = 100)
        self.__wait(5)
        #owner listeners as backup listener
        self.http_client.fetch(sharing_space_url,
            method='POST', headers=headers, body=post_body, callback=self.backup_listener_returned,
            request_timeout = 100, connect_timeout = 100)

        self.__primary_listener_returned = False
        self.__backup_listener_returned = False

        self.__wait(5)

        remove_listener_url = '/'.join(['', 'SharingSpace', sharing_secret, 'Listen', owner_id])
        headers, post_body = HTTPHelper.create_multipart_request_with_parameters({'details' : json_str})
        #owner listens as primary listener
        response = self.fetch(remove_listener_url, method='DELETE', headers=headers,
            request_timeout = 100, connect_timeout = 100)

        self.assertEqual(StorageResponse.OK, response.code)

        self.__wait(10)

        self.assertTrue(self.__primary_listener_returned)
        self.assertTrue(self.__backup_listener_returned)

        self.__cleanup(owner_id, collection_name, subscriber_list)
        SharingSpaceStorage.get_instance().stop_cleanup_service()
 def test_get_sharing_space_fresh(self):
     sharing_secret = 'XXXXXXX1'
     sharing_space_storage = SharingSpaceStorage.get_instance()
     sharing_space = \
         sharing_space_storage.get_sharing_space(sharing_secret)
     self.assertTrue(sharing_space is not None)
     self.assertTrue(isinstance(sharing_space,SharingSpaceController))
     sharing_space_storage.stop_cleanup_service()
     sharing_space_storage.clear()
    def test_get_Sharing_space_used(self):

        sharing_secret = 'XXXXXXX2'
        sharing_space_storage = SharingSpaceStorage.get_instance()
        sharing_space =\
            sharing_space_storage.get_sharing_space(sharing_secret)
        self.assertTrue(sharing_space is not None)
        self.assertTrue(isinstance(sharing_space,SharingSpaceController))

        sharing_space2 =\
            sharing_space_storage.get_sharing_space(sharing_secret)
        self.assertTrue(sharing_space2 is not None)
        self.assertTrue(isinstance(sharing_space2,SharingSpaceController))
        self.assertEqual(sharing_space, sharing_space2)
        sharing_space_storage.stop_cleanup_service()
        sharing_space_storage.clear()
    def test_cleanup_nothing_is_there(self):

        #for testing set the sweep period low
        SharingSpaceStorage.SWEEP_PERIOD = 5
        sharing_space_storage = SharingSpaceStorage.get_instance()
        sharing_space_storage.reset_cleanup_timer()
        self.assertEqual(0, sharing_space_storage.get_sharing_space_count())

        #wait for the cleanup service
        try:
            self.wait(timeout=10)
        except Exception:
            pass

        self.assertEqual(0, sharing_space_storage.get_sharing_space_count())
        sharing_space_storage.stop_cleanup_service()
        sharing_space_storage.clear()
    def get(self, sharing_secret, user_id, collection_name, note_name, secret):

        sharing_storage = SharingSpaceStorage.get_instance()
        sharing_space = sharing_storage.get_sharing_space(sharing_secret)

        #if we are asking for the thumbnail temp image we really don't need
        #the note name as none will indicate it
        if note_name == 'Thumbnail':
            note_name = None
            self.__log.info('SharingSpaceActionHandler - getting collection thumbnail with secret %s for user %s and collection %s from temp images of sharing space %s' % (secret, user_id, collection_name, sharing_secret))
        else:
            self.__log.info('SharingSpaceActionHandler - getting img with secret %s for user %s and collection %s for note %s from temp images of sharing space %s' % (secret, user_id, collection_name, note_name, sharing_secret))



        img = yield gen.Task(sharing_space.get_temp_img, secret,
            user_id, collection_name, note_name)
        if img is None:
            self.set_status(404)
            self.finish()
        else:
            self.set_status(200)
            self.write(img)
            self.finish()
    def delete(self, sharing_secret, user_id):

        sharing_storage = SharingSpaceStorage.get_instance()

        isValid = yield gen.Task(sharing_storage.validate_secret,
            sharing_secret)
        if isValid:
            sharing_space = sharing_storage.get_sharing_space(sharing_secret)
            if sharing_space is None:
                self.set_status(404)
                self.finish()
            else:
                try:

                    self.__log.info('SharingSpaceListener - removing user %s as listener from sharing space %s' % (user_id, sharing_secret))
                    sharing_space.remove_listener(user_id)
                    self.set_status(200)
                    self.finish()
                except Exception as exceptionn:
                    self.set_status(400)
                    self.finish()
        else:
            self.set_status(404)
            self.finish()
    def test_add_listener(self):

        collection_name = 'share_api_col_name' + str(random.randint(0,100))
        owner_id = self.account_id
        subscribers = [self.subscriber_id]

        sharing_secret, subscriber_list =\
        self.__create_shared_collection(owner_id, subscribers,
            collection_name)

        #add primary and secondary listeners
        sharing_space_url = '/'.join(['', 'SharingSpace', sharing_secret, 'Listen'])
        sharing_space_url = self.get_url(sharing_space_url)
        details = {'user_id': owner_id}
        json_str = json.dumps(details)
        headers, post_body = HTTPHelper.create_multipart_request_with_parameters({'details' : json_str})
        #owner listens as primary listener
        self.http_client.fetch(sharing_space_url,
            method='POST', headers=headers, body=post_body, callback=self.primary_listener_returned,
            request_timeout = 100, connect_timeout = 100)
        self.__wait(5)
        #owner listeners as backup listener
        self.http_client.fetch(sharing_space_url,
            method='POST', headers=headers, body=post_body, callback=self.backup_listener_returned,
            request_timeout = 100, connect_timeout = 100)

        self.__primary_listener_returned = False
        self.__backup_listener_returned = False
        #subscriber adds an action
        note_name = 'name' + str(random.randint(0,100))
        note_img_file = open('../../test_resources/note_img.jpg')
        json_str = self.__make_update_note_img_action(self.subscriber_id, collection_name, note_name)
        header, post_body = HTTPHelper.create_multipart_request_with_file_and_params({'action' : json_str},
            'file', note_img_file)

        sharing_space_url = '/'.join(['', 'SharingSpace', sharing_secret])
        response = self.fetch(path=sharing_space_url, method = 'POST',
            headers=header, body=post_body)

        self.__wait(10)
        self.assertEqual(200, response.code)

        self.assertTrue(self.__primary_listener_returned)
        #now the primary listener is returned so add another action for
        #backup listener to record

        note_name = 'name' + str(random.randint(0,100))
        note_img_file = open('../../test_resources/note_img.jpg')
        json_str = self.__make_update_note_img_action(self.subscriber_id, collection_name, note_name)
        header, post_body = HTTPHelper.create_multipart_request_with_file_and_params({'action' : json_str},
            'file', note_img_file)

        sharing_space_url = '/'.join(['', 'SharingSpace', sharing_secret])
        response = self.fetch(path=sharing_space_url, method = 'POST',
            headers=header, body=post_body)

        self.__wait(5)
        self.assertEqual(200, response.code)

        #now send back the primary listener
        sharing_space_url = '/'.join(['', 'SharingSpace', sharing_secret, 'Listen'])
        sharing_space_url = self.get_url(sharing_space_url)
        details = {'user_id': owner_id}
        json_str = json.dumps(details)
        headers, post_body = HTTPHelper.create_multipart_request_with_parameters({'details' : json_str})
        #owner listens as primary listener
        self.http_client.fetch(sharing_space_url,
            method='POST', headers=headers, body=post_body, callback=self.primary_listener_returned,
            request_timeout = 100, connect_timeout = 100)

        self.__wait(5)

        self.assertTrue(self.__backup_listener_returned)

        self.__cleanup(owner_id, collection_name, subscriber_list)
        SharingSpaceStorage.get_instance().stop_cleanup_service()