def test_create_authorization_http_signature(self, mock_repository, mock_create): expected_client_id = "test" client_authorization = ClientAuthorizationHttpSignature( expected_client_id, [Scope("example", ScopesType.CREATE)], "my-public-key") client_authorization_metadata = Collection("client_authorization", "client_id") mock_repository.return_value = None document = { "type": "http_signature", "client_id": expected_client_id, "client_scopes": [{ "collection_name": "example", "scope_type": "CREATE" }], "public_key": "my-public-key" } mock_create.return_value = Model(client_authorization_metadata, document) result = self.systemService.create_client_authorization( client_authorization) self.assertEqual(result.client_id, client_authorization.client_id) self.assertTrue(isinstance(result, ClientAuthorizationHttpSignature)) self.assertEqual(call(document), mock_create.call_args_list[0])
def test_get_client_authorization_http_signature(self, mock_repository, mock_get): expected_client_id = 'my-client-id' mock_repository.return_value = None client_authorization_metadata = Collection("client_authorization", "client_id") document = { "client_id": expected_client_id, "type": "http_signature", "public_key": "my-public-key", "client_scopes": [{ "collection_name": "example", "scope_type": "GET" }] } expected_model = Model(client_authorization_metadata, document) mock_get.return_value = expected_model result = self.systemService.get_client_authorization( expected_client_id) self.assertTrue(mock_get.called_with(expected_client_id)) self.assertEqual(expected_client_id, result.client_id) self.assertIsInstance(result, ClientAuthorizationHttpSignature) self.assertEqual("my-public-key", result.client_public_key) self.assertEqual(1, len(result.client_scopes)) self.assertEqual("example", result.client_scopes[0].collection_name) self.assertEqual("GET", result.client_scopes[0].scope_type.name)
def test_getDocument(self, mock_repository, mock_get): expected_id = "randomId" document = {"id_key": expected_id, "fields": [{"field1": "string"}]} expected_model = Model(self.exampleCollectionMetadata, document) mock_repository.return_value = None mock_get.return_value = expected_model found_document = self.domainService.get_document(expected_id) self.assertDictEqual(found_document, document) mock_repository.assert_called_once_with(self.exampleCollectionMetadata) self.assertTrue(mock_get.called_with(expected_id))
def test_updateDocument(self, mock_repository, mock_update): expected_id = 'randomId' document = {"id_key": expected_id, "fields": [{"field1": "string"}]} expected_model = Model(self.exampleCollectionMetadata, document) mock_repository.return_value = None mock_update.return_value = expected_model result = self.domainService.update_document(document) self.assertIsNotNone(result) self.assertDictEqual(result, document) mock_repository.assert_called_once_with(self.exampleCollectionMetadata) self.assertEqual(call(document), mock_update.call_args_list[0])
def fake_query_result(self, uid, next=None): return QueryResult([ Model( Collection("index", "name"), { "uid": uid, "name": "collection.name", "collection": { "name": "example" + uid }, "conditions": ["collection.name"] }) ], next)
def test_model(self): collection = Collection("example", "id", None) document = {"id": "randomId"} model = Model(collection, document) self.assertEqual(model.pk(), "example#randomId") self.assertEqual(model.sk(), "example") self.assertEqual(model.data(), "randomId") self.assertIsNone(model.order_value())
def test_find_all_documents(self, mock_repository, mock_query_v2): # given mock_repository.return_value = None mock_query_v2.return_value = QueryResult([ Model(self.exampleCollectionMetadata, { "id": "1", "attribute1": "1" }), Model(self.exampleCollectionMetadata, { "id": "2", "attribute1": "1" }) ]) expected_query = Query(AnyMatch(), self.exampleCollectionMetadata) # when documents, last_evaluated_key = self.domainService.find_all() # then self.assertEqual(2, len(documents)) self.assertEqual(documents[0]["id"], "1") self.assertEqual(documents[1]["id"], "2") self.assertIsNone(last_evaluated_key) self.assertTrue(mock_query_v2.called) self.assertEqual(call(expected_query), mock_query_v2.call_args_list[0])
def test_modelWithOrdering(self): # documentConfiguration = DocumentTypeConfiguration("example","id","ordering") collection = Collection("example", "id", "ordering") document = {"id": "randomId", "ordering": "123456"} model = Model(collection, document) self.assertEqual(model.pk(), "example#randomId") self.assertEqual(model.sk(), "example") self.assertEqual(model.data(), "123456") self.assertEqual(model.order_value(), "123456")
def test_queryCollectionByName(self, mock_index_dynamoplus_repository, mock_find): collection_metadata = Collection("example", "name") expected_query = Query(Eq("name", "example"), collection_metadata) mock_index_dynamoplus_repository.return_value = None mock_find.return_value = QueryResult([ Model(Collection("example", "id"), { "name": "example", "id_key": "id" }) ]) collections = self.systemService.find_collections_by_example( collection_metadata) self.assertTrue(len(collections) == 1) self.assertEqual(collections[0].name, "example") self.assertEqual(call(expected_query), mock_find.call_args_list[0])
def test_getCollection(self, mock_repository, mock_get): expected_id = 'example' mock_repository.return_value = None collection_metadata = Collection("collection", "name") document = { "name": expected_id, "id_key": expected_id, "fields": [{ "field1": "string" }] } expected_model = Model(collection_metadata, document) mock_get.return_value = expected_model result = self.systemService.get_collection_by_name(expected_id) # self.assertIn("fields",result) self.assertTrue(mock_get.called_with(expected_id))
def query_v2(self, query: QueryModel) -> QueryResult: """ Runs the query on the table :param table: dynamo table to execute the operation :param query: the predicate, the collection and the query params :return: query results """ limit = query.limit start_from = None if query.start_from: start_from = {"pk": query.start_from.pk(), "sk": query.start_from.sk(), "data": query.start_from.data()} model = query.get_model() if query.predicate.is_range(): v_1, v_2 = model.data() key = Key('sk').eq(model.sk()) & Key('data').between(v_1, v_2) logger.info( "the key that will be used is sk={} and data between {} and {}".format(model.sk(), v_1, v_2)) else: if model.data() is not None: key = Key('sk').eq(model.sk()) & Key('data').begins_with(model.data()) logger.info( "The key that will be used is sk={} begins with data={}".format(model.sk(), model.data())) else: key = Key('sk').eq(model.sk()) logger.info("The key that will be used is sk={} with no data".format(model.sk())) dynamo_query = dict( IndexName="sk-data-index", KeyConditionExpression=key, Limit=limit, ExclusiveStartKey=start_from ) response = self.table.query( **{k: v for k, v in dynamo_query.items() if v is not None} ) logger.info("Response from dynamo db {}".format(str(response))) last_key = None if 'LastEvaluatedKey' in response: last_key = response['LastEvaluatedKey']["pk"].replace(self.collection.name + "#", "") logging.debug("last key = {}", last_key) return QueryResult(list(map(lambda i: Model.from_dynamo_db_item(i, self.collection), response[u'Items'])), last_key)
def test_createCollection(self, mock_repository, mock_create): expected_id = 'example' target_collection = { "name": "example", "id_key": "id", "ordering": None, "auto_generate_id": False } document = {"name": expected_id, "id_key": "id"} collection_metadata = Collection("collection", "name") expected_model = Model(collection_metadata, document) mock_repository.return_value = None mock_create.return_value = expected_model target_metadata = Collection("example", "id") created_collection = self.systemService.create_collection( target_metadata) collection_id = created_collection.name self.assertEqual(collection_id, expected_id) self.assertEqual(call(target_collection), mock_create.call_args_list[0])
def test_createIndexWithNoOrdering(self, mock_repository, mock_create): expected_id = 'field1__field2.field21' expected_conditions = ["field1", "field2.field21"] target_index = { "uid": "1", "name": expected_id, "collection": { "name": "example" }, "conditions": expected_conditions, "ordering_key": None } index_metadata = Collection("index", "name") expected_model = Model(index_metadata, target_index) mock_repository.return_value = None mock_create.return_value = expected_model index = Index("1", "example", expected_conditions) created_index = self.systemService.create_index(index) index_name = created_index.index_name self.assertEqual(index_name, expected_id) self.assertEqual(call(target_index), mock_create.call_args_list[0])
def test_get_client_authorization_api_key(self, mock_repository, mock_get): expected_client_id = 'my-client-id' mock_repository.return_value = None client_authorization_metadata = Collection("client_authorization", "client_id") document = { "client_id": expected_client_id, "type": "api_key", "api_key": "my_api_key", "whitelist_hosts": ["*"], "client_scopes": [{ "collection_name": "example", "scope_type": "GET" }] } expected_model = Model(client_authorization_metadata, document) mock_get.return_value = expected_model result = self.systemService.get_client_authorization( expected_client_id) self.assertTrue(mock_get.called_with(expected_client_id)) self.assertEqual(expected_client_id, result.client_id) self.assertIsInstance(result, ClientAuthorizationApiKey)
def test_delete_authorization_http_signature(self, mock_repository, mock_delete): expected_client_id = "test" client_authorization_metadata = Collection("client_authorization", "client_id") mock_repository.return_value = None document = { "type": "http_signature", "client_id": expected_client_id, "client_scopes": [{ "collection_name": "example", "scope_type": "CREATE" }], "public_key": "my-public-key" } mock_delete.return_value = Model(client_authorization_metadata, document) self.systemService.delete_authorization(expected_client_id) self.assertEqual(call(expected_client_id), mock_delete.call_args_list[0])
def test_queryIndex_by_CollectionByName(self, mock_index_dynamoplus_repository, mock_find): expected_query = Query(Eq("collection.name", "example"), Collection("index", "uid")) mock_index_dynamoplus_repository.return_value = None mock_find.return_value = QueryResult([ Model( Collection("index", "name"), { "uid": "1", "name": "collection.name", "collection": { "name": "example" }, "conditions": ["collection.name"] }) ]) indexes, last_key = self.systemService.find_indexes_from_collection_name( "example") self.assertEqual(1, len(indexes)) self.assertEqual(indexes[0].uid, "1") self.assertEqual(indexes[0].index_name, "collection.name") self.assertEqual(call(expected_query), mock_find.call_args_list[0])
def test_collecton_metadata(self): collection = Collection("collection", "name") model = Model(collection, {"name": "example"}) self.assertEqual(model.pk(), "collection#example") self.assertEqual(model.sk(), "collection") self.assertEqual(model.data(), "example")
def get_model_from_document(self, document: dict): return Model(self.collection, document)
def from_dynamo_db_item(self, result) -> Model: return Model.from_dynamo_db_item(result[u'Item'], self.collection) if 'Item' in result else None