blob: 959b4fbff3be6e8741c38285d0ce1ac7d164f7e4 [file] [log] [blame]
# Copyright 2015 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 actions required templates."""
import unittest
from django.test import client
from melange.templates import actions_required
from seeder import profile as profile_seeder
from seeder import program as program_seeder
from seeder import sponsor as sponsor_seeder
from seeder import survey as survey_seeder
from seeder import user as user_seeder
from soc.views.helper import request_data
from tests import org_utils
from tests import profile_utils
from tests import timeline_utils
# Mock URLNames object
MOCK_URL_NAMES = object()
class CreateBackupAdminNeededActionItemTest(unittest.TestCase):
"""Unit tests for createBackupAdminNeededActionItem function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
sponsor = sponsor_seeder.seedSponsor()
self.program = program_seeder.seedProgram(sponsor_key=sponsor.key())
app_survey = survey_seeder.seedApplicationSurvey(self.program.key())
self.timeline_helper = timeline_utils.TimelineHelper(
self.program.timeline, app_survey)
self.org = org_utils.seedOrganization(self.program.key())
request_factory = client.RequestFactory()
request = request_factory.get('http://some-unused.url.com/')
kwargs = {
'sponsor': sponsor.key().name(),
'program': self.program.program_id
}
self.data = request_data.RequestData(request, [], kwargs)
def testItemNotCreatedForNonAdmins(self):
"""Tests that items are not created for non-admins."""
self.timeline_helper.orgSignup()
items = actions_required.createBackupAdminNeededActionItem(
self.data, MOCK_URL_NAMES)
# check that items are not created for a user with no account
self.assertListEqual([], items)
# the user has an account now
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
items = actions_required.createBackupAdminNeededActionItem(
self.data, MOCK_URL_NAMES)
# check that items are not created
self.assertListEqual([], items)
# the user has a profile now
profile = profile_seeder.seedProfile(self.program.key(), user=user)
items = actions_required.createBackupAdminNeededActionItem(
self.data, MOCK_URL_NAMES)
# check that items are not created
self.assertListEqual([], items)
# the user is an administrator for another organization that has two admins
other_org = org_utils.seedOrganization(self.program.key())
other_admin = profile_seeder.seedProfile(
self.program.key(), admin_for=[other_org.key])
profile.admin_for.append(other_org.key)
profile.put()
items = actions_required.createBackupAdminNeededActionItem(
self.data, MOCK_URL_NAMES)
# check that items are not created
self.assertListEqual([], items)
def testItemNotCreatedAfterOrgsAnnounced(self):
"""Tests that items are not created after orgs are announced."""
self.timeline_helper.orgsAnnounced()
# create a user who is an administrator for the organization
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile = profile_seeder.seedProfile(
self.program.key(), admin_for=[self.org.key], user=user)
items = actions_required.createBackupAdminNeededActionItem(
self.data, MOCK_URL_NAMES)
# there is only one organization administrator but it is after
# organizations are announced so no items should be created
self.assertListEqual([], items)
def testItemNotCreatedWhenBackupAdminPresent(self):
"""Tests that items are not created if a backup admin is present."""
self.timeline_helper.orgSignup()
# create a user who is an administrator for the organization
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile = profile_seeder.seedProfile(
self.program.key(), admin_for=[self.org.key], user=user)
# create another administrator for the organization
profile_seeder.seedProfile(self.program.key(), admin_for=[self.org.key])
items = actions_required.createBackupAdminNeededActionItem(
self.data, MOCK_URL_NAMES)
# check that items are not created
self.assertListEqual([], items)
def testItemCreatedForMissingBackupAdmin(self):
"""Tests that items are created properly if a backup admin is missing."""
self.timeline_helper.orgSignup()
# create a user who is an administrator for the organization
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile = profile_seeder.seedProfile(
self.program.key(), admin_for=[self.org.key], user=user)
items = actions_required.createBackupAdminNeededActionItem(
self.data, MOCK_URL_NAMES)
# check that one item is created
self.assertEqual(1, len(items))
self.assertIn(
actions_required._BACKUP_ADMIN_NEEDED_ID_PREFIX + self.org.org_id,
[item.identifier for item in items])
# the user is now an administrator for two organizations
other_org = org_utils.seedOrganization(self.program.key())
profile.admin_for.append(other_org.key)
profile.put()
items = actions_required.createBackupAdminNeededActionItem(
self.data, MOCK_URL_NAMES)
# check that two items are created
self.assertEqual(2, len(items))
self.assertIn(
actions_required._BACKUP_ADMIN_NEEDED_ID_PREFIX + self.org.org_id,
[item.identifier for item in items])
self.assertIn(
actions_required._BACKUP_ADMIN_NEEDED_ID_PREFIX + other_org.org_id,
[item.identifier for item in items])
# seed another administrator for the other organization
profile_seeder.seedProfile(self.program.key(), admin_for=[other_org.key])
items = actions_required.createBackupAdminNeededActionItem(
self.data, MOCK_URL_NAMES)
# check that only one item is created now
self.assertEqual(1, len(items))
self.assertIn(
actions_required._BACKUP_ADMIN_NEEDED_ID_PREFIX + self.org.org_id,
[item.identifier for item in items])
class CreateAppResponseNeededActionItemsTest(unittest.TestCase):
"""Unit tests for createAppResponseNeededActionItems function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
sponsor = sponsor_seeder.seedSponsor()
self.program = program_seeder.seedProgram(sponsor_key=sponsor.key())
self.app_survey = survey_seeder.seedApplicationSurvey(self.program.key())
self.timeline_helper = timeline_utils.TimelineHelper(
self.program.timeline, self.app_survey)
self.org = org_utils.seedOrganization(self.program.key())
request_factory = client.RequestFactory()
request = request_factory.get('http://some-unused.url.com/')
kwargs = {
'sponsor': sponsor.key().name(),
'program': self.program.program_id
}
self.data = request_data.RequestData(request, [], kwargs)
def testItemNotCreatedForNonAdmins(self):
"""Tests that items are not created for non-admins."""
self.timeline_helper.orgSignup()
items = actions_required.createAppResponseNeededActionItems(
self.data, MOCK_URL_NAMES)
# check that items are not created for a user with no account
self.assertListEqual([], items)
# the user has an account now
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
items = actions_required.createAppResponseNeededActionItems(
self.data, MOCK_URL_NAMES)
# check that items are not created
self.assertListEqual([], items)
# the user has a profile now
profile = profile_seeder.seedProfile(self.program.key(), user=user)
items = actions_required.createAppResponseNeededActionItems(
self.data, MOCK_URL_NAMES)
# check that items are not created
self.assertListEqual([], items)
# the user is an administrator for another organization that has two admins
other_org = org_utils.seedOrganization(self.program.key())
other_admin = profile_seeder.seedProfile(
self.program.key(), admin_for=[other_org.key])
profile.admin_for.append(other_org.key)
profile.put()
items = actions_required.createAppResponseNeededActionItems(
self.data, MOCK_URL_NAMES)
# check that items are not created
self.assertListEqual([], items)
def testItemNotCreatedAfterOrgsAnnounced(self):
"""Tests that items are not created after orgs are announced."""
self.timeline_helper.orgsAnnounced()
# create a user who is an administrator for the organization
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile = profile_seeder.seedProfile(
self.program.key(), admin_for=[self.org.key], user=user)
items = actions_required.createAppResponseNeededActionItems(
self.data, MOCK_URL_NAMES)
# there is only one organization administrator but it is after
# organizations are announced so no items should be created
self.assertListEqual([], items)
def testItemNotCreatedWhenAppResponseExists(self):
"""Tests that items are not created if application response exists."""
self.timeline_helper.orgSignup()
# create a user who is an administrator for the organization
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile = profile_seeder.seedProfile(
self.program.key(), admin_for=[self.org.key], user=user)
# seed application response for the organization
org_utils.seedApplication(self.org.key, self.app_survey.key())
items = actions_required.createAppResponseNeededActionItems(
self.data, MOCK_URL_NAMES)
# app response exists so no item should be created
self.assertListEqual([], items)
def testItemCreatedForMissingAppResponse(self):
"""Tests that items are created properly if app response is missing."""
self.timeline_helper.orgSignup()
# create a user who is an administrator for the organization
user = user_seeder.seedUser()
profile_utils.loginNDB(user)
profile = profile_seeder.seedProfile(
self.program.key(), admin_for=[self.org.key], user=user)
items = actions_required.createAppResponseNeededActionItems(
self.data, MOCK_URL_NAMES)
# check that one item is created
self.assertEqual(1, len(items))
self.assertIn(
actions_required._APP_RESPONSE_NEEDED_ID_PREFIX + self.org.org_id,
[item.identifier for item in items])
# the user is now an administrator for two organizations
other_org = org_utils.seedOrganization(self.program.key())
profile.admin_for.append(other_org.key)
profile.put()
items = actions_required.createAppResponseNeededActionItems(
self.data, MOCK_URL_NAMES)
# check that two items are created
self.assertEqual(2, len(items))
self.assertIn(
actions_required._APP_RESPONSE_NEEDED_ID_PREFIX + self.org.org_id,
[item.identifier for item in items])
self.assertIn(
actions_required._APP_RESPONSE_NEEDED_ID_PREFIX + other_org.org_id,
[item.identifier for item in items])
# seed application response for the other organization
org_utils.seedApplication(other_org.key, self.app_survey.key())
items = actions_required.createAppResponseNeededActionItems(
self.data, MOCK_URL_NAMES)
# check that only one item is created now
self.assertEqual(1, len(items))
self.assertIn(
actions_required._APP_RESPONSE_NEEDED_ID_PREFIX + self.org.org_id,
[item.identifier for item in items])