本文整理匯總了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)
示例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)
示例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
示例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
示例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)
示例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)
示例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)
示例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]))
示例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)
示例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)
示例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)
示例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)
示例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)
# 上傳用戶頭像
示例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)
示例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)