예제 #1
0
    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi('')

        # create the app object
        self.app = App(self.mockapi)

        # create the client object
        self.client_id = self.mockapi.clients_list[0]['client_id']
        self.client = Client(self.app, self.client_id)
예제 #2
0
    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi('')

        # create the app object
        self.app = App(self.mockapi)

        self.schema_name = 'janraintestschema'

        # create the schema object
        self.schema = Schema(self.app, self.schema_name)
예제 #3
0
    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi('')

        # shortcuts to some values
        self.client_id = self.mockapi.clients_list[0]['client_id']
        self.client_settings = self.mockapi.settings['client_settings'][
            self.client_id]

        # create the app object
        self.app = App(self.mockapi)

        # create the client settings object
        self.settings = ClientSettings(self.app, self.client_id)
    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi('')

        # create the app object
        self.app = App(self.mockapi)

        # create the default settings object
        self.settings = DefaultSettings(self.app)
예제 #5
0
    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi("")

        # create the app object
        self.app = App(self.mockapi)

        # create the client object
        self.client_id = self.mockapi.clients_list[0]["client_id"]
        self.client = Client(self.app, self.client_id)
예제 #6
0
    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi('')

        # create the app object
        self.app = App(self.mockapi)

        self.schema_name = 'janraintestschema'

        # create the schema object
        self.schema = Schema(self.app, self.schema_name)
    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi('')

        # shortcuts to some values
        self.client_id = self.mockapi.clients_list[0]['client_id']
        self.client_settings = self.mockapi.settings['client_settings'][self.client_id]

        # create the app object
        self.app = App(self.mockapi)

        # create the client settings object
        self.settings = ClientSettings(self.app, self.client_id)
    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi('')

        # create the app object
        self.app = App(self.mockapi)

        self.schema_name = 'janraintestschema'

        self.uuid = '00000000-0000-0000-0000-000000000001'

        # create the entities object
        self.record = SchemaRecord(self.app, self.schema_name, self.uuid)
예제 #9
0
    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi('')

        # create the app object to be tested
        self.app = App(self.mockapi)
예제 #10
0
class TestApp(unittest.TestCase):

    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi('')

        # create the app object to be tested
        self.app = App(self.mockapi)

    def test_apicall(self):
        args = (402, '', '', '')
        self.mockapi.call.side_effect = janrain.capture.ApiResponseError(*args)
        try:
            self.app.apicall('flows/get')
        except janrain_datalib.exceptions.ApiAuthError:
            pass  # expection
        else:
            self.fail("ApiAuthError not raised on 402 code from api")

        args = (222, '', '', '')
        self.mockapi.call.side_effect = janrain.capture.ApiResponseError(*args)
        try:
            self.app.apicall('flows/get')
        except janrain_datalib.exceptions.ApiNotFoundError:
            pass  # expection
        else:
            self.fail("ApiNotFoundError not raised on 222 code from api")

    def test_cache(self):
        # set
        self.app.set_cache('testkey', 'testvalue')
        expected = {
            'testkey': 'testvalue',
        }
        self.assertEqual(self.app.get_cache(), expected)

        # set non-existant
        self.app.set_cache('testkey.a.b', 'testvalue')
        # value was not set
        self.assertEqual(self.app.get_cache(), expected)

        # force set non-existant
        self.app.set_cache('testkey.a.b', 'testvalue', force=True)
        expected = {
            'testkey': {
                'a': {
                    'b': 'testvalue',
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

        # delete
        self.app.del_cache('testkey')
        self.assertEqual(self.app.get_cache(), {})

        # multi-level
        data = {
            'a': {
                'b': 'testvalue'
            }
        }
        self.app.set_cache('testkey', data)
        expected = {
            'testkey': data,
        }
        self.assertEqual(self.app.get_cache(), expected)

        # add to last level
        expected['testkey']['a']['c'] = 'test2'
        self.app.set_cache('testkey.a.c', 'test2')
        self.assertEqual(self.app.get_cache(), expected)

        # delete non-existant key - no error, no change
        self.app.del_cache('testkey.a.z')
        self.assertEqual(self.app.get_cache(), expected)

        # delete sub-level
        self.app.del_cache('testkey.a')
        expected = {
            'testkey': {}
        }
        self.assertEqual(self.app.get_cache(), expected)

        # get non-existant key
        try:
            self.app.get_cache('testkey.a.z')
        except KeyError:
            pass  # expection
        else:
            self.fail("KeyError was not raised when getting non-existant cache key")

    def test_clients_as_dict(self):
        clients = self.app.clients_as_dict()
        expected = {x['client_id']: x for x in self.mockapi.clients_list}

        self.assertEqual(clients, expected)
        calls = [
            mock.call('clients/list')
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        clients = self.app.clients_as_dict()
        self.assertEqual(clients, expected)
        # no additional api calls were made
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # clear cache and try again
        self.app.del_cache()
        clients = self.app.clients_as_dict()
        self.assertEqual(clients, expected)
        # triggered re-fetch
        calls.append(mock.call('clients/list'))
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_settings_as_dict(self):
        settings = self.app.settings_as_dict()
        expected = self.mockapi.settings

        self.assertEqual(settings, expected)
        calls = [
            mock.call('settings/get_all')
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        settings = self.app.settings_as_dict()
        # no additional api calls were made
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # clear cache and try again
        self.app.del_cache()
        settings = self.app.settings_as_dict()
        self.assertEqual(settings, expected)
        # triggered re-fetch
        calls.append(mock.call('settings/get_all'))
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_list_schemas(self):
        schemas = sorted(self.app.list_schemas())
        expected = sorted(self.mockapi.schemas_list)

        self.assertEqual(schemas, expected)
        calls = [
            mock.call('entityType.list')
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        schemas = sorted(self.app.list_schemas())
        self.assertEqual(schemas, expected)
        # no additional api calls were made
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache was updated
        expected = {
            'schema_names': schemas,
        }
        self.assertEqual(self.app.get_cache(), expected)

        # clear cache and try again
        self.app.del_cache()
        schemas = sorted(self.app.list_schemas())
        expected = sorted(self.mockapi.schemas_list)
        self.assertEqual(schemas, expected)
        # triggered re-fetch
        calls.append(mock.call('entityType.list'))
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_default_settings(self):
        self.assertTrue(isinstance(self.app.default_settings, DefaultSettings))
        # no api calls were made
        self.assertEqual([], self.mockapi.call.mock_calls)

    def test_get_client(self):
        client_id = 'test'
        client = self.app.get_client(client_id)

        self.assertTrue(isinstance(client, Client))
        self.assertEqual(client.client_id, client_id)
        # no api calls were made
        self.assertEqual([], self.mockapi.call.mock_calls)

    def test_get_schema(self):
        schema_name = 'test'
        schema = self.app.get_schema(schema_name)

        self.assertTrue(isinstance(schema, Schema))
        # no api calls were made
        self.assertEqual([], self.mockapi.call.mock_calls)
예제 #11
0
class TestClient(unittest.TestCase):
    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi("")

        # create the app object
        self.app = App(self.mockapi)

        # create the client object
        self.client_id = self.mockapi.clients_list[0]["client_id"]
        self.client = Client(self.app, self.client_id)

    def test_create(self):
        description = "client2"
        features = ["login_client"]
        new_client_id = self.client.create(description, features)
        expected = self.mockapi.new_client_id
        self.assertEqual(new_client_id, expected)

        calls = [mock.call("clients/add", description=description, features=features)]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_delete(self):
        self.client.delete()

        calls = [mock.call("clients/delete", client_id_for_deletion=self.client_id)]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_reset_secret(self):
        # clients must be cached for this test to succeed
        # since the mockapi doesn't actually update the client
        # but the cache will get updated
        self.client.as_dict()  # get clients into cache

        hours_to_live = "3"
        new_client_secret = self.client.reset_secret(hours_to_live)
        expected = self.mockapi.new_client_secret
        self.assertEqual(new_client_secret, expected)

        calls = [
            mock.call("clients/list"),
            mock.call("clients/reset_secret", hours_to_live=hours_to_live, for_client_id=self.client_id),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_set_description(self):
        description = "new description"
        # clients must be cached for this test to succeed
        # since the mockapi doesn't actually update the client
        # but the cache will get updated
        self.client.as_dict()  # get clients into cache

        cache_key = "clients.{}.description".format(self.client_id)
        self.assertNotEqual(self.app.get_cache(cache_key), description)

        # set description
        self.client.set_description(description)

        # cache was updated
        self.assertEqual(self.app.get_cache(cache_key), description)

        calls = [
            mock.call("clients/list"),
            mock.call("clients/set_description", description=description, for_client_id=self.client_id),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_set_features(self):
        features = ["test"]
        # clients must be cached for this test to succeed
        # since the mockapi doesn't actually update the client
        # but the cache will get updated
        self.client.as_dict()  # get clients into cache

        cache_key = "clients.{}.features".format(self.client_id)
        self.assertNotEqual(self.app.get_cache(cache_key), features)

        # set features
        self.client.set_features(features)

        # cache was updated
        self.assertEqual(self.app.get_cache(cache_key), features)

        calls = [
            mock.call("clients/list"),
            mock.call("clients/set_features", features=features, for_client_id=self.client_id),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_set_whitelist(self):
        whitelist = ["192.168.0.0/24"]
        # clients must be cached for this test to succeed
        # since the mockapi doesn't actually update the client
        # but the cache will get updated
        self.client.as_dict()  # get clients into cache

        cache_key = "clients.{}.whitelist".format(self.client_id)
        self.assertNotEqual(self.app.get_cache(cache_key), whitelist)

        # set whitelist
        self.client.set_whitelist(whitelist)

        # cache was updated
        self.assertEqual(self.app.get_cache(cache_key), whitelist)

        calls = [
            mock.call("clients/list"),
            mock.call("clients/set_whitelist", whitelist=whitelist, for_client_id=self.client_id),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_settings(self):
        client_settings = self.client.settings
        self.assertTrue(isinstance(client_settings, ClientSettings))

        # no api calls were made
        self.assertEqual([], self.mockapi.call.mock_calls)

    def test_as_dict(self):
        client_dict = self.client.as_dict()
        expected = self.mockapi.clients_list[0]
        self.assertEqual(client_dict, expected)

        # client is in cache
        expected = self.app.get_cache("clients")
        self.assertEqual(client_dict, expected[self.client_id])

        # modifying dict does not modify app cache
        client_dict["description"] = "test"
        self.assertNotEqual(client_dict, expected)
예제 #12
0
class TestSchema(unittest.TestCase):

    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi('')

        # create the app object
        self.app = App(self.mockapi)

        self.schema_name = 'janraintestschema'

        # create the schema object
        self.schema = Schema(self.app, self.schema_name)

    def test_create_delete(self):
        attr_defs = []
        new_attr_defs = self.schema.create(attr_defs)
        self.assertEqual(new_attr_defs, attr_defs)

        calls = [
            mock.call('entityType.create', type_name=self.schema_name, attr_defs=attr_defs)
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {
            'schemas': {
                self.schema_name: {
                    'attr_defs': attr_defs,
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

        self.schema.delete()

        calls.append(mock.call('entityType.properties', type_name=self.schema_name))
        for prop in self.mockapi.schema_properties:
            calls.append(mock.call('entityType.removeProperty', type_name=self.schema_name, uuid=prop['uuid']))
        calls.append(mock.call('entityType.delete', type_name=self.schema_name, commit=True))
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {
            'schemas': {}
        }
        self.assertEqual(self.app.get_cache(), expected)

    def test_add_attribute(self):
        attr_def = {
            'name': 'test',
            'type': 'test',
        }
        self.schema.add_attribute(attr_def)

        calls = [
            mock.call('entityType.addAttribute', type_name=self.schema_name, attr_def=attr_def),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {
            'schemas': {
                self.schema_name: {
                    'attr_defs': self.mockapi.schema_attributes,
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

    def test_remove_attribute(self):
        attr_name = 'aboutMe'
        self.schema.remove_attribute(attr_name)

        calls = [
            mock.call('entityType.removeAttribute', type_name=self.schema_name, attribute_name=attr_name),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {
            'schemas': {
                self.schema_name: {
                    'attr_defs': self.mockapi.schema_attributes,
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

    def test_set_attribute_description(self):
        attr_name = 'aboutMe'
        description = 'test description'
        self.schema.set_attribute_description(attr_name, description)

        calls = [
            mock.call('entityType.setAttributeDescription', type_name=self.schema_name, attribute_name=attr_name, description='"{}"'.format(description))
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {
            'schemas': {
                self.schema_name: {
                    'attr_defs': self.mockapi.schema_attributes,
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

    def test_set_attribute_constraints(self):
        attr_name = 'aboutMe'
        constraints = ["new", "constraints", "list"]
        self.schema.set_attribute_constraints(attr_name, constraints)

        calls = [
            mock.call('entityType.setAttributeConstraints', type_name=self.schema_name, attribute_name=attr_name, constraints=constraints)
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {
            'schemas': {
                self.schema_name: {
                    'attr_defs': self.mockapi.schema_attributes,
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

    def test_add_rule(self):
        rule_def = {
            'attributes': ['aboutMe'],
            'definition': 'required',
            'description': 'test rule',
        }
        self.schema.add_rule(rule_def)

        prop_def = {
            'attributes': rule_def['attributes'],
            'type': 'rule',
            'definition': rule_def['definition'],
            'description': rule_def['description'],
        }
        calls = [
            mock.call('entityType.addProperty', type_name=self.schema_name, property=prop_def),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache not updated
        self.assertEqual(self.app.get_cache(), {})

    def test_remove_rule(self):
        uuid = self.mockapi.schema_properties[0]['uuid']
        self.schema.remove_rule(uuid)

        calls = [
            mock.call('entityType.removeProperty', type_name=self.schema_name, uuid=uuid),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache not updated
        self.assertEqual(self.app.get_cache(), {})

    def test_get_attr_defs(self):
        attr_defs = self.schema.get_attr_defs()
        expected = self.mockapi.schema_attributes
        self.assertEqual(attr_defs, expected)

        calls = [
            mock.call('entityType', type_name=self.schema_name, remove_reserved=False),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {
            'schemas': {
                self.schema_name: {
                    'attr_defs': self.mockapi.schema_attributes,
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

    def test_get_attr_defs_remove_reserved(self):
        attr_defs = self.schema.get_attr_defs(remove_reserved=True)
        # mockapi doesn't actually remove reserved attributes
        expected = self.mockapi.schema_attributes
        self.assertEqual(attr_defs, expected)

        calls = [
            mock.call('entityType', type_name=self.schema_name, remove_reserved=True),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache not updated
        self.assertEqual(self.app.get_cache(), {})

    def test_get_attr(self):
        attr_name = '/address/street'
        attr = self.schema.get_attr(attr_name)
        expected = {
            "name": "/address/street",
            "type": "string",
            "length": None,
            "case-sensitive": False
        }
        self.assertEqual(attr, expected)

        calls = [
            mock.call('entityType', type_name=self.schema_name, remove_reserved=False),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {
            'schemas': {
                self.schema_name: {
                    'attr_defs': self.mockapi.schema_attributes,
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

    def test_get_rule_defs(self):
        rules = self.schema.get_rule_defs()
        expected = [
            {
                "attributes": [
                    "/birthday"
                ],
                "uuid": "00000000-0000-0000-0000-000000000001",
                "description": "birthday is read-only",
                "definition": "ignore-update"
            },
            {
                "attributes": [
                    "/aboutMe"
                ],
                "uuid": "00000000-0000-0000-0000-000000000002",
                "description": None,
                "definition": {
                    "truncate": 100
                }
            },
            {
                "attributes": [
                    "/givenName",
                    "/familyName"
                ],
                "uuid": "00000000-0000-0000-0000-000000000003",
                "description": "full name must be unique",
                "definition": "unique"
            }
        ]
        self.assertEqual(rules, expected)

        calls = [
            mock.call('entityType.properties', type_name=self.schema_name)
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {
            'schemas': {
                self.schema_name: {
                    'rules': rules
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

    def test_get_rule(self):
        uuid = self.mockapi.schema_properties[0]['uuid']
        rule_def = self.schema.get_rule(uuid)

        self.assertEqual(rule_def['uuid'], uuid)
        self.assertEqual(rule_def['definition'], self.mockapi.schema_properties[0]['definition'])

    def test_records(self):
        records = self.schema.records
        self.assertTrue(isinstance(records, SchemaRecords))

        # no api calls were made
        self.assertEqual([], self.mockapi.call.mock_calls)
예제 #13
0
class TestClientSettings(unittest.TestCase):
    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi('')

        # shortcuts to some values
        self.client_id = self.mockapi.clients_list[0]['client_id']
        self.client_settings = self.mockapi.settings['client_settings'][
            self.client_id]

        # create the app object
        self.app = App(self.mockapi)

        # create the client settings object
        self.settings = ClientSettings(self.app, self.client_id)

    def test_get_existant(self):
        # get existing setting
        key = list(self.client_settings.keys())[0]
        value = self.settings.get(key)
        expected = self.client_settings[key]
        self.assertEqual(value, expected)

        calls = [
            mock.call('settings/get_all'),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache is populated
        expected = self.mockapi.settings
        self.assertEqual(self.app.get_cache('settings'), expected)

        key = list(self.client_settings.keys())[1]
        value = self.settings.get(key)
        expected = self.client_settings[key]
        self.assertEqual(value, expected)

        # getting another setting does not cause more api calls
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_get_nonexistant(self):
        # get setting that does not exist
        key = 'bogus'
        value = self.settings.get(key)
        self.assertIs(value, None)

        # cache is populated
        expected = self.mockapi.settings
        self.assertEqual(self.app.get_cache('settings'), expected)

        calls = [
            mock.call('settings/get_all'),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_get_all(self):
        all_settings = self.settings.get_all()
        expected = self.mockapi.settings['default_settings']
        expected.update(
            self.mockapi.settings['client_settings'][self.settings.client_id])
        self.assertEqual(all_settings, expected)

        # cache is populated
        expected = self.mockapi.settings
        self.assertEqual(self.app.get_cache('settings'), expected)

        calls = [
            mock.call('settings/get_all'),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_set(self):
        # existing key
        key = list(self.client_settings.keys())[0]
        value = 'new value'
        result = self.settings.set(key, value)
        self.assertIs(result, True)

        # cache is not populated
        try:
            self.app.get_cache('settings')
        except KeyError:
            pass  # expection
        else:
            self.fail("KeyError not raised on missing cache key")

        calls = [
            mock.call('settings/set',
                      for_client_id=self.client_id,
                      key=key,
                      value=value),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # non-existing key
        key = 'bogus'
        value = self.settings.get(key)
        self.assertIs(value, None)
        calls.append(mock.call('settings/get_all'))
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache is populated
        expected = self.mockapi.settings
        self.assertEqual(self.app.get_cache('settings'), expected)

        # set non-existing key
        result = self.settings.set(key, value)
        self.assertIs(result, False)

        # cache is updated
        expected = self.app.get_cache('settings.client_settings.{}.{}'.format(
            self.client_id, key))
        self.assertEqual(value, expected)

        calls.append(
            mock.call('settings/set',
                      for_client_id=self.client_id,
                      key=key,
                      value=value))
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_set_multi(self):
        items = {
            list(self.client_settings.keys())[0]: 'updated',
            'bogus': 'new setting',
        }
        report = self.settings.set_multi(items)
        expected = {
            list(self.client_settings.keys())[0]: True,
            'bogus': False,
        }
        self.assertEqual(report, expected)

        # cache is not populated
        try:
            self.app.get_cache('settings')
        except KeyError:
            pass  # expection
        else:
            self.fail("KeyError not raised on missing cache key")

        calls = [
            mock.call('settings/set_multi',
                      for_client_id=self.client_id,
                      items=items)
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_delete(self):
        key = list(self.client_settings.keys())[0]
        result = self.settings.delete(key)
        self.assertIs(result, True)

        calls = [
            mock.call('settings/delete', for_client_id=self.client_id, key=key)
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)
class TestClientSettings(unittest.TestCase):

    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi('')

        # shortcuts to some values
        self.client_id = self.mockapi.clients_list[0]['client_id']
        self.client_settings = self.mockapi.settings['client_settings'][self.client_id]

        # create the app object
        self.app = App(self.mockapi)

        # create the client settings object
        self.settings = ClientSettings(self.app, self.client_id)

    def test_get_existant(self):
        # get existing setting
        key = list(self.client_settings.keys())[0]
        value = self.settings.get(key)
        expected = self.client_settings[key]
        self.assertEqual(value, expected)

        calls = [
            mock.call('settings/get_all'),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache is populated
        expected = self.mockapi.settings
        self.assertEqual(self.app.get_cache('settings'), expected)

        key = list(self.client_settings.keys())[1]
        value = self.settings.get(key)
        expected = self.client_settings[key]
        self.assertEqual(value, expected)

        # getting another setting does not cause more api calls
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_get_nonexistant(self):
        # get setting that does not exist
        key = 'bogus'
        value = self.settings.get(key)
        self.assertIs(value, None)

        # cache is populated
        expected = self.mockapi.settings
        self.assertEqual(self.app.get_cache('settings'), expected)

        calls = [
            mock.call('settings/get_all'),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_get_all(self):
        all_settings = self.settings.get_all()
        expected = self.mockapi.settings['default_settings']
        expected.update(self.mockapi.settings['client_settings'][self.settings.client_id])
        self.assertEqual(all_settings, expected)

        # cache is populated
        expected = self.mockapi.settings
        self.assertEqual(self.app.get_cache('settings'), expected)

        calls = [
            mock.call('settings/get_all'),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_set(self):
        # existing key
        key = list(self.client_settings.keys())[0]
        value = 'new value'
        result = self.settings.set(key, value)
        self.assertIs(result, True)

        # cache is not populated
        try:
            self.app.get_cache('settings')
        except KeyError:
            pass  # expection
        else:
            self.fail("KeyError not raised on missing cache key")

        calls = [
            mock.call('settings/set', for_client_id=self.client_id, key=key, value=value),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # non-existing key
        key = 'bogus'
        value = self.settings.get(key)
        self.assertIs(value, None)
        calls.append(mock.call('settings/get_all'))
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache is populated
        expected = self.mockapi.settings
        self.assertEqual(self.app.get_cache('settings'), expected)

        # set non-existing key
        result = self.settings.set(key, value)
        self.assertIs(result, False)

        # cache is updated
        expected = self.app.get_cache('settings.client_settings.{}.{}'.format(self.client_id, key))
        self.assertEqual(value, expected)

        calls.append(mock.call('settings/set', for_client_id=self.client_id, key=key, value=value))
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_set_multi(self):
        items = {
            list(self.client_settings.keys())[0]: 'updated',
            'bogus': 'new setting',
        }
        report = self.settings.set_multi(items)
        expected = {
            list(self.client_settings.keys())[0]: True,
            'bogus': False,
        }
        self.assertEqual(report, expected)

        # cache is not populated
        try:
            self.app.get_cache('settings')
        except KeyError:
            pass  # expection
        else:
            self.fail("KeyError not raised on missing cache key")

        calls = [
            mock.call('settings/set_multi', for_client_id=self.client_id, items=items)
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_delete(self):
        key = list(self.client_settings.keys())[0]
        result = self.settings.delete(key)
        self.assertIs(result, True)

        calls = [
            mock.call('settings/delete', for_client_id=self.client_id, key=key)
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)
예제 #15
0
class TestSchema(unittest.TestCase):
    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi('')

        # create the app object
        self.app = App(self.mockapi)

        self.schema_name = 'janraintestschema'

        # create the schema object
        self.schema = Schema(self.app, self.schema_name)

    def test_create_delete(self):
        attr_defs = []
        new_attr_defs = self.schema.create(attr_defs)
        self.assertEqual(new_attr_defs, attr_defs)

        calls = [
            mock.call('entityType.create',
                      type_name=self.schema_name,
                      attr_defs=attr_defs)
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {
            'schemas': {
                self.schema_name: {
                    'attr_defs': attr_defs,
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

        self.schema.delete()

        calls.append(
            mock.call('entityType.properties', type_name=self.schema_name))
        for prop in self.mockapi.schema_properties:
            calls.append(
                mock.call('entityType.removeProperty',
                          type_name=self.schema_name,
                          uuid=prop['uuid']))
        calls.append(
            mock.call('entityType.delete',
                      type_name=self.schema_name,
                      commit=True))
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {'schemas': {}}
        self.assertEqual(self.app.get_cache(), expected)

    def test_add_attribute(self):
        attr_def = {
            'name': 'test',
            'type': 'test',
        }
        self.schema.add_attribute(attr_def)

        calls = [
            mock.call('entityType.addAttribute',
                      type_name=self.schema_name,
                      attr_def=attr_def),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {
            'schemas': {
                self.schema_name: {
                    'attr_defs': self.mockapi.schema_attributes,
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

    def test_remove_attribute(self):
        attr_name = 'aboutMe'
        self.schema.remove_attribute(attr_name)

        calls = [
            mock.call('entityType.removeAttribute',
                      type_name=self.schema_name,
                      attribute_name=attr_name),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {
            'schemas': {
                self.schema_name: {
                    'attr_defs': self.mockapi.schema_attributes,
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

    def test_set_attribute_description(self):
        attr_name = 'aboutMe'
        description = 'test description'
        self.schema.set_attribute_description(attr_name, description)

        calls = [
            mock.call('entityType.setAttributeDescription',
                      type_name=self.schema_name,
                      attribute_name=attr_name,
                      description='"{}"'.format(description))
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {
            'schemas': {
                self.schema_name: {
                    'attr_defs': self.mockapi.schema_attributes,
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

    def test_set_attribute_constraints(self):
        attr_name = 'aboutMe'
        constraints = ["new", "constraints", "list"]
        self.schema.set_attribute_constraints(attr_name, constraints)

        calls = [
            mock.call('entityType.setAttributeConstraints',
                      type_name=self.schema_name,
                      attribute_name=attr_name,
                      constraints=constraints)
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {
            'schemas': {
                self.schema_name: {
                    'attr_defs': self.mockapi.schema_attributes,
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

    def test_add_rule(self):
        rule_def = {
            'attributes': ['aboutMe'],
            'definition': 'required',
            'description': 'test rule',
        }
        self.schema.add_rule(rule_def)

        prop_def = {
            'attributes': rule_def['attributes'],
            'type': 'rule',
            'definition': rule_def['definition'],
            'description': rule_def['description'],
        }
        calls = [
            mock.call('entityType.addProperty',
                      type_name=self.schema_name,
                      property=prop_def),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache not updated
        self.assertEqual(self.app.get_cache(), {})

    def test_remove_rule(self):
        uuid = self.mockapi.schema_properties[0]['uuid']
        self.schema.remove_rule(uuid)

        calls = [
            mock.call('entityType.removeProperty',
                      type_name=self.schema_name,
                      uuid=uuid),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache not updated
        self.assertEqual(self.app.get_cache(), {})

    def test_get_attr_defs(self):
        attr_defs = self.schema.get_attr_defs()
        expected = self.mockapi.schema_attributes
        self.assertEqual(attr_defs, expected)

        calls = [
            mock.call('entityType',
                      type_name=self.schema_name,
                      remove_reserved=False),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {
            'schemas': {
                self.schema_name: {
                    'attr_defs': self.mockapi.schema_attributes,
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

    def test_get_attr_defs_remove_reserved(self):
        attr_defs = self.schema.get_attr_defs(remove_reserved=True)
        # mockapi doesn't actually remove reserved attributes
        expected = self.mockapi.schema_attributes
        self.assertEqual(attr_defs, expected)

        calls = [
            mock.call('entityType',
                      type_name=self.schema_name,
                      remove_reserved=True),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache not updated
        self.assertEqual(self.app.get_cache(), {})

    def test_get_attr(self):
        attr_name = '/address/street'
        attr = self.schema.get_attr(attr_name)
        expected = {
            "name": "/address/street",
            "type": "string",
            "length": None,
            "case-sensitive": False
        }
        self.assertEqual(attr, expected)

        calls = [
            mock.call('entityType',
                      type_name=self.schema_name,
                      remove_reserved=False),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {
            'schemas': {
                self.schema_name: {
                    'attr_defs': self.mockapi.schema_attributes,
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

    def test_get_rule_defs(self):
        rules = self.schema.get_rule_defs()
        expected = [{
            "attributes": ["/birthday"],
            "uuid": "00000000-0000-0000-0000-000000000001",
            "description": "birthday is read-only",
            "definition": "ignore-update"
        }, {
            "attributes": ["/aboutMe"],
            "uuid": "00000000-0000-0000-0000-000000000002",
            "description": None,
            "definition": {
                "truncate": 100
            }
        }, {
            "attributes": ["/givenName", "/familyName"],
            "uuid": "00000000-0000-0000-0000-000000000003",
            "description": "full name must be unique",
            "definition": "unique"
        }]
        self.assertEqual(rules, expected)

        calls = [
            mock.call('entityType.properties', type_name=self.schema_name)
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache updated
        expected = {'schemas': {self.schema_name: {'rules': rules}}}
        self.assertEqual(self.app.get_cache(), expected)

    def test_get_rule(self):
        uuid = self.mockapi.schema_properties[0]['uuid']
        rule_def = self.schema.get_rule(uuid)

        self.assertEqual(rule_def['uuid'], uuid)
        self.assertEqual(rule_def['definition'],
                         self.mockapi.schema_properties[0]['definition'])

    def test_records(self):
        records = self.schema.records
        self.assertTrue(isinstance(records, SchemaRecords))

        # no api calls were made
        self.assertEqual([], self.mockapi.call.mock_calls)
예제 #16
0
class TestApp(unittest.TestCase):
    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi('')

        # create the app object to be tested
        self.app = App(self.mockapi)

    def test_apicall(self):
        args = (402, '', '', '')
        self.mockapi.call.side_effect = janrain.capture.ApiResponseError(*args)
        try:
            self.app.apicall('flows/get')
        except janrain_datalib.exceptions.ApiAuthError:
            pass  # expection
        else:
            self.fail("ApiAuthError not raised on 402 code from api")

        args = (222, '', '', '')
        self.mockapi.call.side_effect = janrain.capture.ApiResponseError(*args)
        try:
            self.app.apicall('flows/get')
        except janrain_datalib.exceptions.ApiNotFoundError:
            pass  # expection
        else:
            self.fail("ApiNotFoundError not raised on 222 code from api")

    def test_cache(self):
        # set
        self.app.set_cache('testkey', 'testvalue')
        expected = {
            'testkey': 'testvalue',
        }
        self.assertEqual(self.app.get_cache(), expected)

        # set non-existant
        self.app.set_cache('testkey.a.b', 'testvalue')
        # value was not set
        self.assertEqual(self.app.get_cache(), expected)

        # force set non-existant
        self.app.set_cache('testkey.a.b', 'testvalue', force=True)
        expected = {
            'testkey': {
                'a': {
                    'b': 'testvalue',
                }
            }
        }
        self.assertEqual(self.app.get_cache(), expected)

        # delete
        self.app.del_cache('testkey')
        self.assertEqual(self.app.get_cache(), {})

        # multi-level
        data = {'a': {'b': 'testvalue'}}
        self.app.set_cache('testkey', data)
        expected = {
            'testkey': data,
        }
        self.assertEqual(self.app.get_cache(), expected)

        # add to last level
        expected['testkey']['a']['c'] = 'test2'
        self.app.set_cache('testkey.a.c', 'test2')
        self.assertEqual(self.app.get_cache(), expected)

        # delete non-existant key - no error, no change
        self.app.del_cache('testkey.a.z')
        self.assertEqual(self.app.get_cache(), expected)

        # delete sub-level
        self.app.del_cache('testkey.a')
        expected = {'testkey': {}}
        self.assertEqual(self.app.get_cache(), expected)

        # get non-existant key
        try:
            self.app.get_cache('testkey.a.z')
        except KeyError:
            pass  # expection
        else:
            self.fail(
                "KeyError was not raised when getting non-existant cache key")

    def test_clients_as_dict(self):
        clients = self.app.clients_as_dict()
        expected = {x['client_id']: x for x in self.mockapi.clients_list}

        self.assertEqual(clients, expected)
        calls = [mock.call('clients/list')]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        clients = self.app.clients_as_dict()
        self.assertEqual(clients, expected)
        # no additional api calls were made
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # clear cache and try again
        self.app.del_cache()
        clients = self.app.clients_as_dict()
        self.assertEqual(clients, expected)
        # triggered re-fetch
        calls.append(mock.call('clients/list'))
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_settings_as_dict(self):
        settings = self.app.settings_as_dict()
        expected = self.mockapi.settings

        self.assertEqual(settings, expected)
        calls = [mock.call('settings/get_all')]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        settings = self.app.settings_as_dict()
        # no additional api calls were made
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # clear cache and try again
        self.app.del_cache()
        settings = self.app.settings_as_dict()
        self.assertEqual(settings, expected)
        # triggered re-fetch
        calls.append(mock.call('settings/get_all'))
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_list_schemas(self):
        schemas = sorted(self.app.list_schemas())
        expected = sorted(self.mockapi.schemas_list)

        self.assertEqual(schemas, expected)
        calls = [mock.call('entityType.list')]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        schemas = sorted(self.app.list_schemas())
        self.assertEqual(schemas, expected)
        # no additional api calls were made
        self.assertEqual(calls, self.mockapi.call.mock_calls)

        # cache was updated
        expected = {
            'schema_names': schemas,
        }
        self.assertEqual(self.app.get_cache(), expected)

        # clear cache and try again
        self.app.del_cache()
        schemas = sorted(self.app.list_schemas())
        expected = sorted(self.mockapi.schemas_list)
        self.assertEqual(schemas, expected)
        # triggered re-fetch
        calls.append(mock.call('entityType.list'))
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_default_settings(self):
        self.assertTrue(isinstance(self.app.default_settings, DefaultSettings))
        # no api calls were made
        self.assertEqual([], self.mockapi.call.mock_calls)

    def test_get_client(self):
        client_id = 'test'
        client = self.app.get_client(client_id)

        self.assertTrue(isinstance(client, Client))
        self.assertEqual(client.client_id, client_id)
        # no api calls were made
        self.assertEqual([], self.mockapi.call.mock_calls)

    def test_get_schema(self):
        schema_name = 'test'
        schema = self.app.get_schema(schema_name)

        self.assertTrue(isinstance(schema, Schema))
        # no api calls were made
        self.assertEqual([], self.mockapi.call.mock_calls)
예제 #17
0
    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi('')

        # create the app object to be tested
        self.app = App(self.mockapi)
예제 #18
0
class TestClient(unittest.TestCase):

    def setUp(self):
        # use a mock for the api calls
        self.mockapi = Mockapi('')

        # create the app object
        self.app = App(self.mockapi)

        # create the client object
        self.client_id = self.mockapi.clients_list[0]['client_id']
        self.client = Client(self.app, self.client_id)

    def test_create(self):
        description = 'client2'
        features = ['login_client']
        new_client_id = self.client.create(description, features)
        expected = self.mockapi.new_client_id
        self.assertEqual(new_client_id, expected)

        calls = [
            mock.call('clients/add', description=description, features=features),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_delete(self):
        self.client.delete()

        calls = [
            mock.call('clients/delete', client_id_for_deletion=self.client_id)
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_reset_secret(self):
        # clients must be cached for this test to succeed
        # since the mockapi doesn't actually update the client
        # but the cache will get updated
        self.client.as_dict()  # get clients into cache

        hours_to_live = '3'
        new_client_secret = self.client.reset_secret(hours_to_live)
        expected = self.mockapi.new_client_secret
        self.assertEqual(new_client_secret, expected)

        calls = [
            mock.call('clients/list'),
            mock.call('clients/reset_secret', hours_to_live=hours_to_live, for_client_id=self.client_id)
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_set_description(self):
        description = 'new description'
        # clients must be cached for this test to succeed
        # since the mockapi doesn't actually update the client
        # but the cache will get updated
        self.client.as_dict()  # get clients into cache

        cache_key = 'clients.{}.description'.format(self.client_id)
        self.assertNotEqual(self.app.get_cache(cache_key), description)

        # set description
        self.client.set_description(description)

        # cache was updated
        self.assertEqual(self.app.get_cache(cache_key), description)

        calls = [
            mock.call('clients/list'),
            mock.call('clients/set_description', description=description, for_client_id=self.client_id),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_set_features(self):
        features = ['test']
        # clients must be cached for this test to succeed
        # since the mockapi doesn't actually update the client
        # but the cache will get updated
        self.client.as_dict()  # get clients into cache

        cache_key = 'clients.{}.features'.format(self.client_id)
        self.assertNotEqual(self.app.get_cache(cache_key), features)

        # set features
        self.client.set_features(features)

        # cache was updated
        self.assertEqual(self.app.get_cache(cache_key), features)

        calls = [
            mock.call('clients/list'),
            mock.call('clients/set_features', features=features, for_client_id=self.client_id),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_set_whitelist(self):
        whitelist = ['192.168.0.0/24']
        # clients must be cached for this test to succeed
        # since the mockapi doesn't actually update the client
        # but the cache will get updated
        self.client.as_dict()  # get clients into cache

        cache_key = 'clients.{}.whitelist'.format(self.client_id)
        self.assertNotEqual(self.app.get_cache(cache_key), whitelist)

        # set whitelist
        self.client.set_whitelist(whitelist)

        # cache was updated
        self.assertEqual(self.app.get_cache(cache_key), whitelist)

        calls = [
            mock.call('clients/list'),
            mock.call('clients/set_whitelist', whitelist=whitelist, for_client_id=self.client_id),
        ]
        self.assertEqual(calls, self.mockapi.call.mock_calls)

    def test_settings(self):
        client_settings = self.client.settings
        self.assertTrue(isinstance(client_settings, ClientSettings))

        # no api calls were made
        self.assertEqual([], self.mockapi.call.mock_calls)

    def test_as_dict(self):
        client_dict = self.client.as_dict()
        expected = self.mockapi.clients_list[0]
        self.assertEqual(client_dict, expected)

        # client is in cache
        expected = self.app.get_cache('clients')
        self.assertEqual(client_dict, expected[self.client_id])

        # modifying dict does not modify app cache
        client_dict['description'] = 'test'
        self.assertNotEqual(client_dict, expected)