当前位置: 首页>>代码示例>>Python>>正文


Python models.Token方法代码示例

本文整理汇总了Python中rest_framework.authtoken.models.Token方法的典型用法代码示例。如果您正苦于以下问题:Python models.Token方法的具体用法?Python models.Token怎么用?Python models.Token使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rest_framework.authtoken.models的用法示例。


在下文中一共展示了models.Token方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_scale_with_unauthorized_user_returns_403

# 需要导入模块: from rest_framework.authtoken import models [as 别名]
# 或者: from rest_framework.authtoken.models import Token [as 别名]
def test_scale_with_unauthorized_user_returns_403(self, mock_requests):
        """An unauthorized user should not be able to access an app's resources.

        If an unauthorized user is trying to scale an app he or she does not have access to, it
        should return a 403.
        """
        app_id = self.create_app()

        # post a new build
        url = "/v2/apps/{app_id}/builds".format(**locals())
        body = {
            'image': 'autotest/example',
            'sha': 'a'*40,
            'procfile': {'web': 'node server.js', 'worker': 'node worker.js'}
        }
        response = self.client.post(url, body)
        unauthorized_user = User.objects.get(username='autotest2')
        unauthorized_token = Token.objects.get(user=unauthorized_user).key
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + unauthorized_token)

        # scale up with unauthorized user
        url = "/v2/apps/{app_id}/scale".format(**locals())
        body = {'web': 4}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 403) 
开发者ID:deis,项目名称:controller,代码行数:27,代码来源:test_pods.py

示例2: test_unauthorized_user_cannot_modify_perms

# 需要导入模块: from rest_framework.authtoken import models [as 别名]
# 或者: from rest_framework.authtoken.models import Token [as 别名]
def test_unauthorized_user_cannot_modify_perms(self):
        """
        An unauthorized user should not be able to modify other apps' permissions.

        Since an unauthorized user should not know about the application at all, these
        requests should return a 404.
        """
        app_id = 'autotest'
        url = '/v2/apps'
        body = {'id': app_id}
        response = self.client.post(url, body)

        url = '{}/{}/perms'.format(url, app_id)
        body = {'username': self.user2.username}
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token2)
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 403) 
开发者ID:deis,项目名称:controller,代码行数:19,代码来源:test_perm.py

示例3: setUp

# 需要导入模块: from rest_framework.authtoken import models [as 别名]
# 或者: from rest_framework.authtoken.models import Token [as 别名]
def setUp(self):
        self.user = User.objects.get(username='autotest')
        self.token = Token.objects.get(user=self.user).key
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)

        self.url = '/v2/certs'
        self.app = App.objects.create(owner=self.user, id='test-app-use-case-2')
        self.domains = {
            'foo.com': Domain.objects.create(owner=self.user, app=self.app, domain='foo.com'),
            'bar.com': Domain.objects.create(owner=self.user, app=self.app, domain='bar.com'),
        }

        # only foo.com has a cert
        self.domain = 'foo.com'

        self.certificates = {self.domain: {'name': self.domain.replace('.', '-')}}
        with open('{}/certs/{}.key'.format(TEST_ROOT, self.domain)) as f:
            self.certificates[self.domain]['key'] = f.read()

        with open('{}/certs/{}.cert'.format(TEST_ROOT, self.domain)) as f:
            self.certificates[self.domain]['cert'] = f.read()

        # add expires and fingerprints
        self.certificates['foo.com']['expires'] = '2017-01-14T23:55:59Z'
        self.certificates['foo.com']['fingerprint'] = 'AC:82:58:80:EA:C4:B9:75:C1:1C:52:48:40:28:15:1D:47:AC:ED:88:4B:D4:72:95:B2:C0:A0:DF:4A:A7:60:B6'  # noqa 
开发者ID:deis,项目名称:controller,代码行数:27,代码来源:test_certificate_use_case_2.py

示例4: test_admin_can_create_config_on_other_apps

# 需要导入模块: from rest_framework.authtoken import models [as 别名]
# 或者: from rest_framework.authtoken.models import Token [as 别名]
def test_admin_can_create_config_on_other_apps(self, mock_requests):
        """If a non-admin creates an app, an administrator should be able to set config
        values for that app.
        """
        user = User.objects.get(username='autotest2')
        token = Token.objects.get(user=user).key

        self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
        app_id = self.create_app()
        url = "/v2/apps/{app_id}/config".format(**locals())

        # set an initial config value
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        body = {'values': json.dumps({'PORT': '5000'})}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 201, response.data)
        self.assertIn('PORT', response.data['values'])
        return response 
开发者ID:deis,项目名称:controller,代码行数:20,代码来源:test_config.py

示例5: test_unauthorized_user_cannot_modify_config

# 需要导入模块: from rest_framework.authtoken import models [as 别名]
# 或者: from rest_framework.authtoken.models import Token [as 别名]
def test_unauthorized_user_cannot_modify_config(self, mock_requests):
        """
        An unauthorized user should not be able to modify other config.

        Since an unauthorized user can't access the application, these
        requests should return a 403.
        """
        app_id = self.create_app()

        unauthorized_user = User.objects.get(username='autotest2')
        unauthorized_token = Token.objects.get(user=unauthorized_user).key
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + unauthorized_token)
        url = '/v2/apps/{}/config'.format(app_id)
        body = {'values': {'FOO': 'bar'}}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 403) 
开发者ID:deis,项目名称:controller,代码行数:18,代码来源:test_config.py

示例6: test_admin_can_create_release

# 需要导入模块: from rest_framework.authtoken import models [as 别名]
# 或者: from rest_framework.authtoken.models import Token [as 别名]
def test_admin_can_create_release(self, mock_requests):
        """If a non-user creates an app, an admin should be able to create releases."""
        user = User.objects.get(username='autotest2')
        token = Token.objects.get(user=user).key
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
        app_id = self.create_app()
        # check that updating config rolls a new release
        url = '/v2/apps/{app_id}/config'.format(**locals())
        body = {'values': json.dumps({'NEW_URL1': 'http://localhost:8080/'})}
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 201, response.data)
        self.assertIn('NEW_URL1', response.data['values'])
        # check to see that an initial release was created
        url = '/v2/apps/{app_id}/releases'.format(**locals())
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200, response.data)
        # account for the config release as well
        self.assertEqual(response.data['count'], 2) 
开发者ID:deis,项目名称:controller,代码行数:21,代码来源:test_release.py

示例7: test_regenerate

# 需要导入模块: from rest_framework.authtoken import models [as 别名]
# 或者: from rest_framework.authtoken.models import Token [as 别名]
def test_regenerate(self):
        """ Test that token regeneration works"""
        url = '/v2/auth/tokens/'

        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.admin_token)
        response = self.client.post(url, {})
        self.assertEqual(response.status_code, 200, response.data)
        self.assertNotEqual(response.data['token'], self.admin_token)

        self.admin_token = Token.objects.get(user=self.admin).key
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.admin_token)

        response = self.client.post(url, {"username": "autotest2"})
        self.assertEqual(response.status_code, 200, response.data)
        self.assertNotEqual(response.data['token'], self.user1_token)

        response = self.client.post(url, {"all": "true"})
        self.assertEqual(response.status_code, 200, response.data)

        response = self.client.post(url, {})
        self.assertEqual(response.status_code, 401, response.data) 
开发者ID:deis,项目名称:controller,代码行数:23,代码来源:test_auth.py

示例8: test_admin_can_create_builds_on_other_apps

# 需要导入模块: from rest_framework.authtoken import models [as 别名]
# 或者: from rest_framework.authtoken.models import Token [as 别名]
def test_admin_can_create_builds_on_other_apps(self, mock_requests):
        """If a user creates an application, an administrator should be able
        to push builds.
        """
        # create app as non-admin
        user = User.objects.get(username='autotest2')
        token = Token.objects.get(user=user).key
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)

        app_id = self.create_app()

        # post a new build as admin
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        url = "/v2/apps/{app_id}/builds".format(**locals())
        body = {'image': 'autotest/example'}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 201, response.data)

        build = Build.objects.get(uuid=response.data['uuid'])
        self.assertEqual(str(build), "{}-{}".format(
                         response.data['app'], str(response.data['uuid'])[:7])) 
开发者ID:deis,项目名称:controller,代码行数:23,代码来源:test_build.py

示例9: test_unauthorized_user_cannot_modify_build

# 需要导入模块: from rest_framework.authtoken import models [as 别名]
# 或者: from rest_framework.authtoken.models import Token [as 别名]
def test_unauthorized_user_cannot_modify_build(self, mock_requests):
        """
        An unauthorized user should not be able to modify other builds.

        Since an unauthorized user can't access the application, these
        requests should return a 403.
        """
        app_id = self.create_app()

        unauthorized_user = User.objects.get(username='autotest2')
        unauthorized_token = Token.objects.get(user=unauthorized_user).key
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + unauthorized_token)
        url = '/v2/apps/{}/builds'.format(app_id)
        body = {'image': 'foo'}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 403) 
开发者ID:deis,项目名称:controller,代码行数:18,代码来源:test_build.py

示例10: test_admin_can_manage_other_apps

# 需要导入模块: from rest_framework.authtoken import models [as 别名]
# 或者: from rest_framework.authtoken.models import Token [as 别名]
def test_admin_can_manage_other_apps(self, mock_requests, mock_logger):
        """Administrators of Deis should be able to manage all applications.
        """
        # log in as non-admin user and create an app
        user = User.objects.get(username='autotest2')
        token = Token.objects.get(user=user).key
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
        app_id = self.create_app()

        # log in as admin, check to see if they have access
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        url = '/v2/apps/{}'.format(app_id)
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200, response.data)
        # check app logs
        exp_msg = "autotest2 created initial release"
        exp_log_call = mock.call(logging.INFO, exp_msg)
        mock_logger.log.has_calls(exp_log_call)

        # TODO: test run needs an initial build
        # delete the app
        url = '/v2/apps/{}'.format(app_id)
        response = self.client.delete(url)
        self.assertEqual(response.status_code, 204, response.data) 
开发者ID:deis,项目名称:controller,代码行数:26,代码来源:test_app.py

示例11: test_admin_can_hook

# 需要导入模块: from rest_framework.authtoken import models [as 别名]
# 或者: from rest_framework.authtoken.models import Token [as 别名]
def test_admin_can_hook(self, mock_requests):
        """Administrator should be able to create build hooks on non-admin apps.
        """
        """Test creating a Push via the API"""
        user = User.objects.get(username='autotest2')
        token = Token.objects.get(user=user).key
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)

        app_id = self.create_app()
        # prepare a push body
        DOCKERFILE = """
        FROM busybox
        CMD /bin/true
        """
        body = {'receive_user': 'autotest',
                'receive_repo': app_id,
                'image': '{app_id}:v2'.format(**locals()),
                'sha': 'ecdff91c57a0b9ab82e89634df87e293d259a3aa',
                'dockerfile': DOCKERFILE}
        url = '/v2/hooks/build'
        response = self.client.post(url, body,
                                    HTTP_X_DEIS_BUILDER_AUTH=settings.BUILDER_KEY)
        self.assertEqual(response.status_code, 200, response.data)
        self.assertEqual(response.data['release']['version'], 2) 
开发者ID:deis,项目名称:controller,代码行数:26,代码来源:test_hooks.py

示例12: test_unauthorized_user_cannot_modify_domain

# 需要导入模块: from rest_framework.authtoken import models [as 别名]
# 或者: from rest_framework.authtoken.models import Token [as 别名]
def test_unauthorized_user_cannot_modify_domain(self):
        """
        An unauthorized user should not be able to modify other domains.

        Since an unauthorized user should not know about the application at all, these
        requests should return a 404.
        """
        app_id = self.create_app()

        unauthorized_user = User.objects.get(username='autotest2')
        unauthorized_token = Token.objects.get(user=unauthorized_user).key
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + unauthorized_token)

        url = '/v2/apps/{}/domains'.format(app_id)
        response = self.client.post(url, {'domain': 'example.com'})
        self.assertEqual(response.status_code, 403) 
开发者ID:deis,项目名称:controller,代码行数:18,代码来源:test_domain.py

示例13: post

# 需要导入模块: from rest_framework.authtoken import models [as 别名]
# 或者: from rest_framework.authtoken.models import Token [as 别名]
def post(self, request, format=None):
        data = request.data
        filetype = data.get('filetype')
        # if request.user.is_authenticated:
        # 构建鉴权对象
        q = Auth(configs.get('qiniu').get('AK'), configs.get('qiniu').get('SK'))

        # 生成图片名
        salt = ''.join(random.sample(string.ascii_letters + string.digits, 8))
        key = salt + '_' + str(int(time.time())) + '.' + filetype

        # 生成上传 Token,可以指定过期时间等
        token = q.upload_token(configs.get('qiniu').get('bucket_name'), key, 3600)
        return Response({"stateCode": 200, "token": token, "key": key}, 200)
        # else:
        #     return Response({"stateCode": 201, "msg": "您没有权限执行此操作"}, 201)


# 上传用户头像 
开发者ID:michwh,项目名称:onehome-server,代码行数:21,代码来源:views.py

示例14: test_log_auth_inactive_user

# 需要导入模块: from rest_framework.authtoken import models [as 别名]
# 或者: from rest_framework.authtoken.models import Token [as 别名]
def test_log_auth_inactive_user(self):
        # set up inactive user with token
        user = User.objects.create_user(username='myname', password='secret')
        token = Token.objects.create(user=user)
        token_header = 'Token %s' % token.key
        user.is_active = False
        user.save()

        # force login because regular client.login doesn't work for inactive users
        self.client.get('/token-auth-logging',
                        HTTP_AUTHORIZATION=token_header)

        # test
        log = APIRequestLog.objects.first()
        self.assertIsNone(log.user)
        self.assertIn("User inactive or deleted", log.response) 
开发者ID:aschn,项目名称:drf-tracking,代码行数:18,代码来源:test_mixins.py

示例15: get

# 需要导入模块: from rest_framework.authtoken import models [as 别名]
# 或者: from rest_framework.authtoken.models import Token [as 别名]
def get(self, request, format=None):
        """
        Update thumbnail and tiny file field
        """
        if request.user.is_anonymous:
            # User most login before they can get a token
            # This not only ensures the user has registered, and has an account
            # but that the account is active
            return Response('User not recognized.', status=status.HTTP_403_FORBIDDEN)

        data_dic = {}

        try:
            token = Token.objects.get(user=request.user)
            mystatus = status.HTTP_200_OK
        except:
            token = Token.objects.create(user=request.user)
            mystatus = status.HTTP_201_CREATED

        data_dic['token'] = token.key
        return Response(data_dic, status=mystatus) 
开发者ID:dkarchmer,项目名称:django-aws-template,代码行数:23,代码来源:api_views.py


注:本文中的rest_framework.authtoken.models.Token方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。