Пример #1
0
    def test_check_release_exception(self, mock_log, mock_check):
        """
        Assert that exception thrown in check_project release is correctly handled.
        """
        mock_check.side_effect = exceptions.AnityaPluginException("Error")
        request_data = {
            "backend": "PyPI",
            "homepage": "http://python-requests.org",
            "name": "requests",
            "check_release": "True",
        }

        output = self.app.post("/api/v2/projects/",
                               headers=self.auth,
                               data=request_data)

        mock_check.assert_called_once_with(mock.ANY, mock.ANY)
        self.assertEqual(output.status_code, 201)
        self.assertIn("Error", mock_log.error.call_args_list[0][0])
Пример #2
0
class CheckProjectReleaseTests(DatabaseTestCase):
    """Tests for the :func:`anitya.lib.utilities.check_project_release` function."""
    @mock.patch("anitya.db.models.Project")
    def test_check_project_release_no_backend(self, mock_project):
        """ Test the check_project_release function for Project. """
        m_project = mock_project.return_value
        m_project.backend.return_value = "dummy"
        self.assertRaises(AnityaException, utilities.check_project_release,
                          m_project, self.session)

    @mock.patch(
        "anitya.lib.backends.npmjs.NpmjsBackend.get_versions",
        return_value=["1.0.0", "0.9.9", "0.9.8"],
    )
    def test_check_project_release_backend(self, mock_method):
        """ Test the check_project_release function for Project. """
        project = utilities.create_project(
            self.session,
            name="pypi_and_npm",
            homepage="https://example.com/not-a-real-npmjs-project",
            backend="npmjs",
            user_id="*****@*****.**",
        )
        versions = utilities.check_project_release(project,
                                                   self.session,
                                                   test=True)
        self.assertEqual(versions, ["0.9.8", "0.9.9", "1.0.0"])

    @mock.patch(
        "anitya.lib.backends.npmjs.NpmjsBackend.get_versions",
        mock.Mock(side_effect=exceptions.AnityaPluginException("")),
    )
    def test_check_project_release_plugin_exception(self):
        """ Test the check_project_release function for Project. """
        project = utilities.create_project(
            self.session,
            name="pypi_and_npm",
            homepage="https://example.com/not-a-real-npmjs-project",
            backend="npmjs",
            user_id="*****@*****.**",
        )
        self.assertRaises(
            exceptions.AnityaPluginException,
            utilities.check_project_release,
            project,
            self.session,
        )

    @mock.patch("anitya.lib.backends.npmjs.NpmjsBackend.get_versions",
                return_value=["1.0.0"])
    def test_check_project_release_no_new_version(self, mock_method):
        """ Test the check_project_release function for Project. """
        project = utilities.create_project(
            self.session,
            name="pypi_and_npm",
            homepage="https://example.com/not-a-real-npmjs-project",
            backend="npmjs",
            user_id="*****@*****.**",
        )
        project.latest_version = "1.0.0"
        version = models.ProjectVersion(version="1.0.0", project_id=project.id)
        self.session.add(version)
        self.session.commit()

        utilities.check_project_release(project, self.session)

        self.assertEqual(project.latest_version, "1.0.0")
        self.assertEqual(project.logs, "No new version found")

    @mock.patch(
        "anitya.lib.backends.npmjs.NpmjsBackend.get_versions",
        return_value=["1.0.0", "0.9.9", "0.9.8"],
    )
    def test_check_project_release_new_version(self, mock_method):
        """ Test the check_project_release function for Project. """
        project = utilities.create_project(
            self.session,
            name="pypi_and_npm",
            homepage="https://example.com/not-a-real-npmjs-project",
            backend="npmjs",
            user_id="*****@*****.**",
            version_scheme="RPM",
        )
        utilities.check_project_release(project, self.session)
        versions = project.get_sorted_version_objects()
        self.assertEqual(len(versions), 3)
        self.assertEqual(versions[0].version, "1.0.0")

    @mock.patch(
        "anitya.lib.backends.npmjs.NpmjsBackend.get_versions",
        return_value=["v1.0.0", "v0.9.9", "v0.9.8"],
    )
    def test_check_project_release_prefix_remove(self, mock_method):
        """ Test the check_project_release function for Project. """
        project = utilities.create_project(
            self.session,
            name="pypi_and_npm",
            homepage="https://example.com/not-a-real-npmjs-project",
            backend="npmjs",
            user_id="*****@*****.**",
            version_scheme="RPM",
        )
        utilities.check_project_release(project, self.session)

        versions = project.get_sorted_version_objects()
        self.assertEqual(len(versions), 3)
        self.assertEqual(versions[0].version, "v1.0.0")
        self.assertEqual(project.latest_version, "1.0.0")

    @mock.patch(
        "anitya.lib.backends.npmjs.NpmjsBackend.get_versions",
        return_value=["1.0.0", "0.9.9", "0.9.8"],
    )
    def test_check_project_check_times(self, mock_method):
        """ Test if check times are set. """
        project = utilities.create_project(
            self.session,
            name="pypi_and_npm",
            homepage="https://example.com/not-a-real-npmjs-project",
            backend="npmjs",
            user_id="*****@*****.**",
        )
        last_check_orig = project.last_check

        utilities.check_project_release(project, self.session)
        next_check = (plugins.get_plugin(project.backend).check_interval +
                      project.last_check)
        self.assertTrue(last_check_orig < project.last_check)
        self.assertEqual(next_check, project.next_check)

    @mock.patch(
        "anitya.lib.backends.npmjs.NpmjsBackend.get_versions",
        return_value=["1.0.0", "0.9.9", "0.9.8"],
    )
    def test_check_project_check_times_test(self, mock_method):
        """ Test if check times aren't set in test mode. """
        project = utilities.create_project(
            self.session,
            name="pypi_and_npm",
            homepage="https://example.com/not-a-real-npmjs-project",
            backend="npmjs",
            user_id="*****@*****.**",
        )
        last_check_orig = project.last_check
        next_check_orig = project.next_check

        utilities.check_project_release(project, self.session, test=True)
        self.assertEqual(last_check_orig, project.last_check)
        self.assertEqual(next_check_orig, project.next_check)

    def test_check_project_check_times_exception(self):
        """ Test if check times are set if `exceptions.RateLimitException` is raised. """
        project = utilities.create_project(
            self.session,
            name="pypi_and_npm",
            homepage="https://example.com/not-a-real-npmjs-project",
            backend="npmjs",
            user_id="*****@*****.**",
        )
        last_check_orig = project.last_check
        next_check = arrow.get("2008-09-03T20:56:35.450686").naive

        with mock.patch(
                "anitya.lib.backends.npmjs.NpmjsBackend.get_versions",
                return_value=["1.0.0"],
        ) as mock_object:
            mock_object.side_effect = exceptions.RateLimitException(
                "2008-09-03T20:56:35.450686")
            self.assertRaises(
                exceptions.RateLimitException,
                utilities.check_project_release,
                project,
                self.session,
            )
            self.assertTrue(last_check_orig < project.last_check)
            self.assertEqual(next_check, project.next_check)

    @mock.patch.dict("anitya.config.config", {"GITHUB_ACCESS_TOKEN": "foobar"})
    def test_check_project_version_too_long(self):
        """
        Regression test for https://github.com/release-monitoring/anitya/issues/674
        Crash when version is too long
        """
        project = models.Project(
            name="daiquiri",
            homepage="https://github.com/jd/daiquiri",
            backend="GitHub",
            version_scheme="RPM",
            version_url="jd/daiquiri",
        )
        self.session.add(project)
        self.session.commit()

        utilities.check_project_release(project, self.session)

        versions = project.versions
        self.assertEqual(len(versions), 18)
        self.assertFalse(
            "remove-circular-dependency-ad4f7bc7-2ab4-43b8-afac-36ff0e2ee796"
            in versions)
        self.assertFalse(
            "remove-circular-dependency-7bbe4a64-3514-4f6e-9c03-9dc8bcf4def7"
            in versions)
Пример #3
0
class CheckProjectReleaseTests(DatabaseTestCase):
    """Tests for the :func:`anitya.lib.utilities.check_project_release` function."""

    @mock.patch("anitya.db.models.Project")
    def test_check_project_release_no_backend(self, mock_project):
        """ Test the check_project_release function for Project. """
        m_project = mock_project.return_value
        m_project.backend.return_value = "dummy"
        self.assertRaises(
            AnityaException, utilities.check_project_release, m_project, self.session
        )

    def test_check_project_release_archived(self):
        """ Test the check_project_release function for archived Project. """
        project = models.Project(
            name="foobar", homepage="https://foo.bar", backend="npmjs", archived=True
        )
        self.session.add(project)
        self.session.commit()

        self.assertRaises(
            AnityaException, utilities.check_project_release, project, self.session
        )

    @mock.patch(
        "anitya.lib.backends.npmjs.NpmjsBackend.get_versions",
        return_value=["1.0.0", "0.9.9", "0.9.8"],
    )
    def test_check_project_release_backend(self, mock_method):
        """ Test the check_project_release function for Project. """
        with fml_testing.mock_sends(anitya_schema.ProjectCreated):
            project = utilities.create_project(
                self.session,
                name="pypi_and_npm",
                homepage="https://example.com/not-a-real-npmjs-project",
                backend="npmjs",
                user_id="*****@*****.**",
            )
        versions = utilities.check_project_release(project, self.session, test=True)
        self.assertEqual(versions, ["1.0.0", "0.9.9", "0.9.8"])

    @mock.patch(
        "anitya.lib.backends.npmjs.NpmjsBackend.get_versions",
        mock.Mock(side_effect=exceptions.AnityaPluginException("")),
    )
    def test_check_project_release_plugin_exception(self):
        """ Test the check_project_release function for Project. """
        with fml_testing.mock_sends(anitya_schema.ProjectCreated):
            project = utilities.create_project(
                self.session,
                name="pypi_and_npm",
                homepage="https://example.com/not-a-real-npmjs-project",
                backend="npmjs",
                user_id="*****@*****.**",
            )
        self.assertRaises(
            exceptions.AnityaPluginException,
            utilities.check_project_release,
            project,
            self.session,
        )
        self.assertEqual(project.error_counter, 1)

    @mock.patch(
        "anitya.lib.backends.npmjs.NpmjsBackend.get_versions", return_value=["1.0.0"]
    )
    def test_check_project_release_no_new_version(self, mock_method):
        """ Test the check_project_release function for Project. """
        with fml_testing.mock_sends(anitya_schema.ProjectCreated):
            project = utilities.create_project(
                self.session,
                name="pypi_and_npm",
                homepage="https://example.com/not-a-real-npmjs-project",
                backend="npmjs",
                user_id="*****@*****.**",
            )
        project.latest_version = "1.0.0"
        version = models.ProjectVersion(version="1.0.0", project_id=project.id)
        self.session.add(version)
        self.session.commit()

        utilities.check_project_release(project, self.session)

        self.assertEqual(project.latest_version, "1.0.0")
        self.assertEqual(project.logs, "No new version found")

    @mock.patch(
        "anitya.lib.backends.npmjs.NpmjsBackend.get_versions",
        return_value=["1.0.0", "0.9.9", "0.9.8"],
    )
    def test_check_project_release_new_version(self, mock_method):
        """ Test the check_project_release function for Project. """
        with fml_testing.mock_sends(anitya_schema.ProjectCreated):
            project = utilities.create_project(
                self.session,
                name="pypi_and_npm",
                homepage="https://example.com/not-a-real-npmjs-project",
                backend="npmjs",
                user_id="*****@*****.**",
                version_scheme="RPM",
            )
        with fml_testing.mock_sends(
            anitya_schema.ProjectVersionUpdated, anitya_schema.ProjectVersionUpdatedV2
        ):
            utilities.check_project_release(project, self.session)
        versions = project.get_sorted_version_objects()
        self.assertEqual(len(versions), 3)
        self.assertEqual(versions[0].version, "1.0.0")

    @mock.patch(
        "anitya.lib.backends.npmjs.NpmjsBackend.get_versions",
        return_value=["v1.0.0", "v0.9.9", "v0.9.8"],
    )
    def test_check_project_release_prefix_remove(self, mock_method):
        """ Test the check_project_release function for Project. """
        with fml_testing.mock_sends(anitya_schema.ProjectCreated):
            project = utilities.create_project(
                self.session,
                name="pypi_and_npm",
                homepage="https://example.com/not-a-real-npmjs-project",
                backend="npmjs",
                user_id="*****@*****.**",
                version_scheme="RPM",
            )
        with fml_testing.mock_sends(
            anitya_schema.ProjectVersionUpdated, anitya_schema.ProjectVersionUpdatedV2
        ):
            utilities.check_project_release(project, self.session)

        versions = project.get_sorted_version_objects()
        self.assertEqual(len(versions), 3)
        self.assertEqual(versions[0].version, "v1.0.0")
        self.assertEqual(project.latest_version, "1.0.0")

    @mock.patch(
        "anitya.lib.backends.github.GithubBackend.get_versions",
        return_value=[
            {"version": "1.0.0", "cursor": "Hbgf"},
            {"version": "0.9.9", "cursor": "Hbge"},
            {"version": "0.9.8", "cursor": "Hbgd"},
        ],
    )
    def test_check_project_release_versions_with_cursor(self, mock_method):
        """Test check_project_release() with versions with cursor."""
        with fml_testing.mock_sends(anitya_schema.ProjectCreated):
            project = utilities.create_project(
                self.session,
                name="project_name",
                homepage="https://not-a-real-homepage.com",
                backend="GitHub",
                user_id="*****@*****.**",
            )
        with fml_testing.mock_sends(
            anitya_schema.ProjectVersionUpdated, anitya_schema.ProjectVersionUpdatedV2
        ):
            utilities.check_project_release(project, self.session)

        self.assertEqual(project.latest_version_cursor, "Hbgf")

    @mock.patch(
        "anitya.lib.backends.github.GithubBackend.get_versions",
        return_value=[
            {"version": "1.0.0", "commit_url": "https://example.com/tags/1.0.0"},
            {"version": "0.9.9", "commit_url": "https://example.com/tags/0.9.9"},
            {"version": "0.9.8", "commit_url": "https://example.com/tags/0.9.8"},
        ],
    )
    def test_check_project_release_with_versions_with_urls(self, mock_method):
        """Test check_project_release() with versions with URLs."""
        with fml_testing.mock_sends(anitya_schema.ProjectCreated):
            project = utilities.create_project(
                self.session,
                name="project_name",
                homepage="https://not-a-real-homepage.com",
                backend="GitHub",
                user_id="*****@*****.**",
            )
        with fml_testing.mock_sends(
            anitya_schema.ProjectVersionUpdated, anitya_schema.ProjectVersionUpdatedV2
        ):
            utilities.check_project_release(project, self.session)

        for vo in project.versions_obj:
            self.assertEqual(vo.commit_url, "https://example.com/tags/" + vo.version)

    @mock.patch(
        "anitya.lib.backends.npmjs.NpmjsBackend.get_versions",
        return_value=["1.0.0", "0.9.9", "0.9.8"],
    )
    def test_check_project_check_times(self, mock_method):
        """ Test if check times are set. """
        with fml_testing.mock_sends(anitya_schema.ProjectCreated):
            project = utilities.create_project(
                self.session,
                name="pypi_and_npm",
                homepage="https://example.com/not-a-real-npmjs-project",
                backend="npmjs",
                user_id="*****@*****.**",
            )
        last_check_orig = project.last_check

        with fml_testing.mock_sends(
            anitya_schema.ProjectVersionUpdated, anitya_schema.ProjectVersionUpdatedV2
        ):
            utilities.check_project_release(project, self.session)
        next_check = (
            plugins.get_plugin(project.backend).check_interval + project.last_check
        )
        self.assertTrue(last_check_orig < project.last_check)
        self.assertEqual(next_check, project.next_check)

    @mock.patch(
        "anitya.lib.backends.npmjs.NpmjsBackend.get_versions",
        return_value=["1.0.0", "0.9.9", "0.9.8"],
    )
    def test_check_project_check_times_test(self, mock_method):
        """ Test if check times aren't set in test mode. """
        with fml_testing.mock_sends(anitya_schema.ProjectCreated):
            project = utilities.create_project(
                self.session,
                name="pypi_and_npm",
                homepage="https://example.com/not-a-real-npmjs-project",
                backend="npmjs",
                user_id="*****@*****.**",
            )
        last_check_orig = project.last_check
        next_check_orig = project.next_check

        with fml_testing.mock_sends():
            versions = utilities.check_project_release(project, self.session, test=True)
        self.assertEqual(last_check_orig, project.last_check)
        self.assertEqual(next_check_orig, project.next_check)
        self.assertEqual(versions, ["1.0.0", "0.9.9", "0.9.8"])

    def test_check_project_check_times_exception(self):
        """ Test if check times are set if `exceptions.RateLimitException` is raised. """
        with fml_testing.mock_sends(anitya_schema.ProjectCreated):
            project = utilities.create_project(
                self.session,
                name="pypi_and_npm",
                homepage="https://example.com/not-a-real-npmjs-project",
                backend="npmjs",
                user_id="*****@*****.**",
            )
        last_check_orig = project.last_check
        next_check = arrow.get("2008-09-03T20:56:35.450686").naive

        with mock.patch(
            "anitya.lib.backends.npmjs.NpmjsBackend.get_versions",
            return_value=["1.0.0"],
        ) as mock_object:
            mock_object.side_effect = exceptions.RateLimitException(
                "2008-09-03T20:56:35.450686"
            )
            with fml_testing.mock_sends():
                self.assertRaises(
                    exceptions.RateLimitException,
                    utilities.check_project_release,
                    project,
                    self.session,
                )
            self.assertTrue(last_check_orig < project.last_check)
            self.assertEqual(next_check, project.next_check)

    @mock.patch.dict("anitya.config.config", {"GITHUB_ACCESS_TOKEN": "foobar"})
    def test_check_project_version_too_long(self):
        """
        Regression test for https://github.com/fedora-infra/anitya/issues/674
        Crash when version is too long
        """
        project = models.Project(
            name="daiquiri",
            homepage="https://github.com/jd/daiquiri",
            backend="GitHub",
            version_scheme="RPM",
            version_url="jd/daiquiri",
        )
        self.session.add(project)
        self.session.commit()

        with fml_testing.mock_sends(
            anitya_schema.ProjectVersionUpdated, anitya_schema.ProjectVersionUpdatedV2
        ):
            utilities.check_project_release(project, self.session)

        versions = project.versions
        self.assertEqual(len(versions), 18)
        self.assertFalse(
            "remove-circular-dependency-ad4f7bc7-2ab4-43b8-afac-36ff0e2ee796"
            in versions
        )
        self.assertFalse(
            "remove-circular-dependency-7bbe4a64-3514-4f6e-9c03-9dc8bcf4def7"
            in versions
        )

    @mock.patch(
        "anitya.lib.backends.npmjs.NpmjsBackend.get_versions", return_value=["1.0.0"]
    )
    def test_check_project_release_error_counter_reset(self, mock_method):
        """ Test if error_counter reset to 0, when successful check is done. """
        with fml_testing.mock_sends(anitya_schema.ProjectCreated):
            project = utilities.create_project(
                self.session,
                name="pypi_and_npm",
                homepage="https://example.com/not-a-real-npmjs-project",
                backend="npmjs",
                user_id="*****@*****.**",
            )
        project.error_counter = 30
        self.session.add(project)
        self.session.commit()

        utilities.check_project_release(project, self.session)

        self.assertEqual(project.error_counter, 0)