def test_no_user(self):
     """
     If we try to get info without a login, get an error back
     """
     self.request.user = None
     result = sessions_put_view(self.request)['d']
     self.assertEqual(
         result,
         error_dict('api_errors', 'not authenticated for this request'))
 def test_nonexistent_token_provided(self):
     """
     If the token doesn't exist, say so
     """
     self.request.json_body = {'token': 'badtoken'}
     self.datautils.create_session()
     self.datautils.create_session()
     result = sessions_put_view(self.request)['d']
     self.assertIsInstance(result, dict)
     self.assertEqual(result,
                      error_dict('api_errors', 'no valid token provided'))
 def test_expired_token_provided(self):
     """
     If the token is expired, say so, and delete it
     """
     weeks_ago = datetime.now() - relativedelta(weeks=6)
     s = self.datautils.create_session({
         'lastactive': weeks_ago,
         'started': weeks_ago,
         'user_id': self.request.user.id
     })
     self.assertEqual(self.session.query(Session).count(), 1)
     self.assertEqual(s.started, s.lastactive)
     self.assertEqual(s.started, weeks_ago)
     self.request.json_body = {'token': s.token}
     result = sessions_put_view(self.request)['d']
     self.assertEqual(result,
                      error_dict('api_errors', 'no valid token provided'))
     self.assertEqual(self.session.query(Session).count(), 0)
 def test_good_token(self):
     """
     If the token is valid, return the token and we the timestamp is updated
     """
     days_ago = datetime.now() - relativedelta(days=4)
     self.request.user.origin = 'testorigin'
     s = self.datautils.create_session({
         'lastactive': days_ago,
         'started': days_ago,
         'user_id': self.request.user.id
     })
     self.assertEqual(self.session.query(Session).count(), 1)
     self.assertEqual(s.started, s.lastactive)
     self.request.json_body = {'token': s.token}
     result = sessions_put_view(self.request)['d']
     self.session.flush()
     self.session.refresh(s)
     self.assertIsInstance(result, dict)
     s_dict = dict_from_row(s)
     s_dict['origin'] = 'testorigin'
     self.assertEqual(result, s_dict)
     self.assertNotEqual(s.started, s.lastactive)
     self.assertTrue(s.lastactive > datetime.now() - relativedelta(hours=1))