<commit_before>#!/usr/bin/python
#
# Copyright (c) 2011 rPath, Inc.
#
# All rights reserved.
#

from mint.django_rest.rbuilder.inventory.tests import XMLTestCase

from mint.django_rest.rbuilder.projects import manager # pyflakes=ignore
from mint.django_rest.rbuilder.projects import models # pyflakes=ignore
from mint.django_rest.rbuilder.projects import testsxml # pyflakes=ignore
from mint.django_rest.rbuilder.repos import manager as reposmanager
from mint.django_rest.rbuilder.manager import basemanager
from mint.django_rest.rbuilder.manager import rbuildermanager
from xobj import xobj
from mint.django_rest.rbuilder.rbac.tests import RbacEngine
from testutils import mock
from mint.django_rest.rbuilder.images import models as imagesmodels
from mint import helperfuncs

class ProjectsTestCase(RbacEngine):
    fixtures = ["projects", "project_image_fixtures"]

    def setUp(self):
        RbacEngine.setUp(self)
        mock.mock(reposmanager.ReposManager, "createRepositoryForProject")
        mock.mock(reposmanager.ReposManager, "createSourceTrove")
        mock.mock(reposmanager.ReposManager, "generateConaryrcFile")
        MockProdDef = mock.MockObject()
        MockProdDef.getImageGroup._mock.setReturn("group-foo-appliance")
        MockProdDef.loadFromRepository._mock.setReturn(MockProdDef)
        mock.mock(basemanager.BaseRbuilderManager, "restDb")
        basemanager.BaseRbuilderManager.restDb.getProductVersionDefinitionFromVersion._mock.setDefaultReturn(MockProdDef)
        mock.mock(manager.ProjectManager, "setProductVersionDefinition")
        self.mgr = rbuildermanager.RbuilderManager()
        self.mintConfig = self.mgr.cfg
        
    def _addProject(self, short_name, namespace='ns'):
        project = models.Project()
        project.name = project.hostname = project.short_name = short_name
        project.namespace = namespace
        project.domain_name = 'test.local2'
        project = self.mgr.projectManager.addProject(project)
        
        return project

    def _initProject(self, name='chater-foo'):
        proj = models.Project.objects.get(name='chater-foo')
        branch = models.ProjectVersion(project=proj, name="trunk", label="chater-foo.eng.rpath.com@rpath:chater-foo-trunk")
        branch.save()
        stage = models.Stage(project=proj,
            project_branch=branch, name="Development", label="foo@ns:trunk-devel")
        stage.save()
        stage = models.Stage(project=proj,
            project_branch=branch, name="QA", label="foo@ns:trunk-qa")
        stage.save()
        stage = models.Stage(project=proj,
            project_branch=branch, name="Stage", label="foo@ns:trunk-stage")
        stage.save()
        stage = models.Stage(project=proj,
            project_branch=branch, name="Release", label="foo@ns:trunk")
        stage.save()

    def testGetProjectsAdmin(self):
        response = self._get('projects/',
            username="admin", password="password")
        self.assertEquals(response.status_code, 200)
        projects = xobj.parse(response.content).projects.project
        self.assertEquals(len(projects), 4)

    def testGetProjectsUser(self):
        response = self._get('projects/',
            username="admin", password="password")
        self.assertEquals(response.status_code, 200)
        projects = xobj.parse(response.content).projects.project
        self.assertEquals(len(projects), 4)
        self.assertEquals([p.short_name for p in projects],
            [u'chater-foo', u'postgres', u'postgres-private', u'example2'])
        
        # XXX: Below is a good thing to check, put this back in.  It
        #      was taken out because of the way rbac handles collections,
        #      rbac won't validate the permissions unless the user is admin
    
        # response = self._get('projects/',
        #     username="testuser", password="password")
        # self.assertEquals(response.status_code, 200)
        # projects = xobj.parse(response.content).projects.project
        # self.assertEquals(len(projects), 3)
    
    def testGetProjectAuthenticatedNonAdmin(self):
        response = self._get('projects/chater-foo/',
            username=self.sysadmin_user.user_name, password='password')
        self.assertEquals(response.status_code, 200)
        
        response = self._get('projects/chater-foo/',
            username=self.developer_user.user_name, password='password')
        self.assertEquals(response.status_code, 403)
        
    # def testGetProjectsAnon(self):
    #     response = self._get('projects/')
    #     self.assertEquals(response.status_code, 200)
    #     projects = xobj.parse(response.content).projects.project
    #     self.failUnlessEqual([ x.name for x in projects ],
    #         ['chater-foo', 'postgres', 'name2', ])
    #     self.failUnlessEqual([ x.id for x in projects ],
    #       [
    #         'http://testserver/api/v1/projects/chater-foo',
    #         'http://testserver/api/v1/projects/postgres',
    #         'http://testserver/api/v1/projects/example2',
    #       ])

    def testGetProjectsAnon(self):
        # needs to fail for user w/o rbac/rmember perms
        response = self._get('projects/')
        self.assertEquals(response.status_code, 401)

    # def testGetProjectAnon(self):
    #     response = self._get('projects/chater-foo')
    #     self.assertEquals(response.status_code, 200)
    #     project = xobj.parse(response.content).project
    #     self.failUnlessEqual(project.short_name, 'chater-foo')
    #     self.failUnlessEqual(project.project_branches.id,
    #         'http://testserver/api/v1/projects/chater-foo/project_branches')

    def testGetProjectAnon(self):
        response = self._get('projects/chater-foo')
        self.assertEquals(response.status_code, 401)

    # def testGetProjectBranchesAnon(self):
    #     self._initProject()
    #     response = self._get('projects/chater-foo/project_branches')
    #     self.assertEquals(response.status_code, 200)
    #     branches = xobj.parse(response.content).project_branches.project_branch
    #     self.failUnlessEqual([ x.name for x in branches ],
    #         ['1', 'trunk'])
    #     self.failUnlessEqual([ x.project_branch_stages.id for x in branches ],
    #         [
    #             'http://testserver/api/v1/projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-1/project_branch_stages',
    #             'http://testserver/api/v1/projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk/project_branch_stages',
    #         ])

    def testGetProjectBranchesAnon(self):
        self._initProject()
        response = self._get('projects/chater-foo/project_branches')
        self.assertEquals(response.status_code, 401)

    # def testGetProjectBranchAnon(self):
    #     self._initProject()
    #     response = self._get('projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk')
    #     self.assertEquals(response.status_code, 200)
    #     branch = xobj.parse(response.content).project_branch
    #     self.failUnlessEqual(branch.name, 'trunk')
    #     self.failUnlessEqual(branch.project_branch_stages.id,
    #             'http://testserver/api/v1/projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk/project_branch_stages')
    #     # Comparing XML is more involved maintenance-wise, avoid doing
    #     # that for collections
    #     self.assertXMLEquals(response.content, testsxml.project_branch_xml)

    def testGetProjectBranchAnon(self):
        self._initProject()
        response = self._get('projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk')
        self.assertEquals(response.status_code, 401)

    # def testGetProjectBranchStagesAnon(self):
    #     self._initProject()
    #     response = self._get('projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk/project_branch_stages')
    #     self.assertEquals(response.status_code, 200)
    #     stages = xobj.parse(response.content).project_branch_stages.project_branch_stage
    #     self.failUnlessEqual([ x.name for x in stages ], ['Development', 'QA', 'Stage', 'Release'])
    #     self.failUnlessEqual([ x.id for x in stages ],
    #       [
    #         'http://testserver/api/v1/projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk/project_branch_stages/Development',
    #         'http://testserver/api/v1/projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk/project_branch_stages/QA',
    #         'http://testserver/api/v1/projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk/project_branch_stages/Stage',
    #         'http://testserver/api/v1/projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk/project_branch_stages/Release',
    #      ])

    def testGetProjectBranchStagesAnon(self):
        self._initProject()
        response = self._get('projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk/project_branch_stages')
        self.assertEquals(response.status_code, 401)

    # def testGetProjectBranchStageAnon(self):
    #     self._initProject()
    #     response = self._get('projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk/project_branch_stages/Stage')
    #     self.assertEquals(response.status_code, 200)
    #     stage = xobj.parse(response.content).project_branch_stage
    #     self.failUnlessEqual(stage.name, 'Stage')
    #     self.failUnlessEqual(stage.id, 'http://testserver/api/v1/projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk/project_branch_stages/Stage')
    # 
    #     # Comparing XML is more involved maintenance-wise, avoid doing
    #     # that for collections
    #     self.assertXMLEquals(response.content, testsxml.project_branch_stage_xml)
    
    def testGetProjectBranchStageAnon(self):<commit_msg>Remove commented out parts that have matching function names.<commit_after>#!/usr/bin/python
#
# Copyright (c) 2011 rPath, Inc.
#
# All rights reserved.
#

from mint.django_rest.rbuilder.inventory.tests import XMLTestCase

from mint.django_rest.rbuilder.projects import manager # pyflakes=ignore
from mint.django_rest.rbuilder.projects import models # pyflakes=ignore
from mint.django_rest.rbuilder.projects import testsxml # pyflakes=ignore
from mint.django_rest.rbuilder.repos import manager as reposmanager
from mint.django_rest.rbuilder.manager import basemanager
from mint.django_rest.rbuilder.manager import rbuildermanager
from xobj import xobj
from mint.django_rest.rbuilder.rbac.tests import RbacEngine
from testutils import mock
from mint.django_rest.rbuilder.images import models as imagesmodels
from mint import helperfuncs

class ProjectsTestCase(RbacEngine):
    fixtures = ["projects", "project_image_fixtures"]

    def setUp(self):
        RbacEngine.setUp(self)
        mock.mock(reposmanager.ReposManager, "createRepositoryForProject")
        mock.mock(reposmanager.ReposManager, "createSourceTrove")
        mock.mock(reposmanager.ReposManager, "generateConaryrcFile")
        MockProdDef = mock.MockObject()
        MockProdDef.getImageGroup._mock.setReturn("group-foo-appliance")
        MockProdDef.loadFromRepository._mock.setReturn(MockProdDef)
        mock.mock(basemanager.BaseRbuilderManager, "restDb")
        basemanager.BaseRbuilderManager.restDb.getProductVersionDefinitionFromVersion._mock.setDefaultReturn(MockProdDef)
        mock.mock(manager.ProjectManager, "setProductVersionDefinition")
        self.mgr = rbuildermanager.RbuilderManager()
        self.mintConfig = self.mgr.cfg
        
    def _addProject(self, short_name, namespace='ns'):
        project = models.Project()
        project.name = project.hostname = project.short_name = short_name
        project.namespace = namespace
        project.domain_name = 'test.local2'
        project = self.mgr.projectManager.addProject(project)
        
        return project

    def _initProject(self, name='chater-foo'):
        proj = models.Project.objects.get(name='chater-foo')
        branch = models.ProjectVersion(project=proj, name="trunk", label="ch<EMAIL>@rpath:chater-foo-trunk")
        branch.save()
        stage = models.Stage(project=proj,
            project_branch=branch, name="Development", label="foo@ns:trunk-devel")
        stage.save()
        stage = models.Stage(project=proj,
            project_branch=branch, name="QA", label="foo@ns:trunk-qa")
        stage.save()
        stage = models.Stage(project=proj,
            project_branch=branch, name="Stage", label="foo@ns:trunk-stage")
        stage.save()
        stage = models.Stage(project=proj,
            project_branch=branch, name="Release", label="foo@ns:trunk")
        stage.save()

    def testGetProjectsAdmin(self):
        response = self._get('projects/',
            username="admin", password="password")
        self.assertEquals(response.status_code, 200)
        projects = xobj.parse(response.content).projects.project
        self.assertEquals(len(projects), 4)

    def testGetProjectsUser(self):
        response = self._get('projects/',
            username="admin", password="password")
        self.assertEquals(response.status_code, 200)
        projects = xobj.parse(response.content).projects.project
        self.assertEquals(len(projects), 4)
        self.assertEquals([p.short_name for p in projects],
            [u'chater-foo', u'postgres', u'postgres-private', u'example2'])

    # TODO: this module needs tests to show that if a user has a grant
    # he can get at these resources even if not admin

    def testGetProjectAuthenticatedNonAdmin(self):
        response = self._get('projects/chater-foo/',
            username=self.sysadmin_user.user_name, password='password')
        self.assertEquals(response.status_code, 200)
        
        response = self._get('projects/chater-foo/',
            username=self.developer_user.user_name, password='password')
        self.assertEquals(response.status_code, 403)
        
    def testGetProjectsAnon(self):
        # needs to fail for user w/o rbac/rmember perms
        response = self._get('projects/')
        self.assertEquals(response.status_code, 401)

    def testGetProjectAnon(self):
        response = self._get('projects/chater-foo')
        self.assertEquals(response.status_code, 401)

    def testGetProjectBranchesAnon(self):
        self._initProject()
        response = self._get('projects/chater-foo/project_branches')
        self.assertEquals(response.status_code, 401)

    def testGetProjectBranchAnon(self):
        self._initProject()
        response = self._get('projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk')
        self.assertEquals(response.status_code, 401)

    def testGetProjectBranchStagesAnon(self):
        self._initProject()
        response = self._get('projects/chater-foo/project_branches/chater-foo.eng.rpath.com@rpath:chater-foo-trunk/project_branch_stages')
        self.assertEquals(response.status_code, 401)

    def testGetProjectBranchStageAnon(self):
{'fraction_non_alphanumeric': 0.1151479444880859, 'fraction_numerical': 0.007069913589945012, 'mean_word_length': 3.3314431528210946, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2129375', 'n_tokens_mistral': 4728, 'n_tokens_neox': 4560, 'n_words': 684}