Ejemplo n.º 1
0
ocledger = OcLedger()
data = {}

for user in users:
    try:
        us = user.usersettings
    except UserSettings.DoesNotExist:
        continue

    ocuser = OcUser(user.id)
    redemptions = ocuser.get_offers_redeemed()
    redemptions_req = filter(lambda x: x.action_type == 'req', redemptions)
    redemptions_app = filter(lambda x: x.action_type == 'app', redemptions)

    hours_req = ocuser.get_hours_requested()
    hours_app = ocuser.get_hours_approved()

    print user.id
    data[user.id] = {
        'email':
        user.email,
        'first_name':
        user.first_name.encode('utf-8'),
        'last_name':
        user.last_name.encode('utf-8'),
        'connector':
        user.usersettings.popup_reaction,
        'monthly_updates':
        user.usersettings.monthly_updates,
        'verified':
        user.has_usable_password(),
Ejemplo n.º 2
0
class TestApproveHoursOneWeek(TestCase):
    """Collection of tests for time records within one week."""
    def setUp(self):
        """Set testing environment."""
        # creating org
        self.org1 = OcOrg().setup_org(name="NPF_org_1", status="npf")

        # creating volunteers
        self.volunteer_1 = _create_test_user('volunteer_1')
        self.volunteer_2 = _create_test_user('volunteer_2')

        # getting full names of volunteers
        self.volunteer_1_full_name = self.volunteer_1.first_name + ' ' + self.volunteer_1.last_name
        self.volunteer_2_full_name = self.volunteer_2.first_name + ' ' + self.volunteer_2.last_name

        # creating an admins for NPF_orgs
        self.npf_admin_1 = _create_test_user('npf_admin_1',
                                             org=self.org1,
                                             is_org_admin=True)

        # creating a project
        self.project_1 = _create_project(self.org1, 'org1_project_1')

        # 1st event time = 3 hours
        datetime_start_1 = timezone.now() - timedelta(hours=4)
        datetime_end_1 = datetime_start_1 + timedelta(hours=3)
        # 2nd event time = 2 hours
        datetime_start_2 = timezone.now() - timedelta(hours=4)
        datetime_end_2 = datetime_start_2 + timedelta(hours=2)

        # setting 2 pending events
        _setup_volunteer_hours(self.volunteer_1, self.npf_admin_1, self.org1,
                               self.project_1, datetime_start_1,
                               datetime_end_1)

        _setup_volunteer_hours(
            self.volunteer_2,
            self.npf_admin_1,
            self.org1,
            self.project_1,
            datetime_start_2,
            datetime_end_2,
        )

        # getting previous week start
        self.monday = (
            timezone.now() -
            timedelta(days=timezone.now().weekday())).strftime("%-m-%-d-%Y")

        # oc instances
        self.oc_vol_1 = OcUser(self.volunteer_1.id)
        self.oc_vol_2 = OcUser(self.volunteer_2.id)

        # user entities
        self.user_enitity_id_vol_1 = UserEntity.objects.get(
            user=self.volunteer_1).id
        self.user_enitity_id_vol_2 = UserEntity.objects.get(
            user=self.volunteer_2).id

        # setting up client
        self.client = Client()
        self.client.login(username=self.npf_admin_1.username,
                          password='******')
        self.response = self.client.get('/approve-hours/')

    def test_logged_hours_displayed(self):

        self.assertEqual(self.response.status_code, 200)

        # digging into response dictionaries
        for k in self.response.context[0]['week'][0]:

            self.assertEqual(2, len(self.response.context[0]['week'][0][k]))

            self.assertEqual(
                3,
                len(self.response.context[0]['week'][0][k][
                    self.volunteer_1.email]))
            self.assertEqual(
                3.0, self.response.context[0]['week'][0][k][
                    self.volunteer_1.email]['Total'])
            self.assertEqual(
                self.volunteer_1_full_name, self.response.context[0]['week'][0]
                [k][self.volunteer_1.email]['name'])

            self.assertEqual(
                3,
                len(self.response.context[0]['week'][0][k][
                    self.volunteer_2.email]))
            self.assertEqual(
                2.0, self.response.context[0]['week'][0][k][
                    self.volunteer_2.email]['Total'])
            self.assertEqual(
                self.volunteer_2_full_name, self.response.context[0]['week'][0]
                [k][self.volunteer_2.email]['name'])

    def test_logged_hours_accept(self):

        self.assertEqual(self.response.status_code, 200)

        org_admin_response = self.client.get('/org-admin/')
        self.assertEqual(org_admin_response.status_code, 200)

        # checking pending hours before approving
        self.assertDictEqual(
            org_admin_response.context['hours_pending_by_admin'],
            {self.npf_admin_1: 5.0})
        self.assertEqual(
            0, len(AdminActionUserTime.objects.filter(action_type='app')))
        self.assertEqual(
            2, len(AdminActionUserTime.objects.filter(action_type='req')))

        # checking total approved hours
        self.assertEqual(org_admin_response.context['issued_by_all'], 0)

        # checking initial balance
        self.assertEqual(0, len(self.oc_vol_1.get_hours_approved()))
        self.assertEqual(0, OcLedger().get_balance(self.user_enitity_id_vol_1))
        self.assertEqual(0, len(self.oc_vol_2.get_hours_approved()))
        self.assertEqual(0, OcLedger().get_balance(self.user_enitity_id_vol_2))

        # approving hours
        self.response = self.client.post(
            '/approve-hours/', {
                'post-data':
                self.volunteer_1.username + ':1:' + self.monday + ',' +
                self.volunteer_2.username + ':1:' + self.monday
            })

        # return to org-amdin after approving
        self.assertRedirects(self.response, '/org-admin/2/0/', status_code=302)

        # assert the creation of the corresponding usertimelog and adminaction records
        self.assertEqual(
            2, len(AdminActionUserTime.objects.filter(action_type='app')))
        self.assertEqual(
            0, len(AdminActionUserTime.objects.filter(action_type='req')))

        self.assertEqual(
            1,
            len(
                UserTimeLog.objects.filter(user=self.volunteer_1).filter(
                    is_verified=True)))
        self.assertEqual(
            1,
            len(
                AdminActionUserTime.objects.filter(
                    usertimelog__user=self.volunteer_1).filter(
                        action_type='app')))
        self.assertEqual(
            1,
            len(
                UserTimeLog.objects.filter(user=self.volunteer_2).filter(
                    is_verified=True)))
        self.assertEqual(
            1,
            len(
                AdminActionUserTime.objects.filter(
                    usertimelog__user=self.volunteer_2).filter(
                        action_type='app')))

        # checking ledger records
        ledger_query = Ledger.objects.all()
        self.assertEqual(2, len(ledger_query))
        # asserting the first user
        self.assertEqual(
            1,
            len(ledger_query.filter(
                action__usertimelog__user=self.volunteer_1)))
        self.assertEqual(
            'cur',
            ledger_query.get(
                action__usertimelog__user=self.volunteer_1).currency)
        self.assertEqual(
            3,
            ledger_query.get(
                action__usertimelog__user=self.volunteer_1).amount)
        self.assertEqual(
            True,
            ledger_query.get(
                action__usertimelog__user=self.volunteer_1).is_issued)

        self.assertEqual(1, len(self.oc_vol_1.get_hours_approved()))
        self.assertEqual(3, OcLedger().get_balance(self.user_enitity_id_vol_1))

        # asserting the 2nd user
        self.assertEqual(
            1,
            len(ledger_query.filter(
                action__usertimelog__user=self.volunteer_2)))
        self.assertEqual(
            'cur',
            ledger_query.get(
                action__usertimelog__user=self.volunteer_2).currency)
        self.assertEqual(
            2,
            ledger_query.get(
                action__usertimelog__user=self.volunteer_2).amount)
        self.assertEqual(
            True,
            ledger_query.get(
                action__usertimelog__user=self.volunteer_2).is_issued)

        self.assertEqual(1, len(self.oc_vol_2.get_hours_approved()))
        self.assertEqual(2, OcLedger().get_balance(self.user_enitity_id_vol_2))

    def test_logged_hours_declined(self):
        self.assertEqual(self.response.status_code, 200)

        org_admin_response = self.client.get('/org-admin/')
        self.assertEqual(org_admin_response.status_code, 200)

        # checking pending hours before declining
        self.assertDictEqual(
            org_admin_response.context['hours_pending_by_admin'],
            {self.npf_admin_1: 5.0})

        # checking total approved hours
        self.assertEqual(org_admin_response.context['issued_by_all'], 0)

        # checking initial balance
        self.assertEqual(0, len(self.oc_vol_1.get_hours_approved()))
        self.assertEqual(0, OcLedger().get_balance(self.user_enitity_id_vol_1))
        self.assertEqual(0, len(self.oc_vol_2.get_hours_approved()))
        self.assertEqual(0, OcLedger().get_balance(self.user_enitity_id_vol_2))

        self.response = self.client.post(
            '/approve-hours/', {
                'post-data':
                self.volunteer_1.username + ':0:' + self.monday + ',' +
                self.volunteer_2.username + ':0:' + self.monday
            })

        # return to org-amdin after declining
        self.assertRedirects(self.response, '/org-admin/0/2/', status_code=302)

        # assert the creation of the corresponding usertimelog and adminaction records
        self.assertEqual(
            0, len(AdminActionUserTime.objects.filter(action_type='app')))
        self.assertEqual(
            0, len(AdminActionUserTime.objects.filter(action_type='app')))
        self.assertEqual(
            2, len(AdminActionUserTime.objects.filter(action_type='dec')))

        self.assertEqual(
            1,
            len(
                UserTimeLog.objects.filter(user=self.volunteer_1).filter(
                    is_verified=False)))
        self.assertEqual(
            1,
            len(
                AdminActionUserTime.objects.filter(
                    usertimelog__user=self.volunteer_1).filter(
                        action_type='dec')))
        self.assertEqual(
            1,
            len(
                UserTimeLog.objects.filter(user=self.volunteer_2).filter(
                    is_verified=False)))
        self.assertEqual(
            1,
            len(
                AdminActionUserTime.objects.filter(
                    usertimelog__user=self.volunteer_2).filter(
                        action_type='dec')))

        # checking ledger records
        ledger_query = Ledger.objects.all()
        self.assertEqual(0, len(ledger_query))
        # asserting the first user
        self.assertEqual(
            0,
            len(ledger_query.filter(
                action__usertimelog__user=self.volunteer_1)))
        self.assertEqual(0, len(self.oc_vol_1.get_hours_approved()))

        self.assertEqual(0, len(self.oc_vol_1.get_hours_approved()))
        self.assertEqual(0, OcLedger().get_balance(self.user_enitity_id_vol_1))

        # asserting the 2nd user
        self.assertEqual(
            0,
            len(ledger_query.filter(
                action__usertimelog__user=self.volunteer_2)))
        self.assertEqual(0, len(self.oc_vol_2.get_hours_approved()))

        self.assertEqual(0, len(self.oc_vol_2.get_hours_approved()))
        self.assertEqual(0, OcLedger().get_balance(self.user_enitity_id_vol_1))