Ejemplo n.º 1
0
 def test_session_removed_post_request(self):
     """Assert that the session is cleaned up after a request."""
     session = Session()
     self.assertTrue(session is Session())
     app = self.flask_app.test_client()
     app.get('/about', follow_redirects=False)
     self.assertFalse(session is Session())
Ejemplo n.º 2
0
    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()
Ejemplo n.º 3
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()
Ejemplo n.º 4
0
    def setUp(self):
        super(DeleteProjectVersionTests, self).setUp()
        session = Session()

        # Add a project with a version to delete.
        self.project = models.Project(
            name='test_project',
            homepage='https://example.com/test_project',
            backend='PyPI',
        )
        self.project_version = models.ProjectVersion(project=self.project, version='1.0.0')

        # Add a regular user and an admin user
        self.user = models.User(email='*****@*****.**', username='******')
        self.admin = models.User(email='*****@*****.**', username='******')

        session.add_all([self.user, self.admin, self.project, self.project_version])
        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()
Ejemplo n.º 5
0
    def setUp(self):
        super(BrowseLogsTests, self).setUp()
        session = Session()

        # Add a regular user and an admin user
        self.user = models.User(email='*****@*****.**', username='******')
        self.admin = models.User(email='*****@*****.**', username='******')

        self.user_log = models.Log(
            user='******',
            project='relational_db',
            distro='Fedora',
            description='This is a log',
        )
        self.admin_log = models.Log(
            user='******',
            project='best_project',
            distro='CentOS',
            description='This is also a log',
        )

        session.add_all([self.user, self.admin, self.user_log, self.admin_log])
        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()
Ejemplo n.º 6
0
    def setUp(self):
        super(SetFlagStateTests, self).setUp()
        session = Session()

        # Add a regular user and an admin user
        self.user = models.User(email='*****@*****.**', username='******')
        self.admin = models.User(email='*****@*****.**', username='******')

        self.project1 = models.Project(
            name='test_project', homepage='https://example.com/test_project', backend='PyPI')
        self.project2 = models.Project(
            name='project2', homepage='https://example.com/project2', backend='PyPI')
        self.flag1 = models.ProjectFlag(
            reason='I wanted to flag it', user='******', project=self.project1)
        self.flag2 = models.ProjectFlag(
            reason='This project is wrong', user='******', project=self.project2)

        session.add_all(
            [self.user, self.admin, self.project1, self.project2, self.flag1, self.flag2])
        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()
Ejemplo n.º 7
0
    def setUp(self):
        super(DatabaseTestCase, self).setUp()

        # We don't want our SQLAlchemy session thrown away post-request because that rolls
        # back the transaction and no database assertions can be made.
        self.flask_app.teardown_request_funcs = {}

        if engine is None:
            # In the future we could provide a postgres URI to test against various
            # databases!
            _configure_db()

        self.connection = engine.connect()
        Base.metadata.create_all(bind=self.connection)
        PSABase.metadata.create_all(bind=self.connection)
        self.transaction = self.connection.begin()

        Session.remove()
        Session.configure(bind=self.connection, autoflush=False)
        self.session = Session()

        # Start a transaction after creating the schema, but before anything is
        # placed into the database. We'll roll back to the start of this
        # transaction at the end of every test, and code under test will start
        # nested transactions
        self.session.begin_nested()
Ejemplo n.º 8
0
    def setUp(self):
        super(LoadUserFromSessionTests, self).setUp()

        session = Session()
        self.user = User(email='*****@*****.**', username='******')
        session.add(self.user)
        session.commit()
Ejemplo n.º 9
0
    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())
Ejemplo n.º 10
0
    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()
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
0
    def test_list_projects_with_same_name(self):
        """Assert two projects with the same name are sorted by the ecosystem."""
        self.maxDiff = None
        session = Session()
        project1 = models.Project(
            name="zlib",
            homepage="https://hackage.haskell.org/package/zlib",
            backend='GitHub',
            ecosystem_name="https://hackage.haskell.org/package/zlib",
        )
        project2 = models.Project(
            name="zlib",
            homepage="http://www.zlib.net/",
            backend="custom",
            regex="DEFAULT",
            ecosystem_name="http://www.zlib.net/",
        )
        session.add_all([project1, project2])
        session.commit()

        output = self.app.get('/api/v2/projects/')
        self.assertEqual(output.status_code, 200)
        data = _read_json(output)

        for item in data['items']:
            del item['created_on']
            del item['updated_on']

        exp = {
            'page':
            1,
            'items_per_page':
            25,
            'total_items':
            2,
            'items': [{
                "id": 2,
                "backend": "custom",
                "homepage": "http://www.zlib.net/",
                "name": "zlib",
                "regex": "DEFAULT",
                "version": None,
                "version_url": None,
                "versions": [],
                "ecosystem": "http://www.zlib.net/",
            }, {
                "id": 1,
                "backend": "GitHub",
                "homepage": "https://hackage.haskell.org/package/zlib",
                "name": "zlib",
                "regex": None,
                "version": None,
                "version_url": None,
                "versions": [],
                "ecosystem": "https://hackage.haskell.org/package/zlib",
            }]
        }

        self.assertEqual(data, exp)
Ejemplo n.º 13
0
 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()
Ejemplo n.º 14
0
    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))
Ejemplo n.º 15
0
 def setUp(self):
     super(LoadUserFromRequestTests, self).setUp()
     self.app = self.flask_app.test_client()
     session = Session()
     self.user = User(email='*****@*****.**', username='******')
     self.api_token = ApiToken(user=self.user)
     session.add_all([self.user, self.api_token])
     session.commit()
Ejemplo n.º 16
0
 def setUp(self):
     super(ProjectsResourceGetTests, 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)
     session.add_all([self.user, self.api_token])
     session.commit()
Ejemplo n.º 17
0
    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())
Ejemplo n.º 18
0
    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])
Ejemplo n.º 19
0
    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))
Ejemplo n.º 20
0
    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)
Ejemplo n.º 21
0
    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)
Ejemplo n.º 22
0
 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()
Ejemplo n.º 23
0
 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()
Ejemplo n.º 24
0
    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)
Ejemplo n.º 25
0
 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)
Ejemplo n.º 26
0
    def test_db_config(self):
        """Assert creating the application configures the scoped session."""
        # Assert the scoped session is not bound.
        self.assertRaises(UnboundExecutionError, Session.get_bind)
        Session.remove()

        app.create({
            'DB_URL': 'sqlite://',
            'SOCIAL_AUTH_USER_MODEL': 'anitya.db.models.User',
        })
        self.assertEqual('sqlite://', str(Session().get_bind().url))
Ejemplo n.º 27
0
    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()
Ejemplo n.º 28
0
 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()
Ejemplo n.º 29
0
    def setUp(self):
        super(IsAdminTests, self).setUp()

        # 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])
        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)
Ejemplo n.º 30
0
    def setUp(self):
        super(ProjectsResourceGetTests, 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()