<commit_before>#################################################################################
#                                                                               #
#                       copyright (c) 2004 ThoughtWorks, Inc.                   #
#                                                                               #
#################################################################################

#this is the one to look for Functional tests 
from OFS.SimpleItem import Item
from Globals import InitializeClass, Persistent
from Acquisition import Implicit
from AccessControl.Role import RoleManager

from Products.CMFCore import utils
from Globals import InitializeClass
from AccessControl import ClassSecurityInfo

import random
import os
import time
import urllib


# Member type definitions


class UserType:
    pass

FTuser = UserType()
FTuser.fullname = 'FTuser'
FTuser.roles = ('Member',)

FTmanageruser = UserType()
FTmanageruser.fullname = 'FTmanageruser'
FTmanageruser.roles = ('Member','Manager',)

userTypeChoices = {'Member': FTuser,
                   'Manager': FTmanageruser                       
                  }

class FunctionalTestTool (Item, Persistent, Implicit, RoleManager):
    """ This tool is useful for server-side setup, tear-down,
    and posting of results during functional test runs."""
    id = 'selenium_ft_tool'
    meta_type= 'Selenium Functional Test Tool'

    security = ClassSecurityInfo() 

    manage_options=(
        Item.manage_options + RoleManager.manage_options
        )
    
    security.declarePublic('setup')
    def setup(self, user_type=None, REQUEST=None):
        """ Setup method to be called at the beginning of each functional test.
            Must be explicitly called from the test (not implicit, yet)
            
            You should disable this in your production code, cause this
            is a big freakin hug security hole when not used
            for dev/testing purposes.
            
        """
        if REQUEST:
            user_type = REQUEST.form.get('user_type')
        
        return self.addUser(user_type)

    security.declarePublic('tearDown')                 
    def tearDown(self, user=None, REQUEST=None):
        """ Tear down method to be called at the end of each functional test
            Must be explicitly called from the test (not implicit, yet)
            
            You should disable this in your production code, cause this
            is a big freakin hug security hole when not used
            for dev/testing purposes.
        """
        if REQUEST:
            user = REQUEST.form.get('user','')   
        
        if user:
            self.deleteUser(user)
            
        return 'tearDown complete'
        
    def addUser(self,user_type=None):
        """ Adds a dummy user for the functional tests. """
        pm = utils.getToolByName(self, 'portal_membership')

        try:
            user = userTypeChoices[user_type]
        except KeyError:
            user = userTypeChoices['Member']
        
        user.username = user.password = '%<PASSWORD>' % (user.fullname, random.randrange(10000))
        user.email = <EMAIL>' % user.username
        user.properties = {'username': user.username
                         ,'fullname': user.fullname
                         ,'email': user.email
                         }

        
        try:
            pm.addMember(user.username, user.password, user.roles, '')
            
            #Yes, the following line of code looks odd, I need to find out why this didn't work:
            #pm.addMember(user.username, user.password, user.roles, '', properties=user.properties)
            pm.getMemberById(user.username).setMemberProperties(user.properties)

            return user.username
        except: #which exception?
            return "ERROR: Couldn't create user"
                     
    def deleteUser(self, user):
        """ Deletes the user from Plone. """
        # Delete the user from acl_users
        self.acl_users._delUsers([user])

        # Delete the users member data
        pm = utils.getToolByName(self, 'portal_memberdata')
        pm.pruneMemberDataContents()
        
        
    security.declarePublic('postResults')         
    def postResults(self, formData, userAgent):
        """ called by test runner to write results to file"""
        browserName = self.getBrowserName(userAgent)

        suiteTable = ''
        if formData.has_key('suite'):<commit_msg>Modified addUser and deleteUser methods to raise exceptions on error.<commit_after>#################################################################################
#                                                                               #
#                       copyright (c) 2004 ThoughtWorks, Inc.                   #
#                                                                               #
#################################################################################

#this is the one to look for Functional tests 
from OFS.SimpleItem import Item
from Globals import InitializeClass, Persistent
from Acquisition import Implicit
from AccessControl.Role import RoleManager

from Products.CMFCore import utils
from Globals import InitializeClass
from AccessControl import ClassSecurityInfo

import random
import os
import time
import urllib


# Member type definitions


class UserType:
    pass

FTuser = UserType()
FTuser.fullname = 'FTuser'
FTuser.roles = ('Member',)

FTmanageruser = UserType()
FTmanageruser.fullname = 'FTmanageruser'
FTmanageruser.roles = ('Member','Manager',)

userTypeChoices = {'Member': FTuser,
                   'Manager': FTmanageruser                       
                  }

class FunctionalTestTool (Item, Persistent, Implicit, RoleManager):
    """ This tool is useful for server-side setup, tear-down,
    and posting of results during functional test runs."""
    id = 'selenium_ft_tool'
    meta_type= 'Selenium Functional Test Tool'

    security = ClassSecurityInfo() 

    manage_options=(
        Item.manage_options + RoleManager.manage_options
        )
    
    security.declarePublic('setup')
    def setup(self, user_type=None, REQUEST=None):
        """ Setup method to be called at the beginning of each functional test.
            Must be explicitly called from the test (not implicit, yet)
            
            You should disable this in your production code, cause this
            is a big freakin hug security hole when not used
            for dev/testing purposes.
            
        """
        if REQUEST:
            user_type = REQUEST.form.get('user_type')
        
        return self.addUser(user_type)

    security.declarePublic('tearDown')                 
    def tearDown(self, user=None, REQUEST=None):
        """ Tear down method to be called at the end of each functional test
            Must be explicitly called from the test (not implicit, yet)
            
            You should disable this in your production code, cause this
            is a big freakin hug security hole when not used
            for dev/testing purposes.
        """
        if REQUEST:
            user = REQUEST.form.get('user','')   
        
        if user:
            self.deleteUser(user)
            
        return 'tearDown complete'
        
    def addUser(self,user_type=None):
        """ Adds a dummy user for the functional tests. """
        pm = utils.getToolByName(self, 'portal_membership')

        try:
            user = userTypeChoices[user_type]
        except KeyError:
            user = userTypeChoices['Member']
        
        user.username = user.password = <PASSWORD>' % (user.fullname, random.randrange(10000))
        user.email = <EMAIL>' % user.username
        user.properties = {'username': user.username
                         ,'fullname': user.fullname
                         ,'email': user.email
                         }

        
        try:
            pm.addMember(user.username, user.password, user.roles, '')
            
            #Yes, the following line of code looks odd, I need to find out why this didn't work:
            #pm.addMember(user.username, user.password, user.roles, '', properties=user.properties)
            pm.getMemberById(user.username).setMemberProperties(user.properties)

            return user.username
        except: #which exception?
            raise "ERROR: Couldn't create user"
                     
    def deleteUser(self, user):
        """ Deletes the user from Plone. """
        try:
            # Delete the user from acl_users
            self.acl_users._delUsers([user])

            # Delete the users member data
            pm = utils.getToolByName(self, 'portal_memberdata')
            pm.pruneMemberDataContents()
        except:
            raise "ERROR: Couldn't delete user with username=%s" % (user)
        
    security.declarePublic('postResults')         
    def postResults(self, formData, userAgent):
        """ called by test runner to write results to file"""
        browserName = self.getBrowserName(userAgent)

        suiteTable = ''
        if formData.has_key('suite'):
{'fraction_non_alphanumeric': 0.11184854749211184, 'fraction_numerical': 0.0019584376020019586, 'mean_word_length': 1.979578606158833, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3740937', 'n_tokens_mistral': 2290, 'n_tokens_neox': 2190, 'n_words': 696}