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 test_main_no_env_set(self): """ Assert that correct user data are dumped when nothing is provided. """ user = models.User( email='*****@*****.**', username='******', active=True, ) user_social_auth = social_models.UserSocialAuth(user_id=user.id, user=user) self.session.add(user_social_auth) self.session.add(user) user2 = models.User( email='*****@*****.**', username='******', active=True, ) user_social_auth = social_models.UserSocialAuth(user_id=user2.id, user=user2) self.session.add(user_social_auth) self.session.add(user2) self.session.commit() sar.main() out, err = self.capsys.readouterr() self.assertEqual('[]', out)
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 test_main_username(self): """ Assert that correct user data are dumped when providing username. """ user = models.User(email="*****@*****.**", username="******", active=True) user_social_auth = social_models.UserSocialAuth(user_id=user.id, user=user) self.session.add(user_social_auth) self.session.add(user) user2 = models.User(email="*****@*****.**", username="******", active=True) user_social_auth = social_models.UserSocialAuth(user_id=user2.id, user=user2) self.session.add(user_social_auth) self.session.add(user2) self.session.commit() exp = [ { "id": str(user.id), "username": user.username, "email": user.email, "active": user.active, "social_auth": [{"uid": None, "provider": None, "extra_data": None}], } ] sar.main() out, err = self.capsys.readouterr() obs = json.loads(out) self.assertEqual(exp, obs)
def test_main_username(self): """ Assert that correct user data are dumped when providing username. """ user = models.User( email='*****@*****.**', username='******', active=True, ) user_social_auth = social_models.UserSocialAuth(user_id=user.id, user=user) self.session.add(user_social_auth) self.session.add(user) user2 = models.User( email='*****@*****.**', username='******', active=True, ) user_social_auth = social_models.UserSocialAuth(user_id=user2.id, user=user2) self.session.add(user_social_auth) self.session.add(user2) self.session.commit() exp = [{ 'id': str(user.id), 'username': user.username, 'email': user.email, 'active': user.active, 'social_auth': [{ 'uid': None, 'provider': None, 'extra_data': None }] }] sar.main() out, err = self.capsys.readouterr() obs = json.loads(out) self.assertEqual(exp, obs)
def test_username_unique(self): """Assert User usernames have a uniqueness constraint on them.""" user = models.User(email="*****@*****.**", username="******") user_social_auth = social_models.UserSocialAuth(user_id=user.id, user=user) self.session.add(user) self.session.add(user_social_auth) self.session.commit() user = models.User(email="*****@*****.**", username="******") user_social_auth = social_models.UserSocialAuth(user_id=user.id, user=user) self.session.add(user) self.session.add(user_social_auth) self.assertRaises(IntegrityError, self.session.commit)
def test_to_dict(self): """ Assert the correct dictionary is returned. """ user = models.User(email="*****@*****.**", username="******") user_social_auth = social_models.UserSocialAuth(user_id=user.id, user=user, provider="FAS") user_social_auth.set_extra_data({"wookie": "too hairy"}) self.session.add(user_social_auth) self.session.add(user) self.session.commit() expected = { "id": str(user.id), "email": user.email, "username": user.username, "active": user.active, "social_auth": [{ "provider": user_social_auth.provider, "extra_data": user_social_auth.extra_data, "uid": user_social_auth.uid, }], } json = user.to_dict() self.assertEqual(json, expected)
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(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 setUp(self): super(BrowseLogsTests, self).setUp() session = Session() # Add a regular user and an admin user 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 ) 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([admin_social_auth, 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()
def test_user_id(self): """Assert Users have a UUID id assigned to them.""" user = models.User(email="*****@*****.**", username="******") user_social_auth = social_models.UserSocialAuth(user_id=user.id, user=user) self.session.add(user) self.session.add(user_social_auth) self.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.""" user = models.User(email="*****@*****.**", username="******") user_social_auth = social_models.UserSocialAuth(user_id=user.id, user=user) self.session.add(user) self.session.add(user_social_auth) self.session.commit() self.assertEqual(six.text_type(user.id), user.get_id())
def test_default_admin(self): """Assert default value for admin flag.""" user = models.User(email="*****@*****.**", username="******") user_social_auth = social_models.UserSocialAuth(user_id=user.id, user=user) self.session.add(user) self.session.add(user_social_auth) self.session.commit() self.assertFalse(user.admin) self.assertFalse(user.is_admin)
def test_default_active(self): """Assert User usernames have a uniqueness constraint on them.""" user = models.User(email="*****@*****.**", username="******") user_social_auth = social_models.UserSocialAuth(user_id=user.id, user=user) self.session.add(user) self.session.add(user_social_auth) self.session.commit() self.assertTrue(user.active) self.assertTrue(user.is_active)
def test_without_error(self): """Assert that nothing happens if social_auth info is available.""" user = models.User(email='*****@*****.**', username='******') social_auth_user = social_models.UserSocialAuth( provider='Demo Provider', user=user) self.session.add(social_auth_user) self.session.add(user) self.session.commit() # If the method doesn't throw exception the test is ok app.when_user_log_in(app, user)
def test_not_anonymous(self): """Assert User implements the Flask-Login API for authenticated users.""" user = models.User(email="*****@*****.**", username="******") user_social_auth = social_models.UserSocialAuth(user_id=user.id, user=user) self.session.add(user) self.session.add(user_social_auth) self.session.commit() self.assertFalse(user.is_anonymous) self.assertTrue(user.is_authenticated)
def setUp(self): super(SetFlagStateTests, self).setUp() session = Session() # Add a regular user and an admin user 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 ) 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( [admin_social_auth, 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()
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 test_user_relationship(self): """Assert users have a reference to their tokens.""" user = models.User(email="*****@*****.**", username="******") user_social_auth = social_models.UserSocialAuth(user_id=user.id, user=user) self.session.add(user) self.session.add(user_social_auth) token = models.ApiToken(user=user) self.session.add(token) self.session.commit() self.assertEqual(user.api_tokens, [token])
def test_token_default(self): """Assert creating an ApiToken generates a random token.""" user = models.User(email="*****@*****.**", username="******") user_social_auth = social_models.UserSocialAuth(user_id=user.id, user=user) self.session.add(user) self.session.add(user_social_auth) token = models.ApiToken(user=user) self.session.add(token) self.session.commit() self.assertEqual(40, len(token.token))
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='******', ) 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, 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()
def setUp(self): """Set up the Flask testing environnment""" super(SettingsTests, self).setUp() self.app = self.flask_app.test_client() 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.session.commit()
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()
def setUp(self): super(MapProjectTests, self).setUp() create_distro(self.session) create_project(self.session) self.client = self.flask_app.test_client() 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.session.commit()
def test_is_admin_configured(self): """Assert default value for admin flag.""" user = models.User(email="*****@*****.**", username="******", admin=False) user_social_auth = social_models.UserSocialAuth(user_id=user.id, user=user) self.session.add(user) self.session.add(user_social_auth) self.session.commit() mock_dict = mock.patch.dict( "anitya.config.config", {"ANITYA_WEB_ADMINS": [six.text_type(user.id)]} ) with mock_dict: self.assertFalse(user.admin) self.assertTrue(user.is_admin) self.assertTrue(user.admin)
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 test_check_user_with_social_auth(self): """ Assert user is created. """ user = models.User( email='*****@*****.**', username='******', ) user_social_auth = social_models.UserSocialAuth(user_id=user.id, user=user) self.session.add(user) self.session.add(user_social_auth) self.session.commit() user = self.session.query(models.User).one() self.assertTrue(len(user.social_auth.all()), 1)
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): """Set up the Flask testing environnment""" super(EditProjectTests, self).setUp() self.app = self.flask_app.test_client() # Make a user to login with 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.session.commit() create_distro(self.session) create_project(self.session)
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()