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 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 setUp(self): super(LoadUserFromSessionTests, self).setUp() session = Session() self.user = User(email='*****@*****.**', username='******') session.add(self.user) session.commit()
def test_delete_token(self): """Assert a user can delete an API token.""" session = Session() token = models.ApiToken(user=self.user, description='Test token') session.add(token) session.commit() with login_user(self.flask_app, self.user): with self.flask_app.test_client() as c: output = c.get('/settings/', follow_redirects=False) self.assertEqual(output.status_code, 200) self.assertTrue(b'Test token' in output.data) csrf_token = output.data.split( b'name="csrf_token" type="hidden" value="')[1].split( b'">')[0] data = {'csrf_token': csrf_token} output = c.post('/settings/tokens/delete/{}/'.format( token.token), data=data, follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertFalse(b'Test token' in output.data) self.assertEqual( 0, models.ApiToken.query.filter_by(user=self.user).count())
def setUp(self): super(IsAdminTests, 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 ) session.add_all([admin_social_auth, self.admin]) 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)
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 setUp(self): """Set up the Flask testing environnment""" super(SettingsTests, self).setUp() self.app = self.flask_app.test_client() session = Session() self.user = models.User(email='*****@*****.**', username='******') session.add(self.user) session.commit()
def test_user_id(self): """Assert Users have a UUID id assigned to them.""" session = Session() user = models.User(email='*****@*****.**', username='******') session.add(user) session.commit() self.assertTrue(isinstance(user.id, UUID))
def test_user_get_id(self): """Assert Users implements the Flask-Login API for getting user IDs.""" session = Session() user = models.User(email='*****@*****.**', username='******') session.add(user) session.commit() self.assertEqual(six.text_type(user.id), user.get_id())
def delete_project_version(project_id, version): project = models.Project.get(Session, project_id) if not project: flask.abort(404) version_obj = None for vers in project.versions_obj: if version == vers.version: version_obj = vers break if version_obj is None: flask.abort( 404, "Version %s not found for project %s" % (version, project.name)) if not is_admin(): flask.abort(401) form = anitya.forms.ConfirmationForm() confirm = flask.request.form.get("confirm", False) if form.validate_on_submit(): if confirm: utilities.log( Session, project=project.__json__(), topic="project.version.remove", message=dict(agent=flask.g.user.username, project=project.name, version=version), ) # Delete the record of the version for this project Session.delete(version_obj) # Adjust the latest_version if needed sorted_versions = project.get_sorted_version_objects() if len(sorted_versions ) > 1 and sorted_versions[0].version == version: project.latest_version = sorted_versions[1].parse() Session.add(project) elif len(sorted_versions) == 1: project.latest_version = None Session.add(project) Session.commit() flask.flash("Version for %s has been removed" % version) return flask.redirect( flask.url_for("anitya_ui.project", project_id=project.id)) return flask.render_template( "version_delete.html", current="projects", project=project, version=version, form=form, )
def test_token_default(self): """Assert creating an ApiToken generates a random token.""" session = Session() user = models.User(email='*****@*****.**', username='******') token = models.ApiToken(user=user) session.add(token) session.commit() self.assertEqual(40, len(token.token))
def setUp(self): super(MapProjectTests, self).setUp() create_distro(self.session) create_project(self.session) self.client = self.flask_app.test_client() session = Session() self.user = models.User(email='*****@*****.**', username='******') session.add(self.user) session.commit()
def test_user_relationship(self): """Assert users have a reference to their tokens.""" session = Session() user = models.User(email='*****@*****.**', username='******') token = models.ApiToken(user=user) session.add(token) session.commit() self.assertEqual(user.api_tokens, [token])
def test_default_active(self): """Assert User usernames have a uniqueness constraint on them.""" session = Session() user = models.User(email='*****@*****.**', username='******') session.add(user) session.commit() self.assertTrue(user.active) self.assertTrue(user.is_active)
def test_not_anonymous(self): """Assert User implements the Flask-Login API for authenticated users.""" session = Session() user = models.User(email='*****@*****.**', username='******') session.add(user) session.commit() self.assertFalse(user.is_anonymous) self.assertTrue(user.is_authenticated)
def setUp(self): super(IntegrityErrorHandlerTests, self).setUp() session = Session() user = models.User(email='*****@*****.**', username='******') social_auth_user = social_models.UserSocialAuth( provider='Demo Provider', user=user) session.add(social_auth_user) session.add(user) session.commit()
def test_username_unique(self): """Assert User usernames have a uniqueness constraint on them.""" session = Session() user = models.User(email='*****@*****.**', username='******') session.add(user) session.commit() user2 = models.User(email='*****@*****.**', username='******') session.add(user2) self.assertRaises(IntegrityError, session.commit)
def new_token(): """Create a new API token for the current user.""" form = anitya.forms.TokenForm() if form.validate_on_submit(): token = models.ApiToken(user=flask.g.user, description=form.description.data) Session.add(token) Session.commit() return flask.redirect(flask.url_for("anitya_ui.settings")) else: flask.abort(400)
def setUp(self): """Set up the Flask testing environnment""" super(EditProjectTests, self).setUp() self.app = self.flask_app.test_client() # Make a user to login with session = Session() self.user = models.User(email='*****@*****.**', username='******') session.add(self.user) session.commit() create_distro(self.session) create_project(self.session)
def test_set_automatically(self): """Assert the ecosystem gets set automatically based on the backend.""" project = models.Project( name="requests", homepage="https://pypi.org/requests", backend="PyPI" ) Session.add(project) Session.commit() project = models.Project.query.all()[0] self.assertEqual("pypi", project.ecosystem_name)
def test_invalid(self): """Assert invalid ecosystems raise an exception.""" project = models.Project( name="requests", homepage="https://pypi.org/requests", backend="PyPI", ecosystem_name="invalid_ecosystem", ) Session.add(project) self.assertRaises(ValueError, Session.commit)
def test_invalid(self): """Assert invalid ecosystems raise an exception.""" project = models.Project( name='requests', homepage='https://pypi.org/requests', backend='PyPI', ecosystem_name='invalid_ecosystem', ) Session.add(project) self.assertRaises(ValueError, Session.commit)
def setUp(self): super(LoadUserFromSessionTests, self).setUp() 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) session.commit()
def setUp(self): super(LoadUserFromSessionTests, self).setUp() 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) session.commit()
def delete_project_version(project_id, version): project = models.Project.get(Session, project_id) if not project: flask.abort(404) version_obj = None for vers in project.versions_obj: if version == vers.version: version_obj = vers break if version_obj is None: flask.abort( 404, "Version %s not found for project %s" % (version, project.name) ) if not is_admin(): flask.abort(401) form = anitya.forms.ConfirmationForm() confirm = flask.request.form.get("confirm", False) if form.validate_on_submit(): if confirm: utilities.log( Session, project=project.__json__(), topic="project.version.remove", message=dict( agent=flask.g.user.username, project=project.name, version=version ), ) # Delete the record of the version for this project Session.delete(version_obj) # Adjust the latest_version if needed if project.latest_version == version: project.latest_version = None Session.add(project) Session.commit() flask.flash("Version for %s has been removed" % version) return flask.redirect(flask.url_for("anitya_ui.project", project_id=project.id)) return flask.render_template( "version_delete.html", current="projects", project=project, version=version, form=form, )
def delete_project_version(project_id, version): project = models.Project.get(Session, project_id) if not project: flask.abort(404) version_obj = None for vers in project.versions_obj: if version == vers.version: version_obj = vers break if version_obj is None: flask.abort( 404, 'Version %s not found for project %s' % (version, project.name)) if not is_admin(): flask.abort(401) form = anitya.forms.ConfirmationForm() confirm = flask.request.form.get('confirm', False) if form.validate_on_submit(): if confirm: utilities.log(Session, project=project.__json__(), topic='project.version.remove', message=dict( agent=flask.g.user.username, project=project.name, version=version, )) # Delete the record of the version for this project Session.delete(version_obj) # Adjust the latest_version if needed if project.latest_version == version: project.latest_version = None Session.add(project) Session.commit() flask.flash('Version for %s has been removed' % version) return flask.redirect( flask.url_for('anitya_ui.project', project_id=project.id)) return flask.render_template('version_delete.html', current='projects', project=project, version=version, form=form)
def test_set_manually(self): """Assert the ecosystem can be set manually.""" project = models.Project( name="requests", homepage="https://pypi.org/requests", ecosystem_name="crates.io", backend="PyPI", ) Session.add(project) Session.commit() project = models.Project.query.all()[0] self.assertEqual("crates.io", project.ecosystem_name)
def test_set_manually(self): """Assert the ecosystem can be set manually.""" project = models.Project( name='requests', homepage='https://pypi.org/requests', ecosystem_name='crates.io', backend='PyPI', ) Session.add(project) Session.commit() project = models.Project.query.all()[0] self.assertEqual('crates.io', project.ecosystem_name)
def test_set_backend(self): """Assert the ecosystem gets set to correct backend, even when homepage is changed.""" project = models.Project(name="requests", homepage="https://pypi.org/requests", backend="PyPI") Session.add(project) Session.commit() project = models.Project.query.all()[0] self.assertEqual("pypi", project.ecosystem_name) project.homepage = "https://example.com" Session.add(project) Session.commit() self.assertEqual("pypi", project.ecosystem_name)
def delete_project_versions(project_id): """ Delete all versions on the project. """ project = models.Project.get(Session, project_id) if not project: flask.abort(404) if not is_admin(): flask.abort(401) form = anitya.forms.ConfirmationForm() confirm = flask.request.form.get("confirm", False) if form.validate_on_submit(): if confirm: for version in project.versions_obj: # Delete the record of the version for this project Session.delete(version) utilities.log( Session, project=project.__json__(), topic="project.version.remove", message=dict( agent=flask.g.user.username, project=project.name, version=str(version), ), ) project.latest_version = None project.latest_version_cursor = None Session.add(project) Session.commit() flask.flash("All versions were removed") return flask.redirect( flask.url_for("anitya_ui.project", project_id=project.id)) return flask.render_template( "project_versions_delete.html", current="projects", project=project, form=form, )
def test_set_backend_no_change(self): """Assert the ecosystem is not changed when the backend is set to same value.""" project = models.Project(name="requests", homepage="https://pypi.org/requests", backend="PyPI") Session.add(project) Session.commit() project = models.Project.query.all()[0] self.assertEqual("pypi", project.ecosystem_name) project.backend = "PyPI" Session.add(project) Session.commit() self.assertEqual("pypi", project.ecosystem_name)
def test_set_wrong_backend(self): """Assert the ecosystem will not be set if backend is not related to any ecosystem.""" project = models.Project(name="requests", homepage="https://pypi.org/requests", backend="PyPI") Session.add(project) Session.commit() project = models.Project.query.all()[0] self.assertEqual("pypi", project.ecosystem_name) project.backend = "GitHub" Session.add(project) Session.commit() self.assertEqual("https://pypi.org/requests", project.ecosystem_name)
def setUp(self): super(AddDistroTests, 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) session.commit() self.client = self.flask_app.test_client()
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(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(PackagesResourcePostTests, 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) self.project = models.Project( name="requests", homepage="https://pypi.io/project/requests", backend="PyPI" ) self.fedora = models.Distro("Fedora") session.add_all([self.api_token, self.project, self.fedora]) session.commit() self.auth = {"Authorization": "Token " + self.api_token.token}
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)
def set_user_active_state(user_id, state): if not is_admin(): flask.abort(401) if state.upper() == "TRUE": state = True elif state.upper() == "FALSE": state = False else: flask.abort(422) try: user = Session.query(models.User).filter(models.User.id == user_id).one() except Exception as err: _log.exception(err) user = None if not user: flask.abort(404) form = anitya.forms.ConfirmationForm() if form.validate_on_submit(): try: user.active = state Session.add(user) Session.commit() if state: flask.flash("User {0} is no longer banned".format(user.username)) else: flask.flash("User {0} is banned".format(user.username)) except Exception as err: _log.exception(err) flask.flash(str(err), "errors") Session.rollback() return flask.redirect(flask.url_for("anitya_ui.browse_users"))
def edit_distro(distro_name): distro = models.Distro.by_name(Session, distro_name) if not distro: flask.abort(404) if not is_admin(): flask.abort(401) form = anitya.forms.DistroForm(obj=distro) if form.validate_on_submit(): name = form.name.data if name != distro.name: utilities.log( Session, distro=distro.__json__(), topic="distro.edit", message=dict(agent=flask.g.user.username, old=distro.name, new=name), ) distro.name = name Session.add(distro) Session.commit() message = "Distribution edited" flask.flash(message) return flask.redirect(flask.url_for("anitya_ui.distros")) return flask.render_template( "distro_add_edit.html", context="Edit", current="distros", distro=distro, form=form, )
def setUp(self): super(RequireTokenTests, 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 = ApiToken(user=self.user) session.add(self.api_token) session.commit()
def setUp(self): super(ProjectsResourcePostTests, 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) session.add(self.api_token) session.commit() self.auth = {"Authorization": "Token " + self.api_token.token}
def setUp(self): super(IntegrityErrorHandlerTests, self).setUp() session = Session() self.user = models.User(email="*****@*****.**", username="******") session.add(self.user) session.commit()
def post(self): """ Create a new package associated with an existing project and distribution. **Example request**: .. sourcecode:: http POST /api/v2/packages/ HTTP/1.1 Accept: application/json Accept-Encoding: gzip, deflate Authorization: Token gAOFi2wQPzUJFIfDkscAKjbJfXELCz0r44m57Ur2 Connection: keep-alive Content-Length: 120 Content-Type: application/json Host: localhost:5000 User-Agent: HTTPie/0.9.4 { "distribution": "Fedora", "package_name": "python-requests", "project_ecosystem": "pypi", "project_name": "requests" } .. sourcecode:: http HTTP/1.0 201 CREATED Content-Length: 69 Content-Type: application/json Date: Mon, 15 Jan 2018 21:49:01 GMT Server: Werkzeug/0.14.1 Python/2.7.14 { "distribution": "Fedora", "name": "python-requests" } :reqheader Authorization: API token to use for authentication :reqjson string distribution: The name of the distribution that contains this package. :reqjson string package_name: The name of the package in the distribution repository. :reqjson string project_name: The project name in Anitya. :reqjson string project_ecosystem: The ecosystem the project is a part of. If it's not part of an ecosystem, use the homepage used in the Anitya project. :statuscode 201: When the package was successfully created. :statuscode 400: When required arguments are missing or malformed. :statuscode 401: When your access token is missing or invalid :statuscode 409: When the package already exists. """ distribution_help = _( "The name of the distribution that contains this package." ) package_name_help = _("The name of the package in the distribution repository.") project_name_help = _("The project name in Anitya.") project_ecosystem_help = _( "The ecosystem the project is a part of. If it's not part of an ecosystem," " use the homepage used in the Anitya project." ) parser = _BASE_ARG_PARSER.copy() parser.add_argument( "distribution", type=str, help=distribution_help, required=True ) parser.add_argument( "package_name", type=str, help=package_name_help, required=True ) parser.add_argument( "project_name", type=str, help=project_name_help, required=True ) parser.add_argument( "project_ecosystem", type=str, help=project_ecosystem_help, required=True ) args = parser.parse_args(strict=True) try: project = models.Project.query.filter_by( name=args.project_name, ecosystem_name=args.project_ecosystem ).one() except NoResultFound: return ( { "error": 'Project "{}" in ecosystem "{}" not found'.format( args.project_name, args.project_ecosystem ) }, 400, ) try: distro = models.Distro.query.filter_by(name=args.distribution).one() except NoResultFound: return ( {"error": 'Distribution "{}" not found'.format(args.distribution)}, 400, ) try: package = models.Packages( distro_name=distro.name, project=project, package_name=args.package_name ) Session.add(package) Session.commit() message = dict( agent=flask_login.current_user.email, project=project.name, distro=distro.name, new=package.package_name, ) utilities.log( Session, project=project.__json__(), distro=distro.__json__(), topic="project.map.new", message=message, ) return {u"distribution": distro.name, u"name": package.package_name}, 201 except IntegrityError: Session.rollback() return {"error": "package already exists in distribution"}, 409