| # 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 slot allocation related views.""" |
| |
| import httplib |
| import unittest |
| |
| from melange.request import exception |
| |
| 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 summerofcode.models import slot_transfer as slot_transfer_model |
| from summerofcode.templates import tabs |
| from summerofcode.views import slot_allocation as slot_allocation_view |
| |
| from soc.modules.gsoc.views.helper import request_data |
| |
| from tests import profile_utils |
| from tests import test_utils |
| |
| |
| def _getSlotAllocationDetailsUrl(organization): |
| """Returns URL to 'Slot Allocation Details' page for |
| the specified organization. |
| |
| Args: |
| organization: Organization entity. |
| |
| Returns: |
| The URL to 'Slot Allocation Details' page. |
| """ |
| return '/gsoc/slot_allocation/details/%s' % organization.key.id() |
| |
| |
| _TEST_SLOT_ALLOCATION = 5 |
| _TEST_QUANTITY = 3 |
| _TEST_MESSAGE = u'Test message' |
| |
| class SlotAllocationDetailsPageTest(test_utils.GSoCDjangoTestCase): |
| """Unit tests for SlotAllocationDetailsPage class.""" |
| |
| def setUp(self): |
| """See unittest.TestCase.setUp for specification.""" |
| self.init() |
| self.program.allocations_visible = True |
| self.program.put() |
| |
| def testPageLoads(self): |
| """Tests that the page loads properly.""" |
| user = user_seeder.seedUser() |
| profile_utils.loginNDB(user) |
| profile_seeder.seedProfile( |
| self.program.key(), user=user, admin_for=[self.org.key]) |
| |
| response = self.get(_getSlotAllocationDetailsUrl(self.org)) |
| self.assertResponseOK(response) |
| |
| def testSlotTransferRequestCreated(self): |
| """Tests that a new slot transfer request is created properly.""" |
| user = user_seeder.seedUser() |
| profile_utils.loginNDB(user) |
| profile_seeder.seedProfile( |
| self.program.key(), user=user, admin_for=[self.org.key]) |
| |
| self.org.slot_allocation = _TEST_SLOT_ALLOCATION |
| self.org.put() |
| |
| postdata = { |
| 'quantity': _TEST_QUANTITY, |
| 'org_message': _TEST_MESSAGE, |
| } |
| response = self.post( |
| _getSlotAllocationDetailsUrl(self.org), postdata=postdata) |
| self.assertResponseRedirect( |
| response, _getSlotAllocationDetailsUrl(self.org)) |
| |
| # check that a new slot transfer request |
| slot_transfer = slot_transfer_model.SlotTransfer.query( |
| ancestor=self.org.key).get() |
| self.assertIsNotNone(slot_transfer) |
| self.assertEqual(slot_transfer.status, slot_transfer_model.Status.PENDING) |
| self.assertEqual(slot_transfer.program.to_old_key(), self.program.key()) |
| self.assertEqual(slot_transfer.quantity, _TEST_QUANTITY) |
| self.assertEqual(slot_transfer.org_message, _TEST_MESSAGE) |
| |
| # check that trying to submit a new request is not possible |
| response = self.post( |
| _getSlotAllocationDetailsUrl(self.org), postdata=postdata) |
| self.assertResponseBadRequest(response) |
| |
| def testOrgsTabs(self): |
| """Tests that correct organization related tabs are present in context.""" |
| user = user_seeder.seedUser() |
| profile_utils.loginNDB(user) |
| profile_seeder.seedProfile( |
| self.program.key(), user=user, admin_for=[self.org.key]) |
| |
| response = self.get(_getSlotAllocationDetailsUrl(self.org)) |
| |
| # check that tabs are present in context |
| self.assertIn('tabs', response.context) |
| |
| # check that tab to "Edit Profile" page is the selected one |
| self.assertEqual(response.context['tabs'].selected_tab_id, |
| tabs.ORG_SLOTS_TAB_ID) |
| |
| |
| class SlotAllocationVisibleAccessCheckerTest(unittest.TestCase): |
| """Unit tests for SlotAllocationVisibleAccessChecker class.""" |
| |
| def setUp(self): |
| """See unittest.TestCase.setUp for specification.""" |
| sponsor = sponsor_seeder.seedSponsor() |
| |
| self.program = program_seeder.seedGSoCProgram(sponsor_key=sponsor.key()) |
| |
| user = user_seeder.seedUser() |
| profile_utils.loginNDB(user) |
| self.profile = profile_seeder.seedStudent(self.program, user=user) |
| |
| kwargs = { |
| 'sponsor': sponsor.key().name(), |
| 'program': self.program.program_id |
| } |
| self.data = request_data.RequestData(None, None, kwargs) |
| |
| def testSlotAllocationVisible(self): |
| """Checks that access is granted when slot allocation is visible.""" |
| self.program.allocations_visible = True |
| self.program.put() |
| |
| access_checker = slot_allocation_view.SlotAllocationVisibleAccessChecker() |
| access_checker.checkAccess(self.data, None) |
| |
| def testSlotAllocationNotVisible(self): |
| """Checks that access is granted when slot allocation is not visible.""" |
| self.program.allocations_visible = False |
| self.program.put() |
| |
| access_checker = slot_allocation_view.SlotAllocationVisibleAccessChecker() |
| with self.assertRaises(exception.UserError) as context: |
| access_checker.checkAccess(self.data, None) |
| self.assertEqual(context.exception.status, httplib.FORBIDDEN) |