예제 #1
0
    def setUp(self):
        self._reply_q = "reply_queue_name"

        self._expiration = 1
        self._stopwatch = (timeutils.StopWatch(
            duration=self._expiration).start())

        self._pika_engine = mock.MagicMock()

        self._rpc_reply_exchange = "rpc_reply_exchange"
        self._pika_engine.rpc_reply_exchange = self._rpc_reply_exchange
        self._pika_engine.default_content_type = "application/json"

        self._msg_id = 12345567
    def setUp(self, *args, **kwargs):
        super(TestCassandra, self).setUp(*args, **kwargs)
        # Prepare patchers
        self.read_patcher = mock.patch.object(file_io, 'read')
        self.execute_patcher = mock.patch.object(
            cassandra_interface.TornadoCassandra, 'execute')
        self.cluster_class_patcher = mock.patch.object(cassandra_interface,
                                                       'Cluster')

        # Start patches
        self.read_mock = self.read_patcher.start()
        self.execute_mock = self.execute_patcher.start()
        self.cluster_class_mock = self.cluster_class_patcher.start()

        # Configure mocks
        self.read_mock.return_value = '127.0.0.1'
        self.session_mock = mock.MagicMock()
        self.connect_mock = mock.MagicMock(return_value=self.session_mock)
        self.cluster_mock = mock.MagicMock(connect=self.connect_mock)
        self.cluster_class_mock.return_value = self.cluster_mock

        # Instantiate Datastore proxy
        self.db = cassandra_interface.DatastoreProxy()
예제 #3
0
 def test_metadata_profile(self):
     import_users_and_groups(
         self.domain.name,
         [self._get_spec(data={'key': 'F#', PROFILE_SLUG: self.profile.id})],
         [],
         self.uploading_user,
         mock.MagicMock()
     )
     self.assertEqual(self.user.metadata, {
         'commcare_project': 'mydomain',
         'key': 'F#',
         'mode': 'minor',
         PROFILE_SLUG: self.profile.id,
     })
예제 #4
0
    def test_freebsd(self):
        with mock.patch("platform.system",
                        mock.MagicMock(return_value='FreeBSD')):
            self.assertFalse(OSInfo().is_windows)
            self.assertFalse(OSInfo().is_cygwin)
            self.assertFalse(OSInfo().is_msys)
            self.assertFalse(OSInfo().is_linux)
            self.assertTrue(OSInfo().is_freebsd)
            self.assertFalse(OSInfo().is_macos)
            self.assertFalse(OSInfo().is_solaris)

            with self.assertRaises(ConanException):
                OSInfo.uname()
            self.assertIsNone(OSInfo.detect_windows_subsystem())
예제 #5
0
    def test_metadata(self, CephCluster_mock, MonApi_mock):
        with open('ceph_deployment/tests/metadata.json') as f:
            osd, mon, mds, mgr, expected_result, _, _ = json.load(f)

        MonApi_mock.return_value.osd_metadata.return_value = osd
        MonApi_mock.return_value.mon_metadata.return_value = mon
        MonApi_mock.return_value.mds_metadata.return_value = mds
        MonApi_mock.return_value.mgr_metadata.return_value = mgr
        CephCluster_mock.objects.all.return_value = [
            mock.MagicMock(fsid='fsid')
        ]

        res = all_metadata()
        self.assertEqual(res, expected_result)
예제 #6
0
 def test_check_min_cppstd_gnu_compiler_extension(self):
     """ Current compiler must support GNU extension on Linux when extensions is required
     """
     conanfile = self._create_conanfile("gcc", "9", "Linux", None,
                                        "libstdc++")
     with mock.patch("platform.system",
                     mock.MagicMock(return_value="Linux")):
         with mock.patch.object(OSInfo, '_get_linux_distro_info'):
             with mock.patch("conans.client.tools.settings.cppstd_default",
                             return_value="17"):
                 with self.assertRaises(ConanException) as raises:
                     check_min_cppstd(conanfile, "17", True)
                 self.assertEqual("The cppstd GNU extension is required",
                                  str(raises.exception))
예제 #7
0
    def test_deepsea_keys_login_error(self):
        with mock.patch("requests.Session") as mock_requests_session:
            login_resp_err = mock.MagicMock()
            login_resp_err.ok = False
            login_resp_err.status_code = 503

            mock_requests_session().post.side_effect = [
                self._login_resp, login_resp_err
            ]

            resp_un = mock.MagicMock()
            resp_un.ok = False
            resp_un.status_code = 401

            mock_requests_session().get.side_effect = [resp_un]

            api = DeepSea('localhost', 8000, 'auto', 'hello', 'world')
            with self.assertRaises(RequestException) as context:
                api.key_list()

            self.assertTrue(mock_requests_session().post.called)
            self.assertTrue(mock_requests_session().get.called)
            self.assertEqual(context.exception.status_code, 503)
예제 #8
0
 def test_upload_add_web_user(self, mock_invitation_class):
     username = '******'
     web_user = WebUser.create(self.other_domain.name, username, 'password', None, None)
     mock_invite = mock_invitation_class.return_value
     import_users_and_groups(
         self.domain.name,
         [self._get_spec(web_user='******', is_account_confirmed='True', role=self.role.name)],
         [],
         self.uploading_user,
         mock.MagicMock()
     )
     web_user = WebUser.get_by_username(username)
     self.assertFalse(mock_invite.send_activation_email.called)
     self.assertTrue(web_user.is_member_of(self.domain.name))
예제 #9
0
 def test_02_register_finalizer_request_context(self):
     finalizer1 = mock.MagicMock()
     finalizer2 = mock.MagicMock()
     # test that we can use finalizers
     with self.app.test_request_context('/token/',
                                        method='GET',
                                        headers={'Authorization': self.at}):
         register_finalizer(finalizer1)
         register_finalizer(finalizer2)
         res = self.app.full_dispatch_request()
         result = json.loads(res.data.decode('utf8')).get("result")
         self.assertTrue(result.get("status"))
     finalizer1.assert_called_once()
     finalizer2.assert_called_once()
     # test that they are not called in the next request
     with self.app.test_request_context('/token/',
                                        method='GET',
                                        headers={'Authorization': self.at}):
         res = self.app.full_dispatch_request()
         result = json.loads(res.data.decode('utf8')).get("result")
         self.assertTrue(result.get("status"))
     finalizer1.assert_called_once()
     finalizer2.assert_called_once()
예제 #10
0
    def test_location_remove(self):
        self.setup_locations()
        # first assign both locations
        import_users_and_groups(
            self.domain.name,
            [self._get_spec(location_code=[a.site_code for a in [self.loc1, self.loc2]])],
            [],
            self.uploading_user,
            mock.MagicMock()
        )

        # deassign all locations
        import_users_and_groups(
            self.domain.name,
            [self._get_spec(location_code=[], user_id=self.user._id)],
            [],
            self.uploading_user,
            mock.MagicMock()
        )

        # user should have no locations
        self.assertEqual(self.user.location_id, None)
        self.assertEqual(self.user.metadata.get('commcare_location_id'), None)
        self.assertListEqual(self.user.assigned_location_ids, [])
예제 #11
0
    def test_location_not_list(self):
        self.setup_locations()

        # location_code can also just be string instead of array for single location assignmentss
        import_users_and_groups(
            self.domain.name,
            [self._get_spec(location_code=self.loc1.site_code)],
            [],
            self.uploading_user,
            mock.MagicMock()
        )
        self.assertEqual(self.user.location_id, self.loc1._id)
        self.assertEqual(self.user.location_id, self.user.metadata.get('commcare_location_id'))
        # multiple locations
        self.assertListEqual([self.loc1._id], self.user.assigned_location_ids)
예제 #12
0
    def test_deepsea_service_offline_connection_error(self):
        with mock.patch("requests.Session") as mock_requests_session:
            resp_post = mock.MagicMock()
            resp_post.ok = True
            resp_post.status_code = 200
            resp_post.json.return_value = {'return': [{'token': 'validtoken'}]}
            mock_requests_session().post.return_value = resp_post
            mock_requests_session().get.side_effect = ConnectionError()

            api = DeepSea('localhost', 8000, 'auto', 'hello', 'world')
            with self.assertRaises(RequestException):
                api.is_service_online()

            self.assertTrue(mock_requests_session().get.called)
            self.assertTrue(api._is_logged_in())
예제 #13
0
    def test_start_flushing2(self):
        with Patcher() as patcher:
            patcher.fs.CreateFile(self.observer.recovery_file(self.session), contents=json.dumps([{'est': 'error'}]))

            self.observer.session = self.session
            self.observer.sleep = mock.MagicMock()
            yield self.observer.start_flushing(self.session)
            self.assertEqual(self.observer.logs, [{'est': 'error'}])

            yield self.observer.flush_logs()

            self.assertFalse(os.path.isfile(self.observer.recovery_file(self.session)))

            self.session.flush_logs.assert_called_once_with([{'est': 'error'}])
            self.assertEqual(self.observer.session, self.session)
예제 #14
0
    def test_log_event_callback_and_max_logs(self):
        # GIVEN
        self.given_ngrok_installed(self.pyngrok_config.ngrok_path)
        log_event_callback_mock = mock.MagicMock()
        pyngrok_config = PyngrokConfig(config_path=conf.DEFAULT_NGROK_CONFIG_PATH,
                                       log_event_callback=log_event_callback_mock, max_logs=5)

        # WHEN
        ngrok.connect(pyngrok_config=pyngrok_config)
        ngrok_process = ngrok.get_ngrok_process()
        time.sleep(1)

        # THEN
        self.assertGreater(log_event_callback_mock.call_count, len(ngrok_process.logs))
        self.assertEqual(len(ngrok_process.logs), 5)
예제 #15
0
    def test_deepsea_iscsi_interfaces(self):
        with mock.patch(
                "rest_client.TimeoutRequestsSession") as mock_requests_session:
            login_resp = mock.MagicMock()
            login_resp.ok = True
            login_resp.status_code = 200
            login_resp.json.return_value = {
                'return': [{
                    'token': 'validtoken'
                }]
            }

            rest_iscsi = mock.MagicMock()
            rest_iscsi.ok = True
            rest_iscsi.status_code = 200
            rest_iscsi.json.return_value = {
                'return': [{
                    'minion1': ['192.168.10.101', '192.168.121.41'],
                    'minion2': ['192.168.10.102', '192.168.121.42']
                }]
            }
            mock_requests_session().post.side_effect = [login_resp, rest_iscsi]

            api = DeepSea('localhost', 8000, 'auto', 'admin', 'admin')
            res = api.iscsi_interfaces()

            self.assertTrue(mock_requests_session().post.called)
            self.assertTrue(api._is_logged_in())
            self.assertEqual(
                res, [{
                    'hostname': 'minion1',
                    'interfaces': ['192.168.10.101', '192.168.121.41']
                }, {
                    'hostname': 'minion2',
                    'interfaces': ['192.168.10.102', '192.168.121.42']
                }])
예제 #16
0
    def test_check_safebrowsing(self, requests_mock, plugin):
        response_mock = mock.MagicMock()
        response_mock.json = mock.MagicMock(return_value=[])
        requests_mock.post = mock.MagicMock(return_value=response_mock)

        assert plugin.check_safebrowsing(["http://example.com"]) == []
        requests_mock.post.assert_called_with(
            'https://safebrowsing.googleapis.com/v4/threatMatches:find?key=some-api-key',
            json={
                'client': {
                    'clientId': 'fuglu-safebrowsing',
                    'clientVersion': VERSION
                },
                "threatInfo": {
                    "threatTypes": ["MALWARE", "SOCIAL_ENGINEERING"],
                    "platformTypes": ["ALL_PLATFORMS"],
                    "threatEntryTypes": ["URL"],
                    "threatEntries": [{
                        "url": "http://example.com"
                    }]
                }
            },
            headers={'User-Agent': 'fuglu-safebrowsing %s' % VERSION},
            timeout=1.0)
예제 #17
0
    def test_metadata(self):
        # Set metadata
        import_users_and_groups(self.domain.name,
                                [self._get_spec(data={'key': 'F#'})], [],
                                self.uploading_user, mock.MagicMock())
        self.assertEqual(self.user.metadata, {
            'commcare_project': 'mydomain',
            'key': 'F#'
        })

        # Update metadata
        import_users_and_groups(
            self.domain.name,
            [self._get_spec(data={'key': 'Bb'}, user_id=self.user._id)], [],
            self.uploading_user, mock.MagicMock())
        self.assertEqual(self.user.metadata, {
            'commcare_project': 'mydomain',
            'key': 'Bb'
        })

        # Clear metadata
        import_users_and_groups(
            self.domain.name,
            [self._get_spec(data={'key': ''}, user_id=self.user._id)], [],
            self.uploading_user, mock.MagicMock())
        self.assertEqual(self.user.metadata, {'commcare_project': 'mydomain'})

        # Allow falsy but non-blank values
        import_users_and_groups(
            self.domain.name,
            [self._get_spec(data={'play_count': 0}, user_id=self.user._id)],
            [], self.uploading_user, mock.MagicMock())
        self.assertEqual(self.user.metadata, {
            'commcare_project': 'mydomain',
            'play_count': 0
        })
예제 #18
0
    def test_x86_x64(self):
        settings = Settings.loads(get_default_settings_yml())
        settings.os = "FreeBSD"
        settings.arch = "x86_64"
        self.assertTrue(
            cross_building(settings,
                           self_os="FreeBSD",
                           self_arch="x86",
                           skip_x64_x86=True))
        self.assertTrue(
            cross_building(settings,
                           self_os="FreeBSD",
                           self_arch="x86",
                           skip_x64_x86=False))

        with mock.patch("platform.system", mock.MagicMock(return_value='FreeBSD')), \
             mock.patch("platform.machine", mock.MagicMock(return_value="x86")):
            self.assertTrue(cross_building(settings, skip_x64_x86=True))
            self.assertTrue(cross_building(settings, skip_x64_x86=False))

        settings.os_build = "FreeBSD"
        settings.arch_build = "x86"
        self.assertTrue(cross_building(settings, skip_x64_x86=True))
        self.assertTrue(cross_building(settings, skip_x64_x86=False))
예제 #19
0
 def test_create_with_volume_attach_to_this(self):
     fake_server_id = 'fake_server_123'
     fake_host_name = 'attached_to_this'
     fake_volume_args = {
         'id':
         DEFAULT_VOLUME_ID,
         'status':
         'in-use',
         'attachments': [{
             'server_id': fake_server_id,
             'host_name': fake_host_name
         }]
     }
     fake_cinder_volume = fake_object.FakeCinderVolume(**fake_volume_args)
     self.cinderprovider._get_docker_volume = mock.MagicMock()
     self.cinderprovider._get_docker_volume.return_value \
         = (fake_cinder_volume,
            consts.ATTACH_TO_THIS)
     self.cinderprovider.cinderclient.volumes.get = mock.MagicMock()
     self.cinderprovider.cinderclient.volumes.get.return_value = \
         fake_cinder_volume
     fake_result = self.cinderprovider.create('fake-vol', {})
     self.assertEqual(os.path.join(volume_link_dir, DEFAULT_VOLUME_ID),
                      fake_result['path'])
예제 #20
0
    def test_prep_code(self):
        # GIVEN
        commonhelper.given_python_version_file_exists("1.2.3")
        versioned_file1_path = commonhelper.given_project_python_versioned_file_exists(
            "platform")
        versioned_file2_path = commonhelper.given_project_js_versioned_file_exists(
            "frontend")
        repo_instance = self.mock_git_repo.return_value
        latest_tag = repo_instance.tags[-1]
        latest_tag.commit = mock.MagicMock('git.commit.Commit')
        diff1 = mock.MagicMock('git.diff.Diff')
        diff1.b_rawpath = versioned_file1_path.encode('utf-8')
        diff2 = mock.MagicMock('git.diff.Diff')
        diff2.b_rawpath = versioned_file2_path.encode('utf-8')
        latest_tag.commit.diff = mock.MagicMock(side_effect=[[diff1], [diff2]])

        # WHEN
        main(["main.py", "--init", "prep-code"])

        # THEN
        commonhelper.verify_versioned_file_updated(self, versioned_file1_path,
                                                   "1.2.3")
        commonhelper.verify_versioned_file_updated(self, versioned_file2_path,
                                                   "1.2.3")
예제 #21
0
    def test_policy_resource_limits_count(self):
        p = self.load_policy(
            {
                "name": "limits",
                "resource": "azure.resourcegroup",
                "max-resources": 1,
            },
            validate=True)

        p.ctx.metrics.flush = mock.MagicMock()
        output = self.capture_logging('custodian.policy', level=logging.ERROR)
        self.assertRaises(ResourceLimitExceeded, p.run)
        self.assertTrue("policy:limits exceeded resource-limit:1 found:" in
                        output.getvalue())
        self.assertEqual(p.ctx.metrics.buf[0]['MetricName'],
                         'ResourceLimitExceeded')
예제 #22
0
 def test_location_add(self):
     self.setup_locations()
     import_users_and_groups(self.domain.name, [
         self._get_spec(
             location_code=[a.site_code for a in [self.loc1, self.loc2]])
     ], [], self.uploading_user, mock.MagicMock())
     # first location should be primary location
     self.assertEqual(self.user.location_id, self.loc1._id)
     self.assertEqual(self.user.location_id,
                      self.user.metadata.get('commcare_location_id'))
     # multiple locations
     self.assertListEqual([l._id for l in [self.loc1, self.loc2]],
                          self.user.assigned_location_ids)
     # non-primary location
     self.assertTrue(
         self.loc2._id in self.user.metadata.get('commcare_location_ids'))
 def test_remove_supplier_declaration_for_expired_frameworks(
         self, mocked_api_client):
     with freeze_time("Jan 1st, 2018"):
         sfd = SupplierFrameworkDeclarations(mocked_api_client,
                                             mock.MagicMock(),
                                             dry_run=False,
                                             user='******')
         sfd.remove_supplier_declaration_for_expired_frameworks()
         expected_calls = [
             mock.call(framework_slug="framework-expired-three-years-ago",
                       with_declarations=None),
             mock.call(framework_slug="framework-expired-a-decade-ago",
                       with_declarations=None)
         ]
         mocked_api_client.find_framework_suppliers_iter.assert_has_calls(
             expected_calls, any_order=True)
예제 #24
0
 def test_upload_invite_web_user(self, mock_send_activation_email):
     import_users_and_groups(
         self.domain.name,
         [
             self._get_spec(
                 web_user='******',
                 is_account_confirmed='False',
                 send_confirmation_email='True',
                 role=self.role.name
             )
         ],
         [],
         self.uploading_user,
         mock.MagicMock()
     )
     self.assertEqual(mock_send_activation_email.call_count, 1)
예제 #25
0
    def test_mingw32(self):
        self._uname = 'MINGW32_NT-10.0'
        self._version = '2.10.0(0.325/5/3)'
        with mock.patch("platform.system", mock.MagicMock(return_value=self._uname)):
            self.assertTrue(OSInfo().is_windows)
            self.assertFalse(OSInfo().is_cygwin)
            self.assertTrue(OSInfo().is_msys)
            self.assertFalse(OSInfo().is_linux)
            self.assertFalse(OSInfo().is_freebsd)
            self.assertFalse(OSInfo().is_macos)
            self.assertFalse(OSInfo().is_solaris)

            with environment_append({"CONAN_BASH_PATH": "/fake/bash.exe"}):
                with mock.patch('conans.client.tools.oss.check_output', new=self.subprocess_check_output_mock):
                    self.assertEqual(OSInfo.uname(), self._uname.lower())
                    self.assertEqual(OSInfo.detect_windows_subsystem(), MSYS2)
예제 #26
0
 def test_multi_domain(self):
     self.setup_users()
     dm = DomainPermissionsMirror(source=self.domain.name,
                                  mirror=self.other_domain.name)
     dm.save()
     import_users_and_groups(self.domain.name, [
         self._get_spec(username='******',
                        domain=self.other_domain.name,
                        role=self.other_domain_role.name,
                        email='*****@*****.**')
     ], [], self.uploading_user, mock.MagicMock(), True)
     self.assertIsNotNone(
         Invitation.objects.filter(email='*****@*****.**').first())
     self.assertEqual(
         Invitation.objects.filter(email='*****@*****.**').first().domain,
         self.other_domain.name)
예제 #27
0
    def test_installer_download_fails(self, mock_urlopen):
        # GIVEN
        magic_mock = mock.MagicMock()
        magic_mock.getcode.return_value = 500
        mock_urlopen.return_value = magic_mock

        if os.path.exists(conf.DEFAULT_NGROK_PATH):
            os.remove(conf.DEFAULT_NGROK_PATH)
        self.assertFalse(os.path.exists(conf.DEFAULT_NGROK_PATH))

        # WHEN
        with self.assertRaises(PyngrokNgrokInstallError):
            ngrok.connect(pyngrok_config=self.pyngrok_config)

        # THEN
        self.assertFalse(os.path.exists(conf.DEFAULT_NGROK_PATH))
예제 #28
0
def test_get_context_factory_from_args_all_matchers_defined():
    args = mock.MagicMock()
    args.start_delimiter_matcher = mock.MagicMock()
    args.end_delimiter_matcher = mock.MagicMock()
    args.delimiter_matcher = mock.MagicMock()
    ap = mock.MagicMock()
    ap.error = mock.MagicMock()

    get_context_factory_from_args(ap, args)

    ap.error.assert_called()
예제 #29
0
    def test_policy_resource_limits_count(self):
        session_factory = self.replay_flight_data('disk-query')
        p = self.load_policy(
            {
                'name': 'limits',
                'resource': 'gcp.disk',
                'max-resources': 1
            },
            session_factory=session_factory)

        p.ctx.metrics.flush = mock.MagicMock()
        output = self.capture_logging('custodian.policy', level=logging.ERROR)
        self.assertRaises(ResourceLimitExceeded, p.run)
        self.assertTrue("policy:limits exceeded resource-limit:1 found:" in
                        output.getvalue())
        self.assertEqual(p.ctx.metrics.buf[0]['MetricName'],
                         'ResourceLimitExceeded')
예제 #30
0
    def test_duplicate_password(self):
        user_spec = [{
            'username': '******',
            'user_id': '1990',
            'name': 'Tom Hiddleston',
            'language': None,
            'is_active': 'True',
            'phone-number': '8675309',
            'password': '******',
            'email': None
        }]

        rows = import_users_and_groups(self.domain.name,
                                       list(user_spec + self.user_specs), [],
                                       self.uploading_user,
                                       mock.MagicMock())['messages']['rows']
        self.assertEqual(rows[0]['flag'], "'password' values must be unique")