Пример #1
0
    def test_first_or_raise(self):
        with self.app.app_context():
            db.create_all()
            with self.assertRaises(errors.NoDataFound) as cm:
                Server.query.first_or_raise()

            self.assertEqual(cm.exception.args, ("Server", ))
Пример #2
0
    def setUp(self):
        """Create and configure a new app instance for each test."""
        # create the app with common test config
        self.app = create_app('test')
        self.app_context = self.app.app_context()
        self.app_context.push()
        self.client = self.app.test_client()
        self.headers = {
            "Authorization":
            f"Bearer {create_access_token('00000000-0000-0000-0000-000000000001')}"
        }

        db.create_all()
        # set_initial(server=False)

        self.srv1 = Server('node1',
                           id='00000000-0000-0000-0000-000000000001',
                           me=True)
        self.file = File(source_server=self.srv1,
                         target='/etc/ssh/sshd_config',
                         id='00000000-0000-0000-0000-000000000002')
        self.fsa = FileServerAssociation(file=self.file,
                                         destination_server=self.srv1)

        db.session.add_all([self.srv1, self.file, self.fsa])
        db.session.commit()
Пример #3
0
    def setUp(self):
        """Create and configure a new app instance for each test."""
        # create the app with common test config
        self.app = create_app('test')
        self.app.config['SECURIZER'] = True

        @self.app.route('/', methods=['GET', 'POST'])
        @securizer
        def home():
            return {'msg': 'default response'}

        self.app_context = self.app.app_context()
        self.app_context.push()
        self.client = self.app.test_client()
        db.init_app(self.app)
        db.create_all()
        self.server = Server('me', port=5000, me=True)
        db.session.add(self.server)

        self.dim = generate_dimension('dimension')
        self.dim.current = True
        db.session.add(self.dim)

        self.token = create_access_token('test')
        self.auth = HTTPBearerAuth(self.token)
        self.url = 'https://me:5000/'
Пример #4
0
    def setUp(self):
        """Create and configure a new self.app instance for each test."""
        # create a temporary file to isolate the database for each test
        # create the self.app with common test config

        self.app = Flask(__name__)
        self.app.config['JWT_SECRET_KEY'] = 'super-secret'

        self.jwt = JWTManager(self.app)

        @self.app.route('/', methods=['GET'])
        @jwt_required()
        @lock_catalog
        def hello():
            return {'msg': 'default response'}

        self.app_context = self.app.app_context()
        self.app_context.push()
        self.client = self.app.test_client()
        db.init_app(self.app)
        db.create_all()
        # self.d = generate_dimension('test')
        self.srv1 = Server(id='bbbbbbbb-1234-5678-1234-56781234bbb1', name='server1', me=True)
        Locker.set_initial()
        db.session.add(self.srv1)
        db.session.commit()
Пример #5
0
 def setUp(self):
     """Create and configure a new app instance for each test."""
     # create the app with common test config
     self.app = create_app('test')
     self.app_context = self.app.app_context()
     self.app_context.push()
     db.create_all()
Пример #6
0
    def setUp(self):
        """Create and configure a new app instance for each test."""
        # create the app with common test config
        self.app = create_app('test')
        self.app_context = self.app.app_context()
        self.app_context.push()
        self.client = self.app.test_client()
        self.headers = {
            "Authorization":
            f"Bearer {create_access_token('00000000-0000-0000-0000-000000000001')}"
        }

        db.create_all()
        set_initial()

        self.soft = Software(id='aaaaaaaa-1234-5678-1234-56781234aaa1',
                             name='test',
                             version='1',
                             filename='file')
        self.soft2 = Software(id='aaaaaaaa-1234-5678-1234-56781234aaa2',
                              name='test',
                              version='2',
                              filename='file')
        self.ssa = SoftwareServerAssociation(software=self.soft,
                                             server=Server.get_current(),
                                             path='/root')
        db.session.add_all([self.soft, self.soft2, self.ssa])
        db.session.commit()
Пример #7
0
    def fill_database(self, node):
        db.create_all()
        d = Dimension.from_json(self.dim)
        d.current = True
        s1 = Server(id='00000000-0000-0000-0000-000000000001', name='node1', created_on=now, me=node == 'node1')
        g11 = Gate(id='00000000-0000-0000-0000-000000000011', server=s1, port=5000, dns=s1.name)
        s2 = Server(id='00000000-0000-0000-0000-000000000002', name='node2', created_on=now, me=node == 'node2')
        g12 = Gate(id='00000000-0000-0000-0000-000000000012', server=s2, port=5000, dns=s2.name)
        s3 = Server(id='00000000-0000-0000-0000-000000000003', name='node3', created_on=now, me=node == 'node3')
        g13 = Gate(id='00000000-0000-0000-0000-000000000013', server=s3, port=5000, dns=s3.name)
        s4 = Server(id='00000000-0000-0000-0000-000000000004', name='node4', created_on=now, me=node == 'node4')
        g14 = Gate(id='00000000-0000-0000-0000-000000000014', server=s4, port=5000, dns=s4.name)

        if node == 'node1':
            self.s1 = s1
            self.s2 = s2
            self.s3 = s3
            self.s4 = s4
            Route(s2, g12)
            Route(s3, s2, 1)
        elif node == 'node2':
            Route(s1, g11)
            Route(s3, g13)
        elif node == 'node3':
            Route(s1, s2, 1)
            Route(s2, g12)

        db.session.add_all([d, s1, s2, s3, s4])
        db.session.commit()
Пример #8
0
    def setUp(self):
        self.maxDiff = None
        self.app_join = create_app('test')
        self.app_join.config['SERVER_NAME'] = 'join'
        self.app_join_context = self.app_join.app_context()
        self.client_join = self.app_join.test_client()
        with self.app_join.app_context():
            db.create_all()
            set_initial(server=False)
            self.join_server_id = Server.set_initial()
            db.session.commit()
            self.mock_dm = mock.MagicMock()
            self.mock_dm.flask_app = self.app_join
            self.mock_dm.engine = db.engine
            self.mock_dm.catalog_manager.db_update_catalog = update_db_catalog

        super().setUp()
        self.app.config['SECURIZER'] = True
        self.app_join.config['SECURIZER'] = True
        self.token = create_access_token(JOIN,
                                         additional_claims={'applicant': 'me'})
        self.setUpPyfakefs()

        open('/origin_key', 'w').write('keyfile')
        open('/origin_cert', 'w').write('certfile')
Пример #9
0
    def test_get_or_raise(self):
        with self.app.app_context():
            db.create_all()
            with self.assertRaises(errors.EntityNotFound) as cm:
                Server.query.get_or_raise(1)

            self.assertTupleEqual(cm.exception.args, ("Server", 1))
Пример #10
0
    def setUp(self):
        """Create and configure a new self.app instance for each test."""
        # create a temporary file to isolate the database for each test
        # create the self.app with common test config
        self.app = Flask(__name__)

        @self.app.route('/', methods=['GET', 'POST'])
        @forward_or_dispatch()
        def hello():
            return {'msg': 'default response'}

        self.app_context = self.app.app_context()
        self.app_context.push()
        self.client = self.app.test_client()
        db.init_app(self.app)
        db.create_all()

        self.srv1 = Server(id='bbbbbbbb-1234-5678-1234-56781234bbb1', name='server1',
                           dns_or_ip='192.168.1.9', port=7123)
        Route(self.srv1, cost=0)
        self.srv2 = Server(id='bbbbbbbb-1234-5678-1234-56781234bbb2', name='server2',
                           dns_or_ip='192.168.1.10', port=7124)
        Route(self.srv2, self.srv1, cost=1)
        db.session.add_all([self.srv1, self.srv2])
        db.session.commit()
Пример #11
0
 def setUp(self):
     """Create and configure a new app instance for each test."""
     # create the app with common test config
     self.app = create_app('test')
     self.app_context = self.app.app_context()
     self.app_context.push()
     self.client = self.app.test_client()
     self.auth = HTTPBearerAuth(create_access_token('00000000-0000-0000-0000-000000000001'))
     db.create_all()
Пример #12
0
 def setUp(self):
     self.maxDiff = None
     self.set_scoped_session()
     self.app = create_app('test')
     self.app.config['SERVER_NAME'] = 'me'
     self.app_context = self.app.app_context()
     self.client = self.app.test_client()
     self.app_context.push()
     db.create_all()
Пример #13
0
 def setUp(self) -> None:
     self.maxDiff = None
     self.app = create_app('test')
     self.app.config['SECURIZER'] = False
     # user sqlite in a file as flush does not emits states in an inmemory database
     # self.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+os.path.join(basedir, 'test.db')
     self.app_context = self.app.app_context()
     self.app_context.push()
     db.create_all()
Пример #14
0
 def setUp(self):
     """Create and configure a new app instance for each test."""
     # create the app with common test config
     self.app = create_app('test')
     self.app_context = self.app.app_context()
     self.app_context.push()
     self.client = self.app.test_client()
     db.create_all()
     set_initial()
     self.auth = HTTPBearerAuth(
         create_access_token(User.get_by_name('root').id))
Пример #15
0
 def setUp(self):
     """Create and configure a new app instance for each test."""
     # create the app with common test config
     self.app = create_app('test')
     self.app_context = self.app.app_context()
     self.app_context.push()
     self.client = self.app.test_client()
     self.auth = HTTPBearerAuth(create_access_token('00000000-0000-0000-0000-000000000001'))
     db.create_all()
     self.src = Server('source', port=5000)
     self.dst = Server('destination', port=5000)
Пример #16
0
    def test_from_to_json_with_gate(self, mock_get_now, mock_uuid):
        mock_get_now.return_value = get_now()
        mock_uuid.return_value = '22cd859d-ee91-4079-a112-000000000002'
        s = Server('server2', id='22cd859d-ee91-4079-a112-000000000001')

        gate = mock.MagicMock()
        gate.to_json.return_value = {'server_id': 1, 'server': 'n1'}

        type(s).gates = mock.PropertyMock(return_value=[gate])

        self.assertDictEqual(
            {
                'id':
                '22cd859d-ee91-4079-a112-000000000001',
                'name':
                'server2',
                'granules': [],
                'gates': [{}],
                'created_on':
                mock_get_now.return_value.strftime(defaults.DATETIME_FORMAT),
                '_old_name':
                None,
                'deleted':
                False
            }, s.to_json(add_gates=True))
        db.session.add(s)
        db.session.commit()

        s_json = s.to_json(add_gates=True)
        smashed = Server.from_json(s_json)

        self.assertIs(s, smashed)
        self.assertEqual(s.id, smashed.id)
        self.assertEqual(s.name, smashed.name)
        self.assertEqual(s.granules, smashed.granules)
        self.assertEqual(s.last_modified_at, smashed.last_modified_at)
        self.assertListEqual(s.gates, smashed.gates)

        # from new Server
        db.session.remove()
        db.drop_all()
        db.create_all()

        with patch('dimensigon.domain.entities.server.Gate.from_json'
                   ) as mock_gate_from_json:
            smashed = Server.from_json(s_json)

            self.assertEqual(s.id, smashed.id)
            self.assertEqual(s.name, smashed.name)
            self.assertEqual(s.granules, smashed.granules)
            self.assertEqual(s.last_modified_at, smashed.last_modified_at)
            self.assertEqual(1, len(smashed.gates))
            mock_gate_from_json.assert_called_once()
Пример #17
0
 def setUp(self):
     """Create and configure a new app instance for each test."""
     # create the app with common test config
     self.app = create_app('test')
     self.app_context = self.app.app_context()
     self.app_context.push()
     self.client = self.app.test_client()
     self.auth = HTTPBearerAuth(create_access_token('00000000-0000-0000-0000-000000000001'))
     db.create_all()
     self.me = Server('me', port=5000, _me=True)
     self.remote = Server('remote', port=5000)
     db.session.add_all([self.remote, self.me])
     self.maxDiff = None
Пример #18
0
    def _fill_database(self, app: Flask):
        with mock.patch('dimensigon.domain.entities.get_now') as mock_get_now:
            mock_get_now.return_value = defaults.INITIAL_DATEMARK
            node = app.config['SERVER_NAME']
            with app.app_context():
                db.create_all()
                event.listen(db.session, 'after_commit', receive_after_commit)
                set_initial(**self.initials)
                d = Dimension.from_json(self.dim)
                d.current = True
                s1 = Server('node1',
                            created_on=defaults.INITIAL_DATEMARK,
                            id=self.SERVER1,
                            me=node == 'node1')
                g11 = Gate(id='00000000-0000-0000-0000-000000000011',
                           server=s1,
                           port=5000,
                           dns=s1.name)
                s2 = Server('node2',
                            created_on=defaults.INITIAL_DATEMARK,
                            id=self.SERVER2,
                            me=node == 'node2')
                g12 = Gate(id='00000000-0000-0000-0000-000000000012',
                           server=s2,
                           port=5000,
                           dns=s2.name)
                s3 = Server('node3',
                            created_on=defaults.INITIAL_DATEMARK,
                            id=self.SERVER3,
                            me=node == 'node3')
                g13 = Gate(id='00000000-0000-0000-0000-000000000013',
                           server=s3,
                           port=5000,
                           dns=s3.name)

                if node == 'node1':
                    Route(s2, g12)
                    Route(s3, g13)
                elif node == 'node2':
                    Route(s1, g11)
                    Route(s3, g13)
                elif node == 'node3':
                    Route(s1, g11)
                    Route(s2, g12)

                self.fill_database()
                db.session.add_all([d, s1, s2, s3])
                db.session.commit()
            if node == 'node1':
                self.s1, self.s2, self.s3 = db.session.merge(
                    s1), db.session.merge(s2), db.session.merge(s3)
Пример #19
0
    def setUp(self):
        """Create and configure a new app instance for each test."""
        # create the app with common test config
        self.app = create_app('test')
        self.app_context = self.app.app_context()
        self.app_context.push()
        self.client = self.app.test_client()
        self.headers = {
            "Authorization":
            f"Bearer {create_access_token('00000000-0000-0000-0000-000000000001')}"
        }

        db.create_all()
        db.session.commit()
Пример #20
0
    def setUp(self, mock_now):
        """Create and configure a new app instance for each test."""
        mock_now.return_value = dt.datetime(2019, 4, 1, tzinfo=dt.timezone.utc)

        # create the app with common test config
        self.app = create_app('test')
        self.app_context = self.app.app_context()
        self.app_context.push()
        self.client = self.app.test_client()

        db.create_all()
        Locker.set_initial()
        Server.set_initial()
        db.session.commit()
Пример #21
0
 def setUp(self):
     """Create and configure a new app instance for each test."""
     # create the app with common test config
     self.app = create_app('test')
     self.app_context = self.app.app_context()
     self.app_context.push()
     self.client = self.app.test_client()
     self.auth = HTTPBearerAuth(
         create_access_token('00000000-0000-0000-0000-000000000001'))
     db.create_all()
     self.at = ActionTemplate(name='action',
                              version=1,
                              action_type=ActionType.SHELL,
                              code='code to run',
                              expected_stdout='expected output',
                              expected_rc=0,
                              system_kwargs={})
     ActionTemplate.set_initial()
Пример #22
0
    def setUp(self):
        """Create and configure a new self.app instance for each test."""
        # create a temporary file to isolate the database for each test
        # create the self.app with common test config

        self.app = Flask(__name__)

        @self.app.route('/', methods=['GET', 'POST'])
        @securizer
        def hello():
            return request.get_json() or {'msg': 'default response'}

        @self.app.route('/join', methods=['POST'])
        @securizer
        def join():
            return {'msg': 'default response'}

        @self.app.route('/empty', methods=['GET'])
        @securizer
        def empty():
            return "", 204

        @self.app.route('/list', methods=['GET'])
        @securizer
        def list():
            return [1, 2], 200

        self.app.config['SECURIZER'] = True
        self.app.config['SECURIZER_PLAIN'] = True
        self.app_context = self.app.app_context()
        self.app_context.push()
        self.client = self.app.test_client()
        db.init_app(self.app)
        db.create_all()
        self.d = generate_dimension('test')
        self.d.current = True
        self.srv1 = Server(id='bbbbbbbb-1234-5678-1234-56781234bbb1', name='server1',
                           dns_or_ip='192.168.1.9', port=7123, me=True)
        Route(self.srv1, cost=0)

        db.session.add_all([self.srv1, self.d])
        db.session.commit()
Пример #23
0
    def _fill_database(self):
        with mock.patch('dimensigon.domain.entities.get_now') as mock_get_now:
            mock_get_now.return_value = defaults.INITIAL_DATEMARK
            db.create_all()
            event.listen(db.session, 'after_commit', receive_after_commit)

            set_initial(**self.initials)
            d = Dimension.from_json(self.dim)
            d.current = True
            self.s1 = Server('node1',
                             created_on=defaults.INITIAL_DATEMARK,
                             id=self.SERVER,
                             me=True)
            self.g11 = Gate(id='00000000-0000-0000-0000-000000000011',
                            server=self.s1,
                            port=5000,
                            dns=self.s1.name)
            self.fill_database()
            db.session.add_all([d, self.s1])
            db.session.commit()
Пример #24
0
    def setUp(self):
        """Create and configure a new app instance for each test."""
        # create the app with common test config
        self.app = create_app('test')
        self.app_context = self.app.app_context()
        self.app_context.push()
        self.client = self.app.test_client()
        self.headers = {
            "Authorization":
            f"Bearer {create_access_token('00000000-0000-0000-0000-000000000001')}"
        }

        db.create_all()
        set_initial()

        self.n1 = Server("node1", port=8000)
        Route(self.n1, cost=0)
        self.n2 = Server("node2", port=8000)
        Route(self.n2, cost=0)
        db.session.add_all([self.n1, self.n2])
        db.session.commit()
        self.datamark = Catalog.max_catalog(str)
Пример #25
0
    def setUp(self):
        """Create and configure a new app instance for each test."""
        # create the app with common test config
        self.app = create_app('test')
        self.app_context = self.app.app_context()
        self.app_context.push()
        self.client = self.app.test_client()
        self.headers = {"Authorization": f"Bearer {create_access_token('00000000-0000-0000-0000-000000000001')}"}

        db.create_all()

        self.at1 = ActionTemplate(id='11111111-2222-3333-4444-555555550001', name='action1', version=1,
                                  action_type=ActionType.SHELL, code='code to run',
                                  expected_stdout='expected output', expected_stderr='stderr', expected_rc=0,
                                  system_kwargs={})

        self.at2 = ActionTemplate(id='11111111-2222-3333-4444-555555550002', name='action2', version=1,
                                  action_type=ActionType.SHELL, code='code to run',
                                  expected_stdout='expected output', expected_stderr='stderr', expected_rc=0,
                                  system_kwargs={})

        self.o = Orchestration(id='11111111-2222-3333-4444-666666660001', name='orchestration_name',
                               version=1, description='desc')
Пример #26
0
 def setUp(self):
     self.maxDiff = None
     """Create and configure a new app instance for each test."""
     # create the app with common test config
     self.app = create_app('test')
     self.app_context = self.app.app_context()
     self.app_context.push()
     self.client = self.app.test_client()
     self.auth = HTTPBearerAuth(
         create_access_token('00000000-0000-0000-0000-000000000001'))
     db.create_all()
     self.src = Server('source',
                       port=5000,
                       id='00000000-0000-0000-0000-000000000000')
     self.dst1 = Server('destination1',
                        port=5000,
                        id='00000000-0000-0000-0000-000000000001')
     self.dst2 = Server('destination2',
                        port=5000,
                        id='00000000-0000-0000-0000-000000000002')
     self.dst3 = Server('destination3',
                        port=5000,
                        id='00000000-0000-0000-0000-000000000003')
     db.session.add_all([self.src, self.dst1, self.dst2, self.dst3])