blob: 2d9bc3705897b462b286c2d6a86f2440a9cc69f8 [file] [log] [blame]
# Copyright 2014 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.
"""Unit tests for connection related views."""
import mock
import httplib
import unittest
from django import http
from django.test import client
from melange.logic import connection as connection_logic
from melange.logic import profile as profile_logic
from melange.models import connection as connection_model
from melange.models import organization as org_model
from melange.request import exception
from melange.views import connection as connection_view
from melange.utils import rich_bool
from seeder import connection as connection_seeder
from seeder import profile as profile_seeder
from seeder import program as program_seeder
from seeder import sponsor as sponsor_seeder
from seeder import user as user_seeder
from soc.views import forms
from soc.views.helper import request_data
from tests import org_utils
from tests import profile_utils
from tests import timeline_utils
TEST_FORM_FACTORY = connection_view.ConnectionFormFactory(
forms.BoundField, 'unused-template-path')
class UnacceptedConnectionsLimitNotReachedAccessCheckerTest(unittest.TestCase):
"""Unit tests for UnacceptedConnectionsLimitNotReachedAccessChecker class."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
sponsor = sponsor_seeder.seedSponsor()
self.program = program_seeder.seedProgram(sponsor_key=sponsor.key())
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
self.profile = profile_seeder.seedProfile(self.program.key(), user=user)
kwargs = {
'sponsor': sponsor.key().name(),
'program': self.program.program_id
}
self.data = request_data.RequestData(None, None, kwargs)
def testLimitReached(self):
"""Tests that access is denied if the limit is reached."""
for _ in range(connection_logic.UNACCEPTED_CONNECTIONS_LIMIT):
org = org_utils.seedOrganization(self.program.key())
connection_seeder.seedConnection(
self.profile.key, org.key, init_by_user=True)
access_checker = (
connection_view.UnacceptedConnectionsLimitNotReachedAccessChecker())
with self.assertRaises(exception.UserError) as context:
access_checker.checkAccess(self.data, None)
self.assertEqual(context.exception.status, httplib.FORBIDDEN)
def testLimitNotReached(self):
"""Tests that access is granted if the limit is not reached."""
access_checker = (
connection_view.UnacceptedConnectionsLimitNotReachedAccessChecker())
access_checker.checkAccess(self.data, None)
class UrlConnectionIsForCurrentUserAccessCheckerTest(unittest.TestCase):
"""Unit tests for UrlConnectionIsForCurrentUserAccessChecker class."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
sponsor = sponsor_seeder.seedSponsor()
self.program = program_seeder.seedProgram(sponsor_key=sponsor.key())
self.organization = org_utils.seedOrganization(self.program.key())
self.user = user_seeder.seedUser()
profile = profile_seeder.seedProfile(
self.program.key(), user=self.user)
connection = connection_seeder.seedConnection(
profile.key, self.organization.key)
kwargs = {
'sponsor': sponsor.key().name(),
'program': self.program.program_id,
'organization': self.organization.org_id,
'user': self.user.user_id,
'id': str(connection.key.id())
}
self.data = request_data.RequestData(None, None, kwargs)
def testConnectedUserAccessGranted(self):
"""Tests that access is granted for the connected user."""
profile_utils.loginNDB(self.user)
access_checker = (
connection_view.UrlConnectionIsForCurrentUserAccessChecker())
access_checker.checkAccess(self.data, None)
def testAnotherUserAccessDenied(self):
"""Tests that another (not connected) user is denied access."""
# seed another user who is currently logged in
other_user = user_seeder.seedUser()
profile_utils.loginNDB(other_user)
access_checker = (
connection_view.UrlConnectionIsForCurrentUserAccessChecker())
with self.assertRaises(exception.UserError) as context:
access_checker.checkAccess(self.data, None)
self.assertEqual(context.exception.status, httplib.FORBIDDEN)
def testNotLoggedInUserAskedToLogIn(self):
"""Tests that login is required."""
# check for not logged-in user with no profile
profile_utils.logout()
access_checker = (
connection_view.UrlConnectionIsForCurrentUserAccessChecker())
with self.assertRaises(exception.LoginRequired) as context:
access_checker.checkAccess(self.data, None)
def testUserWithNoProfileAccessDenied(self):
"""Tests that access for a user with no profile is denied."""
# check for another user who is currently logged in but
# does not have a profile
other_user = user_seeder.seedUser()
profile_utils.loginNDB(other_user)
access_checker = (
connection_view.UrlConnectionIsForCurrentUserAccessChecker())
with self.assertRaises(exception.UserError) as context:
access_checker.checkAccess(self.data, None)
self.assertEqual(context.exception.status, httplib.FORBIDDEN)
def testOrgAdminAccessDenied(self):
"""Tests that org admin for connected organization is denied access."""
# seed another user who is currently logged in
other_user = user_seeder.seedUser()
profile_utils.loginNDB(other_user)
profile_seeder.seedProfile(
self.program.key(), user=other_user, admin_for=[self.organization.key])
access_checker = (
connection_view.UrlConnectionIsForCurrentUserAccessChecker())
with self.assertRaises(exception.UserError) as context:
access_checker.checkAccess(self.data, None)
self.assertEqual(context.exception.status, httplib.FORBIDDEN)
class IsUrlConnectionManageableTest(unittest.TestCase):
"""Unit tests for IsUrlConnectionManageable class."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
sponsor = sponsor_seeder.seedSponsor()
self.program = program_seeder.seedProgram(sponsor_key=sponsor.key())
self.organization = org_utils.seedOrganization(self.program.key())
self.user = user_seeder.seedUser()
profile = profile_seeder.seedProfile(
self.program.key(), user=self.user)
connection = connection_seeder.seedConnection(
profile.key, self.organization.key)
self.kwargs = {
'sponsor': sponsor.key().name(),
'program': self.program.program_id,
'organization': self.organization.org_id,
'user': self.user.user_id,
'id': str(connection.key.id())
}
def testAccessForRejectedOrganization(self):
"""Tests access for a rejected organization."""
self.organization.status = org_model.Status.REJECTED
self.organization.put()
access_checker = connection_view.IsUrlConnectionManageable()
# check that access is denied for POST method
request = client.RequestFactory().post('http://some-unused.url.com/')
data = request_data.RequestData(request, None, self.kwargs)
with self.assertRaises(exception.UserError) as context:
access_checker.checkAccess(data, None)
self.assertEqual(httplib.FORBIDDEN, context.exception.status)
self.assertEqual(
connection_view._CONNECTION_NOT_MANAGEABLE_REJECTED_ORG,
context.exception.message)
# check that access is granted for GET method
request = client.RequestFactory().get('http://some-unused.url.com/')
data = request_data.RequestData(request, None, self.kwargs)
access_checker.checkAccess(data, None)
def testAccessGrantedForNonRejectedOrganization(self):
"""Tests that access is granted for non-rejected organizations."""
access_checker = connection_view.IsUrlConnectionManageable()
for status in org_model.Status:
# rejected organizations are not handled here
if status == org_model.Status.REJECTED:
continue
self.organization.status = status
self.organization.put()
# check that access is granted for POST method
request = client.RequestFactory().post('http://some-unused.url.com/')
data = request_data.RequestData(request, None, self.kwargs)
access_checker.checkAccess(data, None)
# check that access is granted for GET method
request = client.RequestFactory().post('http://some-unused.url.com/')
data = request_data.RequestData(request, None, self.kwargs)
access_checker.checkAccess(data, None)
def testAccessForInactiveProgram(self):
"""Tests access for an inactive program."""
timeline_helper = timeline_utils.TimelineHelper(self.program.timeline, None)
timeline_helper.offSeason()
access_checker = connection_view.IsUrlConnectionManageable()
# check that access is denied for POST method
request = client.RequestFactory().post('http://some-unused.url.com/')
data = request_data.RequestData(request, None, self.kwargs)
with self.assertRaises(exception.UserError) as context:
access_checker.checkAccess(data, None)
self.assertEqual(httplib.FORBIDDEN, context.exception.status)
self.assertEqual(
connection_view._CONNECTION_NOT_MANAGEABLE_INACTIVE_PROGRAM,
context.exception.message)
# check that access is granted for GET method
request = client.RequestFactory().get('http://some-unused.url.com/')
data = request_data.RequestData(request, None, self.kwargs)
access_checker.checkAccess(data, None)
class IsUserOrgAdminForUrlConnectionTest(unittest.TestCase):
"""Unit tests for IsUserOrgAdminForUrlConnection class."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
sponsor = sponsor_seeder.seedSponsor()
self.program = program_seeder.seedProgram(sponsor_key=sponsor.key())
self.organization = org_utils.seedOrganization(self.program.key())
self.user = user_seeder.seedUser()
profile = profile_seeder.seedProfile(
self.program.key(), user=self.user)
connection = connection_seeder.seedConnection(
profile.key, self.organization.key)
kwargs = {
'sponsor': sponsor.key().name(),
'program': self.program.program_id,
'organization': self.organization.org_id,
'user': self.user.user_id,
'id': str(connection.key.id())
}
self.data = request_data.RequestData(None, None, kwargs)
def testOrgAdminAccessGranted(self):
"""Tests that access is granted for org admin for the connected org."""
# seed a user who is currently logged in
other_user = user_seeder.seedUser()
profile_utils.loginNDB(other_user)
profile_seeder.seedProfile(
self.program.key(), user=other_user, admin_for=[self.organization.key])
access_checker = connection_view.IsUserOrgAdminForUrlConnection()
access_checker.checkAccess(self.data, None)
def testConnectedUserAccessDenied(self):
"""Tests that access is denied for connected user."""
profile_utils.loginNDB(self.user)
access_checker = connection_view.IsUserOrgAdminForUrlConnection()
with self.assertRaises(exception.UserError) as context:
access_checker.checkAccess(self.data, None)
self.assertEqual(context.exception.status, httplib.FORBIDDEN)
def testOtherOrgAdminAccessDenied(self):
"""Tests that access is denied for org admin for another org."""
# seed another organization
other_org = org_utils.seedOrganization(self.program.key())
# seed a user who is currently logged in
other_user = user_seeder.seedUser()
profile_utils.loginNDB(other_user)
profile_seeder.seedProfile(
self.program.key(), user=other_user, admin_for=[other_org.key])
access_checker = connection_view.IsUserOrgAdminForUrlConnection()
with self.assertRaises(exception.UserError) as context:
access_checker.checkAccess(self.data, None)
self.assertEqual(context.exception.status, httplib.FORBIDDEN)
class _MockView(object):
"""Simple request handler to be used as a callback for other handlers."""
def get(self, data, access, mutator):
"""See base.RequestHandler.get for specification."""
pass
class UserActionsFormHandlerTest(unittest.TestCase):
"""Unit tests for UserActionsFormHandler class."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.sponsor = sponsor_seeder.seedSponsor()
self.program = program_seeder.seedProgram(sponsor_key=self.sponsor.key())
self.org = org_utils.seedOrganization(self.program.key())
# unused object used as a callback for the handler
self.view = _MockView()
def testUserNoRoleToNoRoleWhileNoRoleOffered(self):
"""Tests NO ROLE if user has no role and no role is offered."""
profile = profile_seeder.seedProfile(self.program.key())
# no role is offered to the user; the user does not request any role
connection = connection_seeder.seedConnection(
profile.key, self.org.key)
old_seen_by_org = connection.seen_by_org
old_seen_by_user = connection.seen_by_user
old_last_modified = connection.last_modified
kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': str(connection.key.id())
}
request = http.HttpRequest()
# the user still does not request any role
request.POST = {'role': connection_model.NO_ROLE}
data = request_data.RequestData(request, None, kwargs)
# assume that the profile is not eligible to have no role
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.TRUE):
handler = connection_view.UserActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.NO_ROLE)
self.assertEqual(connection.org_role, connection_model.NO_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)
def testUserNoRoleToNoRoleWhileMentorRoleOffered(self):
"""Tests NO ROLE if user has no role and mentor role is offered."""
profile = profile_seeder.seedProfile(self.program.key())
# mentor role is offered to the user; the user does not request any role
connection = connection_seeder.seedConnection(
profile.key, self.org.key, org_role=connection_model.MENTOR_ROLE)
old_seen_by_org = connection.seen_by_org
old_seen_by_user = connection.seen_by_user
old_last_modified = connection.last_modified
kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': str(connection.key.id())
}
request = http.HttpRequest()
# the user still does not request any role
request.POST = {'role': connection_model.NO_ROLE}
data = request_data.RequestData(request, None, kwargs)
# assume that the profile is not eligible to have no role
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.TRUE):
handler = connection_view.UserActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.NO_ROLE)
self.assertEqual(connection.org_role, connection_model.MENTOR_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)
def testUserNoRoleToNoRoleWhileOrgAdminRoleOffered(self):
"""Tests NO ROLE if user has no role and org admin role is offered."""
profile = profile_seeder.seedProfile(self.program.key())
# org admin role is offered to the user; the user does not request any role
connection = connection_seeder.seedConnection(
profile.key, self.org.key, org_role=connection_model.ORG_ADMIN_ROLE)
old_seen_by_org = connection.seen_by_org
old_seen_by_user = connection.seen_by_user
old_last_modified = connection.last_modified
kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': str(connection.key.id())
}
request = http.HttpRequest()
# the user still does not request any role
request.POST = {'role': connection_model.NO_ROLE}
data = request_data.RequestData(request, None, kwargs)
# assume that the profile is not eligible to have no role
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.TRUE):
handler = connection_view.UserActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.NO_ROLE)
self.assertEqual(connection.org_role, connection_model.ORG_ADMIN_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)
def testUserNoRoleToRoleWhileNoRoleOffered(self):
"""Tests ROLE if user has no role and no role is offered."""
profile = profile_seeder.seedProfile(self.program.key())
# no role is offered to the user; the user does not request any role
connection = connection_seeder.seedConnection(
profile.key, self.org.key)
old_last_modified = connection.last_modified
kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': str(connection.key.id())
}
request = http.HttpRequest()
# the user requests a role now
request.POST = {'role': connection_model.ROLE}
data = request_data.RequestData(request, None, kwargs)
handler = connection_view.UserActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.NO_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertTrue(connection.seen_by_user)
self.assertFalse(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(connection_logic._USER_REQUESTS_ROLE, message.content)
def testUserNoRoleToRoleWhileMentorRoleOffered(self):
"""Tests ROLE if user has no role and mentor role is offered."""
profile = profile_seeder.seedProfile(self.program.key())
# mentor role is offered to the user; the user does not request any role
connection = connection_seeder.seedConnection(
profile.key, self.org.key, org_role=connection_model.MENTOR_ROLE)
old_last_modified = connection.last_modified
kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': str(connection.key.id())
}
request = http.HttpRequest()
# the user requests a role now
request.POST = {'role': connection_model.ROLE}
data = request_data.RequestData(request, None, kwargs)
handler = connection_view.UserActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.MENTOR_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertTrue(connection.seen_by_user)
self.assertFalse(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(connection_logic._USER_REQUESTS_ROLE, message.content)
def testUserNoRoleToRoleWhileOrgAdminRoleOffered(self):
"""Tests ROLE if user has no role and org admin role is offered."""
profile = profile_seeder.seedProfile(self.program.key())
# org admin role is offered to the user; the user does not request any role
connection = connection_seeder.seedConnection(
profile.key, self.org.key, org_role=connection_model.ORG_ADMIN_ROLE)
old_last_modified = connection.last_modified
kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': str(connection.key.id())
}
request = http.HttpRequest()
# the user requests a role now
request.POST = {'role': connection_model.ROLE}
data = request_data.RequestData(request, None, kwargs)
handler = connection_view.UserActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.ORG_ADMIN_ROLE)
self.assertIn(self.org.key, profile.admin_for)
self.assertIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertTrue(connection.seen_by_user)
self.assertFalse(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(connection_logic._USER_REQUESTS_ROLE, message.content)
def testUserRoleToRoleWhileNoRoleOffered(self):
"""Tests ROLE if user has role and no role is offered."""
profile = profile_seeder.seedProfile(self.program.key())
# no role is offered to the user; the user requests role
connection = connection_seeder.seedConnection(
profile.key, self.org.key, user_role=connection_model.ROLE)
old_seen_by_org = connection.seen_by_org
old_seen_by_user = connection.seen_by_user
old_last_modified = connection.last_modified
kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': str(connection.key.id())
}
request = http.HttpRequest()
# the user still requests a role
request.POST = {'role': connection_model.ROLE}
data = request_data.RequestData(request, None, kwargs)
handler = connection_view.UserActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.NO_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)
def testUserRoleToRoleWhileMentorRoleOffered(self):
"""Tests ROLE if user has role and mentor role is offered."""
# mentor role is offered to the user; the user requests role
profile = profile_seeder.seedProfile(
self.program.key(), mentor_for=[self.org.key])
connection = connection_model.Connection.query(
connection_model.Connection.organization == self.org.key,
ancestor=profile.key).get()
old_seen_by_org = connection.seen_by_org
old_seen_by_user = connection.seen_by_user
old_last_modified = connection.last_modified
kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': str(connection.key.id())
}
request = http.HttpRequest()
# the user still requests a role
request.POST = {'role': connection_model.ROLE}
data = request_data.RequestData(request, None, kwargs)
handler = connection_view.UserActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.MENTOR_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)
def testUserRoleToRoleWhileOrgAdminRoleOffered(self):
"""Tests ROLE if user has role and org admin role is offered."""
# org admin role is offered to the user; the user requests role
profile = profile_seeder.seedProfile(
self.program.key(), admin_for=[self.org.key])
connection = connection_model.Connection.query(
connection_model.Connection.organization == self.org.key,
ancestor=profile.key).get()
old_seen_by_org = connection.seen_by_org
old_seen_by_user = connection.seen_by_user
old_last_modified = connection.last_modified
kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': str(connection.key.id())
}
request = http.HttpRequest()
# the user still requests a role
request.POST = {'role': connection_model.ROLE}
data = request_data.RequestData(request, None, kwargs)
handler = connection_view.UserActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.ORG_ADMIN_ROLE)
self.assertIn(self.org.key, profile.admin_for)
self.assertIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)
def testUserRoleToNoRoleWhileNoRoleOffered(self):
"""Tests NO ROLE if user has role and no role is offered."""
profile = profile_seeder.seedProfile(self.program.key())
# no role is offered to the user; the user requests role
connection = connection_seeder.seedConnection(
profile.key, self.org.key, user_role=connection_model.ROLE)
old_last_modified = connection.last_modified
kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': str(connection.key.id())
}
request = http.HttpRequest()
# the user does not request role anymore
request.POST = {'role': connection_model.NO_ROLE}
data = request_data.RequestData(request, None, kwargs)
# assume that the profile is not eligible to have no role
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.TRUE):
handler = connection_view.UserActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.NO_ROLE)
self.assertEqual(connection.org_role, connection_model.NO_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertTrue(connection.seen_by_user)
self.assertFalse(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(
connection_logic._USER_DOES_NOT_REQUEST_ROLE, message.content)
def testUserRoleToNoRoleWhileMentorRoleOffered(self):
"""Tests NO ROLE if user has role and mentor role is offered."""
# mentor role is offered to the user; the user requests role
profile = profile_seeder.seedProfile(
self.program.key(), mentor_for=[self.org.key])
connection = connection_model.Connection.query(
connection_model.Connection.organization == self.org.key,
ancestor=profile.key).get()
old_seen_by_user = connection.seen_by_user
old_seen_by_org = connection.seen_by_org
old_last_modified = connection.last_modified
kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': str(connection.key.id()),
}
request = http.HttpRequest()
# the user does not request role anymore
request.POST = {'role': connection_model.NO_ROLE}
data = request_data.RequestData(request, None, kwargs)
# assume that mentor is not eligible to quit
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.FALSE):
handler = connection_view.UserActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(),
url='unused')
with self.assertRaises(exception.UserError) as context:
handler.handle(data, None, None)
self.assertEqual(context.exception.status, httplib.BAD_REQUEST)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.MENTOR_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)
# try again but now, the user is eligible to quit
request = http.HttpRequest()
# the user does not request role anymore
request.POST = {'role': connection_model.NO_ROLE}
data = request_data.RequestData(request, None, kwargs)
# assume that mentor is eligible to quit
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.TRUE):
handler = connection_view.UserActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(),
url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.NO_ROLE)
self.assertEqual(connection.org_role, connection_model.MENTOR_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertTrue(connection.seen_by_user)
self.assertFalse(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(
connection_logic._USER_DOES_NOT_REQUEST_ROLE, message.content)
def testUserRoleToNoRoleWhileOrgAdminRoleOffered(self):
"""Tests NO ROLE if user has role and org admin role is offered."""
# org admin role is offered to the user; the user requests role
profile = profile_seeder.seedProfile(
self.program.key(), admin_for=[self.org.key])
connection = connection_model.Connection.query(
connection_model.Connection.organization == self.org.key,
ancestor=profile.key).get()
old_seen_by_user = connection.seen_by_user
old_seen_by_org = connection.seen_by_org
old_last_modified = connection.last_modified
kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': str(connection.key.id())
}
request = http.HttpRequest()
# the user does not request role anymore
request.POST = {'role': connection_model.NO_ROLE}
data = request_data.RequestData(request, None, kwargs)
# assume that mentor is not eligible to quit
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.FALSE):
handler = connection_view.UserActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
with self.assertRaises(exception.UserError) as context:
handler.handle(data, None, None)
self.assertEqual(context.exception.status, httplib.BAD_REQUEST)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.ORG_ADMIN_ROLE)
self.assertIn(self.org.key, profile.admin_for)
self.assertIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)
# try again but now, the user is eligible to quit
request = http.HttpRequest()
# the user does not request role anymore
request.POST = {'role': connection_model.NO_ROLE}
data = request_data.RequestData(request, None, kwargs)
# assume that mentor is eligible to quit
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.TRUE):
handler = connection_view.UserActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.NO_ROLE)
self.assertEqual(connection.org_role, connection_model.ORG_ADMIN_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertTrue(connection.seen_by_user)
self.assertFalse(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(connection_logic._USER_DOES_NOT_REQUEST_ROLE, message.content)
def _generatedMessageContent(org_role, org_admin):
"""Returns part of content of a message that is generated when role offered
by organization changes.
Args:
org_role: new role offered by organization.
org_admin: profile entity of org admin who changed the role
Returns:
a string that is a part of message content that is generated.
"""
return connection_logic._ORG_ROLE_CHANGED % (
connection_model.VERBOSE_ROLE_NAMES[org_role], org_admin.public_name)
class OrgActionsFormHandlerTest(unittest.TestCase):
"""Unit tests for OrgActionsFormHandler class."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.sponsor = sponsor_seeder.seedSponsor()
self.program = program_seeder.seedProgram(sponsor_key=self.sponsor.key())
self.org = org_utils.seedOrganization(self.program.key())
# unused object used as a callback for the handler
self.view = _MockView()
def testIgnoredByOrg(self):
"""Tests that connection may be ignored by the organization."""
profile = profile_seeder.seedProfile(self.program.key())
connection = connection_seeder.seedConnection(
profile.key, self.org.key)
# check that connection is not ignored at this point
self.assertFalse(connection.ignored_by_org)
old_last_modified = connection.last_modified
kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# ignore the connection
request.POST = {'ignored': True, 'role': connection.org_role}
data = request_data.RequestData(request, None, kwargs)
# assume that no role is eligible
# it is sent along with "ignored" data
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.TRUE):
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
handler.handle(data, None, None)
# check that connection is ignored now
connection = connection.key.get()
self.assertTrue(connection.ignored_by_org)
# property has changed, so seen by properties are changed
self.assertFalse(connection.seen_by_user)
self.assertTrue(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
old_last_modified = connection.last_modified
# unignore the connection
request.POST = {'ignored': False, 'role': connection.org_role}
data = request_data.RequestData(request, None, kwargs)
# assume that no role is eligible
# it is sent along with "ignored" data
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.TRUE):
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
handler.handle(data, None, None)
# check that connection is not ignored anymore
connection = connection.key.get()
self.assertFalse(connection.ignored_by_org)
# property has changed, so seen by properties are changed
self.assertFalse(connection.seen_by_user)
self.assertTrue(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
def testNoRoleToNoRoleWhileNoRoleRequested(self):
"""Tests NO ROLE if no role offered and user requests no role."""
profile = profile_seeder.seedProfile(self.program.key())
# user does not request any role from organization
connection = connection_seeder.seedConnection(
profile.key, self.org.key)
old_seen_by_org = connection.seen_by_org
old_seen_by_user = connection.seen_by_user
old_last_modified = connection.last_modified
kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# no role is still offered
request.POST = {'role': connection_model.NO_ROLE}
data = request_data.RequestData(request, None, kwargs)
# assume that no role is eligible
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.TRUE):
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.NO_ROLE)
self.assertEqual(connection.org_role, connection_model.NO_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)
def testNoRoleToNoRoleWhileRoleRequested(self):
"""Tests NO ROLE if no role offered and user requests role."""
profile = profile_seeder.seedProfile(self.program.key())
# user requests role from organization
connection = connection_seeder.seedConnection(
profile.key, self.org.key, user_role=connection_model.ROLE)
old_seen_by_org = connection.seen_by_org
old_seen_by_user = connection.seen_by_user
old_last_modified = connection.last_modified
self.kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# no role is still offered
request.POST = {'role': connection_model.NO_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
# assume that no role is eligible
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.TRUE):
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.NO_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)
def testNoRoleToMentorRoleWhileNoRoleRequested(self):
"""Tests MENTOR ROLE if no role offered and user requests no role."""
profile = profile_seeder.seedProfile(self.program.key())
# user does not request any role from organization
connection = connection_seeder.seedConnection(
profile.key, self.org.key)
old_last_modified = connection.last_modified
self.kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# mentor role is offered now
request.POST = {'role': connection_model.MENTOR_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
# seed an organization administrator who makes the decision
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile_seeder.seedProfile(
self.program.key(), user=user, admin_for=[self.org.key])
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.NO_ROLE)
self.assertEqual(connection.org_role, connection_model.MENTOR_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertFalse(connection.seen_by_user)
self.assertTrue(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(_generatedMessageContent(
connection_model.MENTOR_ROLE, data.ndb_profile), message.content)
def testNoRoleToMentorRoleWhileRoleRequested(self):
"""Tests MENTOR ROLE if no role offered and user requests role."""
profile = profile_seeder.seedProfile(self.program.key())
# user requests role from organization
connection = connection_seeder.seedConnection(
profile.key, self.org.key, user_role=connection_model.ROLE)
old_last_modified = connection.last_modified
self.kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# mentor role is offered now
request.POST = {'role': connection_model.MENTOR_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
# seed an organization administrator who makes the decision
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile_seeder.seedProfile(
self.program.key(), user=user, admin_for=[self.org.key])
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.MENTOR_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertFalse(connection.seen_by_user)
self.assertTrue(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(_generatedMessageContent(
connection_model.MENTOR_ROLE, data.ndb_profile), message.content)
def testNoRoleToOrgAdminRoleWhileNoRoleRequested(self):
"""Tests ORG ADMIN ROLE if no role offered and user requests no role."""
profile = profile_seeder.seedProfile(self.program.key())
# user does not request any role from organization
connection = connection_seeder.seedConnection(
profile.key, self.org.key)
old_last_modified = connection.last_modified
self.kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# org admin role is offered now
request.POST = {'role': connection_model.ORG_ADMIN_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
# seed an organization administrator who makes the decision
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile_seeder.seedProfile(
self.program.key(), user=user, admin_for=[self.org.key])
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.NO_ROLE)
self.assertEqual(connection.org_role, connection_model.ORG_ADMIN_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertFalse(connection.seen_by_user)
self.assertTrue(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(_generatedMessageContent(
connection_model.ORG_ADMIN_ROLE, data.ndb_profile), message.content)
def testNoRoleToOrgAdminRoleWhileRoleRequested(self):
"""Tests ORG ADMIN ROLE if no role offered and user requests role."""
profile = profile_seeder.seedProfile(self.program.key())
# user requests role from organization
connection = connection_seeder.seedConnection(
profile.key, self.org.key, user_role=connection_model.ROLE)
old_last_modified = connection.last_modified
self.kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# org admin role is offered now
request.POST = {'role': connection_model.ORG_ADMIN_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
# seed an organization administrator who makes the decision
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile_seeder.seedProfile(
self.program.key(), user=user, admin_for=[self.org.key])
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.ORG_ADMIN_ROLE)
self.assertIn(self.org.key, profile.admin_for)
self.assertIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertFalse(connection.seen_by_user)
self.assertTrue(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(_generatedMessageContent(
connection_model.ORG_ADMIN_ROLE, data.ndb_profile), message.content)
def testMentorRoleToNoRoleWhileNoRoleRequested(self):
"""Tests NO ROLE if mentor role offered and user requests no role."""
profile = profile_seeder.seedProfile(self.program.key())
# user does not request any role from organization
connection = connection_seeder.seedConnection(
profile.key, self.org.key, org_role=connection_model.MENTOR_ROLE)
old_last_modified = connection.last_modified
self.kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# no role is offered now
request.POST = {'role': connection_model.NO_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
# seed an organization administrator who makes the decision
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile_seeder.seedProfile(
self.program.key(), user=user, admin_for=[self.org.key])
# assume that no role is eligible
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.TRUE):
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.NO_ROLE)
self.assertEqual(connection.org_role, connection_model.NO_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertFalse(connection.seen_by_user)
self.assertTrue(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(_generatedMessageContent(
connection_model.NO_ROLE, data.ndb_profile), message.content)
def testMentorRoleToNoRoleWhileRoleRequested(self):
"""Tests NO ROLE if mentor role offered and user requests role."""
# user is a mentor for organization
profile = profile_seeder.seedProfile(
self.program.key(), mentor_for=[self.org.key])
connection = connection_model.Connection.query(
connection_model.Connection.organization == self.org.key,
ancestor=profile.key).get()
old_seen_by_org = connection.seen_by_org
old_seen_by_user = connection.seen_by_user
old_last_modified = connection.last_modified
self.kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# no role is offered now
request.POST = {'role': connection_model.NO_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
# assume that mentor cannot be removed
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.FALSE):
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
with self.assertRaises(exception.UserError) as context:
handler.handle(data, None, None)
self.assertEqual(context.exception.status, httplib.BAD_REQUEST)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.MENTOR_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)
# now the mentor can be removed
request = http.HttpRequest()
# no role is offered now
request.POST = {'role': connection_model.NO_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
# seed an organization administrator who makes the decision
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile_seeder.seedProfile(
self.program.key(), user=user, admin_for=[self.org.key])
# assume that mentor can be removed
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.TRUE):
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.NO_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertFalse(connection.seen_by_user)
self.assertTrue(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(_generatedMessageContent(
connection_model.NO_ROLE, data.ndb_profile), message.content)
def testMentorRoleToMentorRoleWhileNoRoleRequested(self):
"""Tests MENTOR ROLE if mentor role offered and user requests no role."""
profile = profile_seeder.seedProfile(self.program.key())
# user does not request any role from organization
connection = connection_seeder.seedConnection(
profile.key, self.org.key, org_role=connection_model.MENTOR_ROLE)
old_seen_by_org = connection.seen_by_org
old_seen_by_user = connection.seen_by_user
old_last_modified = connection.last_modified
self.kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# mentor role is offered now
request.POST = {'role': connection_model.MENTOR_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.NO_ROLE)
self.assertEqual(connection.org_role, connection_model.MENTOR_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)
def testMentorRoleToMentorRoleWhileRoleRequested(self):
"""Tests MENTOR ROLE if mentor role offered and user requests role."""
# user is a mentor for organization
profile = profile_seeder.seedProfile(
self.program.key(), mentor_for=[self.org.key])
connection = connection_model.Connection.query(
connection_model.Connection.organization == self.org.key,
ancestor=profile.key).get()
old_seen_by_org = connection.seen_by_org
old_seen_by_user = connection.seen_by_user
old_last_modified = connection.last_modified
self.kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# mentor role is offered now
request.POST = {'role': connection_model.MENTOR_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.MENTOR_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)
def testMentorRoleToOrgAdminRoleWhileNoRoleRequested(self):
"""Tests ORG ADMIN if mentor role offered and user requests no role."""
profile = profile_seeder.seedProfile(self.program.key())
# user does not request any role from organization
connection = connection_seeder.seedConnection(
profile.key, self.org.key, org_role=connection_model.MENTOR_ROLE)
old_last_modified = connection.last_modified
self.kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# org admin role is offered now
request.POST = {'role': connection_model.ORG_ADMIN_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
# seed an organization administrator who makes the decision
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile_seeder.seedProfile(
self.program.key(), user=user, admin_for=[self.org.key])
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.NO_ROLE)
self.assertEqual(connection.org_role, connection_model.ORG_ADMIN_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertFalse(connection.seen_by_user)
self.assertTrue(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(_generatedMessageContent(
connection_model.ORG_ADMIN_ROLE, data.ndb_profile), message.content)
def testMentorRoleToOrgAdminRoleWhileRoleRequested(self):
"""Tests ORG ADMIN if mentor role offered and user requests role."""
# user is a mentor for organization
profile = profile_seeder.seedProfile(
self.program.key(), mentor_for=[self.org.key])
connection = connection_model.Connection.query(
connection_model.Connection.organization == self.org.key,
ancestor=profile.key).get()
old_last_modified = connection.last_modified
self.kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# org admin role is offered now
request.POST = {'role': connection_model.ORG_ADMIN_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
# seed an organization administrator who makes the decision
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile_seeder.seedProfile(
self.program.key(), user=user, admin_for=[self.org.key])
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.ORG_ADMIN_ROLE)
self.assertIn(self.org.key, profile.admin_for)
self.assertIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertFalse(connection.seen_by_user)
self.assertTrue(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(_generatedMessageContent(
connection_model.ORG_ADMIN_ROLE, data.ndb_profile), message.content)
def testOrgAdminRoleToNoRoleWhileNoRoleRequested(self):
"""Tests NO ROLE if org admin offered and user requests no role."""
profile = profile_seeder.seedProfile(self.program.key())
# user does not request any role from organization
connection = connection_seeder.seedConnection(
profile.key, self.org.key, org_role=connection_model.ORG_ADMIN_ROLE)
old_last_modified = connection.last_modified
self.kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# no role is offered now
request.POST = {'role': connection_model.NO_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
# seed an organization administrator who makes the decision
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile_seeder.seedProfile(
self.program.key(), user=user, admin_for=[self.org.key])
# assume that the user is eligible to have no role
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.TRUE):
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.NO_ROLE)
self.assertEqual(connection.org_role, connection_model.NO_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertFalse(connection.seen_by_user)
self.assertTrue(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(_generatedMessageContent(
connection_model.NO_ROLE, data.ndb_profile), message.content)
def testOrgAdminRoleToNoRoleWhileRoleRequested(self):
"""Tests NO ROLE if org admin offered and user requests role."""
# user is an org admin for organization
profile = profile_seeder.seedProfile(
self.program.key(), admin_for=[self.org.key])
connection = connection_model.Connection.query(
connection_model.Connection.organization == self.org.key,
ancestor=profile.key).get()
old_seen_by_org = connection.seen_by_org
old_seen_by_user = connection.seen_by_user
old_last_modified = connection.last_modified
kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# no role is offered now
request.POST = {'role': connection_model.NO_ROLE}
data = request_data.RequestData(request, None, kwargs)
# assume that org admin cannot be removed
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.FALSE):
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
with self.assertRaises(exception.UserError) as context:
handler.handle(data, None, None)
self.assertEqual(context.exception.status, httplib.BAD_REQUEST)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.ORG_ADMIN_ROLE)
self.assertIn(self.org.key, profile.admin_for)
self.assertIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)
# now the mentor can be removed
request = http.HttpRequest()
# no role is offered now
request.POST = {'role': connection_model.NO_ROLE}
data = request_data.RequestData(request, None, kwargs)
# seed an organization administrator who makes the decision
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile_seeder.seedProfile(
self.program.key(), user=user, admin_for=[self.org.key])
# assume that org admin can be removed
with mock.patch.object(
connection_view.RoleEligibilityManager, 'isNoRoleEligibleForOrg',
return_value=rich_bool.TRUE):
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.NO_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertFalse(connection.seen_by_user)
self.assertTrue(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(_generatedMessageContent(
connection_model.NO_ROLE, data.ndb_profile), message.content)
def testOrgAdminRoleToMentorRoleWhileNoRoleRequested(self):
"""Tests MENTOR ROLE if org admin offered and user requests no role."""
profile = profile_seeder.seedProfile(self.program.key())
# user does not request any role from organization
connection = connection_seeder.seedConnection(
profile.key, self.org.key, org_role=connection_model.ORG_ADMIN_ROLE)
old_last_modified = connection.last_modified
self.kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# mentor role is offered now
request.POST = {'role': connection_model.MENTOR_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
# seed an organization administrator who makes the decision
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile_seeder.seedProfile(
self.program.key(), user=user, admin_for=[self.org.key])
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY,
connection_view.RoleEligibilityManager(), url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.NO_ROLE)
self.assertEqual(connection.org_role, connection_model.MENTOR_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertFalse(connection.seen_by_user)
self.assertTrue(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(_generatedMessageContent(
connection_model.MENTOR_ROLE, data.ndb_profile), message.content)
def testOrgAdminRoleToMentorRoleWhileRoleRequested(self):
"""Tests MENTOR ROLE if org admin offered and user requests role."""
# user is an org admin for organization
profile = profile_seeder.seedProfile(
self.program.key(), admin_for=[self.org.key])
connection = connection_model.Connection.query(
connection_model.Connection.organization == self.org.key,
ancestor=profile.key).get()
old_seen_by_org = connection.seen_by_org
old_seen_by_user = connection.seen_by_user
old_last_modified = connection.last_modified
self.kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# mentor role is offered now
request.POST = {'role': connection_model.MENTOR_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
# assume that org admin cannot be removed
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
with mock.patch.object(
profile_logic, 'isMentorRoleEligibleForOrg',
return_value=rich_bool.FALSE):
with self.assertRaises(exception.UserError) as context:
handler.handle(data, None, None)
self.assertEqual(context.exception.status, httplib.BAD_REQUEST)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.ORG_ADMIN_ROLE)
self.assertIn(self.org.key, profile.admin_for)
self.assertIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)
# now the org admin can be removed
request = http.HttpRequest()
# mentor role is offered now
request.POST = {'role': connection_model.MENTOR_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
# seed an organization administrator who makes the decision
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile_seeder.seedProfile(
self.program.key(), user=user, admin_for=[self.org.key])
# assume that org admin can be removed
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
with mock.patch.object(
profile_logic, 'isMentorRoleEligibleForOrg',
return_value=rich_bool.TRUE):
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.MENTOR_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertIn(self.org.key, profile.mentor_for)
# connection changed, so seen by properties are changed
self.assertFalse(connection.seen_by_user)
self.assertTrue(connection.seen_by_org)
self.assertGreater(connection.last_modified, old_last_modified)
# check that a connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIn(_generatedMessageContent(
connection_model.MENTOR_ROLE, data.ndb_profile), message.content)
def testOrgAdminRoleToOrgAdminRoleWhileNoRoleRequested(self):
"""Tests ORG ADMIN if org admin offered and user requests no role."""
profile = profile_seeder.seedProfile(self.program.key())
# user does not request any role from organization
connection = connection_seeder.seedConnection(
profile.key, self.org.key, org_role=connection_model.ORG_ADMIN_ROLE)
old_seen_by_org = connection.seen_by_org
old_seen_by_user = connection.seen_by_user
old_last_modified = connection.last_modified
self.kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# org admin role is offered now
request.POST = {'role': connection_model.ORG_ADMIN_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.NO_ROLE)
self.assertEqual(connection.org_role, connection_model.ORG_ADMIN_ROLE)
self.assertNotIn(self.org.key, profile.admin_for)
self.assertNotIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)
def testOrgAdminRoleToOrgAdminRoleWhileRoleRequested(self):
"""Tests ORG ADMIN if org admin offered and user requests role."""
# user is a org admin for organization
profile = profile_seeder.seedProfile(
self.program.key(), admin_for=[self.org.key])
connection = connection_model.Connection.query(
connection_model.Connection.organization == self.org.key,
ancestor=profile.key).get()
old_seen_by_org = connection.seen_by_org
old_seen_by_user = connection.seen_by_user
old_last_modified = connection.last_modified
self.kwargs = {
'sponsor': self.sponsor.link_id,
'program': self.program.program_id,
'user': profile.profile_id,
'id': connection.key.id()
}
request = http.HttpRequest()
# org admin role is offered now
request.POST = {'role': connection_model.ORG_ADMIN_ROLE}
data = request_data.RequestData(request, None, self.kwargs)
handler = connection_view.OrgActionsFormHandler(
self.view, TEST_FORM_FACTORY, connection_view.RoleEligibilityManager(),
url='unused')
handler.handle(data, None, None)
# check if all data is updated properly
connection = connection.key.get()
profile = profile.key.get()
self.assertEqual(connection.user_role, connection_model.ROLE)
self.assertEqual(connection.org_role, connection_model.ORG_ADMIN_ROLE)
self.assertIn(self.org.key, profile.admin_for)
self.assertIn(self.org.key, profile.mentor_for)
# nothing has changed, so seen by properties are not changed
self.assertEqual(connection.seen_by_user, old_seen_by_user)
self.assertEqual(connection.seen_by_org, old_seen_by_org)
self.assertEqual(old_last_modified, connection.last_modified)
# check that no connection message is created
query = connection_model.ConnectionMessage.query(ancestor=connection.key)
message = query.get()
self.assertIsNone(message)