def test_register_metadata_duplicate_keys_repeated(self):
        """Test for registering a service; running out of unique identifiers.
        """
        endpoint_config = deepcopy(ENDPOINT_CONFIG)
        endpoint_config['services']['id']['length'] = MOCK_ID_ONE_CHAR
        endpoint_config['services']['id']['length'] = 1
        app = Flask(__name__)
        app.config['FOCA'] = Config(
            db=MongoConfig(**MONGO_CONFIG),
            endpoints=endpoint_config,
        )
        mock_resp = deepcopy(MOCK_SERVICE)
        app.config['FOCA'].db.dbs['serviceStore'].collections['services'] \
            .client = mongomock.MongoClient().db.collection
        app.config['FOCA'].db.dbs['serviceStore'].collections['services'] \
            .client.insert_one(mock_resp)

        data = deepcopy(MOCK_SERVICE)
        with app.app_context():
            with pytest.raises(InternalServerError):
                obj = RegisterService(data=data)
                obj.register_metadata()
                obj = RegisterService(data=data)
                obj.register_metadata()
                print(obj.data['id'])
示例#2
0
    def test_init(self):
        """Test for constructing class."""
        app = Flask(__name__)
        app.config['FOCA'] = Config(
            db=MongoConfig(**MONGO_CONFIG),
            endpoints=ENDPOINT_CONFIG,
        )

        with app.app_context():
            service_info = RegisterServiceInfo()
            assert service_info.url_prefix == SERVICE_CONFIG['url_prefix']
示例#3
0
    def test__get_headers(self):
        """Test for response headers getter."""
        app = Flask(__name__)
        app.config['FOCA'] = Config(
            db=MongoConfig(**MONGO_CONFIG),
            endpoints=ENDPOINT_CONFIG,
        )

        with app.app_context():
            service_info = RegisterServiceInfo()
            headers = service_info._get_headers()
            assert headers == HEADERS_SERVICE_INFO
    def test_init(self):
        """Test for constructing class."""
        app = Flask(__name__)
        app.config['FOCA'] = Config(
            db=MongoConfig(**MONGO_CONFIG),
            endpoints=ENDPOINT_CONFIG,
        )

        data = deepcopy(MOCK_SERVICE)
        with app.app_context():
            obj = RegisterService(data=data)
            assert obj.data['name'] == MOCK_SERVICE['name']
            assert obj.data['id'] is None
示例#5
0
    def test_get_service_info_na(self):
        """Test for getting service info when service info is unavailable."""
        app = Flask(__name__)
        app.config['FOCA'] = Config(
            db=MongoConfig(**MONGO_CONFIG),
            endpoints=ENDPOINT_CONFIG,
        )
        app.config['FOCA'].db.dbs[DB].collections[coll] \
            .client = mongomock.MongoClient().db.collection

        with app.app_context():
            with pytest.raises(NotFound):
                RegisterServiceInfo().get_service_info()
示例#6
0
    def test_set_service_info_from_config(self):
        """Test for setting service info from config."""
        app = Flask(__name__)
        app.config['FOCA'] = Config(
            db=MongoConfig(**MONGO_CONFIG),
            endpoints=ENDPOINT_CONFIG,
        )
        app.config['FOCA'].db.dbs[DB].collections[coll] \
            .client = mongomock.MongoClient().db.collection

        with app.app_context():
            service_info = RegisterServiceInfo()
            service_info.set_service_info_from_config()
            assert service_info.get_service_info() == SERVICE_INFO_CONFIG
    def test_register_metadata_with_id(self):
        """Test for registering a service with a user-supplied identifier."""
        app = Flask(__name__)
        app.config['FOCA'] = Config(
            db=MongoConfig(**MONGO_CONFIG),
            endpoints=ENDPOINT_CONFIG,
        )
        app.config['FOCA'].db.dbs['serviceStore'].collections['services'] \
            .client = MagicMock()

        data = deepcopy(MOCK_SERVICE)
        with app.app_context():
            obj = RegisterService(data=data, id=MOCK_ID)
            obj.register_metadata()
            assert obj.data['id'] == MOCK_ID
示例#8
0
    def test_set_service_info_from_config_invalid(self):
        """Test for setting service info from corrupt config."""
        app = Flask(__name__)
        mock_resp = deepcopy(ENDPOINT_CONFIG)
        del mock_resp[coll]['id']
        app.config['FOCA'] = Config(
            db=MongoConfig(**MONGO_CONFIG),
            endpoints=mock_resp,
        )
        app.config['FOCA'].db.dbs[DB].collections[coll] \
            .client = mongomock.MongoClient().db.collection

        with app.app_context():
            with pytest.raises(ValidationError):
                service_info = RegisterServiceInfo()
                service_info.set_service_info_from_config()
示例#9
0
    def test__upsert_service_info_insert(self):
        """Test for creating service info document in database."""
        app = Flask(__name__)
        app.config['FOCA'] = Config(
            db=MongoConfig(**MONGO_CONFIG),
            endpoints=ENDPOINT_CONFIG,
        )
        app.config['FOCA'].db.dbs[DB].collections[coll] \
            .client = mongomock.MongoClient().db.collection

        data = deepcopy(SERVICE_INFO_CONFIG)
        del data['contactUrl']
        with app.app_context():
            service_info = RegisterServiceInfo()
            service_info._upsert_service_info(data=data)
            assert service_info.get_service_info() == data
            assert service_info.get_service_info() != SERVICE_INFO_CONFIG
示例#10
0
    def test_get_service_info(self):
        """Test for getting service info."""
        app = Flask(__name__)
        app.config['FOCA'] = Config(
            db=MongoConfig(**MONGO_CONFIG),
            endpoints=ENDPOINT_CONFIG,
        )
        mock_resp = deepcopy(SERVICE_INFO_CONFIG)
        app.config['FOCA'].db.dbs[DB].collections[coll] \
            .client = mongomock.MongoClient().db.collection
        app.config['FOCA'].db.dbs[DB].collections[coll] \
            .client.insert_one(mock_resp)

        with app.app_context():
            service_info = RegisterServiceInfo()
            res = service_info.get_service_info()
            assert res == SERVICE_INFO_CONFIG
    def test_register_metadata_duplicate_key(self):
        """Test for registering a service; duplicate key error occurs."""
        app = Flask(__name__)
        app.config['FOCA'] = Config(
            db=MongoConfig(**MONGO_CONFIG),
            endpoints=ENDPOINT_CONFIG,
        )
        mock_resp = MagicMock(side_effect=[DuplicateKeyError(''), None])
        app.config['FOCA'].db.dbs['serviceStore'].collections['services'] \
            .client = MagicMock()
        app.config['FOCA'].db.dbs['serviceStore'].collections['services'] \
            .client.insert_one = mock_resp

        data = deepcopy(MOCK_SERVICE)
        with app.app_context():
            obj = RegisterService(data=data)
            obj.register_metadata()
            assert isinstance(obj.data['id'], str)
    def test_register_metadata_with_id_replace(self):
        """Test for updating an existing obj."""
        app = Flask(__name__)
        app.config['FOCA'] = Config(
            db=MongoConfig(**MONGO_CONFIG),
            endpoints=ENDPOINT_CONFIG,
        )
        mock_resp = deepcopy(MOCK_SERVICE)
        mock_resp["id"] = MOCK_ID
        app.config['FOCA'].db.dbs['serviceStore'].collections['services'] \
            .client = MagicMock()
        app.config['FOCA'].db.dbs['serviceStore'].collections['services'] \
            .client.insert_one(mock_resp)

        data = deepcopy(MOCK_SERVICE)
        with app.app_context():
            obj = RegisterService(data=data, id=MOCK_ID)
            obj.register_metadata()
            assert obj.data['id'] == MOCK_ID
示例#13
0
    def test_set_service_info_from_config_skip(self):
        """Test for skipping setting service info because identical service
        info is already available.
        """
        app = Flask(__name__)
        app.config['FOCA'] = Config(
            db=MongoConfig(**MONGO_CONFIG),
            endpoints=ENDPOINT_CONFIG,
        )
        mock_resp = deepcopy(SERVICE_INFO_CONFIG)
        app.config['FOCA'].db.dbs[DB].collections[coll] \
            .client = mongomock.MongoClient().db.collection
        app.config['FOCA'].db.dbs[DB].collections[coll] \
            .client.insert_one(mock_resp)

        with app.app_context():
            service_info = RegisterServiceInfo()
            service_info.set_service_info_from_config()
            assert service_info.get_service_info() == SERVICE_INFO_CONFIG
    def test_register_metadata_literal_id_charset(self):
        """Test for registering a service with a randomly assigned identifier
        generated from a literal character set.
        """
        app = Flask(__name__)
        endpoint_config = deepcopy(ENDPOINT_CONFIG)
        endpoint_config['services']['id']['charset'] = MOCK_ID_ONE_CHAR
        endpoint_config['services']['id']['length'] = 1
        app.config['FOCA'] = Config(
            db=MongoConfig(**MONGO_CONFIG),
            endpoints=endpoint_config,
        )
        app.config['FOCA'].db.dbs['serviceStore'].collections['services'] \
            .client = MagicMock()

        data = deepcopy(MOCK_SERVICE)
        with app.app_context():
            obj = RegisterService(data=data)
            obj.register_metadata()
            assert isinstance(obj.data['id'], str)
示例#15
0
def test_mongo_config_empty():
    """Test basic creation of the MongoConfig model."""
    res = MongoConfig()
    assert isinstance(res, MongoConfig)
示例#16
0
def test_mongo_config_with_data():
    """Test creation of the MongoConfig model with data."""
    res = MongoConfig(**MONGO_CONFIG)
    assert isinstance(res, MongoConfig)
示例#17
0
        }
    }
}
DB_DICT_CUST_COLL = {
    'my_db': {
        'collections': {
            'my_collection': {
                'indexes': [{
                    'keys': [('indexed_field', 1)],
                    'sparse': False
                }]
            }
        }
    }
}
MONGO_CONFIG_MINIMAL = MongoConfig(**MONGO_DICT_MIN, dbs=None)
MONGO_CONFIG_NO_COLL = MongoConfig(**MONGO_DICT_MIN, dbs=DB_DICT_NO_COLL)
MONGO_CONFIG_DEF_COLL = MongoConfig(**MONGO_DICT_MIN, dbs=DB_DICT_DEF_COLL)
MONGO_CONFIG_CUST_COLL = MongoConfig(**MONGO_DICT_MIN, dbs=DB_DICT_CUST_COLL)


def test_create_mongo_client(monkeypatch):
    """When MONGO_USERNAME environement variable is NOT defined"""
    monkeypatch.setenv("MONGO_USERNAME", None)
    app = Flask(__name__)
    res = create_mongo_client(app=app, )
    assert isinstance(res, PyMongo)


def test_create_mongo_client_auth(monkeypatch):
    """When MONGO_USERNAME environement variable IS defined"""