Example #1
0
 def seed(self, i, entities=None):
   user_properties = {
       'key_name': 'user_%04d' % i,
       'link_id': 'user_%04d' % i,
       'account': users.User(email='*****@*****.**' % i),
       'name': 'User %04d' % i,
       }
   entity = User(**user_properties)
   if entities is None:
     entity.put()
   else:
     entities.append(entity)
Example #2
0
 def seed(self, i, entities=None):
     user_properties = {
         'key_name': 'user_%04d' % i,
         'link_id': 'user_%04d' % i,
         'account': users.User(email='*****@*****.**' % i),
         'name': 'User %04d' % i,
     }
     entity = User(**user_properties)
     if entities is None:
         entity.put()
         user_logic._onCreate(entity)
     else:
         entities.append(entity)
Example #3
0
def ensureUser():
  """Returns the current user account and associated user object.
  """

  account = accounts.getCurrentAccount()

  if not account:
    account = users.User(email='*****@*****.**')

  user_properties = {
      'key_name': 'test',
      'link_id': 'test',
      'account': account,
      'name': 'Test',
      }

  current_user = User(**user_properties)
  current_user.put()

  return account, current_user
Example #4
0
def ensureUser():
    """Returns the current user account and associated user object.
  """

    account = accounts.getCurrentAccount()

    if not account:
        account = users.User(email='*****@*****.**')

    user_properties = {
        'key_name': 'test',
        'link_id': 'test',
        'account': account,
        'name': 'Test',
    }

    current_user = User(**user_properties)
    current_user.put()

    return account, current_user
Example #5
0
class CleaningTest(GSoCDjangoTestCase):
  """Tests related to cleaning logic.
  """
  def setUp(self):
    """Set up required for the cleaning logic tests.
    """
    self.init()
    # Ensure that current user is created
    user_properties = {
        'account': users.get_current_user(),
        'link_id': 'current_user',
        'key_name': 'current_user',
        'name': 'Current User',
        }
    self.user = User(**user_properties)
    self.user.put()
    # Create another user
    another_user_properties = {
        'account': users.User(email="*****@*****.**"),
        'link_id': 'another_user',
        'key_name': 'another_user',
        'name': 'Another User',
        }
    self.another_user = User(**another_user_properties)
    self.another_user.put()
    # Create a dummy form object
    self.form = Form()

  def testCleanEmptyField(self):
    """Tests that empty field can be cleaned.
    """
    field_name = 'field'
    clean_field = cleaning.clean_empty_field(field_name)
    # Test that the same value will be returned, the cleaned_data of form
    # does not change and there is no error message if the value of field
    # is not empty
    field_value = 'v1_@?'
    cleaned_data_before = {field_name: field_value}
    self.form.cleaned_data = cleaned_data_before.copy()
    self.assertEqual(clean_field(self.form), field_value)
    self.assertEqual(self.form.cleaned_data, cleaned_data_before)
    self.assertEqual(self.form._errors, {})
    # Test that None will be returned, the cleaned_data of form does not change
    # and there is no error message if the value of field is empty
    field_value = ''
    cleaned_data_before = {field_name: field_value}
    self.form.cleaned_data = cleaned_data_before.copy()
    self.assertEqual(clean_field(self.form), None)
    self.assertEqual(self.form.cleaned_data, cleaned_data_before)
    self.assertEqual(self.form._errors, {})

  def testCleanEmail(self):
    """Tests that an email is cleaned.
    """
    field_name = 'email'
    clean_field = cleaning.clean_email(field_name)
    #Test that the same value is returned, the cleaned_data of the from does
    #not change and there is no error message if the value of the field has a
    #valid email
    field_value = '*****@*****.**'
    cleaned_data_before = {field_name: field_value}
    self.form.cleaned_data = cleaned_data_before.copy()
    self.assertEqual(clean_field(self.form), field_value)
    self.assertEqual(self.form.cleaned_data, cleaned_data_before)
    self.assertEqual(self.form._errors, {})
    #Test that forms.ValidationError is raised if email is not valid.
    field_value = '#$test&*('
    cleaned_data_before = {field_name: field_value}
    self.form.cleaned_data = cleaned_data_before.copy()
    self.assertRaises(forms.ValidationError, clean_field, self.form)
    self.assertEqual(self.form.cleaned_data, cleaned_data_before)
    self.assertEqual(self.form._errors, {})

  def testCleanLinkId(self):
    """Tests that link_id field can be cleaned.
    """
    field_name = 'link_id'
    clean_field = cleaning.clean_link_id(field_name)
    # Test that the value will be returned, the cleaned_data of form does not
    # change and there is no error message if the value of field has a valid
    # link_id format
    field_value = 'valid_link_id'
    cleaned_data_before = {field_name: field_value}
    self.form.cleaned_data = cleaned_data_before.copy()
    self.assertEqual(clean_field(self.form), field_value)
    self.assertEqual(self.form.cleaned_data, cleaned_data_before)
    self.assertEqual(self.form._errors, {})
    # Test that forms.ValidationError will be raised, the cleaned_data of form
    # does not change and there is no error message if the value of field has
    # not a valid link_id format
    field_value = 'v1_@?'
    cleaned_data_before = {field_name: field_value}
    self.form.cleaned_data = cleaned_data_before.copy()
    self.assertRaises(forms.ValidationError, clean_field, self.form)
    self.assertEqual(self.form.cleaned_data, cleaned_data_before)
    self.assertEqual(self.form._errors, {})

  def testCleanScopePath(self):
    """Tests that scope_path field can be cleaned.
    """
    field_name = 'scope_path'
    clean_field = cleaning.clean_scope_path(field_name)
    # Test that the value will be returned if the value of field
    # has a valid scope_path format
    field_value = 'valid_scope_path'
    cleaned_data_before = {field_name: field_value}
    self.form.cleaned_data = cleaned_data_before.copy()
    self.assertEqual(clean_field(self.form), field_value)
    self.assertEqual(self.form.cleaned_data, cleaned_data_before)
    self.assertEqual(self.form._errors, {})
    # Test that forms.ValidationError will be raised if the value of field
    # has not a valid scope_path format
    field_value = 'v1_@?'
    cleaned_data_before = {field_name: field_value}
    self.form.cleaned_data = cleaned_data_before.copy()
    self.assertRaises(forms.ValidationError, clean_field, self.form)
    self.assertEqual(self.form.cleaned_data, cleaned_data_before)
    self.assertEqual(self.form._errors, {})

  def testCleanExistingUser(self):
    """Tests that the user field can be cleaned for existing users.
    """
    field_name = 'user'
    clean_field = cleaning.clean_existing_user(field_name)
    # Test that the user will be returned if the value of field
    # is an existent user's link_id
    field_value = self.user.link_id
    self.form.cleaned_data = {field_name: field_value}
    cleaned_data_after = clean_field(self.form)
    self.assertEqual(cleaned_data_after.link_id, self.user.link_id)
    # Test that forms.ValidationError will be raised if the value of field
    # is not an existent user's link_id
    field_value = 'non_existent_user'
    self.form.cleaned_data = {field_name: field_value}
    self.assertRaises(forms.ValidationError, clean_field, self.form)

  def testCleanUserIsCurrent(self):
    """Tests that the user field can be cleaned for current users.
    """
    field_name = 'user'
    clean_field = cleaning.clean_user_is_current(field_name)
    # Test that the user will be returned if the value of field is
    # an existent user's link_id
    field_value = self.user.link_id
    self.form.cleaned_data = {field_name: field_value}
    cleaned_data_after = clean_field(self.form)
    self.assertEqual(cleaned_data_after.link_id, self.user.link_id)
    # Test that forms.ValidationError will be raised if the value of field
    # is a user's link_id other than the current user's
    field_value = self.another_user.link_id
    self.form.cleaned_data = {field_name: field_value}
    self.assertRaises(forms.ValidationError, clean_field, self.form)
    # Test that forms.ValidationError will be raised if the value of field
    # is not an existent user's link_id
    field_value = 'non_existent_user'
    self.form.cleaned_data = {field_name: field_value}
    self.assertRaises(forms.ValidationError, clean_field, self.form)

  def testCleanUserNotExist(self):
    """Tests that the user field can be cleaned for non-existent users.
    """
    field_name = 'user'
    clean_field = cleaning.clean_user_not_exist(field_name)
    # Test that the value will be returned if the value of field
    # is not an existent user's link_id
    field_value = 'non_existent_user'
    self.form.cleaned_data = {field_name: field_value}
    self.assertEqual(clean_field(self.form), field_value)
    # Test that forms.ValidationError will be raised if the value of field
    # is an existent user's link_id
    field_value = self.user.link_id
    self.form.cleaned_data = {field_name: field_value}
    self.assertRaises(forms.ValidationError, clean_field, self.form)

  def testCleanUsersNotSame(self):
    """Tests that the user field can be cleaned for non current users.
    """
    field_name = 'not_current_user'
    clean_field = cleaning.clean_users_not_same(field_name)
    # Test that forms.ValidationError will be raised if the value of field
    # is the current user's link_id
    field_value = self.user.link_id
    self.form.cleaned_data = {field_name: field_value}
    self.assertRaises(forms.ValidationError, clean_field, self.form)
    # Test that the user will be returned if the value of field is
    # a user's link_id other than the current user
    field_value = self.another_user.link_id
    self.form.cleaned_data = {field_name: field_value}
    self.assertEqual(clean_field(self.form).link_id, self.another_user.link_id)
    # Test that forms.ValidationError will be raised if the value of field
    # is not an existent user's link_id
    field_value = 'non_existent_user'
    self.form.cleaned_data = {field_name: field_value}
    self.assertRaises(forms.ValidationError, clean_field, self.form)

  def testCleanUserAccount(self):
    """Test that user account can be cleaned.
    """
    field_name = 'user_account'
    clean_field = cleaning.clean_user_account(field_name)
    # Test that a new account will be returned if the value of field is
    # a valid new email address
    field_value = '*****@*****.**'
    self.form.cleaned_data = {field_name: field_value}
    cleaned_data_after = clean_field(self.form)
    self.assertEqual(cleaned_data_after.email(), field_value)
    # Test that the existing account will be returned if the value of field is
    # an existent user's email address
    field_value = self.user.account.email()
    self.form.cleaned_data = {field_name: field_value}
    cleaned_data_after = clean_field(self.form)
    self.assertEqual(cleaned_data_after.email(), field_value)
    self.assertEqual(cleaned_data_after, self.user.account)
    # Test that a new account will be returned even if the value of field is
    # an invalid email address
    field_value = 'invalid_*mail'
    self.form.cleaned_data = {field_name: field_value}
    self.assertEqual(clean_field(self.form).email(), field_value)

  def testCleanUserAccountNotInUse(self):
    """Tests that user account can be cleaned for non-existent user accounts.
    """
    field_name = 'user_account_not_in_use'
    clean_field = cleaning.clean_user_account_not_in_use(field_name)
    # Test that a new account will be created and returned
    # if the value of field is a valid new email address
    field_value = '*****@*****.**'
    self.form.cleaned_data = {field_name: field_value}
    cleaned_data_after = clean_field(self.form)
    self.assertEqual(cleaned_data_after.email(), field_value)
    # Test that forms.ValidationError will be raised if the value of field is
    # an existent user's email address
    field_value = self.user.account.email()
    self.form.cleaned_data = {field_name: field_value}
    self.assertRaises(forms.ValidationError, clean_field, self.form)
    # Test that a new account will be created and returned
    # even if the value of field is an invalid email address
    field_value = 'invalid_*mail'
    self.form.cleaned_data = {field_name: field_value}
    cleaned_data_after = clean_field(self.form)
    self.assertEqual(cleaned_data_after.email(), field_value)

  def testCleanValidShippingChars(self):
    """Tests that the shipping fields can be cleaned.
    """
    field_name = 'ascii'
    clean_field = cleaning.clean_valid_shipping_chars(field_name)
    # Test that the value will be returned if the value of field is valid
    field_value = 'ab12'
    self.form.cleaned_data = {field_name: field_value}
    self.assertEqual(clean_field(self.form), field_value)
    # Test that forms.ValidationError will be raised if the value of field
    # is not valid ascii
    field_value = u'\ua000'
    self.form.cleaned_data = {field_name: field_value}
    self.assertRaises(forms.ValidationError, clean_field, self.form)

  def testCleanContentLength(self):
    """Tests that content length can be cleaned.
    """
    field_name = 'content_length'
    clean_field = cleaning.clean_content_length(field_name, 3, 5)
    # Test that the value will be returned if the length of the value of field
    # is within min_length and max_length
    field_value = 'a1&'
    self.form.cleaned_data = {field_name: field_value}
    self.assertEqual(clean_field(self.form), field_value)
    # Test that forms.ValidationError will be raised if the length of the value
    # of field is less than min_length
    field_value = 'ab'
    self.form.cleaned_data = {field_name: field_value}
    self.assertRaises(forms.ValidationError, clean_field, self.form)
    # Test that forms.ValidationError will be raised if the length of the value
    # of field is more than max_length
    field_value = 'ab12&*'
    self.form.cleaned_data = {field_name: field_value}
    self.assertRaises(forms.ValidationError, clean_field, self.form)

  def testCleanPhoneNumber(self):
    """Tests that phone number can be cleaned.
    """
    field_name = 'phone'
    clean_field = cleaning.clean_phone_number(field_name)
    # Test that the phone number will be returned if it contains digits only
    field_value = '0010208636479'
    self.form.cleaned_data = {field_name: field_value}
    self.assertEqual(clean_field(self.form), field_value)
    # Test that forms.ValidationError will be raised if
    # the phone number contains non digits (except '+')
    field_value = '001-020-8636479'
    self.form.cleaned_data[field_name] = field_value
    self.assertRaises(forms.ValidationError, clean_field, self.form)
    # Test that the '+' will be replaced with 00 and then the modified number
    # will be returned if the phone number starts with a '+'
    field_value = '+10208636479'
    self.form.cleaned_data[field_name] = field_value
    expected = '00' + field_value[1:]
    self.assertEqual(clean_field(self.form), expected)
    # Test that forms.ValidationError will be raised if
    # a '+' is in the middle of the phone number
    field_value = '1+0208636479'
    self.form.cleaned_data[field_name] = field_value
    self.assertRaises(forms.ValidationError, clean_field, self.form)
    # Test that forms.ValidationError will be raised if
    # a '+' is at the end of the phone number
    field_value = '10208636479+'
    self.form.cleaned_data[field_name] = field_value
    self.assertRaises(forms.ValidationError, clean_field, self.form)

  def testCleanFeedUrl(self):
    """Tests that feed url can be cleaned.

    Note: unlike other cleaning functions, it has not used a decorator.
    So, the field name 'feed_url' is hardwired in the code.
    """
    field_name = 'feed_url'
    clean_field = cleaning.clean_feed_url(field_name)
    # Test that the value of the feed url field will be returned if
    # the value of the feed url field is an existent feed url
    field_value = 'http://rss.cnn.com/rss/edition.rss'
    self.form.cleaned_data = {field_name: field_value}
    self.assertEqual(clean_field(self.form), field_value)
    # Test that None will be returned if the value of the feed url field is
    # an empty string
    field_value = ''
    self.form.cleaned_data = {field_name: field_value}
    self.assertEqual(clean_field(self.form), None)
    # Test that forms.ValidationError error will be raised if the value of
    # the feed url field is not an existent feed url
    field_value = 'http://example.com/invalidfeed/'
    self.form.cleaned_data = {field_name: field_value}
    self.assertRaises(forms.ValidationError, clean_field, self.form)

  def testCleanHtmlContent(self):
    """Tests that html content can be cleaned.
    """
    field_name = 'html'
    clean_field = cleaning.clean_html_content(field_name)
    # Test that normal html can be cleaned
    expected = html = '<div>f9-+@4</div>'
    self.form.cleaned_data = {field_name: html}
    self.assertEqual(clean_field(self.form), expected)
    # Test that normal html can be cleaned
    html = '<html>f9-+@4</html>'
    self.form.cleaned_data = {field_name: html}
    expected = html[6:-7]
    self.assertEqual(clean_field(self.form), expected)
    # Test that unicode is also supported
    expected = html = u'\ua000'
    self.form.cleaned_data = {field_name: html}
    self.assertEqual(clean_field(self.form), expected)
    # Test that input with scripts will raise forms.ValidationError
    html = '<script></script>'
    self.form.cleaned_data = {field_name: html}
    self.assertRaises(forms.ValidationError, clean_field, self.form)
    # Test that input can contain scripts when the current user is a developer
    self.user.is_developer = True
    self.user.put()
    expected = html = '<script></script>'
    self.form.cleaned_data = {field_name: html}
    self.assertEqual(clean_field(self.form), expected)

  def testCleanUrl(self):
    """Tests that url can be cleaned.
    """
    field_name = 'url'
    clean_field = cleaning.clean_url(field_name)
    # Test that the value of the url field will be returned 
    # if it is a valid url
    field_value = 'http://exampleabc.com/'
    self.form.cleaned_data = {field_name: field_value}
    self.form.fields = {field_name: forms.URLField()}
    self.assertEqual(clean_field(self.form), field_value)
    # Test that None will be returned if the value of the url field 
    # is an empty string
    field_value = ''
    self.form.cleaned_data = {field_name: field_value}
    self.assertEqual(clean_field(self.form), None)
    # Test that forms.ValidationError error will be raised 
    # if the value of the url field is not a valid url
    field_value = 'exampleabc'
    self.form.cleaned_data = {field_name: field_value}
    self.assertRaises(forms.ValidationError, clean_field, self.form)

  def testStr2Set(self):
    """Tests if symbol separated strings are cleaned.
    """
    string_field = 'string_field'
    clean_field = cleaning.str2set(string_field, separator=',')

    string_field_value = "a,b,c"
    cleaned_data_before = {string_field: string_field_value}
    self.form.cleaned_data = cleaned_data_before
    expected = string_field_value.split(',')
    self.assertEqual(clean_field(self.form), expected)

    string_field_value = "a"
    cleaned_data_before = {string_field: string_field_value}
    self.form.cleaned_data = cleaned_data_before
    expected = string_field_value.split()
    self.assertEqual(clean_field(self.form), expected)

    string_field_value = "a b c"
    clean_field = cleaning.str2set(string_field, separator=' ')
    cleaned_data_before = {string_field: string_field_value}
    self.form.cleaned_data = cleaned_data_before
    expected = string_field_value.split()
    self.assertEqual(clean_field(self.form), expected)
    
    string_field_value = "a, b, c, a"
    clean_field = cleaning.str2set(string_field, separator=',')
    cleaned_data_before = {string_field: string_field_value}
    self.form.cleaned_data = cleaned_data_before
    temp = string_field_value.split(',')
    expected = set([char.strip() for char in temp])
    actual = set(clean_field(self.form))
    self.assertEqual(expected, actual)
Example #6
0
class CleaningTest(GSoCDjangoTestCase):
    """Tests related to cleaning logic.
  """
    def setUp(self):
        """Set up required for the cleaning logic tests.
    """
        self.init()
        # Ensure that current user is created
        user_properties = {
            'account': users.get_current_user(),
            'link_id': 'current_user',
            'key_name': 'current_user',
            'name': 'Current User',
        }
        self.user = User(**user_properties)
        self.user.put()
        # Create another user
        another_user_properties = {
            'account': users.User(email="*****@*****.**"),
            'link_id': 'another_user',
            'key_name': 'another_user',
            'name': 'Another User',
        }
        self.another_user = User(**another_user_properties)
        self.another_user.put()
        # Create a dummy form object
        self.form = Form()

    def testCleanEmptyField(self):
        """Tests that empty field can be cleaned.
    """
        field_name = 'field'
        clean_field = cleaning.clean_empty_field(field_name)
        # Test that the same value will be returned, the cleaned_data of form
        # does not change and there is no error message if the value of field
        # is not empty
        field_value = 'v1_@?'
        cleaned_data_before = {field_name: field_value}
        self.form.cleaned_data = cleaned_data_before.copy()
        self.assertEqual(clean_field(self.form), field_value)
        self.assertEqual(self.form.cleaned_data, cleaned_data_before)
        self.assertEqual(self.form._errors, {})
        # Test that None will be returned, the cleaned_data of form does not change
        # and there is no error message if the value of field is empty
        field_value = ''
        cleaned_data_before = {field_name: field_value}
        self.form.cleaned_data = cleaned_data_before.copy()
        self.assertEqual(clean_field(self.form), None)
        self.assertEqual(self.form.cleaned_data, cleaned_data_before)
        self.assertEqual(self.form._errors, {})

    def testCleanEmail(self):
        """Tests that an email is cleaned.
    """
        field_name = 'email'
        clean_field = cleaning.clean_email(field_name)
        #Test that the same value is returned, the cleaned_data of the from does
        #not change and there is no error message if the value of the field has a
        #valid email
        field_value = '*****@*****.**'
        cleaned_data_before = {field_name: field_value}
        self.form.cleaned_data = cleaned_data_before.copy()
        self.assertEqual(clean_field(self.form), field_value)
        self.assertEqual(self.form.cleaned_data, cleaned_data_before)
        self.assertEqual(self.form._errors, {})
        #Test that forms.ValidationError is raised if email is not valid.
        field_value = '#$test&*('
        cleaned_data_before = {field_name: field_value}
        self.form.cleaned_data = cleaned_data_before.copy()
        self.assertRaises(forms.ValidationError, clean_field, self.form)
        self.assertEqual(self.form.cleaned_data, cleaned_data_before)
        self.assertEqual(self.form._errors, {})

    def testCleanLinkId(self):
        """Tests that link_id field can be cleaned.
    """
        field_name = 'link_id'
        clean_field = cleaning.clean_link_id(field_name)
        # Test that the value will be returned, the cleaned_data of form does not
        # change and there is no error message if the value of field has a valid
        # link_id format
        field_value = 'valid_link_id'
        cleaned_data_before = {field_name: field_value}
        self.form.cleaned_data = cleaned_data_before.copy()
        self.assertEqual(clean_field(self.form), field_value)
        self.assertEqual(self.form.cleaned_data, cleaned_data_before)
        self.assertEqual(self.form._errors, {})
        # Test that forms.ValidationError will be raised, the cleaned_data of form
        # does not change and there is no error message if the value of field has
        # not a valid link_id format
        field_value = 'v1_@?'
        cleaned_data_before = {field_name: field_value}
        self.form.cleaned_data = cleaned_data_before.copy()
        self.assertRaises(forms.ValidationError, clean_field, self.form)
        self.assertEqual(self.form.cleaned_data, cleaned_data_before)
        self.assertEqual(self.form._errors, {})

    def testCleanScopePath(self):
        """Tests that scope_path field can be cleaned.
    """
        field_name = 'scope_path'
        clean_field = cleaning.clean_scope_path(field_name)
        # Test that the value will be returned if the value of field
        # has a valid scope_path format
        field_value = 'valid_scope_path'
        cleaned_data_before = {field_name: field_value}
        self.form.cleaned_data = cleaned_data_before.copy()
        self.assertEqual(clean_field(self.form), field_value)
        self.assertEqual(self.form.cleaned_data, cleaned_data_before)
        self.assertEqual(self.form._errors, {})
        # Test that forms.ValidationError will be raised if the value of field
        # has not a valid scope_path format
        field_value = 'v1_@?'
        cleaned_data_before = {field_name: field_value}
        self.form.cleaned_data = cleaned_data_before.copy()
        self.assertRaises(forms.ValidationError, clean_field, self.form)
        self.assertEqual(self.form.cleaned_data, cleaned_data_before)
        self.assertEqual(self.form._errors, {})

    def testCleanExistingUser(self):
        """Tests that the user field can be cleaned for existing users.
    """
        field_name = 'user'
        clean_field = cleaning.clean_existing_user(field_name)
        # Test that the user will be returned if the value of field
        # is an existent user's link_id
        field_value = self.user.link_id
        self.form.cleaned_data = {field_name: field_value}
        cleaned_data_after = clean_field(self.form)
        self.assertEqual(cleaned_data_after.link_id, self.user.link_id)
        # Test that forms.ValidationError will be raised if the value of field
        # is not an existent user's link_id
        field_value = 'non_existent_user'
        self.form.cleaned_data = {field_name: field_value}
        self.assertRaises(forms.ValidationError, clean_field, self.form)

    def testCleanUserIsCurrent(self):
        """Tests that the user field can be cleaned for current users.
    """
        field_name = 'user'
        clean_field = cleaning.clean_user_is_current(field_name)
        # Test that the user will be returned if the value of field is
        # an existent user's link_id
        field_value = self.user.link_id
        self.form.cleaned_data = {field_name: field_value}
        cleaned_data_after = clean_field(self.form)
        self.assertEqual(cleaned_data_after.link_id, self.user.link_id)
        # Test that forms.ValidationError will be raised if the value of field
        # is a user's link_id other than the current user's
        field_value = self.another_user.link_id
        self.form.cleaned_data = {field_name: field_value}
        self.assertRaises(forms.ValidationError, clean_field, self.form)
        # Test that forms.ValidationError will be raised if the value of field
        # is not an existent user's link_id
        field_value = 'non_existent_user'
        self.form.cleaned_data = {field_name: field_value}
        self.assertRaises(forms.ValidationError, clean_field, self.form)

    def testCleanUserNotExist(self):
        """Tests that the user field can be cleaned for non-existent users.
    """
        field_name = 'user'
        clean_field = cleaning.clean_user_not_exist(field_name)
        # Test that the value will be returned if the value of field
        # is not an existent user's link_id
        field_value = 'non_existent_user'
        self.form.cleaned_data = {field_name: field_value}
        self.assertEqual(clean_field(self.form), field_value)
        # Test that forms.ValidationError will be raised if the value of field
        # is an existent user's link_id
        field_value = self.user.link_id
        self.form.cleaned_data = {field_name: field_value}
        self.assertRaises(forms.ValidationError, clean_field, self.form)

    def testCleanUsersNotSame(self):
        """Tests that the user field can be cleaned for non current users.
    """
        field_name = 'not_current_user'
        clean_field = cleaning.clean_users_not_same(field_name)
        # Test that forms.ValidationError will be raised if the value of field
        # is the current user's link_id
        field_value = self.user.link_id
        self.form.cleaned_data = {field_name: field_value}
        self.assertRaises(forms.ValidationError, clean_field, self.form)
        # Test that the user will be returned if the value of field is
        # a user's link_id other than the current user
        field_value = self.another_user.link_id
        self.form.cleaned_data = {field_name: field_value}
        self.assertEqual(
            clean_field(self.form).link_id, self.another_user.link_id)
        # Test that forms.ValidationError will be raised if the value of field
        # is not an existent user's link_id
        field_value = 'non_existent_user'
        self.form.cleaned_data = {field_name: field_value}
        self.assertRaises(forms.ValidationError, clean_field, self.form)

    def testCleanUserAccount(self):
        """Test that user account can be cleaned.
    """
        field_name = 'user_account'
        clean_field = cleaning.clean_user_account(field_name)
        # Test that a new account will be returned if the value of field is
        # a valid new email address
        field_value = '*****@*****.**'
        self.form.cleaned_data = {field_name: field_value}
        cleaned_data_after = clean_field(self.form)
        self.assertEqual(cleaned_data_after.email(), field_value)
        # Test that the existing account will be returned if the value of field is
        # an existent user's email address
        field_value = self.user.account.email()
        self.form.cleaned_data = {field_name: field_value}
        cleaned_data_after = clean_field(self.form)
        self.assertEqual(cleaned_data_after.email(), field_value)
        self.assertEqual(cleaned_data_after, self.user.account)
        # Test that a new account will be returned even if the value of field is
        # an invalid email address
        field_value = 'invalid_*mail'
        self.form.cleaned_data = {field_name: field_value}
        self.assertEqual(clean_field(self.form).email(), field_value)

    def testCleanUserAccountNotInUse(self):
        """Tests that user account can be cleaned for non-existent user accounts.
    """
        field_name = 'user_account_not_in_use'
        clean_field = cleaning.clean_user_account_not_in_use(field_name)
        # Test that a new account will be created and returned
        # if the value of field is a valid new email address
        field_value = '*****@*****.**'
        self.form.cleaned_data = {field_name: field_value}
        cleaned_data_after = clean_field(self.form)
        self.assertEqual(cleaned_data_after.email(), field_value)
        # Test that forms.ValidationError will be raised if the value of field is
        # an existent user's email address
        field_value = self.user.account.email()
        self.form.cleaned_data = {field_name: field_value}
        self.assertRaises(forms.ValidationError, clean_field, self.form)
        # Test that a new account will be created and returned
        # even if the value of field is an invalid email address
        field_value = 'invalid_*mail'
        self.form.cleaned_data = {field_name: field_value}
        cleaned_data_after = clean_field(self.form)
        self.assertEqual(cleaned_data_after.email(), field_value)

    def testCleanValidShippingChars(self):
        """Tests that the shipping fields can be cleaned.
    """
        field_name = 'ascii'
        clean_field = cleaning.clean_valid_shipping_chars(field_name)
        # Test that the value will be returned if the value of field is valid
        field_value = 'ab12'
        self.form.cleaned_data = {field_name: field_value}
        self.assertEqual(clean_field(self.form), field_value)
        # Test that forms.ValidationError will be raised if the value of field
        # is not valid ascii
        field_value = u'\ua000'
        self.form.cleaned_data = {field_name: field_value}
        self.assertRaises(forms.ValidationError, clean_field, self.form)

    def testCleanContentLength(self):
        """Tests that content length can be cleaned.
    """
        field_name = 'content_length'
        clean_field = cleaning.clean_content_length(field_name, 3, 5)
        # Test that the value will be returned if the length of the value of field
        # is within min_length and max_length
        field_value = 'a1&'
        self.form.cleaned_data = {field_name: field_value}
        self.assertEqual(clean_field(self.form), field_value)
        # Test that forms.ValidationError will be raised if the length of the value
        # of field is less than min_length
        field_value = 'ab'
        self.form.cleaned_data = {field_name: field_value}
        self.assertRaises(forms.ValidationError, clean_field, self.form)
        # Test that forms.ValidationError will be raised if the length of the value
        # of field is more than max_length
        field_value = 'ab12&*'
        self.form.cleaned_data = {field_name: field_value}
        self.assertRaises(forms.ValidationError, clean_field, self.form)

    def testCleanPhoneNumber(self):
        """Tests that phone number can be cleaned.
    """
        field_name = 'phone'
        clean_field = cleaning.clean_phone_number(field_name)
        # Test that the phone number will be returned if it contains digits only
        field_value = '0010208636479'
        self.form.cleaned_data = {field_name: field_value}
        self.assertEqual(clean_field(self.form), field_value)
        # Test that forms.ValidationError will be raised if
        # the phone number contains non digits (except '+')
        field_value = '001-020-8636479'
        self.form.cleaned_data[field_name] = field_value
        self.assertRaises(forms.ValidationError, clean_field, self.form)
        # Test that the '+' will be replaced with 00 and then the modified number
        # will be returned if the phone number starts with a '+'
        field_value = '+10208636479'
        self.form.cleaned_data[field_name] = field_value
        expected = '00' + field_value[1:]
        self.assertEqual(clean_field(self.form), expected)
        # Test that forms.ValidationError will be raised if
        # a '+' is in the middle of the phone number
        field_value = '1+0208636479'
        self.form.cleaned_data[field_name] = field_value
        self.assertRaises(forms.ValidationError, clean_field, self.form)
        # Test that forms.ValidationError will be raised if
        # a '+' is at the end of the phone number
        field_value = '10208636479+'
        self.form.cleaned_data[field_name] = field_value
        self.assertRaises(forms.ValidationError, clean_field, self.form)

    def testCleanFeedUrl(self):
        """Tests that feed url can be cleaned.

    Note: unlike other cleaning functions, it has not used a decorator.
    So, the field name 'feed_url' is hardwired in the code.
    """
        field_name = 'feed_url'
        clean_field = cleaning.clean_feed_url(field_name)
        # Test that the value of the feed url field will be returned if
        # the value of the feed url field is an existent feed url
        field_value = 'http://rss.cnn.com/rss/edition.rss'
        self.form.cleaned_data = {field_name: field_value}
        self.assertEqual(clean_field(self.form), field_value)
        # Test that None will be returned if the value of the feed url field is
        # an empty string
        field_value = ''
        self.form.cleaned_data = {field_name: field_value}
        self.assertEqual(clean_field(self.form), None)
        # Test that forms.ValidationError error will be raised if the value of
        # the feed url field is not an existent feed url
        field_value = 'http://example.com/invalidfeed/'
        self.form.cleaned_data = {field_name: field_value}
        self.assertRaises(forms.ValidationError, clean_field, self.form)

    def testCleanHtmlContent(self):
        """Tests that html content can be cleaned.
    """
        field_name = 'html'
        clean_field = cleaning.clean_html_content(field_name)
        # Test that normal html can be cleaned
        expected = html = '<div>f9-+@4</div>'
        self.form.cleaned_data = {field_name: html}
        self.assertEqual(clean_field(self.form), expected)
        # Test that normal html can be cleaned
        html = '<html>f9-+@4</html>'
        self.form.cleaned_data = {field_name: html}
        expected = html[6:-7]
        self.assertEqual(clean_field(self.form), expected)
        # Test that unicode is also supported
        expected = html = u'\ua000'
        self.form.cleaned_data = {field_name: html}
        self.assertEqual(clean_field(self.form), expected)
        # Test that input with scripts will raise forms.ValidationError
        html = '<script></script>'
        self.form.cleaned_data = {field_name: html}
        self.assertRaises(forms.ValidationError, clean_field, self.form)
        # Test that input can contain scripts when the current user is a developer
        self.user.is_developer = True
        self.user.put()
        expected = html = '<script></script>'
        self.form.cleaned_data = {field_name: html}
        self.assertEqual(clean_field(self.form), expected)

    def testCleanUrl(self):
        """Tests that url can be cleaned.
    """
        field_name = 'url'
        clean_field = cleaning.clean_url(field_name)
        # Test that the value of the url field will be returned
        # if it is a valid url
        field_value = 'http://exampleabc.com/'
        self.form.cleaned_data = {field_name: field_value}
        self.form.fields = {field_name: forms.URLField()}
        self.assertEqual(clean_field(self.form), field_value)
        # Test that None will be returned if the value of the url field
        # is an empty string
        field_value = ''
        self.form.cleaned_data = {field_name: field_value}
        self.assertEqual(clean_field(self.form), None)
        # Test that forms.ValidationError error will be raised
        # if the value of the url field is not a valid url
        field_value = 'exampleabc'
        self.form.cleaned_data = {field_name: field_value}
        self.assertRaises(forms.ValidationError, clean_field, self.form)

    def testStr2Set(self):
        """Tests if symbol separated strings are cleaned.
    """
        string_field = 'string_field'
        clean_field = cleaning.str2set(string_field, separator=',')

        string_field_value = "a,b,c"
        cleaned_data_before = {string_field: string_field_value}
        self.form.cleaned_data = cleaned_data_before
        expected = string_field_value.split(',')
        self.assertEqual(clean_field(self.form), expected)

        string_field_value = "a"
        cleaned_data_before = {string_field: string_field_value}
        self.form.cleaned_data = cleaned_data_before
        expected = string_field_value.split()
        self.assertEqual(clean_field(self.form), expected)

        string_field_value = "a b c"
        clean_field = cleaning.str2set(string_field, separator=' ')
        cleaned_data_before = {string_field: string_field_value}
        self.form.cleaned_data = cleaned_data_before
        expected = string_field_value.split()
        self.assertEqual(clean_field(self.form), expected)

        string_field_value = "a, b, c, a"
        clean_field = cleaning.str2set(string_field, separator=',')
        cleaned_data_before = {string_field: string_field_value}
        self.form.cleaned_data = cleaned_data_before
        temp = string_field_value.split(',')
        expected = set([char.strip() for char in temp])
        actual = set(clean_field(self.form))
        self.assertEqual(expected, actual)
Example #7
0
def seed(request, *args, **kwargs):
  """Seeds the datastore with some default values.
  """

  site_properties = {
      'key_name': 'site',
      'link_id': 'site',
      }

  site = Site(**site_properties)
  site.put()

  account = accounts.getCurrentAccount()

  if not account:
    account = users.User(email='*****@*****.**')

  user_properties = {
      'key_name': 'test',
      'link_id': 'test',
      'account': account,
      'name': 'Test',
      }

  current_user = User(**user_properties)
  current_user.put()

  group_properties = {
       'key_name': 'google',
       'link_id': 'google',
       'name': 'Google Inc.',
       'short_name': 'Google',
       'founder': current_user,
       'home_page': 'http://www.google.com',
       'email': '*****@*****.**',
       'description': 'This is the profile for Google.',
       'contact_street': 'Some Street',
       'contact_city': 'Some City',
       'contact_country': 'United States',
       'contact_postalcode': '12345',
       'phone': '1-555-BANANA',
       'status': 'active',
       }

  google = Sponsor(**group_properties)
  google.put()


  role_properties = {
      'key_name': 'google/test',
      'link_id': 'test',
      'public_name': 'test',
      'scope': google,
      'scope_path': 'google',
      'user': current_user,
      'given_name': 'Test',
      'surname': 'Example',
      'name_on_documents': 'Test Example',
      'email': '*****@*****.**',
      'res_street': 'Some Street',
      'res_city': 'Some City',
      'res_state': 'Some State',
      'res_country': 'United States',
      'res_postalcode': '12345',
      'phone': '1-555-BANANA',
      'birth_date': db.DateProperty.now(),
      'agreed_to_tos': True,
      'is_org_admin': True,
      'is_mentor': True,
      }


  current_user.host_for = [google.key()]
  current_user.put()

  google_host = Host(**role_properties)
  google_host.put()

  from datetime import datetime
  from datetime import timedelta

  now = datetime.now()
  before = now - timedelta(365)
  after = now + timedelta(365)

  timeline_properties = {
      'key_name': 'google/gsoc2009',
      'link_id': 'gsoc2009',
      'scope_path': 'google',
      'scope': google,
      'program_start': before,
      'program_end': after,
      'accepted_organization_announced_deadline': before,
      'student_signup_start': before,
      'student_signup_end': after,
  }

  gsoc2009_timeline = GSoCTimeline(**timeline_properties)
  gsoc2009_timeline.put()


  program_properties = {
      'key_name': 'google/gsoc2009',
      'link_id': 'gsoc2009',
      'scope_path': 'google',
      'scope': google,
      'name': 'Google Summer of Code 2009',
      'short_name': 'GSoC 2009',
      'group_label': 'GSOC',
      'description': 'This is the program for GSoC 2009.',
      'apps_tasks_limit': 42,
      'slots': 42,
      'timeline': gsoc2009_timeline,
      'status': 'visible',
      }

  gsoc2009 = GSoCProgram(**program_properties)
  gsoc2009.put()


  timeline_properties.update({
      'key_name': 'google/gsoc2010',
      'link_id': 'gsoc2010',
  })

  gsoc2010_timeline = GSoCTimeline(**timeline_properties)
  gsoc2010_timeline.put()

  program_properties.update({
      'key_name': 'google/gsoc2010',
      'link_id': 'gsoc2010',
      'name': 'Google Summer of Code 2010',
      'description': 'This is the program for GSoC 2010.',
      'short_name': 'GSoC 2010',
      'timeline': gsoc2010_timeline,
  })

  gsoc2010 = GSoCProgram(**program_properties)
  gsoc2010.put()

  timeline_properties = {
        'key_name': 'google/gci2009',
        'link_id': 'gci2009',
        'scope_path': 'google',
        'scope': google,
        'program_start': before,
        'program_end': after,
        'accepted_organization_announced_deadline': before,
        'student_signup_start': before,
        'student_signup_end': after,
        'tasks_publicly_visible': before,
        'task_claim_deadline': after,
        'stop_all_work_deadline': after,
  }

  gci2009_timeline = GCITimeline(**timeline_properties)
  gci2009_timeline.put()


  program_properties.update({
      'key_name': 'google/gci2009',
      'link_id': 'gci2009',
      'name': 'Google Code In Contest 2009',
      'short_name': 'GCI 2009',
      'group_label': 'GCI',
      'description': 'This is the program for GCI 2009.',
      'timeline': gci2009_timeline,
      })

  gci2009 = GCIProgram(**program_properties)
  gci2009.put()

  site.active_program = gci2009
  site.put()


  group_properties.update({
    'key_name': 'google/gci2009/melange',
    'link_id': 'melange',
    'name': 'Melange Development Team',
    'short_name': 'Melange',
    'scope_path': 'google/gci2009',
    'scope': gci2009,
    'home_page': 'http://code.google.com/p/soc',
    'description': 'Melange, share the love!',
    'license_name': 'Apache License',
    'ideas': 'http://code.google.com/p/soc/issues',
    })

  melange = GCIOrganization(**group_properties)
  melange.put()

  group_properties.update({
    'scope_path': 'google/gsoc2009',
    'scope': gsoc2009,
    })

  role_properties.update({
      'key_name': 'google/gsoc2009/test',
      'link_id': 'test',
      'scope_path': 'google/gsoc2009',
      'scope': gsoc2009,
      'program': gsoc2009,
      'parent': current_user,
      })

  profile = GSoCProfile(**role_properties)
  role_properties.pop('parent')

  orgs = []
  for i in range(15):
    group_properties.update({
        'key_name': 'google/gsoc2009/org_%d' % i,
        'link_id': 'org_%d' % i,
        'name': 'Organization %d' % i,
        'short_name': 'Org %d' % i,
        'description': 'Organization %d!' % i,
        })

    entity = GSoCOrganization(**group_properties)
    orgs.append(entity)
    entity.put()

    # Admin (and thus mentor) for the first org
    if i == 0:
      profile.org_admin_for.append(entity.key())
      profile.mentor_for.append(entity.key())
      profile.is_mentor = True
      profile.is_org_admin = True
      profile.put()

    # Mentor for the second org
    if i == 1:
      profile.mentor_for.append(entity.key())
      profile.is_mentor = True
      profile.put()

  role_properties.update({
      'key_name': 'google/gci2009/test',
      'link_id': 'test',
      'scope_path': 'google/gci2009',
      'scope': gci2009,
      'program': gci2009,
      'org_admin_for': [melange.key()],
      'mentor_for': [melange.key()],
      'parent': current_user,
      })

  melange_admin = GCIProfile(**role_properties)
  # TODO: add GCI orgs
  melange_admin.put()

  task_properties = {
      'status': 'Open',
      'modified_by': melange_admin.key(),
      'subscribers': [melange_admin.key()],
      'title': 'Awesomeness',
      'created_by': melange_admin.key(),
      'created_on': now,
      'program': gci2009,
      'time_to_complete': 1337,
      'modified_on': now,
      'org': melange.key(),
      'description': '<p>AWESOME</p>',
      'difficulty_level': DifficultyLevel.MEDIUM,
      'types': ['Code']
  }

  gci_task = GCITask(**task_properties)
  gci_task.put()

  user_properties = {
      'key_name': 'student',
      'link_id': 'student',
      'account': users.User(email='*****@*****.**'),
      'name': 'Student',
      }

  student_user = User(**user_properties)
  student_user.put()

  student_id = 'student'
  student_properties = {
      'key_name': gsoc2009.key().name() + "/" + student_id,
      'link_id': student_id, 
      'scope_path': gsoc2009.key().name(),
      'parent': student_user,
      'scope': gsoc2009,
      'program': gsoc2009,
      'user': student_user,
      'is_student': True,
      'public_name': 'Student',
      'given_name': 'Student',
      'surname': 'Student',
      'birth_date': db.DateProperty.now(),
      'email': '*****@*****.**',
      'im_handle': 'student_im_handle',
      'major': 'test major',
      'name_on_documents': 'Student',
      'res_country': 'United States',
      'res_city': 'city',
      'res_street': 'test street',
      'res_postalcode': '12345',
      'publish_location': True,
      'blog': 'http://www.blog.com/',
      'home_page': 'http://www.homepage.com/',
      'photo_url': 'http://www.photosite.com/thumbnail.png',
      'ship_state': None,
      'tshirt_size': 'XS',
      'tshirt_style': 'male',
      'degree': 'Undergraduate',
      'phone': '1650253000',
      'can_we_contact_you': True, 
      'program_knowledge': 'I heard about this program through a friend.'
      }

  melange_student = GSoCProfile(**student_properties)

  student_info_properties = {
      'key_name': melange_student.key().name(),
      'parent': melange_student,
      'expected_graduation': 2009,
      'program': gsoc2009,
      'school_country': 'United States',
      'school_name': 'Test School',
      'school_home_page': 'http://www.example.com',
      'program': gsoc2009,
  }
  student_info = GSoCStudentInfo(**student_info_properties)
  student_info.put()

  melange_student.student_info = student_info
  melange_student.put()

  user_properties = {
      'key_name': 'student2',
      'link_id': 'student2',
      'account': users.User(email='*****@*****.**'),
      'name': 'Student 2',
      }

  student_user2 = User(**user_properties)
  student_user2.put()
  student_id = 'student2'
  student_properties.update({
      'key_name': gsoc2009.key().name() + "/" + student_id,
      'link_id': student_id,
      'user': student_user2,
      'parent': student_user2,
  })

  melange_student2 = GSoCProfile(**student_properties)
  melange_student2.put()

  project_id = 'test_project'
  project_properties = {
      'key_name':  gsoc2009.key().name() + "/org_1/" + project_id,
      'link_id': project_id, 
      'scope_path': gsoc2009.key().name() + "/org_1",
      'scope': orgs[1].key(),

      'title': 'test project',
      'abstract': 'test abstract',
      'status': 'accepted',
      'student': melange_student,
      'mentor': profile,
      'program':  gsoc2009
       }

  melange_project = StudentProject(**project_properties)
  melange_project.put()
  student_info_properties.update({'number_of_projects': 1,
                                  'project_for_orgs': [orgs[1].key()]})
  student_info = GSoCStudentInfo(**student_info_properties)
  student_info.put()

  melange_student.student_info = student_info
  melange_student.put()

  project_id = 'test_project2'
  project_properties.update({
      'key_name':  gsoc2009.key().name() + "/org_1/" + project_id,
      'link_id': project_id,
      'student': melange_student2,
      'title': 'test project2'
      })
      
  student_info_properties.update({
      'key_name': gsoc2009.key().name() + "/" + student_id,
      'link_id': student_id,
      'parent': melange_student2,
  })
  student_info2 = GSoCStudentInfo(**student_info_properties)
  student_info2.put()

  melange_student2.student_info = student_info2
  melange_student2.put()

  melange_project2 = StudentProject(**project_properties)
  melange_project2.put()

  student_id = 'student'
  student_properties.update({
      'key_name': gci2009.key().name() + '/' + student_id,
      'parent': student_user,
      'scope': gci2009,
      'scope_path': gci2009.key().name(),
  })
  gci_student = GCIProfile(**student_properties)
  gci_student.put()

  student_info_properties.update({
      'key_name': gci_student.key().name(),
      'parent': gci_student,
      'program': gci2009,
  })
  student_info = GCIStudentInfo(**student_info_properties)
  student_info.put()
  gci_student.student_info = student_info
  gci_student.put()

  score_properties = {
      'parent': gci_student,
      'program': gci2009,
      'points': 5,
      'tasks': [gci_task.key()]
      }
  score = GCIScore(**score_properties)
  score.put()

  document_properties = {
      'key_name': 'site/site/home',
      'link_id': 'home',
      'scope_path': 'site',
      'scope': site,
      'prefix': 'site',
      'author': current_user,
      'title': 'Home Page',
      'short_name': 'Home',
      'content': 'This is the Home Page',
      'modified_by': current_user,
      }

  home_document = Document(**document_properties)
  home_document.put()


  document_properties = {
      'key_name': 'user/test/notes',
      'link_id': 'notes',
      'scope_path': 'test',
      'scope': current_user,
      'prefix': 'user',
      'author': current_user,
      'title': 'My Notes',
      'short_name': 'Notes',
      'content': 'These are my notes',
      'modified_by': current_user,
      }

  notes_document = Document(**document_properties)
  notes_document.put()

  site.home = home_document
  site.put()
  # pylint: disable=E1101
  memcache.flush_all()

  return http.HttpResponse('Done')