Esempio n. 1
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)
Esempio n. 2
0
 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])
Esempio n. 3
0
 def test_getSystemCollectionmetadata(self, mock_system_service, mock_get_system_collection):
     mock_system_service.return_value = None
     mock_get_system_collection.return_value = Collection("example", "id", "ordering")
     collection_metadata = get("collection", "example")
     self.assertDictEqual(collection_metadata,
                          dict(id_key="id", name="example", ordering_key="ordering", attribute_definition=None, auto_generate_id=False))
     self.assertTrue(mock_get_system_collection.called_with("example"))
Esempio n. 4
0
 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])
Esempio n. 5
0
 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())
Esempio n. 6
0
def from_dict_to_collection(d: dict):
    attributes = list(map(from_dict_to_attribute_definition,
                          d["attributes"])) if "attributes" in d else None
    auto_generate_id = d[
        "auto_generate_id"] if "auto_generate_id" in d else False
    return Collection(d["name"], d["id_key"],
                      d["ordering"] if "ordering" in d else None, attributes,
                      auto_generate_id)
Esempio n. 7
0
 def test_query_model_range(self):
     collection = Collection("example", "id", "ordering")
     query_model = QueryModel(collection, Range("field1", "value1",
                                                "value2"))
     self.assertIsNone(query_model.pk())
     self.assertEqual("example#field1", query_model.sk())
     self.assertEqual(2, len(query_model.data()))
     self.assertEqual("value1", query_model.data()[0])
     self.assertEqual("value2", query_model.data()[1])
Esempio n. 8
0
 def test_indexModelWithNoValue(self):
     collection = Collection("example", "id", "ordering")
     document = {}
     index = None
     model = IndexModel(collection, document, None)
     self.assertEqual(model.pk(), None)
     self.assertEqual(model.sk(), "example")
     self.assertEqual(model.data(), None)
     self.assertEqual(model.order_value(), None)
Esempio n. 9
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")
Esempio n. 10
0
 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])
Esempio n. 11
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)
Esempio n. 12
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])
Esempio n. 13
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))
Esempio n. 14
0
 def test_indexModel(self):
     collection = Collection("example", "id", None)
     document = {
         "id": "randomId",
         "attr1": "value2",
         "nested": {
             "condition": {
                 "1": "value1"
             }
         }
     }
     index = Index("1", "example", ["nested.condition.1", "attr1"], None)
     model = IndexModel(collection, document, index)
     self.assertEqual(model.pk(), "example#randomId")
     self.assertEqual(model.sk(), "example#nested.condition.1#attr1")
     self.assertEqual(model.data(), "value1#value2")
     self.assertIsNone(model.order_value())
Esempio n. 15
0
 def test_queryIndex_by_CollectionByName_generator(
         self, mock_index_dynamoplus_repository, mock_find):
     expected_query = Query(Eq("collection.name", "example"),
                            Collection("index", "uid"), 2)
     mock_index_dynamoplus_repository.return_value = None
     mock_find.side_effect = [
         self.fake_query_result("1", "2"),
         self.fake_query_result("2", "3"),
         self.fake_query_result("3", "4"),
         self.fake_query_result("4", "5"),
         self.fake_query_result("5"),
     ]
     indexes = self.systemService.get_indexes_from_collection_name_generator(
         "example", 2)
     uids = list(map(lambda i: i.uid, indexes))
     self.assertEqual(5, len(uids))
     self.assertEqual(["1", "2", "3", "4", "5"], uids)
     self.assertEqual(call(expected_query), mock_find.call_args_list[0])
Esempio n. 16
0
 def test_get_documents_by_index(self, mock_system_service, mock_get_collection_by_name, mock_get_index,
                                 mock_domain_service, mock_find_by_index):
     mock_system_service.return_value = None
     mock_get_collection_by_name.return_value = Collection("example", "id", "ordering")
     mock_domain_service.return_value = None
     expected_index = Index("1","example", ["attribute1"])
     expected_predicate = Eq("attribute1","1")
     mock_get_index.return_value=expected_index
     expected_document_example = {"attribute1": "1"}
     expected_documents = [
         {"id": "1", "attribute1": "1"},
         {"id": "2", "attribute1": "1"}
     ]
     mock_find_by_index.return_value = expected_documents, None
     documents = query("example", {"matches": {"eq":{"field_name":"attribute1", "value":"1"}}})
     self.assertEqual(len(documents), len(expected_documents))
     self.assertTrue(mock_get_collection_by_name.called_with("example"))
     self.assertTrue(mock_get_index.called_with("attribute1"))
     self.assertEqual(call(expected_predicate, None, None), mock_find_by_index.call_args_list[0])
Esempio n. 17
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])
Esempio n. 18
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)
Esempio n. 19
0
    def setUp(self):
        os.environ["TEST_FLAG"] = "true"
        os.environ["DYNAMODB_DOMAIN_TABLE"] = "example-domain"
        self.dynamodb = boto3.resource("dynamodb")
        self.dynamodb.create_table(TableName="example-domain",
                                   KeySchema=[{
                                       'AttributeName': 'pk',
                                       'KeyType': 'HASH'
                                   }, {
                                       'AttributeName': 'sk',
                                       'KeyType': 'RANGE'
                                   }],
                                   AttributeDefinitions=[{
                                       'AttributeName': 'pk',
                                       'AttributeType': 'S'
                                   }, {
                                       'AttributeName': 'sk',
                                       'AttributeType': 'S'
                                   }, {
                                       'AttributeName': 'data',
                                       'AttributeType': 'S'
                                   }],
                                   GlobalSecondaryIndexes=[{
                                       'IndexName':
                                       'sk-data-index',
                                       'KeySchema': [{
                                           'AttributeName': 'sk',
                                           'KeyType': 'HASH'
                                       }, {
                                           'AttributeName': 'data',
                                           'KeyType': 'RANGE'
                                       }],
                                       "Projection": {
                                           "ProjectionType": "ALL"
                                       }
                                   }])
        self.collection = Collection("example", "id", "ordering")

        self.table = self.dynamodb.Table('example-domain')
Esempio n. 20
0
 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])
Esempio n. 21
0
 def test_query_model_any_match(self):
     collection = Collection("example", "id", "ordering")
     query_model = QueryModel(collection, AnyMatch())
     self.assertIsNone(query_model.pk())
     self.assertEqual("example", query_model.sk())
     self.assertIsNone(query_model.data())
Esempio n. 22
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")
Esempio n. 23
0
import logging
from typing import *

from dynamoplus.models.query.conditions import Predicate, And, Range, Eq, AnyMatch
from dynamoplus.repository.models import Query as QueryRepository
from dynamoplus.repository.repositories import DynamoPlusRepository, IndexDynamoPlusRepository
from dynamoplus.models.system.collection.collection import Collection, AttributeDefinition, AttributeType, \
    AttributeConstraint
from dynamoplus.models.system.index.index import Index
from dynamoplus.models.system.client_authorization.client_authorization import ClientAuthorization, \
    ClientAuthorizationApiKey, ClientAuthorizationHttpSignature, Scope, ScopesType

collectionMetadata = Collection("collection", "name")
index_metadata = Collection("index", "uid")
client_authorization_metadata = Collection("client_authorization", "client_id")

logger = logging.getLogger()
logger.setLevel(logging.INFO)


def from_collection_to_dict(collection_metadata: Collection):
    result = {
        "name": collection_metadata.name,
        "id_key": collection_metadata.id_key
    }
    if collection_metadata.ordering_key:
        result["ordering_key"] = collection_metadata.ordering_key
    return result


def from_index_to_dict(index_metadata: Index):
Esempio n. 24
0
 def setUp(self):
     self.exampleCollectionMetadata = Collection("example", "id",
                                                 "ordering")
     self.domainService = DomainService(self.exampleCollectionMetadata)
Esempio n. 25
0
import os
from typing import *
from dynamoplus.models.system.collection.collection import Collection

from dynamoplus.models.query.query import Query, Index

collectionMetadata = Collection("collection", "name")
indexMetadata = Collection("index", "uid")
systemCollections = {"collection": collectionMetadata, "index": indexMetadata}


class DynamoPlusService(object):
    def __init__(self):
        pass

    @staticmethod
    def is_system(collection_name):
        system_entities = os.environ['ENTITIES']
        return collection_name in system_entities.split(",")

    @staticmethod
    def build_index(index_str: str):
        index_str_array = index_str.split("#")
        document_type = index_str_array[0]
        index_name = index_str_array[1]
        parts1 = index_name.split("__ORDER_BY__")
        conditions = parts1[0].split("__")
        return Index(document_type,
                     conditions,
                     ordering_key=parts1[1] if len(parts1) > 1 else None)