def setUp(self): super(EditProjectMappingTests, self).setUp() # Set up a mapping to edit session = Session() self.user = models.User( email='*****@*****.**', username='******', ) user_social_auth = social_models.UserSocialAuth(user_id=self.user.id, user=self.user) self.session.add(self.user) self.session.add(user_social_auth) self.distro1 = models.Distro(name='CentOS') self.distro2 = models.Distro(name='Fedora') self.project = models.Project( name='python_project', homepage='https://example.com/python_project', backend='PyPI', ecosystem_name='pypi', ) self.package = models.Packages(package_name='python_project', distro=self.distro1.name, project=self.project) session.add_all( [self.distro1, self.distro2, self.project, self.package]) session.commit() self.client = self.flask_app.test_client()
def setUp(self): super(EditDistroTests, self).setUp() # Add a regular user and an admin user session = Session() self.user = models.User( email='*****@*****.**', username='******', ) user_social_auth = social_models.UserSocialAuth( user_id=self.user.id, user=self.user ) session.add(self.user) session.add(user_social_auth) self.admin = models.User(email='*****@*****.**', username='******') admin_social_auth = social_models.UserSocialAuth( user_id=self.admin.id, user=self.admin ) # Add distributions to edit self.fedora = models.Distro(name='Fedora') self.centos = models.Distro(name='CentOS') session.add_all([admin_social_auth, self.admin, self.fedora, self.centos]) session.commit() mock_config = mock.patch.dict( models.anitya_config, {'ANITYA_WEB_ADMINS': [six.text_type(self.admin.id)]}) mock_config.start() self.addCleanup(mock_config.stop) self.client = self.flask_app.test_client()
def create_distro(session): """ Create some basic distro for testing. """ distro = models.Distro(name="Fedora") session.add(distro) distro = models.Distro(name="Debian") session.add(distro) session.commit()
def setUp(self): super(PackagesResourceGetTests, self).setUp() self.app = self.flask_app.test_client() session = Session() self.user = models.User(email='*****@*****.**', username='******') self.api_token = models.ApiToken(user=self.user) fedora = models.Distro('Fedora') debian = models.Distro('Debian') jcline_linux = models.Distro('jcline linux') session.add_all( [self.user, self.api_token, fedora, debian, jcline_linux]) session.commit()
def add_distro(): form = anitya.forms.DistroForm() if form.validate_on_submit(): name = form.name.data distro = models.Distro(name) utilities.log(Session, distro=distro, topic='distro.add', message=dict( agent=flask.g.user.username, distro=distro.name, )) try: Session.add(distro) Session.commit() flask.flash('Distribution added') except SQLAlchemyError: Session.rollback() flask.flash('Could not add this distro, already exists?', 'error') return flask.redirect(flask.url_for('anitya_ui.distros')) return flask.render_template('distro_add_edit.html', context='Add', current='distros', form=form)
def test_distro_delete_cascade(self): """ Assert deletion of mapped packages when project is deleted """ project = models.Project( name="test", homepage="https://example.com", backend="custom", ecosystem_name="pypi", version_scheme="Invalid", ) self.session.add(project) distro = models.Distro(name="Fedora") self.session.add(distro) package = models.Packages(project_id=1, distro_name="Fedora", package_name="test") self.session.add(package) self.session.commit() distros = self.session.query(models.Distro).all() self.assertEqual(len(distros), 1) self.assertEqual(len(distros[0].package), 1) self.session.delete(distros[0]) self.session.commit() distros = self.session.query(models.Distro).all() packages = self.session.query(models.Packages).all() self.assertEqual(len(distros), 0) self.assertEqual(len(packages), 0)
def setUp(self): super(DeleteProjectMappingTests, self).setUp() self.project = models.Project( name='test_project', homepage='https://example.com/test_project', backend='PyPI', ) self.distro = models.Distro(name='Fedora') self.package = models.Packages( distro=self.distro.name, project=self.project, package_name='test-project') # Add a regular user and an admin user session = Session() self.user = models.User(email='*****@*****.**', username='******') self.admin = models.User(email='*****@*****.**', username='******') session.add_all([self.user, self.admin, self.distro, self.project, self.package]) session.commit() mock_config = mock.patch.dict( models.anitya_config, {'ANITYA_WEB_ADMINS': [six.text_type(self.admin.id)]}) mock_config.start() self.addCleanup(mock_config.stop) self.client = self.flask_app.test_client()
def add_distro(): form = anitya.forms.DistroForm() if form.validate_on_submit(): name = form.name.data distro = models.Distro(name) utilities.publish_message( distro=distro.__json__(), topic="distro.add", message=dict(agent=flask.g.user.username, distro=distro.name), ) try: Session.add(distro) Session.commit() flask.flash("Distribution added") except SQLAlchemyError: Session.rollback() flask.flash("Could not add this distro, already exists?", "error") return flask.redirect(flask.url_for("anitya_ui.distros")) return flask.render_template("distro_add_edit.html", context="Add", current="distros", form=form)
def test_log_project_edit(self, mock_method): """ Assert that 'project.edit' topic is handled correctly. """ project = models.Project(name="test") distro = models.Distro(name="Fedora") message = { "agent": "anitya", "project": "test", "changes": { "name": { "old": "dummy", "new": "test" } }, } exp = "anitya edited the project: test fields: " "{}".format( message["changes"]) final_msg = utilities.log( self.session, project=project, distro=distro, topic="project.edit", message=message, ) mock_method.assert_called_with( topic="project.edit", msg=dict(project=project, distro=distro, message=message), ) self.assertEqual(final_msg, exp)
def test_log_project_map_update(self, mock_method): """ Assert that 'project.map.update' topic is handled correctly. """ project = models.Project(name="test") distro = models.Distro(name="Fedora") exp = "anitya updated the name of test in Fedora from: test_old" " to: test_new" message = { "agent": "anitya", "project": "test", "distro": "Fedora", "prev": "test_old", "new": "test_new", } final_msg = utilities.log( self.session, project=project, distro=distro, topic="project.map.update", message=message, ) mock_method.assert_called_with( topic="project.map.update", msg=dict(project=project, distro=distro, message=message), ) self.assertEqual(final_msg, exp)
def test_log_project_version_update(self, mock_method): """ Assert that 'project.version.update' topic is handled correctly. """ project = models.Project(name="test") distro = models.Distro(name="Fedora") exp = ("new version: 1.0.0 found for project test " "in ecosystem pypi (project id: 1).") message = { "agent": "anitya", "upstream_version": "1.0.0", "ecosystem": "pypi", "project": { "name": "test", "id": "1" }, } final_msg = utilities.log( self.session, project=project, distro=distro, topic="project.version.update", message=message, ) mock_method.assert_called_with( topic="project.version.update", msg=dict(project=project, distro=distro, message=message), ) self.assertEqual(final_msg, exp)
def test_is_delete_candidate_mapping_no_version(self): """ Assert that project is marked as delete candidate, if it has mapping added and no version. """ project = models.Project( name="Foobar", backend="GitHub", homepage="www.fakeproject.com", next_check=arrow.utcnow().datetime, error_counter=100, ) self.session.add(project) self.session.commit() distro = models.Distro(name="Fedora") self.session.add(distro) self.session.commit() mapping = models.Packages(distro_name="Fedora", project_id=project.id) self.session.add(mapping) self.session.commit() result = self.checker.is_delete_candidate(project) self.assertTrue(result)
def setUp(self): super(PackagesResourceGetTests, self).setUp() self.app = self.flask_app.test_client() session = Session() self.user = models.User(email="*****@*****.**", username="******") user_social_auth = social_models.UserSocialAuth(user_id=self.user.id, user=self.user) session.add(self.user) session.add(user_social_auth) self.api_token = models.ApiToken(user=self.user) fedora = models.Distro("Fedora") debian = models.Distro("Debian") jcline_linux = models.Distro("jcline linux") session.add_all([self.api_token, fedora, debian, jcline_linux]) session.commit()
def setUp(self): super(DeleteDistroTests, self).setUp() # Add a regular user and an admin user session = Session() self.user = models.User(email='*****@*****.**', username='******') self.admin = models.User(email='*****@*****.**', username='******') # Add distributions to delete self.fedora = models.Distro(name='Fedora') self.centos = models.Distro(name='CentOS') session.add_all([self.user, self.admin, self.fedora, self.centos]) session.commit() mock_config = mock.patch.dict( models.anitya_config, {'ANITYA_WEB_ADMINS': [six.text_type(self.admin.id)]}) mock_config.start() self.addCleanup(mock_config.stop) self.client = self.flask_app.test_client()
def test_log_project_add_tried(self): """ Assert that 'project.add.tried' topic is handled correctly. """ project = models.Project(name='test') distro = models.Distro(name='Fedora') exp = "anitya tried to add an already existing project: test" message = {'agent': 'anitya', 'project': 'test'} final_msg = utilities.log(self.session, project=project, distro=distro, topic='project.add.tried', message=message) self.assertEqual(final_msg, exp)
def test_log_distro_remove(self): """ Assert that 'distro.remove' topic is handled correctly. """ project = models.Project(name='test') distro = models.Distro(name='Fedora') exp = "anitya deleted the distro named: Fedora" message = {'agent': 'anitya', 'distro': 'Fedora'} final_msg = utilities.log(self.session, project=project, distro=distro, topic='distro.remove', message=message) self.assertEqual(final_msg, exp)
def test_log_project_flag(self): """ Assert that 'project.flag' topic is handled correctly. """ project = models.Project(name='test') distro = models.Distro(name='Fedora') exp = "anitya flagged the project: test with reason: reason" message = {'agent': 'anitya', 'project': 'test', 'reason': 'reason'} final_msg = utilities.log(self.session, project=project, distro=distro, topic='project.flag', message=message) self.assertEqual(final_msg, exp)
def test_log_distro_edit(self): """ Assert that 'distro.edit' topic is handled correctly. """ project = models.Project(name='test') distro = models.Distro(name='Fedora') exp = "anitya edited distro name from: Dummy to: Fedora" message = {'agent': 'anitya', 'old': 'Dummy', 'new': 'Fedora'} final_msg = utilities.log(self.session, project=project, distro=distro, topic='distro.edit', message=message) self.assertEqual(final_msg, exp)
def test_log_project_flag_set(self): """ Assert that 'project.flag.set' topic is handled correctly. """ project = models.Project(name='test') distro = models.Distro(name='Fedora') exp = "anitya set flag test to open" message = {'agent': 'anitya', 'flag': 'test', 'state': 'open'} final_msg = utilities.log(self.session, project=project, distro=distro, topic='project.flag.set', message=message) self.assertEqual(final_msg, exp)
def test_log_project_remove(self): """ Assert that 'project.remove' topic is handled correctly. """ project = models.Project(name='test') distro = models.Distro(name='Fedora') exp = "anitya removed the project: test" message = { 'agent': 'anitya', 'project': 'test', } final_msg = utilities.log(self.session, project=project, distro=distro, topic='project.remove', message=message) self.assertEqual(final_msg, exp)
def setUp(self): super(PackagesResourcePostTests, self).setUp() self.app = self.flask_app.test_client() session = Session() self.user = models.User(email='*****@*****.**', username='******') self.api_token = models.ApiToken(user=self.user) self.project = models.Project( name='requests', homepage='https://pypi.io/project/requests', backend='PyPI', ) self.fedora = models.Distro('Fedora') session.add_all([self.user, self.api_token, self.project, self.fedora]) session.commit() self.auth = {'Authorization': 'Token ' + self.api_token.token}
def test_log_project_map_new(self): """ Assert that 'project.map.new' topic is handled correctly. """ project = models.Project(name='test') distro = models.Distro(name='Fedora') exp = "anitya mapped the name of test in Fedora as test_package" message = { 'agent': 'anitya', 'project': 'test', 'distro': 'Fedora', 'new': 'test_package' } final_msg = utilities.log(self.session, project=project, distro=distro, topic='project.map.new', message=message) self.assertEqual(final_msg, exp)
def test_log_project_version_remove(self, mock_method): """ Assert that 'project.version.remove' topic is handled correctly. """ project = models.Project(name="test") distro = models.Distro(name="Fedora") exp = "anitya removed the version 1.0.0 of test" message = {"agent": "anitya", "project": "test", "version": "1.0.0"} final_msg = utilities.log( self.session, project=project, distro=distro, topic="project.version.remove", message=message, ) mock_method.assert_called_with( topic="project.version.remove", msg=dict(project=project, distro=distro, message=message), ) self.assertEqual(final_msg, exp)
def test_log_project_flag_set(self, mock_method): """ Assert that 'project.flag.set' topic is handled correctly. """ project = models.Project(name="test") distro = models.Distro(name="Fedora") exp = "anitya set flag test to open" message = {"agent": "anitya", "flag": "test", "state": "open"} final_msg = utilities.log( self.session, project=project, distro=distro, topic="project.flag.set", message=message, ) mock_method.assert_called_with( topic="project.flag.set", msg=dict(project=project, distro=distro, message=message), ) self.assertEqual(final_msg, exp)
def test_log_project_flag(self, mock_method): """ Assert that 'project.flag' topic is handled correctly. """ project = models.Project(name="test") distro = models.Distro(name="Fedora") exp = "anitya flagged the project: test with reason: reason" message = {"agent": "anitya", "project": "test", "reason": "reason"} final_msg = utilities.log( self.session, project=project, distro=distro, topic="project.flag", message=message, ) mock_method.assert_called_with( topic="project.flag", msg=dict(project=project, distro=distro, message=message), ) self.assertEqual(final_msg, exp)
def test_log_project_add_tried(self, mock_method): """ Assert that 'project.add.tried' topic is handled correctly. """ project = models.Project(name="test") distro = models.Distro(name="Fedora") exp = "anitya tried to add an already existing project: test" message = {"agent": "anitya", "project": "test"} final_msg = utilities.log( self.session, project=project, distro=distro, topic="project.add.tried", message=message, ) mock_method.assert_called_with( topic="project.add.tried", msg=dict(project=project, distro=distro, message=message), ) self.assertEqual(final_msg, exp)
def test_log_distro_remove(self, mock_method): """ Assert that 'distro.remove' topic is handled correctly. """ project = models.Project(name="test") distro = models.Distro(name="Fedora") exp = "anitya deleted the distro named: Fedora" message = {"agent": "anitya", "distro": "Fedora"} final_msg = utilities.log( self.session, project=project, distro=distro, topic="distro.remove", message=message, ) mock_method.assert_called_with( topic="distro.remove", msg=dict(project=project, distro=distro, message=message), ) self.assertEqual(final_msg, exp)
def test_log_distro_edit(self, mock_method): """ Assert that 'distro.edit' topic is handled correctly. """ project = models.Project(name="test") distro = models.Distro(name="Fedora") exp = "anitya edited distro name from: Dummy to: Fedora" message = {"agent": "anitya", "old": "Dummy", "new": "Fedora"} final_msg = utilities.log( self.session, project=project, distro=distro, topic="distro.edit", message=message, ) mock_method.assert_called_with( topic="distro.edit", msg=dict(project=project, distro=distro, message=message), ) self.assertEqual(final_msg, exp)
def test_log_project_map_update(self): """ Assert that 'project.map.update' topic is handled correctly. """ project = models.Project(name='test') distro = models.Distro(name='Fedora') exp = "anitya updated the name of test in Fedora from: test_old" \ " to: test_new" message = { 'agent': 'anitya', 'project': 'test', 'distro': 'Fedora', 'prev': 'test_old', 'new': 'test_new' } final_msg = utilities.log(self.session, project=project, distro=distro, topic='project.map.update', message=message) self.assertEqual(final_msg, exp)
def test_same_package_two_distros(self): """Assert packages can be created.""" Session.add(models.Distro('Debian')) Session.commit() request_data = { "project_ecosystem": "pypi", "project_name": "requests", "package_name": "python-requests", "distribution": "Fedora", } output = self.app.post('/api/v2/packages/', headers=self.auth, data=request_data) self.assertEqual(output.status_code, 201) request_data['distribution'] = 'Debian' output = self.app.post('/api/v2/packages/', headers=self.auth, data=request_data) self.assertEqual(output.status_code, 201)