<commit_before>#!/usr/bin/env python2.5
#
# Copyright 2011 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for invite view.
"""


from nose.plugins import skip

from google.appengine.ext import db

from soc.modules.gsoc.models.request import GSoCRequest

from tests.profile_utils import GSoCProfileHelper
from tests.test_utils import GSoCDjangoTestCase
from tests.test_utils import MailTestCase


class RequestTest(MailTestCase, GSoCDjangoTestCase):
  """Tests request page.
  """

  def setUp(self):
    super(RequestTest, self).setUp()
    self.init()

  def createRequest(self):
    """Creates and returns an accepted invitation for the current user.
    """
    # create other user to send invite to
    other_data = GSoCProfileHelper(self.gsoc, self.dev_test)
    other_data.createOtherUser('<EMAIL>')
    other_data.createProfile()
    request = other_data.createMentorRequest(self.org)

    return (other_data, request)

  def assertRequestTemplatesUsed(self, response):
    """Asserts that all the request templates were used.
    """
    self.assertGSoCTemplatesUsed(response)
    self.assertTemplateUsed(response, 'v2/modules/gsoc/invite/base.html')
    self.assertTemplateUsed(response, 'v2/modules/gsoc/_form.html')

  def testRequestMentor(self):
    admin = GSoCProfileHelper(self.gsoc, self.dev_test)
    admin.createOtherUser('<EMAIL>')
    admin.createOrgAdmin(self.org)
    admin.notificationSettings(new_requests=True)

    other_admin = GSoCProfileHelper(self.gsoc, self.dev_test)
    other_admin.createOtherUser('<EMAIL>')
    other_admin.createOrgAdmin(self.org)
    other_admin.notificationSettings()

    # test GET
    self.data.createProfile()
    url = '/gsoc/request/' + self.org.key().name()
    response = self.get(url)
    self.assertRequestTemplatesUsed(response)

    # test POST
    override = {'status': 'pending', 'role': 'mentor', 'type': 'Request',
                'user': self.data.user, 'org': self.org}
    response, properties = self.modelPost(url, GSoCRequest, override)

    request = GSoCRequest.all().get()
    self.assertPropertiesEqual(properties, request)

    self.assertEmailSent(to=admin.profile.email, n=1)
    self.assertEmailNotSent(to=other_admin.profile.email)

    # test withdrawing a request
    url = '/gsoc/request/%s/%s/%s' % (
        self.gsoc.key().name(),
        request.parent_key().name(),
        request.key().id())

    postdata = {'action': 'Withdraw'}
    response = self.post(url, postdata)
    self.assertResponseRedirect(response)
    request = GSoCRequest.all().get()
    self.assertEqual('withdrawn', request.status)

    # test that you can resubmit
    postdata = {'action': 'Resubmit'}
    response = self.post(url, postdata)
    self.assertResponseRedirect(response)
    request = GSoCRequest.all().get()
    self.assertEqual('pending', request.status)

  def testAcceptRequest(self):
    self.data.createOrgAdmin(self.org)
    other_data, request = self.createRequest()
    other_data.notificationSettings(request_handled=True)
    url = '/gsoc/request/%s/%s/%s' % (
        self.gsoc.key().name(),
        request.parent_key().name(),
        request.key().id())
    response = self.get(url)
    self.assertGSoCTemplatesUsed(response)
    self.assertTemplateUsed(response, 'soc/request/base.html')

    postdata = {'action': 'Reject'}
    response = self.post(url, postdata)
    self.assertResponseRedirect(response)
    request = GSoCRequest.all().get()
    self.assertEqual('rejected', request.status)

    self.assertEmailSent(to=other_data.profile.email, n=1)

    # test that you can change after the fact
    postdata = {'action': 'Accept'}
    response = self.post(url, postdata)

    def checkPostAccept():
      self.assertResponseRedirect(response)
      request = GSoCRequest.all().get()
      self.assertEqual('accepted', request.status)

      profile = db.get(other_data.profile.key())
      self.assertEqual(1, profile.mentor_for.count(self.org.key()))
      self.assertTrue(profile.is_mentor)
      self.assertFalse(profile.is_student)
      self.assertFalse(profile.is_org_admin)
      self.assertFalse(profile.org_admin_for)

    checkPostAccept()

    # TODO(nathaniel): Fix this test bankruptcy.
    raise skip.SkipTest("TODO(nathaniel): test bankruptcy.")

    self.assertEmailSent(to=other_data.profile.email, n=2)

    request.status = 'pending'
    request.put()
    other_data.notificationSettings()

    response = self.post(url, postdata)

    checkPostAccept()
    self.assertEmailSent(to=other_data.profile.email, n=2)<commit_msg>Fixed broken tests in test_request.py.<commit_after>#!/usr/bin/env python2.5
#
# Copyright 2011 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for invite view.
"""


from google.appengine.ext import db

from soc.modules.gsoc.models.request import GSoCRequest

from tests.profile_utils import GSoCProfileHelper
from tests.test_utils import GSoCDjangoTestCase
from tests.test_utils import MailTestCase


class RequestTest(MailTestCase, GSoCDjangoTestCase):
  """Tests request page.
  """

  def setUp(self):
    super(RequestTest, self).setUp()
    self.init()

  def createRequest(self):
    """Creates and returns an accepted invitation for the current user.
    """
    # create other user to send invite to
    other_data = GSoCProfileHelper(self.gsoc, self.dev_test)
    other_data.createOtherUser('<EMAIL>')
    other_data.createProfile()
    request = other_data.createMentorRequest(self.org)

    return (other_data, request)

  def assertRequestTemplatesUsed(self, response):
    """Asserts that all the request templates were used.
    """
    self.assertGSoCTemplatesUsed(response)
    self.assertTemplateUsed(response, 'v2/modules/gsoc/invite/base.html')
    self.assertTemplateUsed(response, 'v2/modules/gsoc/_form.html')

  def testRequestMentor(self):
    admin = GSoCProfileHelper(self.gsoc, self.dev_test)
    admin.createOtherUser('<EMAIL>')
    admin.createOrgAdmin(self.org)
    admin.notificationSettings(new_requests=True)

    other_admin = GSoCProfileHelper(self.gsoc, self.dev_test)
    other_admin.createOtherUser('<EMAIL>')
    other_admin.createOrgAdmin(self.org)
    other_admin.notificationSettings()

    # test GET
    self.data.createProfile()
    url = '/gsoc/request/' + self.org.key().name()
    response = self.get(url)
    self.assertRequestTemplatesUsed(response)

    # test POST
    override = {'status': 'pending', 'role': 'mentor', 'type': 'Request',
                'user': self.data.user, 'org': self.org}
    response, properties = self.modelPost(url, GSoCRequest, override)

    request = GSoCRequest.all().get()
    self.assertPropertiesEqual(properties, request)

    self.assertEmailSent(to=admin.profile.email, n=1)
    self.assertEmailNotSent(to=other_admin.profile.email)

    # test withdrawing a request
    url = '/gsoc/request/%s/%s/%s' % (
        self.gsoc.key().name(),
        request.parent_key().name(),
        request.key().id())

    postdata = {'action': 'Withdraw'}
    response = self.post(url, postdata)
    self.assertResponseRedirect(response)
    request = GSoCRequest.all().get()
    self.assertEqual('withdrawn', request.status)

    # test that you can resubmit
    postdata = {'action': 'Resubmit'}
    response = self.post(url, postdata)
    self.assertResponseRedirect(response)
    request = GSoCRequest.all().get()
    self.assertEqual('pending', request.status)

  def testAcceptRequest(self):
    self.data.createOrgAdmin(self.org)
    other_data, request = self.createRequest()
    other_data.notificationSettings(request_handled=True)
    url = '/gsoc/request/%s/%s/%s' % (
        self.gsoc.key().name(),
        request.parent_key().name(),
        request.key().id())
    response = self.get(url)
    self.assertGSoCTemplatesUsed(response)
    self.assertTemplateUsed(response, 'soc/request/base.html')

    postdata = {'action': 'Reject'}
    response = self.post(url, postdata)
    self.assertResponseRedirect(response)
    request = GSoCRequest.all().get()
    self.assertEqual('rejected', request.status)

    self.assertEmailSent(to=other_data.profile.email, n=1)

    # test that you can change after the fact
    postdata = {'action': 'Accept'}
    response = self.post(url, postdata)

    def checkPostAccept():
      self.assertResponseRedirect(response)
      request = GSoCRequest.all().get()
      self.assertEqual('accepted', request.status)

      profile = db.get(other_data.profile.key())
      self.assertEqual(1, profile.mentor_for.count(self.org.key()))
      self.assertTrue(profile.is_mentor)
      self.assertFalse(profile.is_student)
      self.assertFalse(profile.is_org_admin)
      self.assertFalse(profile.org_admin_for)

    checkPostAccept()

    self.assertEmailSent(to=other_data.profile.email, n=1+2)

    request.status = 'pending'
    request.put()
    other_data.notificationSettings()

    response = self.post(url, postdata)

    checkPostAccept()
    self.assertEmailSent(to=other_data.profile.email, n=1+2+1)
{'fraction_non_alphanumeric': 0.1213993820392704, 'fraction_numerical': 0.0036878301604704477, 'mean_word_length': 3.8970229380185457, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5556503', 'n_tokens_mistral': 3094, 'n_tokens_neox': 3031, 'n_words': 658}