Esempio n. 1
0
    def test_refresh(self):
        """If token expires, it should refresh automatically"""

        with patch.object(OAuth2Session, 'request') as r:
            r.side_effect = [
                MagicMock(
                    status_code=401,
                    content=b'{"errors": [{"errorType": "expired_token"}]}'),
                MagicMock(status_code=200,
                          content=b'{"activities-steps": [1, 2, 3]}')
            ]
            with patch.object(OAuth2Session, 'refresh_token') as rt:
                rt.return_value = {
                    'access_token': 'fake_access_token',
                    'refresh_token': 'fake_refresh_token',
                    'expires_at': time.time() + 300,
                }
                resource_type = TimeSeriesDataType.objects.get(
                    category=TimeSeriesDataType.activities, resource='steps')
                utils.get_fitbit_data(self.fbuser,
                                      resource_type,
                                      base_date=self.base_date,
                                      period=self.period,
                                      end_date=self.end_date)

        self.assertEqual(self.fbuser.access_token, 'fake_access_token')
        self.assertEqual(self.fbuser.refresh_token, 'fake_refresh_token')
Esempio n. 2
0
    def test_refresh(self):
        """If token expires, it should refresh automatically"""

        with patch.object(OAuth2Session, 'request') as r:
            r.side_effect = [
                MagicMock(
                    status_code=401,
                    content=b'{"errors": [{"errorType": "expired_token"}]}'),
                MagicMock(status_code=200,
                          content=b'{"activities-steps": [1, 2, 3]}')
            ]
            with patch.object(OAuth2Session, 'refresh_token') as rt:
                rt.return_value = {
                    'access_token': 'fake_access_token',
                    'refresh_token': 'fake_refresh_token',
                    'expires_at': time.time() + 300,
                }
                resource_type = TimeSeriesDataType.objects.get(
                    category=TimeSeriesDataType.activities, resource='steps')
                utils.get_fitbit_data(
                    self.fbuser, resource_type, base_date=self.base_date,
                    period=self.period, end_date=self.end_date)

        self.assertEqual(self.fbuser.access_token, 'fake_access_token')
        self.assertEqual(self.fbuser.refresh_token, 'fake_refresh_token')
Esempio n. 3
0
 def _mock_time_series(self, time_series=None, error=None, response=None,
                       error_attrs={}):
     if error:
         exc = error(self._error_response())
         for k, v in error_attrs.items():
             setattr(exc, k, v)
         time_series.side_effect = exc
     elif response:
         time_series.return_value = response
     resource_type = TimeSeriesDataType.objects.get(
         category=TimeSeriesDataType.activities, resource='steps')
     return utils.get_fitbit_data(
         self.fbuser, resource_type, base_date=self.base_date,
         period=self.period, end_date=self.end_date)
Esempio n. 4
0
    def test_refresh(self):
        """If token expires, it should refresh automatically"""

        with patch.object(FitbitOauth2Client, '_request') as r:
            r.side_effect = [
                fitbit_exceptions.HTTPUnauthorized(
                    MagicMock(status_code=401, content=b'unauth response')),
                MagicMock(status_code=200,
                          content=b'{"activities-steps": [1, 2, 3]}')
            ]
            with patch.object(OAuth2Session, 'refresh_token') as rt:
                rt.return_value = {
                    'access_token': 'fake_access_token',
                    'refresh_token': 'fake_refresh_token'
                }
                resource_type = TimeSeriesDataType.objects.get(
                    category=TimeSeriesDataType.activities, resource='steps')
                utils.get_fitbit_data(
                    self.fbuser, resource_type, base_date=self.base_date,
                    period=self.period, end_date=self.end_date)

        self.assertEqual(self.fbuser.access_token, 'fake_access_token')
        self.assertEqual(self.fbuser.refresh_token, 'fake_refresh_token')
Esempio n. 5
0
 def _mock_time_series(self, time_series=None, error=None, response=None,
                       error_attrs={}):
     if error:
         exc = error(self._error_response())
         for k, v in error_attrs.items():
             setattr(exc, k, v)
         time_series.side_effect = exc
     elif response:
         time_series.return_value = response
     resource_type = TimeSeriesDataType.objects.get(
         category=TimeSeriesDataType.activities, resource='steps')
     return utils.get_fitbit_data(
         self.fbuser, resource_type, base_date=self.base_date,
         period=self.period, end_date=self.end_date)