<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 related views.
"""

__authors__ = [
  '"<NAME>" <<EMAIL>>',
  ]


from soc.logic.exceptions import BadRequest
from soc.modules.gci.models.request import GCIRequest

from tests.profile_utils import GCIProfileHelper
from tests.test_utils import GCIDjangoTestCase
from tests.utils.invite_utils import GCIInviteHelper


class BaseInviteTest(GCIDjangoTestCase):
  """Base class for invite tests.
  """ 

  def _invitee(self):
    invitee_data = GCIProfileHelper(self.gci, self.dev_test)
    invitee_data.createOtherUser('<EMAIL>')
    invitee_data.createProfile()
    invitee_data.notificationSettings(new_invites=True)
    return invitee_data.profile


class InviteViewTest(BaseInviteTest):
  """Tests user invite views.
  """

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

  def assertInviteTemplatesUsed(self, response):
    """Asserts that all the templates are used.
    """
    self.assertGCITemplatesUsed(response)
    self.assertTemplateUsed(response, 'v2/modules/gci/invite/base.html')

  def testLoggedInCannotInvite(self):
    url = self._inviteMentorUrl()
    response = self.get(url)
    self.assertResponseForbidden(response)

    url = self._inviteOrgAdminUrl()
    response = self.get(url)
    self.assertResponseForbidden(response)

  def testUserCannotInvite(self):
    self.data.createUser()

    url = self._inviteMentorUrl()
    response = self.get(url)
    self.assertResponseForbidden(response)

    url = self._inviteOrgAdminUrl()
    response = self.get(url)
    self.assertResponseForbidden(response)

  def testMentorCannotInvite(self):
    self.data.createMentor(self.org)

    url = self._inviteMentorUrl()
    response = self.get(url)
    self.assertResponseForbidden(response)

    url = self._inviteOrgAdminUrl()
    response = self.get(url)
    self.assertResponseForbidden(response)

  def testOrgAdminCanInvite(self):
    self.data.createOrgAdmin(self.org)

    url = self._inviteMentorUrl()
    response = self.get(url)
    self.assertInviteTemplatesUsed(response)

    url = self._inviteOrgAdminUrl()
    response = self.get(url)
    self.assertInviteTemplatesUsed(response)

  def testInviteOrgAdmin(self):
    self.data.createOrgAdmin(self.org)

    invitee = self._invitee()
    
    post_data = {
        'identifiers': invitee.user.link_id,
        }
    response = self.post(self._inviteOrgAdminUrl(), post_data)
    self.assertResponseRedirect(response,
        '/gci/dashboard/%s' % self.gci.key().name())

    invite = GCIRequest.all().get()
    self.assertPropertiesEqual(self._defaultOrgAdminInviteProperties(), invite)

  def testInviteMentor(self):
    self.data.createOrgAdmin(self.org)

    invitee = self._invitee()
    
    post_data = {
        'identifiers': invitee.user.link_id,
        }
    response = self.post(self._inviteMentorUrl(), post_data)
    self.assertResponseRedirect(response,
        '/gci/dashboard/%s' % self.gci.key().name())

    invite = GCIRequest.all().get()
    self.assertPropertiesEqual(self._defaultMentorInviteProperties(), invite)

  def testSecondInviteForbidden(self):
    self.data.createOrgAdmin(self.org)
    invitee = self._invitee()

    GCIInviteHelper().createMentorInvite(self.org, invitee.user)

    post_data = {
        'identifiers': invitee.user.link_id,
        }
    response = self.post(self._inviteMentorUrl(), post_data)
    self.assertResponseOK(response)
    self._assertFormValidationError(response, 'identifiers')

  def testOrgAdminInviteAfterMentorInvite(self):
    self.data.createOrgAdmin(self.org)
    invitee = self._invitee()

    GCIInviteHelper().createMentorInvite(self.org, invitee.user)

    post_data = {
        'identifiers': invitee.user.link_id,
        }
    response = self.post(self._inviteOrgAdminUrl(), post_data)
    self.assertResponseRedirect(response,
        '/gci/dashboard/%s' % self.gci.key().name())

    invite = GCIRequest.all().filter('role =', 'org_admin').get()
    self.assertPropertiesEqual(self._defaultOrgAdminInviteProperties(), invite)

  def testMentorInviteAfterOrgAdminInvite(self):
    # TODO(dhans): this test should fail in the future:
    # a existing mentor invite should be extended to become org_admin one

    self.data.createOrgAdmin(self.org)
    invitee = self._invitee()

    GCIInviteHelper().createOrgAdminInvite(self.org, invitee.user)

    post_data = {
        'identifiers': invitee.user.link_id,
        }
    response = self.post(self._inviteMentorUrl(), post_data)
    self.assertResponseRedirect(response,
        '/gci/dashboard/%s' % self.gci.key().name())

    invite = GCIRequest.all().filter('role =', 'mentor').get()
    self.assertPropertiesEqual(self._defaultMentorInviteProperties(), invite)

  def _invitee(self):
    invitee_data = GCIProfileHelper(self.gci, self.dev_test)<commit_msg>testInviteByEmailAddress added.<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 related views.
"""

__authors__ = [
  '"<NAME>" <<EMAIL>>',
  ]


from soc.logic.exceptions import BadRequest
from soc.modules.gci.models.request import GCIRequest

from tests.profile_utils import GCIProfileHelper
from tests.test_utils import GCIDjangoTestCase
from tests.utils.invite_utils import GCIInviteHelper


class BaseInviteTest(GCIDjangoTestCase):
  """Base class for invite tests.
  """ 

  def _invitee(self):
    invitee_data = GCIProfileHelper(self.gci, self.dev_test)
    invitee_data.createOtherUser('<EMAIL>')
    invitee_data.createProfile()
    invitee_data.notificationSettings(new_invites=True)
    return invitee_data.profile


class InviteViewTest(BaseInviteTest):
  """Tests user invite views.
  """

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

  def assertInviteTemplatesUsed(self, response):
    """Asserts that all the templates are used.
    """
    self.assertGCITemplatesUsed(response)
    self.assertTemplateUsed(response, 'v2/modules/gci/invite/base.html')

  def testLoggedInCannotInvite(self):
    url = self._inviteMentorUrl()
    response = self.get(url)
    self.assertResponseForbidden(response)

    url = self._inviteOrgAdminUrl()
    response = self.get(url)
    self.assertResponseForbidden(response)

  def testUserCannotInvite(self):
    self.data.createUser()

    url = self._inviteMentorUrl()
    response = self.get(url)
    self.assertResponseForbidden(response)

    url = self._inviteOrgAdminUrl()
    response = self.get(url)
    self.assertResponseForbidden(response)

  def testMentorCannotInvite(self):
    self.data.createMentor(self.org)

    url = self._inviteMentorUrl()
    response = self.get(url)
    self.assertResponseForbidden(response)

    url = self._inviteOrgAdminUrl()
    response = self.get(url)
    self.assertResponseForbidden(response)

  def testOrgAdminCanInvite(self):
    self.data.createOrgAdmin(self.org)

    url = self._inviteMentorUrl()
    response = self.get(url)
    self.assertInviteTemplatesUsed(response)

    url = self._inviteOrgAdminUrl()
    response = self.get(url)
    self.assertInviteTemplatesUsed(response)

  def testInviteOrgAdmin(self):
    self.data.createOrgAdmin(self.org)

    invitee = self._invitee()
    
    post_data = {
        'identifiers': invitee.user.link_id,
        }
    response = self.post(self._inviteOrgAdminUrl(), post_data)
    self.assertResponseRedirect(response,
        '/gci/dashboard/%s' % self.gci.key().name())

    invite = GCIRequest.all().get()
    self.assertPropertiesEqual(self._defaultOrgAdminInviteProperties(), invite)

  def testInviteMentor(self):
    self.data.createOrgAdmin(self.org)

    invitee = self._invitee()
    
    post_data = {
        'identifiers': invitee.user.link_id,
        }
    response = self.post(self._inviteMentorUrl(), post_data)
    self.assertResponseRedirect(response,
        '/gci/dashboard/%s' % self.gci.key().name())

    invite = GCIRequest.all().get()
    self.assertPropertiesEqual(self._defaultMentorInviteProperties(), invite)

  def testSecondInviteForbidden(self):
    self.data.createOrgAdmin(self.org)
    invitee = self._invitee()

    GCIInviteHelper().createMentorInvite(self.org, invitee.user)

    post_data = {
        'identifiers': invitee.user.link_id,
        }
    response = self.post(self._inviteMentorUrl(), post_data)
    self.assertResponseOK(response)
    self._assertFormValidationError(response, 'identifiers')

  def testOrgAdminInviteAfterMentorInvite(self):
    self.data.createOrgAdmin(self.org)
    invitee = self._invitee()

    GCIInviteHelper().createMentorInvite(self.org, invitee.user)

    post_data = {
        'identifiers': invitee.user.link_id,
        }
    response = self.post(self._inviteOrgAdminUrl(), post_data)
    self.assertResponseRedirect(response,
        '/gci/dashboard/%s' % self.gci.key().name())

    invite = GCIRequest.all().filter('role =', 'org_admin').get()
    self.assertPropertiesEqual(self._defaultOrgAdminInviteProperties(), invite)

  def testMentorInviteAfterOrgAdminInvite(self):
    # TODO(dhans): this test should fail in the future:
    # a existing mentor invite should be extended to become org_admin one

    self.data.createOrgAdmin(self.org)
    invitee = self._invitee()

    GCIInviteHelper().createOrgAdminInvite(self.org, invitee.user)

    post_data = {
        'identifiers': invitee.user.link_id,
        }
    response = self.post(self._inviteMentorUrl(), post_data)
    self.assertResponseRedirect(response,
        '/gci/dashboard/%s' % self.gci.key().name())

    invite = GCIRequest.all().filter('role =', 'mentor').get()
    self.assertPropertiesEqual(self._defaultMentorInviteProperties(), invite)

  def testInviteByEmailAddress(self):
    self.data.createOrgAdmin(self.org)
    self._invitee()

    post_data = {
        'identifiers': '<EMAIL>'
        }
    response = self.post(self._inviteMentorUrl(), post_data)
    self.assertResponseRedirect(response,
        '/gci/dashboard/%s' % self.gci.key().name())

    invite = GCIRequest.all().get()
    self.assertPropertiesEqual(self._defaultMentorInviteProperties(), invite)

  def _invitee(self):
    invitee_data = GCIProfileHelper(self.gci, self.dev_test)
{'fraction_non_alphanumeric': 0.11359026369168357, 'fraction_numerical': 0.001940206367404533, 'mean_word_length': 3.9606299212598426, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '1781542', 'n_tokens_mistral': 3682, 'n_tokens_neox': 3598, 'n_words': 663}