Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ast
- from unittest import skipUnless
- from django.core.urlresolvers import reverse
- from django.utils import timezone
- from django.conf import settings
- from rest_framework import status
- from rest_framework.test import APITestCase, APIClient
- from oauth2_provider.models import Application
- from sibylsurveys.modules.surveys.factories import SurveyFactory
- from sibylsurveys.modules.devices.models import GCMDevice
- from sibylsurveys.modules.devices.factories import GCMDeviceFactory
- from sibylsurveys.modules.engagements.factories import EngagementFactory
- from sibylsurveys.common.utils.constants import responsibyl_identifier_header
- from sibylsurveys.common.serializers import TimestampField
- from sibylsurveys.modules.profiles.factories import UserProfileFactory
- class UpdateProfileTestCase(APITestCase):
- def setUp(self):
- self.test_profile1 = UserProfileFactory(username='user1', email='fake1@gmail.com')
- self.test_company = self.test_profile1.company
- self.app = Application.objects.create(
- user=self.test_profile1.user,
- client_type=Application.CLIENT_PUBLIC,
- authorization_grant_type=Application.GRANT_PASSWORD,
- name='test app',
- client_id='VwYwTabLDcPXrTGlvz8hycTX8Q0nYndkeVJDj91C',
- client_secret='XmAbDtQRQKAKp70LVDyACdGoixN1F3FjnU8woTTyGSTHLhzkIfAYjctegDXKxxjuqkl7IgfmpMVKpHv8MuKUhmXx5zQNTK97p7AqQf09dj8P5DzAZCEkRcKkWufDhDOt'
- )
- self.test_profile2 = UserProfileFactory(username='user2', email='fake2@gmail.com')
- self.client = APIClient()
- self.client.login(username='user1', password='pass')
- self.test_engagement = EngagementFactory(owner=self.test_profile1)
- self.test_device1 = GCMDeviceFactory(last_user_logged=self.test_profile1, current_user=None)
- self.test_device2 = GCMDeviceFactory(last_user_logged=self.test_profile1, current_user=None)
- self.test_survey = SurveyFactory(responses_goal=100, engagement=self.test_engagement)
- self.test_datetime_to_timestamp = TimestampField(field_type='datetime').to_representation(
- timezone.datetime(year=1973, month=11, day=11, hour=21, minute=33, second=9))
- @skipUnless(settings.API in ['BO', 'APP'], 'API is either "APP" or "BO".')
- def test_last_online(self):
- """
- First, both last online and last sync time are null.
- First request, throws exception, but sets last online. Sync still null.
- Second request, success, updates last online to a greater time. Sync still null.
- :return:
- """
- self.assertIsNone(self.test_profile1.last_online)
- self.assertIsNone(self.test_profile1.last_sync)
- self.client.get('/api/v1/something/')
- self.test_profile1.refresh_from_db()
- self.assertIsNotNone(self.test_profile1.last_online)
- last_online = self.test_profile1.last_online
- self.client.get('/api/v1/user/')
- self.test_profile1.refresh_from_db()
- self.assertGreater(self.test_profile1.last_online, last_online)
- self.assertIsNone(self.test_profile1.last_sync)
- print('test_last_online PASSED')
- @skipUnless(settings.API == 'APP', 'API must be "APP".')
- def test_last_sync(self):
- """
- Last sync is null, after the request it should be almost equal to the post time.
- :return:
- """
- self.assertIsNone(self.test_profile1.last_sync)
- url = reverse('responses:survey-response-list', kwargs={'survey_id': self.test_survey.id})
- data_to_send = {
- 'question_screen_responses': [],
- 'taken': self.test_datetime_to_timestamp,
- 'duration': '00:10:00',
- 'latitude': 5,
- 'longitude': 15,
- 'device': self.test_device1.id,
- 'respondent': {
- 'gender': 1,
- 'age': 18
- }
- }
- self.client.credentials(**{
- responsibyl_identifier_header: self.test_device1.id
- })
- post_time = (timezone.datetime.now(timezone.get_current_timezone()) - timezone.datetime(1970, 1, 1, 0, 0, 0, 0, timezone.get_current_timezone())).total_seconds()
- self.client.post(url, data_to_send, format='json')
- self.test_profile1.refresh_from_db()
- self.assertIsNotNone(self.test_profile1.last_sync)
- last_sync = (timezone.make_aware(self.test_profile1.last_sync,
- timezone.get_current_timezone()) - timezone.datetime(1970, 1, 1, 0, 0, 0, 0, timezone.get_current_timezone())).total_seconds()
- self.assertAlmostEqual(int(post_time), int(last_sync))
- print('test_last_sync PASSED')
- @skipUnless(settings.API == 'APP', 'API must be "APP".')
- def test_current_device(self):
- """
- Initial device is null.
- First request - user logs in, so it becomes current user on the device.
- Second request - user logs out, so current user becomes null.
- :return:
- """
- self.assertIsNone(self.test_device1.current_user)
- self.client.credentials(**{
- responsibyl_identifier_header: self.test_device2.id
- })
- url = '/oauth2/token/'
- url_params = {
- 'grant_type': 'password',
- 'client_id': self.app.client_id,
- 'client_secret': self.app.client_secret,
- 'username': self.test_profile1.username,
- 'password': 'pass',
- }
- post_url = url + '?'
- for parameter, value in url_params.iteritems():
- post_url += parameter + '=' + value + '&'
- post_url = post_url[:-1]
- response = self.client.post(post_url)
- self.client.post(post_url, format='json')
- self.assertEqual(response.status_code, status.HTTP_200_OK)
- self.test_device2 = GCMDevice.objects.get(pk=self.test_device2.id)
- self.assertEqual(self.test_device2.current_user, self.test_profile1)
- url = '/oauth2/revoke_token/'
- token = ast.literal_eval(response.content)['access_token']
- post_url = url + '?token={}&client_id={}'.format(token, self.app.client_id)
- response = self.client.post(post_url)
- self.assertEqual(response.status_code, status.HTTP_200_OK)
- self.test_device2 = GCMDevice.objects.get(pk=self.test_device2.id)
- self.assertEqual(self.test_device2.current_user, None)
- print('test_current_device PASSED')
- @skipUnless(settings.API in ['BO', 'APP'], 'API is either "APP" or "BO".')
- def test_change_user_password(self):
- # Anon client, should not work.
- url = reverse('profiles:user-change-password')
- data_to_send = {
- 'old_password': 'pass',
- 'new_password': 'pass',
- 'confirm_password': 'pass'
- }
- client = APIClient()
- response = client.post(url, data_to_send, format='json')
- self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
- # Incorrect password
- data_to_send = {
- 'old_password': 'pass2',
- 'new_password': 'pas',
- 'confirm_password': 'pas'
- }
- response = self.client.post(url, data_to_send, format='json')
- self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
- self.assertEqual(response.data, {'old_password': ['Incorrect password.']})
- # New password identical to old one
- data_to_send = {
- 'old_password': 'pass',
- 'new_password': 'pass',
- 'confirm_password': 'pass'
- }
- response = self.client.post(url, data_to_send, format='json')
- self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
- self.assertEqual(response.data, {'new_password': ["New password shouldn't match old password."]})
- # Confirm password not matching
- data_to_send = {
- 'old_password': 'pass',
- 'new_password': 'pass2',
- 'confirm_password': 'pass1'
- }
- response = self.client.post(url, data_to_send, format='json')
- self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
- self.assertEqual(response.data, {'confirm_password': ["Passwords don't match."]})
- # Password should contain at least 4 characters.
- data_to_send = {
- 'old_password': 'pass',
- 'new_password': 'pas',
- 'confirm_password': 'pas'
- }
- response = self.client.post(url, data_to_send, format='json')
- self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
- # Current user
- data_to_send = {
- 'old_password': 'pass',
- 'new_password': 'newPass',
- 'confirm_password': 'newPass'
- }
- response = self.client.post(url, data_to_send, format='json')
- # Check if password was changed.
- self.assertEqual(response.status_code, status.HTTP_200_OK)
- self.client = APIClient()
- self.client.login(username='user1', password='pass')
- print('test_change_user_password PASSED')
- def tearDown(self):
- self.client.logout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement