Example #1
0
class ServiceTest(TestCase):
    def setUp(self):
        self.service = ServiceFactory()

    def test_service_deploy_method(self):
        """service deploy method return number of related logs"""
        self.assertEqual(self.service.deploys(), 0)
        log = LogFactory(service=self.service)

        self.assertEqual(self.service.deploys(), 1)

        log.status = False
        log.save()

        self.assertEqual(self.service.deploys(), 0)

    def test_service_fail_deploys_method(self):
        """"service fail_deploy method return number of related logs with status False"""
        log = LogFactory(service=self.service)
        self.assertEqual(self.service.fail_deploys(), 0)
        log.status = False
        log.save()

        self.assertEqual(self.service.fail_deploys(), 1)

    def test_service_skipped_deploys_method(self):
        """service skipped_deploys method return number of relatet logs with skiped status True """
        log = LogFactory(service=self.service)

        self.assertEqual(self.service.skipped_deploys(), 0)

        log.status = False
        log.skip = True
        log.save()

        self.assertEqual(self.service.skipped_deploys(), 1)

    def test_service_latest_log_date_method(self):
        """service latest_log_date method return latest related log create date"""
        log1 = LogFactory(service=self.service)
        log2 = LogFactory(service=self.service)
        self.assertEqual(self.service.latest_log_date(),
                         self.service.log_set.latest('created').created)

    def test_service_latest_log_user_method(self):
        """service latest_log_user method return latest related log user"""
        log1 = LogFactory(service=self.service)
        log2 = LogFactory(service=self.service)
        self.assertEqual(self.service.latest_log_user(),
                         self.service.log_set.latest('created').user)

    def test_service_hook_url_method(self):
        """service hook_url method return hook url for appropriate repository"""
        self.assertEqual(
            self.service.hook_url(),
            reverse('ftpdeploy_deploy',
                    kwargs={'secret_key': self.service.secret_key}))

    def test_service_get_logs_tree_method(self):
        """service get_logs_tree method return logs in proper order and omit skipped"""
        log1 = LogFactory(service=self.service)
        log2 = LogFactory(service=self.service, status=False, skip=True)
        log3 = LogFactory(service=self.service)
        log4 = LogFactory(service=self.service, status=False)
        log5 = LogFactory(service=self.service)
        log6 = LogFactory(service=self.service, status=False, skip=True)
        log7 = LogFactory(service=self.service)

        self.assertListEqual(list(self.service.get_logs_tree()),
                             [log3, log4, log5, log7])

    @patch('ftp_deploy.models.service.service_check')
    def test_service_check_method(self, mock_service_check):
        """service check method perform check, set repo_hook and status_message"""
        mock_service_check().check_all = MagicMock(
            'check_all',
            return_value=([True, True, True], ['message1', 'message2']))
        self.service.validate()

        mock_service_check().check_all.assert_called_once_with()
        self.assertEqual(self.service.status_message, 'message1<br>message2')
        self.assertFalse(self.service.repo_hook)

        mock_service_check().check_all = MagicMock(
            'check_all', return_value=([False, False, False], []))
        self.service.validate()
        self.assertEqual(self.service.status_message, '')
        self.assertTrue(self.service.repo_hook)

    def test_service_lock_method(self):
        task = TaskFactory(service=self.service)
        self.assertFalse(self.service.lock())
        task1 = TaskFactory(service=self.service, active=True)
        self.assertTrue(self.service.lock())

    def test_service_has_queue_method(self):
        task = TaskFactory(service=self.service)
        self.assertTrue(self.service.has_queue())
Example #2
0
class UtilsRepoAPI(TestCase):
    def setUp(self):
        self.service_bb = ServiceFactory()
        self.service_gh = ServiceFactory(repo_source='gh')

    @patch('ftp_deploy.utils.repo.curl_connection')
    def test_repo_init_bb_perform_curl_initialization(self,
                                                      mock_curl_connection):
        api = repository_api(self.service_bb.repo_source)
        mock_curl_connection.assert_called_once_with(
            BITBUCKET_SETTINGS['username'], BITBUCKET_SETTINGS['password'])
        mock_curl_connection.assert_has_calls(call().authenticate())

    @patch('ftp_deploy.utils.repo.curl_connection')
    def test_repo_init_gh_perform_curl_initialization(self,
                                                      mock_curl_connection):
        api = repository_api(self.service_gh.repo_source)
        mock_curl_connection.assert_called_once_with(
            GITHUB_SETTINGS['username'], GITHUB_SETTINGS['password'])
        mock_curl_connection.assert_has_calls(call().authenticate())

    @patch('ftp_deploy.utils.repo.curl_connection')
    def test_repo_repositories_bb_use_proper_url(self, mock_curl_connection):
        api = repository_api(self.service_bb.repo_source)
        api.curl = MagicMock(name="mock_curl")
        api.repositories()
        api.curl.assert_has_calls(
            [call.perform('https://bitbucket.org/api/1.0/user/repositories')])

    @patch('ftp_deploy.utils.repo.curl_connection')
    def test_repo_repositories_gh_use_proper_url(self, mock_curl_connection):
        api = repository_api(self.service_gh.repo_source)
        api.curl = MagicMock(name="mock_curl")
        api.repositories()
        api.curl.assert_has_calls(
            [call.perform('https://api.github.com/user/repos')])

    @patch('ftp_deploy.utils.repo.absolute_url')
    @patch('ftp_deploy.utils.repo.curl_connection')
    def test_repo_add_hook_bb_use_proper_url(self, mock_curl_connection,
                                             mock_absolute_url):
        absolute_url = MagicMock(name='absolute_url')
        absolute_url.build = MagicMock(return_value='build')
        mock_absolute_url.return_value = absolute_url
        api = repository_api(self.service_bb.repo_source)
        api.curl = MagicMock(name="mock_curl")
        api.add_hook(self.service_bb,
                     MagicMock(name='request', return_value='request'))

        calls = [
            call.perform_post(
                'https://api.bitbucket.org/1.0/repositories/%s/%s/services/' %
                (BITBUCKET_SETTINGS['username'],
                 self.service_bb.repo_slug_name),
                'type=POST&URL=build/ftpdeploy/deploy/%s' %
                (self.service_bb.secret_key))
        ]
        api.curl.assert_has_calls(calls)

    @patch('ftp_deploy.utils.repo.absolute_url')
    @patch('ftp_deploy.utils.repo.curl_connection')
    def test_repo_add_hook_gh_use_proper_url(self, mock_curl_connection,
                                             mock_absolute_url):
        absolute_url = MagicMock(name='absolute_url')
        absolute_url.build = MagicMock(return_value='build')
        mock_absolute_url.return_value = absolute_url
        api = repository_api(self.service_gh.repo_source)
        api.curl = MagicMock(name="mock_curl")
        api.add_hook(self.service_gh,
                     MagicMock(name='request', return_value='request'))

        json_call = json.loads(api.curl.mock_calls[0][1][1])
        url_call = api.curl.mock_calls[0][1][0]

        self.assertEqual(
            url_call, 'https://api.github.com/repos/%s/%s/hooks' %
            (GITHUB_SETTINGS['username'], self.service_gh.repo_slug_name))

        self.assertEqual(json_call['name'], 'web')
        self.assertEqual(json_call['active'], True)
        self.assertEqual(json_call['config']['url'],
                         'build%s' % self.service_gh.hook_url())
        self.assertEqual(json_call['config']['content_type'], 'json')