Exemplo n.º 1
0
    def test_monthly_submissions_with_year_param(self):
        """
        Test that by passing only the value for year
        the month is assumed to be the current month
        """
        view = UserProfileViewSet.as_view({'get': 'monthly_submissions'})
        month = datetime.datetime.now().month

        # publish form and make submissions dated the year 2013
        # and the current month
        self._publish_xls_form_to_project()
        survey = self.surveys[0]
        _time = parse_datetime('2013-' + str(month) + '-18 15:54:01Z')
        self._make_submission(os.path.join(self.main_directory, 'fixtures',
                                           'transportation', 'instances',
                                           survey, survey + '.xml'),
                              forced_submission_time=_time)
        count = Instance.objects.filter(xform=self.xform).filter(
            date_created__year=2013).filter(date_created__month=month).count()

        data = {'year': 2013}
        request = self.factory.get('/', data=data, **self.extra)
        response = view(request, user=self.user.username)
        self.assertEquals(response.status_code, 200)
        self.assertFalse(self.xform.shared)
        self.assertEquals(response.data, {'private': count})
Exemplo n.º 2
0
 def setUp(self):
     super(self.__class__, self).setUp()
     self.view = UserProfileViewSet.as_view({
         'get': 'list',
         'post': 'create',
         'patch': 'partial_update'
     })
Exemplo n.º 3
0
 def test_profiles_get(self):
     view = UserProfileViewSet.as_view({'get': 'retrieve'})
     request = self.factory.get('/', **self.extra)
     response = view(request)
     self.assertEqual(response.status_code, 400)
     self.assertEqual(response.data,
                      {'detail': 'Expected URL keyword argument `user`.'})
     request = self.factory.get('/', **self.extra)
     response = view(request, user='******')
     data = {
         'url': 'http://testserver/api/v1/profiles/bob',
         'username': u'bob',
         'name': u'Bob',
         'email': u'*****@*****.**',
         'city': u'Bobville',
         'country': u'US',
         'organization': u'Bob Inc.',
         'website': u'bob.com',
         'twitter': u'boberama',
         'gravatar': self.user.profile.gravatar,
         'require_auth': False,
         'user': '******'
     }
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.data, data)
Exemplo n.º 4
0
    def test_get_monthly_submissions(self):
        """
        Test getting monthly submissions for a user
        """
        view = UserProfileViewSet.as_view({'get': 'monthly_submissions'})
        # publish form and make submissions
        self._publish_xls_form_to_project()
        self._make_submissions()
        count1 = Instance.objects.filter(xform=self.xform).count()

        request = self.factory.get('/', **self.extra)
        response = view(request, user=self.user.username)
        self.assertEquals(response.status_code, 200)
        self.assertFalse(self.xform.shared)
        self.assertEquals(response.data, {'private': count1})

        # publish another form, make submission and make it public
        self._publish_form_with_hxl_support()
        self.assertEquals(self.xform.id_string, 'hxl_example')
        count2 = Instance.objects.filter(xform=self.xform).filter(
            date_created__year=datetime.datetime.now().year).filter(
                date_created__month=datetime.datetime.now().month).filter(
                    date_created__day=datetime.datetime.now().day).count()

        self.xform.shared = True
        self.xform.save()
        request = self.factory.get('/', **self.extra)
        response = view(request, user=self.user.username)
        self.assertEquals(response.status_code, 200)
        self.assertEquals(response.data, {'private': count1, 'public': count2})
Exemplo n.º 5
0
 def test_profiles_get(self):
     view = UserProfileViewSet.as_view({
         'get': 'retrieve'
     })
     request = self.factory.get('/', **self.extra)
     response = view(request)
     self.assertEqual(response.status_code, 400)
     self.assertEqual(
         response.data, {'detail': 'Expected URL keyword argument `user`.'})
     request = self.factory.get('/', **self.extra)
     response = view(request, user='******')
     data = {
         'url': 'http://testserver/api/v1/profiles/bob',
         'username': u'bob',
         'name': u'Bob',
         'email': u'*****@*****.**',
         'city': u'Bobville',
         'country': u'US',
         'organization': u'Bob Inc.',
         'website': u'bob.com',
         'twitter': u'boberama',
         'gravatar': self.user.profile.gravatar,
         'require_auth': False,
         'user': '******'
     }
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.data, data)
Exemplo n.º 6
0
    def test_monthly_submissions_with_year_param(self):
        """
        Test that by passing only the value for year
        the month is assumed to be the current month
        """
        view = UserProfileViewSet.as_view({'get': 'monthly_submissions'})
        month = datetime.datetime.now().month

        # publish form and make submissions dated the year 2013
        # and the current month
        self._publish_xls_form_to_project()
        survey = self.surveys[0]
        _time = parse_datetime('2013-' + str(month) + '-18 15:54:01Z')
        self._make_submission(
            os.path.join(self.main_directory, 'fixtures', 'transportation',
                         'instances', survey, survey + '.xml'),
            forced_submission_time=_time)
        count = Instance.objects.filter(xform=self.xform).filter(
                date_created__year=2013).filter(
                    date_created__month=month).count()

        data = {'year': 2013}
        request = self.factory.get('/', data=data, **self.extra)
        response = view(request, user=self.user.username)
        self.assertEquals(response.status_code, 200)
        self.assertFalse(self.xform.shared)
        self.assertEquals(response.data, {'private': count})
Exemplo n.º 7
0
    def test_get_monthly_submissions(self):
        """
        Test getting monthly submissions for a user
        """
        view = UserProfileViewSet.as_view({'get': 'monthly_submissions'})
        # publish form and make submissions
        self._publish_xls_form_to_project()
        self._make_submissions()
        count1 = Instance.objects.filter(xform=self.xform).count()

        request = self.factory.get('/', **self.extra)
        response = view(request, user=self.user.username)
        self.assertEquals(response.status_code, 200)
        self.assertFalse(self.xform.shared)
        self.assertEquals(response.data, {'private': count1})

        # publish another form, make submission and make it public
        self._publish_form_with_hxl_support()
        self.assertEquals(self.xform.id_string, 'hxl_example')
        count2 = Instance.objects.filter(xform=self.xform).filter(
            date_created__year=datetime.datetime.now().year).filter(
                date_created__month=datetime.datetime.now().month).filter(
                    date_created__day=datetime.datetime.now().day).count()

        self.xform.shared = True
        self.xform.save()
        request = self.factory.get('/', **self.extra)
        response = view(request, user=self.user.username)
        self.assertEquals(response.status_code, 200)
        self.assertEquals(response.data, {'private': count1, 'public': count2})
Exemplo n.º 8
0
    def test_profiles_get(self):
        """Test get user profile"""
        view = UserProfileViewSet.as_view({
            'get': 'retrieve'
        })
        request = self.factory.get('/', **self.extra)
        response = view(request)
        self.assertEqual(response.status_code, 400)
        self.assertEqual(
            response.data, {'detail': 'Expected URL keyword argument `user`.'})

        # by username
        response = view(request, user='******')
        self.assertNotEqual(response.get('Cache-Control'), None)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, self.user_profile_data())

        # by username mixed case
        response = view(request, user='******')
        self.assertEqual(response.status_code, 200)
        self.assertNotEqual(response.get('Cache-Control'), None)
        self.assertEqual(response.data, self.user_profile_data())

        # by pk
        response = view(request, user=self.user.pk)
        self.assertNotEqual(response.get('Cache-Control'), None)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, self.user_profile_data())
Exemplo n.º 9
0
    def test_verification_key_is_valid_with_redirect_url_set(self):
        data = _profile_data()
        self._create_user_using_profiles_endpoint(data)

        view = UserProfileViewSet.as_view({'get': 'verify_email'})
        rp = RegistrationProfile.objects.get(
            user__username=data.get('username')
        )
        _data = {
            'verification_key': rp.activation_key,
            'redirect_url': 'http://red.ir.ect'
        }
        request = self.factory.get('/', data=_data)
        response = view(request)

        self.assertEquals(response.status_code, 302)
        self.assertIn('is_email_verified', response.url)
        self.assertIn('username', response.url)

        string_query_params = urlparse(response.url).query
        dict_query_params = parse_qs(string_query_params)
        self.assertEquals(dict_query_params.get(
            'is_email_verified'), ['True'])
        self.assertEquals(
            dict_query_params.get('username'),
            [data.get('username')]
        )

        up = UserProfile.objects.get(user__username=data.get('username'))
        self.assertIn('is_email_verified', up.metadata)
        self.assertTrue(up.metadata.get('is_email_verified'))
Exemplo n.º 10
0
    def test_verification_key_is_valid_with_redirect_url_set(self):
        data = _profile_data()
        self._create_user_using_profiles_endpoint(data)

        view = UserProfileViewSet.as_view({'get': 'verify_email'})
        rp = RegistrationProfile.objects.get(
            user__username=data.get('username'))
        _data = {
            'verification_key': rp.activation_key,
            'redirect_url': 'http://red.ir.ect'
        }
        request = self.factory.get('/', data=_data)
        response = view(request)

        self.assertEquals(response.status_code, 302)
        self.assertIn('is_email_verified', response.url)
        self.assertIn('username', response.url)

        string_query_params = urlparse(response.url).query
        dict_query_params = parse_qs(string_query_params)
        self.assertEquals(dict_query_params.get('is_email_verified'), ['True'])
        self.assertEquals(dict_query_params.get('username'),
                          [data.get('username')])

        up = UserProfile.objects.get(user__username=data.get('username'))
        self.assertIn('is_email_verified', up.metadata)
        self.assertTrue(up.metadata.get('is_email_verified'))
Exemplo n.º 11
0
    def test_profiles_get(self):
        """Test get user profile"""
        view = UserProfileViewSet.as_view({
            'get': 'retrieve'
        })
        request = self.factory.get('/', **self.extra)
        response = view(request)
        self.assertEqual(response.status_code, 400)
        self.assertEqual(
            response.data, {'detail': 'Expected URL keyword argument `user`.'})

        # by username
        response = view(request, user='******')
        self.assertNotEqual(response.get('Cache-Control'), None)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, self.user_profile_data())

        # by username mixed case
        response = view(request, user='******')
        self.assertEqual(response.status_code, 200)
        self.assertNotEqual(response.get('Cache-Control'), None)
        self.assertEqual(response.data, self.user_profile_data())

        # by pk
        response = view(request, user=self.user.pk)
        self.assertNotEqual(response.get('Cache-Control'), None)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, self.user_profile_data())
Exemplo n.º 12
0
 def test_profiles_get_org_anon(self):
     self._org_create()
     self.client.logout()
     view = UserProfileViewSet.as_view({'get': 'retrieve'})
     request = self.factory.get('/')
     response = view(request, user=self.company_data['org'])
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.data['name'], self.company_data['name'])
     self.assertIn('is_org', response.data)
     self.assertEqual(response.data['is_org'], True)
Exemplo n.º 13
0
 def test_profiles_get_org_anon(self):
     self._org_create()
     self.client.logout()
     view = UserProfileViewSet.as_view({"get": "retrieve"})
     request = self.factory.get("/")
     response = view(request, user=self.company_data["org"])
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.data["first_name"], self.company_data["name"])
     self.assertIn("is_org", response.data)
     self.assertEqual(response.data["is_org"], True)
Exemplo n.º 14
0
    def test_change_password_wrong_current_password(self):
        view = UserProfileViewSet.as_view({"post": "change_password"})
        current_password = "******"
        new_password = "******"
        post_data = {"current_password": current_password, "new_password": new_password}

        request = self.factory.post("/", data=post_data, **self.extra)
        response = view(request, user="******")
        user = User.objects.get(username__iexact=self.user.username)
        self.assertEqual(response.status_code, 400)
        self.assertFalse(user.check_password(new_password))
Exemplo n.º 15
0
 def test_profiles_get_org_anon(self):
     self._org_create()
     self.client.logout()
     view = UserProfileViewSet.as_view({
         'get': 'retrieve'
     })
     request = self.factory.get('/')
     response = view(request, user=self.company_data['org'])
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.data['name'], self.company_data['name'])
     self.assertIn('is_org', response.data)
     self.assertEqual(response.data['is_org'], True)
Exemplo n.º 16
0
    def test_change_password(self):
        view = UserProfileViewSet.as_view(
            {'post': 'change_password'})
        current_password = "******"
        new_password = "******"
        post_data = {'current_password': current_password,
                     'new_password': new_password}

        request = self.factory.post('/', data=post_data, **self.extra)
        response = view(request, user='******')
        user = User.objects.get(username__iexact=self.user.username)
        self.assertEqual(response.status_code, 200)
        self.assertTrue(user.check_password(new_password))
Exemplo n.º 17
0
 def test_profiles_get(self):
     view = UserProfileViewSet.as_view({
         'get': 'retrieve'
     })
     request = self.factory.get('/', **self.extra)
     response = view(request)
     self.assertEqual(response.status_code, 400)
     self.assertEqual(
         response.data, {'detail': 'Expected URL keyword argument `user`.'})
     request = self.factory.get('/', **self.extra)
     response = view(request, user='******')
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.data, self.user_profile_data())
Exemplo n.º 18
0
    def test_change_password_wrong_current_password(self):
        view = UserProfileViewSet.as_view(
            {'post': 'change_password'})
        current_password = "******"
        new_password = "******"
        post_data = {'current_password': current_password,
                     'new_password': new_password}

        request = self.factory.post('/', data=post_data, **self.extra)
        response = view(request, user='******')
        user = User.objects.get(username__iexact=self.user.username)
        self.assertEqual(response.status_code, 400)
        self.assertFalse(user.check_password(new_password))
Exemplo n.º 19
0
    def test_sending_verification_email_fails(self):
        data = _profile_data()
        self._create_user_using_profiles_endpoint(data)

        view = UserProfileViewSet.as_view({'post': 'send_verification_email'})

        # trigger permission error when username of requesting user is
        # different from username in post details
        request = self.factory.post('/',
                                    data={'username': '******'},
                                    **self.extra)
        response = view(request)
        self.assertEquals(response.status_code, 403)
Exemplo n.º 20
0
    def test_sending_verification_email_fails(self):
        data = _profile_data()
        self._create_user_using_profiles_endpoint(data)

        view = UserProfileViewSet.as_view({'post': 'send_verification_email'})

        # trigger permission error when username of requesting user is
        # different from username in post details
        request = self.factory.post('/',
                                    data={'username': '******'},
                                    **self.extra)
        response = view(request)
        self.assertEquals(response.status_code, 403)
Exemplo n.º 21
0
    def test_profiles_get_anon(self):
        view = UserProfileViewSet.as_view({"get": "retrieve"})
        request = self.factory.get("/")
        response = view(request)
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.data, {"detail": "Expected URL keyword argument `user`."})
        request = self.factory.get("/")
        response = view(request, user="******")
        data = self.user_profile_data()
        del data["email"]

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, data)
        self.assertNotIn("email", response.data)
Exemplo n.º 22
0
    def test_change_password_attempts(self):
        view = UserProfileViewSet.as_view(
            {'post': 'change_password'})

        # clear cache
        cache.delete('change_password_attempts-bob')
        cache.delete('lockout_change_password_user-bob')
        self.assertIsNone(cache.get('change_password_attempts-bob'))
        self.assertIsNone(cache.get('lockout_change_password_user-bob'))

        # first attempt
        current_password = "******"
        new_password = "******"
        post_data = {'current_password': current_password,
                     'new_password': new_password}
        request = self.factory.post('/', data=post_data, **self.extra)
        response = view(request, user='******')
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.data,
                         "Invalid password."
                         u" You have 9 attempts left.")
        self.assertEqual(cache.get('change_password_attempts-bob'), 1)

        # second attempt
        request = self.factory.post('/', data=post_data, **self.extra)
        response = view(request, user='******')
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.data,
                         "Invalid password. You have 8 attempts left.")
        self.assertEqual(cache.get('change_password_attempts-bob'), 2)

        # check user is locked out
        request = self.factory.post('/', data=post_data, **self.extra)
        cache.set('change_password_attempts-bob', 9)
        self.assertIsNone(cache.get('lockout_change_password_user-bob'))
        response = view(request, user='******')
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.data,
                         "Too many password reset attempts,"
                         u" Try again in 30 minutes")
        self.assertEqual(cache.get('change_password_attempts-bob'), 10)
        self.assertIsNotNone(cache.get('lockout_change_password_user-bob'))
        lockout = datetime.datetime.strptime(
            cache.get('lockout_change_password_user-bob'), '%Y-%m-%dT%H:%M:%S')
        self.assertIsInstance(lockout, datetime.datetime)

        # clear cache
        cache.delete('change_password_attempts-bob')
        cache.delete('lockout_change_password_user-bob')
Exemplo n.º 23
0
    def test_profiles_get_anon(self):
        view = UserProfileViewSet.as_view({'get': 'retrieve'})
        request = self.factory.get('/')
        response = view(request)
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.data,
                         {'detail': 'Expected URL keyword argument `user`.'})
        request = self.factory.get('/')
        response = view(request, user='******')
        data = self.user_profile_data()
        del data['email']

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, data)
        self.assertNotIn('email', response.data)
Exemplo n.º 24
0
 def test_profiles_get_anon(self):
     view = UserProfileViewSet.as_view({
         'get': 'retrieve'
     })
     request = self.factory.get('/')
     response = view(request)
     self.assertEqual(response.status_code, 400)
     self.assertEqual(
         response.data, {'detail': 'Expected URL keyword argument `user`.'})
     request = self.factory.get('/')
     response = view(request, user='******')
     data = self.user_profile_data()
     del data['email']
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.data, data)
     self.assertNotIn('email', response.data)
Exemplo n.º 25
0
    def test_update_org_name(self):
        self._org_create()

        # update name
        data = {'name': "Dennis2"}
        request = self.factory.patch('/', data=data, **self.extra)
        response = self.view(request, user='******')
        self.assertEqual(response.data['name'], "Dennis2")
        self.assertEqual(response.status_code, 200)

        # check in user profile endpoint
        view_user = UserProfileViewSet.as_view({'get': 'retrieve'})
        request = self.factory.get('/', **self.extra)

        response = view_user(request, user='******')
        self.assertNotEqual(response.get('Cache-Control'), None)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['name'], "Dennis2")
Exemplo n.º 26
0
    def test_change_password(self):
        view = UserProfileViewSet.as_view(
            {'post': 'change_password'})
        current_password = "******"
        new_password = "******"
        post_data = {'current_password': current_password,
                     'new_password': new_password}

        request = self.factory.post('/', data=post_data, **self.extra)
        response = view(request, user='******')
        now = timezone.now().isoformat()
        user = User.objects.get(username__iexact=self.user.username)
        user_profile = UserProfile.objects.get(user_id=user.id)
        self.assertEqual(response.status_code, 204)
        self.assertEqual(
            type(parse_datetime(user_profile.metadata['last_password_edit'])),
            type(parse_datetime(now)))
        self.assertTrue(user.check_password(new_password))
Exemplo n.º 27
0
    def test_change_password(self):
        view = UserProfileViewSet.as_view(
            {'post': 'change_password'})
        current_password = "******"
        new_password = "******"
        post_data = {'current_password': current_password,
                     'new_password': new_password}

        request = self.factory.post('/', data=post_data, **self.extra)
        response = view(request, user='******')
        now = timezone.now().isoformat()
        user = User.objects.get(username__iexact=self.user.username)
        user_profile = UserProfile.objects.get(user_id=user.id)
        self.assertEqual(response.status_code, 204)
        self.assertEqual(
            type(parse_datetime(user_profile.metadata['last_password_edit'])),
            type(parse_datetime(now)))
        self.assertTrue(user.check_password(new_password))
Exemplo n.º 28
0
    def test_sending_verification_email_succeeds(self,
                                                 mock_send_verification_email):
        data = _profile_data()
        self._create_user_using_profiles_endpoint(data)

        data = {'username': data.get('username')}
        view = UserProfileViewSet.as_view({'post': 'send_verification_email'})

        user = User.objects.get(username=data.get('username'))
        extra = {'HTTP_AUTHORIZATION': 'Token %s' % user.auth_token}
        request = self.factory.post('/', data=data, **extra)
        response = view(request)
        self.assertTrue(mock_send_verification_email.called)
        self.assertEquals(response.status_code, 200)
        self.assertEquals(response.data, "Verification email has been sent")

        user = User.objects.get(username=data.get('username'))
        self.assertFalse(user.profile.metadata.get('is_email_verified'))
Exemplo n.º 29
0
    def test_sending_verification_email_succeeds(
            self, mock_send_verification_email):
        data = _profile_data()
        self._create_user_using_profiles_endpoint(data)

        data = {'username': data.get('username')}
        view = UserProfileViewSet.as_view({'post': 'send_verification_email'})

        user = User.objects.get(username=data.get('username'))
        extra = {'HTTP_AUTHORIZATION': 'Token %s' % user.auth_token}
        request = self.factory.post('/', data=data, **extra)
        response = view(request)
        self.assertTrue(mock_send_verification_email.called)
        self.assertEquals(response.status_code, 200)
        self.assertEquals(response.data, "Verification email has been sent")

        user = User.objects.get(username=data.get('username'))
        self.assertFalse(user.profile.metadata.get('is_email_verified'))
Exemplo n.º 30
0
    def test_verification_key_is_valid(self):
        data = _profile_data()
        self._create_user_using_profiles_endpoint(data)

        view = UserProfileViewSet.as_view({'get': 'verify_email'})
        rp = RegistrationProfile.objects.get(
            user__username=data.get('username'))
        _data = {'verification_key': rp.activation_key}
        request = self.factory.get('/', data=_data)
        response = view(request)
        self.assertEquals(response.status_code, 200)
        self.assertIn('is_email_verified', response.data)
        self.assertIn('username', response.data)
        self.assertTrue(response.data.get('is_email_verified'))
        self.assertEquals(response.data.get('username'), data.get('username'))

        up = UserProfile.objects.get(user__username=data.get('username'))
        self.assertIn('is_email_verified', up.metadata)
        self.assertTrue(up.metadata.get('is_email_verified'))
Exemplo n.º 31
0
    def test_profiles_get(self):
        """Test get user profile"""
        view = UserProfileViewSet.as_view({"get": "retrieve"})
        request = self.factory.get("/", **self.extra)
        response = view(request)
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.data, {"detail": "Expected URL keyword argument `user`."})

        # by username
        response = view(request, user="******")
        self.assertNotEqual(response.get("Last-Modified"), None)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, self.user_profile_data())

        # by pk
        response = view(request, user=self.user.pk)
        self.assertNotEqual(response.get("Last-Modified"), None)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, self.user_profile_data())
Exemplo n.º 32
0
    def test_return_204_if_email_verification_variables_are_not_set(self):
        data = _profile_data()
        self._create_user_using_profiles_endpoint(data)

        view = UserProfileViewSet.as_view({'get': 'verify_email',
                                           'post': 'send_verification_email'})
        rp = RegistrationProfile.objects.get(
            user__username=data.get('username')
        )
        _data = {'verification_key': rp.activation_key}
        request = self.factory.get('/', data=_data, **self.extra)
        response = view(request)
        self.assertEquals(response.status_code, 204)

        data = {'username': data.get('username')}
        user = User.objects.get(username=data.get('username'))
        extra = {'HTTP_AUTHORIZATION': 'Token %s' % user.auth_token}
        request = self.factory.post('/', data=data, **extra)
        response = view(request)
        self.assertEquals(response.status_code, 204)
    def test_return_204_if_email_verification_variables_are_not_set(self):
        data = _profile_data()
        self._create_user_using_profiles_endpoint(data)

        view = UserProfileViewSet.as_view({'get': 'verify_email',
                                           'post': 'send_verification_email'})
        rp = RegistrationProfile.objects.get(
            user__username=data.get('username')
        )
        _data = {'verification_key': rp.activation_key}
        request = self.factory.get('/', data=_data, **self.extra)
        response = view(request)
        self.assertEquals(response.status_code, 204)

        data = {'username': data.get('username')}
        user = User.objects.get(username=data.get('username'))
        extra = {'HTTP_AUTHORIZATION': 'Token %s' % user.auth_token}
        request = self.factory.post('/', data=data, **extra)
        response = view(request)
        self.assertEquals(response.status_code, 204)
    def test_update_org_name(self):
        self._org_create()

        # update name
        data = {'name': "Dennis2"}
        request = self.factory.patch('/', data=data, **self.extra)
        response = self.view(request, user='******')
        self.assertEqual(response.data['name'], "Dennis2")
        self.assertEqual(response.status_code, 200)

        # check in user profile endpoint
        view_user = UserProfileViewSet.as_view({
            'get': 'retrieve'
        })
        request = self.factory.get('/', **self.extra)

        response = view_user(request, user='******')
        self.assertNotEqual(response.get('Cache-Control'), None)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['name'], "Dennis2")
Exemplo n.º 35
0
    def test_monthly_submissions_with_month_param(self):
        """
        Test that by passing only the value for month,
        the year is assumed to be the current year
        """
        view = UserProfileViewSet.as_view({'get': 'monthly_submissions'})
        month = datetime.datetime.now().month
        year = datetime.datetime.now().year

        # publish form and make submissions
        self._publish_xls_form_to_project()
        self._make_submissions()
        count = Instance.objects.filter(xform=self.xform).filter(
            date_created__year=year).filter(date_created__month=month).count()

        data = {'month': month}
        request = self.factory.get('/', data=data, **self.extra)
        response = view(request, user=self.user.username)
        self.assertEquals(response.status_code, 200)
        self.assertFalse(self.xform.shared)
        self.assertEquals(response.data, {'private': count})
Exemplo n.º 36
0
    def test_monthly_submissions_with_month_param(self):
        """
        Test that by passing only the value for month,
        the year is assumed to be the current year
        """
        view = UserProfileViewSet.as_view({'get': 'monthly_submissions'})
        month = datetime.datetime.now().month
        year = datetime.datetime.now().year

        # publish form and make submissions
        self._publish_xls_form_to_project()
        self._make_submissions()
        count = Instance.objects.filter(xform=self.xform).filter(
            date_created__year=year).filter(date_created__month=month).count()

        data = {'month': month}
        request = self.factory.get('/', data=data, **self.extra)
        response = view(request, user=self.user.username)
        self.assertEquals(response.status_code, 200)
        self.assertFalse(self.xform.shared)
        self.assertEquals(response.data, {'private': count})
Exemplo n.º 37
0
    def test_verification_key_is_valid(self):
        data = _profile_data()
        self._create_user_using_profiles_endpoint(data)

        view = UserProfileViewSet.as_view({'get': 'verify_email'})
        rp = RegistrationProfile.objects.get(
            user__username=data.get('username')
        )
        _data = {'verification_key': rp.activation_key}
        request = self.factory.get('/', data=_data)
        response = view(request)
        self.assertEquals(response.status_code, 200)
        self.assertIn('is_email_verified', response.data)
        self.assertIn('username', response.data)
        self.assertTrue(response.data.get('is_email_verified'))
        self.assertEquals(
            response.data.get('username'), data.get('username')
        )

        up = UserProfile.objects.get(user__username=data.get('username'))
        self.assertIn('is_email_verified', up.metadata)
        self.assertTrue(up.metadata.get('is_email_verified'))
Exemplo n.º 38
0
    def test_get_monthly_submissions_with_year_and_month_params(self):
        """
        Test passing both month and year params
        """
        view = UserProfileViewSet.as_view({'get': 'monthly_submissions'})
        # publish form and make a submission dated 2013-02-18
        self._publish_xls_form_to_project()
        survey = self.surveys[0]
        submission_time = parse_datetime('2013-02-18 15:54:01Z')
        self._make_submission(
            os.path.join(self.main_directory, 'fixtures', 'transportation',
                         'instances', survey, survey + '.xml'),
            forced_submission_time=submission_time)
        count = Instance.objects.filter(xform=self.xform).filter(
                date_created__month=2).filter(date_created__year=2013).count()

        # get submission count and assert the response is correct
        data = {'month': 2, 'year': 2013}
        request = self.factory.get('/', data=data, **self.extra)
        response = view(request, user=self.user.username)
        self.assertEquals(response.status_code, 200)
        self.assertFalse(self.xform.shared)
        self.assertEquals(response.data, {'private': count})
Exemplo n.º 39
0
    def test_get_monthly_submissions_with_year_and_month_params(self):
        """
        Test passing both month and year params
        """
        view = UserProfileViewSet.as_view({'get': 'monthly_submissions'})
        # publish form and make a submission dated 2013-02-18
        self._publish_xls_form_to_project()
        survey = self.surveys[0]
        submission_time = parse_datetime('2013-02-18 15:54:01Z')
        self._make_submission(os.path.join(self.main_directory, 'fixtures',
                                           'transportation', 'instances',
                                           survey, survey + '.xml'),
                              forced_submission_time=submission_time)
        count = Instance.objects.filter(xform=self.xform).filter(
            date_created__month=2).filter(date_created__year=2013).count()

        # get submission count and assert the response is correct
        data = {'month': 2, 'year': 2013}
        request = self.factory.get('/', data=data, **self.extra)
        response = view(request, user=self.user.username)
        self.assertEquals(response.status_code, 200)
        self.assertFalse(self.xform.shared)
        self.assertEquals(response.data, {'private': count})
Exemplo n.º 40
0
 def setUp(self):
     super(self.__class__, self).setUp()
     self.view = UserProfileViewSet.as_view(
         {"get": "list", "post": "create", "patch": "partial_update", "put": "update"}
     )