Ejemplo n.º 1
0
    def setUp(self):
        # setup duty manager
        self.duty_manager = DutyManager()

        # setup mock user
        self.email = self.generate_email()
        self.password = self.generate_alphanumeric(10)
        self.user = self.create_user(email=self.email, password=self.password)
Ejemplo n.º 2
0
    def test_duty_manager_basic_creation(self):
        """Ability to create duty manager as singleton.
        """
        # create 1st singleton manager
        duty_manager = DutyManager()

        # create 2nd singleton manager, verify it's the same instance with 1st
        duty_manager2 = DutyManager()
        self.assertIs(duty_manager, duty_manager2)
        self.assertIs(duty_manager2, DutyManager.instance)
Ejemplo n.º 3
0
    def test_force_clear_ongoing_duty(self):
        """Force clean duty will cleanse ongoing duty. Hence duty manager 
        can re-create new duty over the previously ongoing one.
        """
        duty_manager = DutyManager()

        # verify initially no existing duty
        self.assertIsNone(duty_manager.duty)

        # start 3 hour 1st duty with user1
        duty_manager.start_duty(self.user1)

        # verify duty started in duty manager
        self.assertIsNotNone(duty_manager.duty)

        # force clear 1st duty
        duty_manager._clear()

        # # expect 1st duty to be deleted, hence DoesNotExist must be raised
        # with self.assertRaises(Duty.DoesNotExist):
        #     duty = self.user1.duty
        self.assertIsNone(duty_manager.duty)

        # enable to create new duty
        duty_manager.start_duty(self.user2)
        self.assertIsNotNone(duty_manager.duty)

        # verify duty created and relationship is still correct
        self.assertIs(duty_manager.duty, self.user2.duty)
Ejemplo n.º 4
0
    def test_only_one_active_duty_and_unable_clear_unfinished_duty(self):
        """One duty at a time handled by single manager. 
        New duty cannot be created when ongoing duty not finished yet.
        """
        duty_manager = DutyManager()

        # verify initially no existing duty
        self.assertIsNone(duty_manager.duty)

        # start 3 hour 1st duty with user1
        duty_manager.start_duty(self.user1)

        # verify duty started in duty manager
        self.assertIsNotNone(duty_manager.duty)

        # expect 2nd duty cannot be started since
        # there is already an active one
        with self.assertRaises(CannotStartOverOngoingDuty):
            duty_manager.start_duty(self.user2)

        # expect 2nd duty not created, hence DoesNotExist must be raised
        with self.assertRaises(Duty.DoesNotExist):
            duty2 = self.user2.duty

        # expect 1st duty can't be cleared since
        # not finished yet
        with self.assertRaises(CannotClearUnfinishedDuty):
            duty_manager.clear_duty()

        # 1st duty remain active in duty manager
        self.assertIsNotNone(duty_manager.duty)
Ejemplo n.º 5
0
    def test_start_and_clear_duty(self):
        """Duty manager can start duty if previously there is no duty
        then the duty can be cleared and replaced with newer one.
        """
        duty_manager = DutyManager()

        # verify initially no existing duty
        self.assertIsNone(duty_manager.duty)

        # start duty with auto_flush off
        duty_manager.start_duty(self.user1)

        # verify duty is started in duty manager
        self.assertIsNotNone(duty_manager.duty)
        self.assertIs(duty_manager.duty, self.user1.duty)
        self.assertIs(self.user1, duty_manager.user)

        # clear current duty
        duty_manager.force_fast_forward_duty(next_minutes=-1)
        duty_manager.clear_duty()

        # verify duty is cleared
        self.assertIsNone(duty_manager.duty)

        # expect duty to be deleted, hence DoesNotExist must be raised
        with self.assertRaises(Duty.DoesNotExist):
            self.user1.duty
Ejemplo n.º 6
0
 def tearDown(self):
     # reset duty manager from any duty associated
     duty_manager = DutyManager()
     duty_manager.reset()
Ejemplo n.º 7
0
class DutyAPITests(APITestCase, RandomSupport):
    """Test endpoints in `duties/api/` API.
    """
    client = Client()

    def create_user(self, name=None, email=None, password=None):
        """Helper that create mock user that is:
            - is_active (bool) True
            - is_staff (bool) & is_superuser (bool) are False
            - credential using email & password field

        Args:
            name (str): name for user, generate random name if None
            email (str): email for credential, generate random [email protected] email if None
            password (str): password for credential, generate random password if None
        
        Returns:
            user (User)
        """
        # random generate value if not specified
        name = name if name else self.generate_name()
        email = email if email else self.generate_email()
        password = password if password else self.generate_alphanumeric()

        # create user with specified model fields
        user = User.objects.create_user(name=name,
                                        email=email,
                                        password=password)
        user.save()

        return user

    def setUp(self):
        # setup duty manager
        self.duty_manager = DutyManager()

        # setup mock user
        self.email = self.generate_email()
        self.password = self.generate_alphanumeric(10)
        self.user = self.create_user(email=self.email, password=self.password)

    def test_request_get_started_duty(self):
        """Test GET duty is valid only if active duty is associated with 
            the authenticated user.
        """
        # initially no duty associated, hence DoesNotExist is raised
        with self.assertRaises(Duty.DoesNotExist):
            duty = self.user.duty

        # authenticate & login user
        is_logged_in = self.client.login(email=self.email,
                                         password=self.password)
        self.assertTrue(is_logged_in)

        # GET expect Http400 or 404 since no duty ever associated with user before.
        response = self.client.get(reverse('duty-api'))
        self.assertIn(response.status_code,
                      [status.HTTP_400_BAD_REQUEST, status.HTTP_404_NOT_FOUND])

        # start/create duty associated with user internally
        self.duty_manager.start_duty(self.user)

        # verify duty is created in duty manager and associated to user
        self.assertIsNotNone(self.duty_manager.duty)
        self.assertIs(self.duty_manager.duty, self.user.duty)

        # GET expect Http200 success since duty is associated with authenticated user
        serialized = DutySerializer(self.user.duty)
        response = self.client.get(reverse('duty-api'))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data.get('payload'), serialized.data)

    def test_request_post_create_duty(self):
        """Test POST duty is valid if user has not associated with any duty and
            no duty is active in duty manager.
        """
        # initially no duty associated, hence DoesNotExist is raised
        with self.assertRaises(Duty.DoesNotExist):
            duty = self.user.duty

        # initially no duty is active in duty manager
        self.assertIsNone(self.duty_manager.duty)

        # authenticate & login user
        is_logged_in = self.client.login(email=self.email,
                                         password=self.password)
        self.assertTrue(is_logged_in)

        # POST expect Http200 success since creation is valid
        response = self.client.post(reverse('duty-api'))
        self.user.refresh_from_db()  # refresh to get one-one duty
        serialized = DutySerializer(self.user.duty)
        self.assertIn(response.status_code,
                      [status.HTTP_200_OK, status.HTTP_201_CREATED])
        self.assertEqual(response.data.get('payload'), serialized.data)

    def test_request_delete_duty(self):
        """Test DELETE duty is valid only if duty has been finished.
        """
        pass

    def tearDown(self):
        # reset duty manager from any duty associated
        duty_manager = DutyManager()
        duty_manager.reset()