def test_fetch_username_from_none_token(self, mock_user_model, mock_g,
                                            mock_session):
        """Test the Token to username function with None as user's token."""
        mock_user_model.query.filter.return_value.first.return_value = MockUser(
        )

        return_value = fetch_username_from_token()

        mock_user_model.query.filter.assert_called_once_with(
            mock_user_model.id == mock_g.user.id)
        self.assertIsNone(return_value)
        mock_session.assert_not_called()
        mock_g.log.error.assert_not_called()
    def test_fetch_username_from_token_exception(self, mock_user_model, mock_g,
                                                 mock_session):
        """Test the Token to username function with requests throwing exception."""
        mock_user_model.query.filter.return_value.first.return_value = MockUser(
            github_token='token')
        mock_session.return_value.get.side_effect = Exception

        return_value = fetch_username_from_token()

        mock_user_model.query.filter.assert_called_once_with(
            mock_user_model.id == mock_g.user.id)
        mock_session.assert_called_once()
        self.assertIsNone(return_value)
        mock_g.log.error.assert_called_once_with(
            "Failed to fetch the user token")
    def test_fetch_username_from_valid_token(self, mock_user_model, mock_g,
                                             mock_session):
        """Test the Token to username function with dummy token value."""
        mock_user_model.query.filter.return_value.first.return_value = MockUser(
            github_token='token')
        mock_session.return_value.get.return_value.json.return_value = {
            'login': '******'
        }

        return_value = fetch_username_from_token()

        mock_user_model.query.filter.assert_called_once_with(
            mock_user_model.id == mock_g.user.id)
        mock_session.assert_called_once()
        self.assertEqual(return_value, 'username', "unexpected return value")
        mock_g.log.error.assert_not_called()
Beispiel #4
0
def index():
    """
        Display a form to allow users to run tests.
        User can enter commit or select the commit from their repo that are not more than 30 days old.
        User can customized test based on selected regression tests and platforms.
        Also Display list of customized tests started by user.
        User will be redirected to the same page on submit.
    """
    fork_test_form = TestForkForm(request.form)
    username = fetch_username_from_token()
    commit_options = False
    if username is not None:
        gh = GitHub(access_token=g.github['bot_token'])
        repository = gh.repos(username)(g.github['repository'])
        # Only commits since last month
        last_month = datetime.now() - timedelta(days=30)
        commit_since = last_month.isoformat() + 'Z'
        commits = repository.commits().get(since=commit_since)
        commit_arr = []
        for commit in commits:
            commit_url = commit['html_url']
            commit_sha = commit['sha']
            commit_option = (
                '<a href="{url}">{sha}</a>').format(url=commit_url, sha=commit_sha)
            commit_arr.append((commit_sha, commit_option))
        # If there are commits present, display it on webpage
        if len(commit_arr) > 0:
            fork_test_form.commit_select.choices = commit_arr
            commit_options = True
        fork_test_form.regression_test.choices = [(regression_test.id, regression_test)
                                                  for regression_test in RegressionTest.query.all()]
        if fork_test_form.add.data and fork_test_form.validate_on_submit():
            import requests
            regression_tests = fork_test_form.regression_test.data
            commit_hash = fork_test_form.commit_hash.data
            repo = g.github['repository']
            platforms = fork_test_form.platform.data
            api_url = ('https://api.github.com/repos/{user}/{repo}/commits/{hash}').format(
                user=username, repo=repo, hash=commit_hash
            )
            # Show error if github fails to recognize commit
            response = requests.get(api_url)
            if response.status_code == 500:
                fork_test_form.commit_hash.errors.append('Error contacting Github')
            elif response.status_code != 200:
                fork_test_form.commit_hash.errors.append('Wrong Commit Hash')
            else:
                add_test_to_kvm(username, commit_hash, platforms, regression_tests)
                return redirect(url_for('custom.index'))

    populated_categories = g.db.query(regressionTestLinkTable.c.category_id).subquery()
    categories = Category.query.filter(Category.id.in_(populated_categories)).order_by(Category.name.asc()).all()

    tests = Test.query.filter(and_(TestFork.user_id == g.user.id, TestFork.test_id == Test.id)).order_by(
        Test.id.desc()).limit(50).all()
    return {
        'addTestFork': fork_test_form,
        'commit_options': commit_options,
        'tests': tests,
        'TestType': TestType,
        'GitUser': username,
        'categories': categories,
        'customize': True
    }