本文整理汇总了Python中max.tests.base.MaxTestApp.post方法的典型用法代码示例。如果您正苦于以下问题:Python MaxTestApp.post方法的具体用法?Python MaxTestApp.post怎么用?Python MaxTestApp.post使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类max.tests.base.MaxTestApp
的用法示例。
在下文中一共展示了MaxTestApp.post方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: FunctionalTests
# 需要导入模块: from max.tests.base import MaxTestApp [as 别名]
# 或者: from max.tests.base.MaxTestApp import post [as 别名]
class FunctionalTests(unittest.TestCase, MaxTestBase):
def setUp(self):
conf_dir = os.path.dirname(__file__)
self.app = loadapp('config:tests.ini', relative_to=conf_dir)
self.reset_database(self.app)
self.app.registry.max_store.security.insert(test_default_security)
self.patched_post = patch('requests.post', new=partial(mock_post, self))
self.patched_post.start()
self.testapp = MaxTestApp(self)
# BEGIN TESTS
def test_follow_user(self):
"""
"""
username = 'messi'
username2 = 'xavi'
self.create_user(username)
self.create_user(username2)
res = self.testapp.post('/people/%s/follows/%s' % (username, username2), '', oauth2Header(username), status=201)
self.assertEqual(res.json['verb'], 'follow')
res = self.testapp.get('/people/%s' % (username), '', oauth2Header(username), status=200)
self.assertEqual(username2, res.json['following'][0]['username'])
def test_user_sees_followed_activity(self):
"""
"""
from .mockers import user_status
username = 'messi'
username2 = 'xavi'
self.create_user(username)
self.create_user(username2)
self.create_activity(username, user_status)
self.create_activity(username2, user_status)
res = self.testapp.post('/people/%s/follows/%s' % (username, username2), '', oauth2Header(username), status=201)
self.assertEqual(res.json['verb'], 'follow')
res = self.testapp.get('/people/%s/timeline' % (username), '', oauth2Header(username), status=200)
self.assertEqual(len(res.json), 2)
示例2: SecurityACLTests
# 需要导入模块: from max.tests.base import MaxTestApp [as 别名]
# 或者: from max.tests.base.MaxTestApp import post [as 别名]
class SecurityACLTests(unittest.TestCase, MaxTestBase):
def setUp(self):
conf_dir = os.path.dirname(os.path.dirname(__file__))
self.app = loadapp('config:tests.ini', relative_to=conf_dir)
self.app.registry.max_store.drop_collection('users')
self.app.registry.max_store.drop_collection('activity')
self.app.registry.max_store.drop_collection('contexts')
self.app.registry.max_store.drop_collection('security')
self.app.registry.max_store.drop_collection('conversations')
self.app.registry.max_store.drop_collection('messages')
self.app.registry.max_store.security.insert(test_default_security)
self.patched_post = patch('requests.post', new=partial(mock_post, self))
self.patched_post.start()
self.testapp = MaxTestApp(self)
self.create_user(test_manager)
def test_forbidden_access_to_security_settings(self):
"""
Given i'm a regular user
When i try to interact with security endpoints
Then i get a Forbidden Exception
"""
username = 'sheldon'
self.testapp.get('/admin/security', headers=oauth2Header(username), status=403)
self.testapp.get('/admin/security/users', headers=oauth2Header(username), status=403)
self.testapp.get('/admin/security/roles/Manager/users/test_manager', headers=oauth2Header(username), status=403)
self.testapp.post('/admin/security/roles/Manager/users/test_manager', headers=oauth2Header(username), status=403)
self.testapp.delete('/admin/security/roles/Manager/users/test_manager', headers=oauth2Header(username), status=403)
def test_access_to_security_settings(self):
"""
Given i'm a Manager user
When i try to interact with security endpoints
Then i suceed
"""
self.testapp.get('/admin/security', headers=oauth2Header(test_manager), status=200)
self.testapp.get('/admin/security/users', headers=oauth2Header(test_manager), status=200)
self.testapp.get('/admin/security/roles/Manager/users/test_manager', headers=oauth2Header(test_manager), status=200)
self.testapp.post('/admin/security/roles/Manager/users/test_manager', headers=oauth2Header(test_manager), status=200)
self.testapp.delete('/admin/security/roles/Manager/users/test_manager', headers=oauth2Header(test_manager), status=204)
示例3: ActivitiesACLTests
# 需要导入模块: from max.tests.base import MaxTestApp [as 别名]
# 或者: from max.tests.base.MaxTestApp import post [as 别名]
class ActivitiesACLTests(unittest.TestCase, MaxTestBase):
def setUp(self):
conf_dir = os.path.dirname(os.path.dirname(__file__))
self.app = loadapp('config:tests.ini', relative_to=conf_dir)
self.app.registry.max_store.drop_collection('users')
self.app.registry.max_store.drop_collection('activity')
self.app.registry.max_store.drop_collection('contexts')
self.app.registry.max_store.drop_collection('security')
self.app.registry.max_store.drop_collection('conversations')
self.app.registry.max_store.drop_collection('messages')
self.app.registry.max_store.security.insert(test_default_security)
self.patched_post = patch('requests.post', new=partial(mock_post, self))
self.patched_post.start()
self.testapp = MaxTestApp(self)
self.create_user(test_manager)
# Favorite activities tests
def test_favorite(self):
"""
Given i'm a regular user
When i try to favorite an activity
I succeed
"""
from max.tests.mockers import user_status
username = 'messi'
username_not_me = 'xavi'
self.create_user(username)
self.create_user(username_not_me)
res = self.create_activity(username, user_status)
activity_id = res.json['id']
self.testapp.post('/activities/%s/favorites' % activity_id, '', oauth2Header(username_not_me), status=201)
def test_favorite_impersonate(self):
"""
Given i'm a regular user
When i try to favorite an activity impersonated as another user
I get a Forbidden Exception
"""
from max.tests.mockers import user_status
username = 'messi'
username_not_me = 'xavi'
self.create_user(username)
self.create_user(username_not_me)
res = self.create_activity(username, user_status)
activity_id = res.json['id']
impersonated_actor = {'actor': {'objectType': 'person', 'username': username}}
self.testapp.post('/activities/%s/favorites' % activity_id, json.dumps(impersonated_actor), oauth2Header(username_not_me), status=403)
def test_favorite_impersonate_as_manager(self):
"""
Given i'm a Manager user
When i try to favorite an activity impersonated as another user
I get a Forbidden Exception
"""
from max.tests.mockers import user_status
username = 'messi'
username_not_me = 'xavi'
self.create_user(username)
self.create_user(username_not_me)
res = self.create_activity(username, user_status)
activity_id = res.json['id']
impersonated_actor = {'actor': {'objectType': 'person', 'username': username}}
self.testapp.post('/activities/%s/favorites' % activity_id, json.dumps(impersonated_actor), oauth2Header(test_manager), status=201)
# Unfavorite activities tests
def test_unfavorite(self):
"""
Given i'm a regular user
When i try to unfavorite an activity
I succeed
"""
from max.tests.mockers import user_status
username = 'messi'
username_not_me = 'xavi'
self.create_user(username)
self.create_user(username_not_me)
res = self.create_activity(username, user_status)
activity_id = res.json['id']
self.testapp.post('/activities/%s/favorites' % activity_id, '', oauth2Header(username_not_me), status=201)
self.testapp.delete('/activities/%s/favorites/%s' % (activity_id, username_not_me), '', oauth2Header(username_not_me), status=200)
def test_unfavorite_impersonate(self):
"""
Given i'm a regular user
When i try to unfavorite an activity imperonsated as another user
I get a Forbidden Exception
"""
from max.tests.mockers import user_status
username = 'messi'
username_not_me = 'xavi'
self.create_user(username)
self.create_user(username_not_me)
res = self.create_activity(username, user_status)
activity_id = res.json['id']
self.testapp.post('/activities/%s/favorites' % activity_id, '', oauth2Header(username_not_me), status=201)
#.........这里部分代码省略.........
示例4: FunctionalTests
# 需要导入模块: from max.tests.base import MaxTestApp [as 别名]
# 或者: from max.tests.base.MaxTestApp import post [as 别名]
class FunctionalTests(unittest.TestCase, MaxTestBase, MaxAvatarsTestBase):
def setUp(self):
self.conf_dir = os.path.dirname(__file__)
self.app = loadapp('config:tests.ini', relative_to=self.conf_dir)
self.reset_database(self.app)
self.app.registry.max_store.security.insert(test_default_security)
self.patched_post = patch('requests.post', new=partial(mock_post, self))
self.patched_post.start()
self.testapp = MaxTestApp(self)
self.create_user(test_manager)
def tearDown(self):
shutil.rmtree('{}/exceptions'.format(self.conf_dir))
# BEGIN TESTS
def test_root(self):
"""
Test site root is accessible and returns html
"""
res = self.testapp.get('/', status=200)
self.assertEqual(res.content_type, 'text/html')
def test_bad_test_call_warning(self):
"""
Test calling a service with missing body parameter, and the authorization as body.
As this will only probably happen in tests, The error message is targeted so.
"""
username = 'messi'
self.create_user(username)
res = self.testapp.post('/people/%s/activities' % username, oauth2Header(test_manager), status=401)
self.assertEqual(res.json['error_description'], u'Authorization found in url params, not in request. Check your tests, you may be passing the authentication headers as the request body...')
@patch('max.models.user.User.insert', fucked_up_insert)
def test_generic_exception_catching(self):
"""
Test calling a webservice mocked to force an exception, to test the scavenger
that formats beautiful json error messages for uncatched exceptions
"""
username = 'messi'
res = self.create_user(username, expect=500)
self.assertEqual(res.json['error'], 'ServerError')
self.assertIn('BEGIN EXCEPTION REPORT', res.json['error_description'])
self.assertIn('END EXCEPTION REPORT', res.json['error_description'])
def test_bad_body_content_parsing(self):
"""
Test calling a service with a list on post body, that expects a json object.
It should fail
"""
username = 'messi'
self.testapp.post('/people/%s' % username, '[]', oauth2Header(username), status=400)
def test_post_tunneling_on_delete(self):
"""
Test that calling a endpoint with DELETE indirectly within a POST
actually calls the real delete method
"""
from .mockers import user_status
username = 'messi'
self.create_user(username)
res = self.create_activity(username, user_status)
activity_id = res.json['id']
headers = oauth2Header(test_manager)
headers['X-HTTP-Method-Override'] = 'DELETE'
self.testapp.post('/activities/{}'.format(activity_id), '', headers, status=204)
def test_compat_id_match(self):
"""
"""
username = 'messi'
self.create_user(username)
headers = oauth2Header(username)
headers['X-Max-Compat-ID'] = 'test'
self.testapp.get('/people', headers=headers, status=200)
def test_compat_id_mismatch(self):
"""
"""
username = 'messi'
self.create_user(username)
headers = oauth2Header(username)
headers['X-Max-Compat-ID'] = 'test2'
self.testapp.get('/people', headers=headers, status=412)
def test_post_tunneling_on_put(self):
"""
Test that calling a endpoint with PUT indirectly within a POST
actually calls the real PUT method
"""
username = 'messi'
self.create_user(username)
headers = oauth2Header(username)
headers['X-HTTP-Method-Override'] = 'PUT'
res = self.testapp.post('/people/{}'.format(username), json.dumps({"displayName": "Lionel Messi"}), headers, status=200)
self.assertEqual(res.request.method, 'PUT')
self.assertEqual(res.json['displayName'], 'Lionel Messi')
#.........这里部分代码省略.........
示例5: PeopleACLTests
# 需要导入模块: from max.tests.base import MaxTestApp [as 别名]
# 或者: from max.tests.base.MaxTestApp import post [as 别名]
class PeopleACLTests(unittest.TestCase, MaxTestBase):
def setUp(self):
conf_dir = os.path.dirname(os.path.dirname(__file__))
self.app = loadapp('config:tests.ini', relative_to=conf_dir)
self.app.registry.max_store.drop_collection('users')
self.app.registry.max_store.drop_collection('activity')
self.app.registry.max_store.drop_collection('contexts')
self.app.registry.max_store.drop_collection('security')
self.app.registry.max_store.drop_collection('conversations')
self.app.registry.max_store.drop_collection('messages')
self.app.registry.max_store.security.insert(test_default_security)
self.patched_post = patch('requests.post', new=partial(mock_post, self))
self.patched_post.start()
self.testapp = MaxTestApp(self)
# Add people tests
def test_create_person_as_manager(self):
"""
Given i'm a user that has the Manager role
When i try to create a person
I succeed
"""
username = 'sheldon'
self.testapp.post('/people', json.dumps({"username": username}), headers=oauth2Header(test_manager), status=201)
def test_create_person_as_non_manager(self):
"""
Given i'm user that doesn't have the Manager role
When i try to create a person
I get a Forbidden exception
"""
username = 'sheldon'
other = 'penny'
self.testapp.post('/people', json.dumps({"username": other}), headers=oauth2Header(username), status=403)
def test_create_person_as_oneself(self):
"""
Given i'm user that doesn't have the Manager role
When i try to create a person
I succeed
"""
username = 'sheldon'
self.testapp.post('/people', json.dumps({"username": username}), headers=oauth2Header(username), status=201)
# View profile tests
def test_get_person_as_manager(self):
"""
Given i'm a user that has the Manager role
When i try to view a user profile
I succeed
"""
username = 'sheldon'
self.create_user(test_manager)
self.create_user(username)
self.testapp.get('/people/{}'.format(username), "", headers=oauth2Header(test_manager), status=200)
def test_get_person_as_non_manager(self):
"""
Given i'm a user that doesn't have the Manager role
When i try to view a user profile
I succeed
"""
username = 'sheldon'
self.create_user(test_manager)
self.create_user(username)
self.testapp.get('/people/{}'.format(test_manager), "", headers=oauth2Header(username), status=200)
# Get all people
def test_get_all_people_as_manager(self):
"""
Given i'm a user that has the Manager role
When i try to get all people
I succeed
"""
self.create_user(test_manager)
self.testapp.get('/people', "", headers=oauth2Header(test_manager), status=200)
def test_get_all_people_as_non_manager(self):
"""
Given i'm a user that doesn't have the Manager role
When i try to get a visible people listing
I suceed
"""
username = 'sheldon'
self.create_user(test_manager)
self.create_user(username)
self.testapp.get('/people', "", headers=oauth2Header(username), status=200)
# Modify user tests
def test_modify_person_as_manager(self):
"""
#.........这里部分代码省略.........
示例6: FunctionalTests
# 需要导入模块: from max.tests.base import MaxTestApp [as 别名]
# 或者: from max.tests.base.MaxTestApp import post [as 别名]
class FunctionalTests(unittest.TestCase, MaxTestBase):
def setUp(self):
conf_dir = os.path.dirname(__file__)
self.app = loadapp('config:tests.ini', relative_to=conf_dir)
self.reset_database(self.app)
self.app.registry.max_store.security.insert(test_default_security)
self.patched_post = patch('requests.post', new=partial(mock_post, self))
self.patched_post.start()
self.testapp = MaxTestApp(self)
self.create_user(test_manager)
# BEGIN TESTS
def test_create_context(self):
""" doctests .. http:post:: /contexts"""
from .mockers import create_context
new_context = dict(create_context)
self.testapp.post('/contexts', json.dumps(new_context), oauth2Header(test_manager), status=201)
def test_create_context_creator_is_admin(self):
"""
Given a admin user
When I create a context
Then the creator of the context is the admin user
"""
from .mockers import create_context
res = self.testapp.post('/contexts', json.dumps(create_context), oauth2Header(test_manager), status=201)
self.assertEqual(res.json['creator'], test_manager)
def test_create_context_default_fields(self):
"""
Given an admin user
When I create a context
Then non-required fields with defaults are set
"""
from .mockers import create_context
res = self.testapp.post('/contexts', json.dumps(create_context), oauth2Header(test_manager), status=201)
self.assertIn('permissions', res.json)
self.assertIn('tags', res.json)
self.assertIn('objectType', res.json)
self.assertEqual(res.json['objectType'], 'context')
def test_post_activity_with_public_context(self):
""" Post an activity to a context which allows everyone to read and write
"""
from .mockers import subscribe_context, create_context
from .mockers import user_status_context
username = 'messi'
self.create_user(username)
self.create_context(create_context)
self.admin_subscribe_user_to_context(username, subscribe_context)
res = self.create_activity(username, user_status_context)
result = json.loads(res.text)
self.assertEqual(result.get('actor', None).get('username', None), 'messi')
self.assertEqual(result.get('object', None).get('objectType', None), 'note')
self.assertEqual(result.get('contexts', None)[0]['url'], subscribe_context['object']['url'])
def test_post_activity_with_private_read_write_context(self):
""" Post an activity to a context which needs the user to be subscribed to read and write
and we have previously subscribed the user.
"""
from .mockers import subscribe_context, create_context
from .mockers import user_status_context
from hashlib import sha1
username = 'messi'
self.create_user(username)
url_hash = sha1(create_context['url']).hexdigest()
context_permissions = dict(read='subscribed', write='subscribed', subscribe='restricted', invite='restricted')
self.create_context(create_context, permissions=context_permissions)
self.admin_subscribe_user_to_context(username, subscribe_context)
res = self.create_activity(username, user_status_context)
result = json.loads(res.text)
self.assertEqual(result.get('actor', None).get('username', None), 'messi')
self.assertEqual(result.get('object', None).get('objectType', None), 'note')
self.assertEqual(result.get('contexts', None)[0]['url'], subscribe_context['object']['url'])
return url_hash, username, user_status_context
def test_post_activity_with_private_read_context(self):
""" Try to post an activity to a context which needs the user to be subscribed to read
but needs to explicitly give write permission on the user to post and we have previously
subscribed the user but not given write permission.
"""
from .mockers import subscribe_context, create_context
from .mockers import user_status_context
username = 'messi'
self.create_user(username)
context_permissions = dict(read='subscribed', write='restricted', subscribe='restricted', invite='restricted')
self.create_context(create_context, permissions=context_permissions)
self.admin_subscribe_user_to_context(username, subscribe_context)
self.create_activity(username, user_status_context, expect=403)
def test_add_public_context(self):
from hashlib import sha1
from .mockers import create_context
#.........这里部分代码省略.........
示例7: FunctionalTests
# 需要导入模块: from max.tests.base import MaxTestApp [as 别名]
# 或者: from max.tests.base.MaxTestApp import post [as 别名]
class FunctionalTests(unittest.TestCase, MaxTestBase):
def setUp(self):
conf_dir = os.path.dirname(__file__)
self.app = loadapp('config:tests_restricted_user_visibility.ini', relative_to=conf_dir)
self.reset_database(self.app)
self.app.registry.max_store.security.insert(test_default_security)
self.patched_post = patch('requests.post', new=partial(mock_post, self))
self.patched_post.start()
self.testapp = MaxTestApp(self)
self.create_user(test_manager)
def tearDown(self):
import pyramid.testing
pyramid.testing.tearDown()
# ############################################################################################################################
#
# !!! IMPORTANT INFO !!! All this tests are run with the max.restricted_user_visibility_mode=True set in the .ini
# Tests for NonVisible users without restricted_user_visibility live in test_nonvisible.py, wich uses a different .ini
#
##############################################################################################################################
# Tests for listing people without sharing contexts (2 tests)
def test_get_people_as_a_nonvisible_user_without_subscriptions(self):
"""
Given i'm a nonvisible user
When I search users
And we don't share any context subscription
Then I cannot see any of them
"""
username_visible1 = 'user1'
username_visible2 = 'user2'
username_nonvisible1 = 'usernonvisible1'
username_nonvisible2 = 'usernonvisible2'
self.create_user(username_visible1)
self.create_user(username_visible2)
self.create_user(username_nonvisible1)
self.create_user(username_nonvisible2)
self.testapp.post('/admin/security/roles/%s/users/%s' % ('NonVisible', username_nonvisible1), "", oauth2Header(test_manager), status=201)
self.testapp.post('/admin/security/roles/%s/users/%s' % ('NonVisible', username_nonvisible2), "", oauth2Header(test_manager), status=201)
res = self.testapp.get('/people', "", oauth2Header(username_nonvisible1), status=200)
self.assertEqual(len(res.json), 0)
def test_get_people_as_visible_user_without_subscriptions(self):
"""
Given i'm a visible user
When I search users
And we don't share any context subscription
Then I cannot see any of them
"""
username_visible1 = 'user1'
username_visible2 = 'user2'
username_nonvisible1 = 'usernonvisible1'
username_nonvisible2 = 'usernonvisible2'
self.create_user(username_visible1)
self.create_user(username_visible2)
self.create_user(username_nonvisible1)
self.create_user(username_nonvisible2)
self.testapp.post('/admin/security/roles/%s/users/%s' % ('NonVisible', username_nonvisible1), "", oauth2Header(test_manager), status=201)
self.testapp.post('/admin/security/roles/%s/users/%s' % ('NonVisible', username_nonvisible2), "", oauth2Header(test_manager), status=201)
res = self.testapp.get('/people', "", oauth2Header(username_visible1), status=200)
self.assertEqual(len(res.json), 0)
# Tests for listing people sharing contexts (2 tests)
def test_get_people_as_nonvisible_user(self):
"""
Given i'm a nonvisible person
When I search users
Then I can see all people on the same contexts as I, including other nonvisible users
"""
from .mockers import subscribe_context, create_context
username_visible1 = 'user1'
username_visible2 = 'user2'
username_nonvisible1 = 'usernonvisible1'
username_nonvisible2 = 'usernonvisible2'
self.create_user(username_visible1)
self.create_user(username_visible2)
self.create_user(username_nonvisible1)
self.create_user(username_nonvisible2)
self.create_context(create_context)
self.admin_subscribe_user_to_context(username_visible1, subscribe_context)
self.admin_subscribe_user_to_context(username_nonvisible1, subscribe_context)
self.admin_subscribe_user_to_context(username_nonvisible2, subscribe_context)
self.testapp.post('/admin/security/roles/%s/users/%s' % ('NonVisible', username_nonvisible1), "", oauth2Header(test_manager), status=201)
self.testapp.post('/admin/security/roles/%s/users/%s' % ('NonVisible', username_nonvisible2), "", oauth2Header(test_manager), status=201)
#.........这里部分代码省略.........
示例8: DeprecationTests
# 需要导入模块: from max.tests.base import MaxTestApp [as 别名]
# 或者: from max.tests.base.MaxTestApp import post [as 别名]
class DeprecationTests(unittest.TestCase, MaxTestBase):
def setUp(self):
conf_dir = os.path.dirname(__file__)
self.app = loadapp('config:tests.ini', relative_to=conf_dir)
self.reset_database(self.app)
self.app.registry.max_store.security.insert(test_default_security)
self.patched_post = patch('requests.post', new=partial(mock_post, self))
self.patched_post.start()
self.testapp = MaxTestApp(self)
self.create_user(test_manager)
# Test deprecated Add people
def test_deprecated_request_create_user(self):
"""
Given a request to the deprecated POST /people/{username}
When the request is processed
Then the request is rewrited as POST /people
And the username is now in the body
And the displayName is preserved in the body
"""
res = self.testapp.post('/people/sheldon', json.dumps({"displayName": 'Sheldon'}), headers=oauth2Header(test_manager), status=201)
rewrited_request = res.request
rewrited_request_url = urlparse.urlparse(rewrited_request.url).path
self.assertEqual(rewrited_request_url, '/people')
self.assertEqual(res.json['username'], 'sheldon')
self.assertEqual(res.json['displayName'], 'Sheldon')
# Test deprecated subscribe user
def test_deprecated_subscribe_user(self):
"""
Given a request to the deprecated POST /people/{username}/subscriptions
When the request is processed
Then the request is rewrited as POST /contexts/{hash}/subscriptions
And the actor now is in the body
"""
from max.tests.mockers import create_context, subscribe_context
username = 'sheldon'
self.create_user(username)
res = self.create_context(create_context)
context_hash = res.json['hash']
res = self.testapp.post('/people/%s/subscriptions' % username, json.dumps(subscribe_context), oauth2Header(test_manager), status=201)
rewrited_request = res.request
rewrited_request_url = urlparse.urlparse(rewrited_request.url).path
self.assertEqual(rewrited_request_url, '/contexts/{}/subscriptions'.format(context_hash))
self.assertEqual(res.json['actor']['username'], 'sheldon')
self.assertEqual(res.json['actor']['objectType'], 'person')
# Test deprecated unsubscribe user
def test_deprecated_unsubscribe_user(self):
"""
Given a request to the deprecated DELETE /people/{username}/subscriptions
When the request is processed
Then the request is rewrited as DELETE /contexts/{hash}/subscriptions
And the actor now is in the body
"""
from max.tests.mockers import create_context, subscribe_context
username = 'sheldon'
self.create_user(username)
res = self.create_context(create_context)
context_hash = res.json['hash']
self.admin_subscribe_user_to_context(username, subscribe_context)
res = self.testapp.delete('/people/%s/subscriptions/%s' % (username, context_hash), "", oauth2Header(test_manager), status=204)
rewrited_request = res.request
rewrited_request_url = urlparse.urlparse(rewrited_request.url).path
self.assertEqual(rewrited_request_url, '/contexts/{}/subscriptions/{}'.format(context_hash, username))
# Test deprecated create context activity
def test_deprecated_user_post_activity_to_context(self):
"""
Given a request to the deprecated POST /people/{username}/activities
And the request has a "contexts" parameter
When the request is processed
Then the request is rewrited as POST /contexts/{hash}/activities
And the actor is in the body
and object is preserved in the body
"""
from max.tests.mockers import create_context, subscribe_context
from max.tests.mockers import user_status_context as activity
username = 'sheldon'
self.create_user(username)
res = self.create_context(create_context)
#.........这里部分代码省略.........
示例9: FunctionalTests
# 需要导入模块: from max.tests.base import MaxTestApp [as 别名]
# 或者: from max.tests.base.MaxTestApp import post [as 别名]
class FunctionalTests(unittest.TestCase, MaxTestBase):
def setUp(self):
conf_dir = os.path.dirname(__file__)
self.app = loadapp('config:tests.ini', relative_to=conf_dir)
self.reset_database(self.app)
self.app.registry.max_store.security.insert(test_default_security)
self.patched_post = patch('requests.post', new=partial(mock_post, self))
self.patched_post.start()
self.testapp = MaxTestApp(self)
def tearDown(self):
import pyramid.testing
pyramid.testing.tearDown()
def test_add_nonvisible_role(self):
username = 'messi'
self.create_user(username)
self.testapp.post('/admin/security/roles/%s/users/%s' % ('NonVisible', username), "", oauth2Header(test_manager), status=201)
# ############################################################################################################################
#
# !!! IMPORTANT INFO !!! All this tests are run with the max.restricted_user_visibility_mode=False in set the .ini
# Tests for NonVisible users WITH restricted_user_visibility live in test_restricted_user_visibility.py, wich uses a different .ini
#
# The tests on this file are the same tests on test_restricted_user_visibility.py but WITHOUT excluding the ones that test
# Users with shared contexts, And with different asserts, as here we have the restricted_user_visibility disabled,
# and shared subscriptions doesn't affect us
#
##############################################################################################################################
# Tests for listing people without sharing contexts (2 tests)
def test_get_people_as_a_nonvisible_user_without_subscriptions(self):
"""
Given i'm a nonvisible user
When I search users
Then I see everyone
"""
username_visible1 = 'user1'
username_visible2 = 'user2'
username_nonvisible1 = 'usernonvisible1'
username_nonvisible2 = 'usernonvisible2'
self.create_user(username_visible1)
self.create_user(username_visible2)
self.create_user(username_nonvisible1)
self.create_user(username_nonvisible2)
self.testapp.post('/admin/security/roles/%s/users/%s' % ('NonVisible', username_nonvisible1), "", oauth2Header(test_manager), status=201)
self.testapp.post('/admin/security/roles/%s/users/%s' % ('NonVisible', username_nonvisible2), "", oauth2Header(test_manager), status=201)
res = self.testapp.get('/people', "", oauth2Header(username_nonvisible1), status=200)
self.assertEqual(len(res.json), 4)
self.assertEqual(res.json[0]['username'], username_nonvisible2)
self.assertEqual(res.json[1]['username'], username_nonvisible1)
self.assertEqual(res.json[2]['username'], username_visible2)
self.assertEqual(res.json[3]['username'], username_visible1)
def test_get_people_as_visible_user_without_subscriptions(self):
"""
Given i'm a visible user
When I search users
Then I only see the visible ones
"""
username_visible1 = 'user1'
username_visible2 = 'user2'
username_nonvisible1 = 'usernonvisible1'
username_nonvisible2 = 'usernonvisible2'
self.create_user(username_visible1)
self.create_user(username_visible2)
self.create_user(username_nonvisible1)
self.create_user(username_nonvisible2)
self.testapp.post('/admin/security/roles/%s/users/%s' % ('NonVisible', username_nonvisible1), "", oauth2Header(test_manager), status=201)
self.testapp.post('/admin/security/roles/%s/users/%s' % ('NonVisible', username_nonvisible2), "", oauth2Header(test_manager), status=201)
res = self.testapp.get('/people', "", oauth2Header(username_visible1), status=200)
self.assertEqual(len(res.json), 2)
self.assertEqual(res.json[0]['username'], username_visible2)
self.assertEqual(res.json[1]['username'], username_visible1)
# Tests for start Conversations without sharing contexts (4 tests)
def test_start_conversation_with_visible_as_nonvisible_without_sharing_contexts(self):
from .mockers import message
"""
Given i'm a nonvisible person
When I try to start a conversation with a visible
Then I can start the conversation
"""
username_visible1 = 'user1'
username_nonvisible1 = 'usernonvisible1'
self.create_user(username_visible1)
self.create_user(username_nonvisible1)
#.........这里部分代码省略.........
示例10: FunctionalTests
# 需要导入模块: from max.tests.base import MaxTestApp [as 别名]
# 或者: from max.tests.base.MaxTestApp import post [as 别名]
class FunctionalTests(unittest.TestCase, MaxTestBase):
def setUp(self):
conf_dir = os.path.dirname(__file__)
self.app = loadapp("config:tests.ini", relative_to=conf_dir)
self.reset_database(self.app)
self.app.registry.max_store.security.insert(test_default_security)
self.patched_post = patch("requests.post", new=partial(mock_post, self))
self.patched_post.start()
self.testapp = MaxTestApp(self)
def tearDown(self):
import pyramid.testing
pyramid.testing.tearDown()
# BEGIN TESTS
def test_create_user(self):
username = "messi"
self.testapp.post("/people/%s" % username, "", oauth2Header(test_manager), status=201)
return username
def test_create_user_missing_username(self):
self.testapp.post("/people", json.dumps({}), oauth2Header(test_manager), status=400)
def test_create_user_creator_is_admin(self):
"""
Given an admin user
When I create a user
Then the creator must be the admin user
"""
username = "messi"
res = self.testapp.post("/people/%s" % username, "", oauth2Header(test_manager), status=201)
self.assertEqual(res.json["creator"], test_manager)
def test_create_user_default_fields(self):
"""
Given an admin user
When I create a user
Then non-required fields with defaults are set
"""
username = "messi"
res = self.testapp.post("/people/%s" % username, "", oauth2Header(test_manager), status=201)
self.assertIn("objectType", res.json)
self.assertIn("following", res.json)
self.assertIn("subscribedTo", res.json)
self.assertEqual(res.json["objectType"], "person")
def test_create_user_not_manager(self):
username = "messi"
self.testapp.post("/people/%s" % username, "", oauth2Header("imnotallowed"), status=403)
def test_user_exist(self):
username = "messi"
self.create_user(username)
res = self.testapp.post("/people/%s" % username, "", oauth2Header(test_manager), status=200)
result = json.loads(res.text)
self.assertEqual(result.get("username", None), "messi")
def test_create_same_user_case_insensitive(self):
username = "messi"
username_case = "Messi"
self.create_user(username)
res = self.testapp.post("/people/%s" % username_case, "", oauth2Header(test_manager), status=200)
result = json.loads(res.text)
self.assertEqual(result.get("username", None), "messi")
def test_get_user(self):
""" Doctest .. http:get:: /people/{username} """
username = "messi"
self.create_user(username)
res = self.testapp.get("/people/%s" % username, "", oauth2Header(username), status=200)
result = json.loads(res.text)
self.assertEqual(result.get("username", None), "messi")
self.assertIn("username", result)
self.assertIn("displayName", result)
self.assertIn("objectType", result)
self.assertGreater(len(result.keys()), 3)
def test_get_user_as_someone_else(self):
""" Doctest .. http:get:: /people/{username} """
username = "messi"
usernamenotme = "xavi"
self.create_user(username)
self.create_user(usernamenotme)
res = self.testapp.get("/people/%s" % username, "", oauth2Header(usernamenotme), status=200)
result = json.loads(res.text)
self.assertEqual(result.get("username", None), "messi")
self.assertIn("username", result)
self.assertIn("displayName", result)
self.assertIn("objectType", result)
self.assertIn("published", result)
self.assertEqual(len(result.keys()), 4)
def test_get_user_case_insensitive(self):
""" Doctest .. http:get:: /people/{username} """
username = "messi"
self.create_user(username)
#.........这里部分代码省略.........
示例11: AvatarsACLTests
# 需要导入模块: from max.tests.base import MaxTestApp [as 别名]
# 或者: from max.tests.base.MaxTestApp import post [as 别名]
class AvatarsACLTests(unittest.TestCase, MaxTestBase, MaxAvatarsTestBase):
def setUp(self):
self.conf_dir = os.path.dirname(os.path.dirname(__file__))
self.app = loadapp('config:tests.ini', relative_to=self.conf_dir)
self.app.registry.max_store.drop_collection('users')
self.app.registry.max_store.drop_collection('activity')
self.app.registry.max_store.drop_collection('contexts')
self.app.registry.max_store.drop_collection('security')
self.app.registry.max_store.drop_collection('conversations')
self.app.registry.max_store.drop_collection('messages')
self.app.registry.max_store.security.insert(test_default_security)
self.patched_post = patch('requests.post', new=partial(mock_post, self))
self.patched_post.start()
self.testapp = MaxTestApp(self)
self.create_user(test_manager)
MaxAvatarsTestBase.setUp(self)
def tearDown(self):
"""
Deletes test avatar folder with all test images
"""
self.patched_post.stop()
MaxAvatarsTestBase.tearDown(self)
# Add person avatar tests
def test_add_user_avatar(self):
"""
Given i'm a regular user
When i try to update my avatar
I succeed
"""
username = 'sheldon'
self.create_user(username)
avatar_file = open(os.path.join(self.conf_dir, "avatar.png"), "rb")
files = [('image', 'avatar.png', avatar_file.read(), 'image/png')]
self.testapp.post('/people/{}/avatar'.format(username), '', headers=oauth2Header(username), upload_files=files, status=201)
def test_add_user_avatar_as_manager(self):
"""
Given i'm a regular user
When i try to update my avatar
I succeed
"""
username = 'sheldon'
self.create_user(username)
avatar_file = open(os.path.join(self.conf_dir, "avatar.png"), "rb")
files = [('image', 'avatar.png', avatar_file.read(), 'image/png')]
self.testapp.post('/people/{}/avatar'.format(username), '', headers=oauth2Header(test_manager), upload_files=files, status=201)
def test_add_other_user_avatar(self):
"""
Given i'm a regular user
When i try to update another user's avatar
I get a Forbidden Exception
"""
username = 'sheldon'
self.create_user(username)
other = 'penny'
self.create_user(other)
avatar_file = open(os.path.join(self.conf_dir, "avatar.png"), "rb")
files = [('image', 'avatar.png', avatar_file.read(), 'image/png')]
self.testapp.post('/people/{}/avatar'.format(username), '', headers=oauth2Header(other), upload_files=files, status=403)
# Get person avatar tests, Large avatars acl's are not tested as is the same endpoint
def test_get_user_avatar_unauthenticated(self):
"""
Given i'm a unauthenticated user
When I try to get a user avatar
I succeed
"""
username = 'sheldon'
self.create_user(username)
self.upload_user_avatar(username, "avatar.png")
self.testapp.get('/people/%s/avatar' % username, '', {}, status=200)
# Get twitter avatar tests
def test_get_twitter_avatar_unauthenticated(self):
"""
Given i'm a unauthenticated user
When I try to get a context avatar coming from twitter
I succeed
"""
from hashlib import sha1
from max.tests.mockers import create_context_full
avatar_image = os.path.join(self.conf_dir, "avatar.png")
http_mock_twitter_user_image(avatar_image)
self.testapp.post('/contexts', json.dumps(create_context_full), oauth2Header(test_manager), status=201)
url_hash = sha1(create_context_full['url']).hexdigest()
#.........这里部分代码省略.........
示例12: AvatarTests
# 需要导入模块: from max.tests.base import MaxTestApp [as 别名]
# 或者: from max.tests.base.MaxTestApp import post [as 别名]
class AvatarTests(unittest.TestCase, MaxTestBase, MaxAvatarsTestBase):
"""
Tests to check the uploading, downlading and retrieving of all
avatar types used in max, included the ones coming from twitter.
"""
def setUp(self):
self.conf_dir = os.path.dirname(__file__)
self.app = loadapp('config:tests.ini', relative_to=self.conf_dir)
self.reset_database(self.app)
self.app.registry.max_store.security.insert(test_default_security)
self.app.registry.max_store.cloudapis.insert(test_cloudapis)
self.patched_post = patch('requests.post', new=partial(mock_post, self))
self.patched_post.start()
self.patched_get = patch('requests.get', new=partial(mock_get, self))
self.patched_get.start()
self.testapp = MaxTestApp(self)
self.create_user(test_manager)
MaxAvatarsTestBase.setUp(self)
def tearDown(self):
"""
Deletes test avatar folder with all test images
"""
self.patched_post.stop()
self.patched_get.stop()
MaxAvatarsTestBase.tearDown(self)
# BEGIN TESTS
def test_upload_user_avatar(self):
"""
Given a user without avatar
When I upload an image for that user
Then a normal 48x48 image is stored in the correct avatar folder
And a large 250x250 image is stored in the correct avatar folder
"""
username = 'messi'
self.create_user(username)
avatar_file = open(os.path.join(self.conf_dir, "avatar.png"), "rb")
files = [('image', 'avatar.png', avatar_file.read(), 'image/png')]
self.testapp.post('/people/{}/avatar'.format(username), '', headers=oauth2Header(username), upload_files=files, status=201)
self.assertEqual(self.get_user_avatar_dimensions(username), (48, 48))
self.assertEqual(self.get_user_avatar_dimensions(username, 'large'), (250, 250))
def test_invalid_upload_user_avatar(self):
"""
Given a user without avatar
When I upload an image for that user on the json body
I get an error
"""
username = 'messi'
self.create_user(username)
self.testapp.post('/people/{}/avatar'.format(username), '', headers=oauth2Header(username), status=400)
def test_get_user_avatar(self):
"""
Given a user with avatar
When I retrieve the avatar
Then I get the 48x48 version of that avatar
"""
username = 'messi'
self.create_user(username)
self.upload_user_avatar(username, "avatar.png")
response = self.testapp.get('/people/%s/avatar' % username, '', {}, status=200)
self.assertIn('image', response.content_type)
self.assertEqual(self.get_image_dimensions_from(response), (48, 48))
def test_get_user_avatar_large(self):
"""
Given a user without avatar
When I retrieve the large avatar
Then I get the 250x250 version of that avatar
"""
username = 'messi'
self.create_user(username)
self.upload_user_avatar(username, "avatar.png")
response = self.testapp.get('/people/%s/avatar/%s' % (username, 'large'), '', {}, status=200)
self.assertIn('image', response.content_type)
self.assertIn('image', response.content_type)
self.assertEqual(self.get_image_dimensions_from(response), (250, 250))
def test_get_user_avatar_large_missing_named_size(self):
"""
Given a user without avatar
When I retrieve the large avatar
And the large avatars has disappeared
Then I get the 48x48 version of that avatar
"""
username = 'messi'
self.create_user(username)
#.........这里部分代码省略.........
示例13: FunctionalTests
# 需要导入模块: from max.tests.base import MaxTestApp [as 别名]
# 或者: from max.tests.base.MaxTestApp import post [as 别名]
class FunctionalTests(unittest.TestCase, MaxTestBase):
def setUp(self):
conf_dir = os.path.dirname(__file__)
self.app = loadapp('config:tests.ini', relative_to=conf_dir)
self.reset_database(self.app)
self.app.registry.max_store.security.insert(test_default_security)
self.patched_post = patch('requests.post', new=partial(mock_post, self))
self.patched_post.start()
self.testapp = MaxTestApp(self)
self.create_user(test_manager)
# BEGIN TESTS
def test_create_context_with_notifications(self):
""" doctests .. http:post:: /contexts"""
from .mockers import create_context_post_notifications as create_context
new_context = dict(create_context)
res = self.testapp.post('/contexts', json.dumps(new_context), oauth2Header(test_manager), status=201)
self.assertEqual(res.json['notifications'], 'posts')
def test_delete_context_with_notifications_removes_subscriptions(self):
"""
"""
from .mockers import subscribe_context
from .mockers import create_context_post_notifications as create_context
from .mockers import user_status_context
from hashlib import sha1
username = 'messi'
self.create_user(username)
self.create_context(create_context)
self.admin_subscribe_user_to_context(username, subscribe_context)
self.create_activity(username, user_status_context)
url_hash = sha1(create_context['url']).hexdigest()
self.testapp.delete('/contexts/%s' % url_hash, "", oauth2Header(test_manager), status=204)
res = self.testapp.get('/people/%s' % username, "", oauth2Header(username))
result = json.loads(res.text)
self.assertEqual(result.get('username', None), 'messi')
self.assertEqual(len(result.get('subscribedTo', [])), 0)
return url_hash
def test_subscribe_user_to_context_with_notifications(self):
"""
"""
from .mockers import create_context_post_notifications as create_context
from .mockers import subscribe_context
from hashlib import sha1
username = 'messi'
url_hash = sha1(create_context['url']).hexdigest()
self.create_user(username)
self.create_context(create_context, permissions=dict(read='public', write='restricted', subscribe='restricted', invite='restricted'))
self.testapp.post('/people/%s/subscriptions' % username, json.dumps(subscribe_context), oauth2Header(test_manager), status=201)
res = self.testapp.get('/people/%s/subscriptions' % username, json.dumps(subscribe_context), oauth2Header(username), status=200)
self.assertEqual(res.json[0]['notifications'], 'posts')
return url_hash, username
def test_unsubscribe_user_from_context_with_notifications(self):
"""
"""
from .mockers import create_context_post_notifications as create_context
from .mockers import subscribe_context
from hashlib import sha1
username = 'messi'
url_hash = sha1(create_context['url']).hexdigest()
self.create_user(username)
self.create_context(create_context, permissions=dict(read='public', write='restricted', subscribe='restricted', invite='restricted'))
self.testapp.post('/people/%s/subscriptions' % username, json.dumps(subscribe_context), oauth2Header(test_manager), status=201)
res = self.testapp.delete('/people/%s/subscriptions/%s' % (username, url_hash), {}, oauth2Header(test_manager), status=204)
res = self.testapp.get('/people/%s/subscriptions' % username, json.dumps(subscribe_context), oauth2Header(username), status=200)
self.assertEqual(len(res.json), 0)
return url_hash, username
def test_post_activity_on_context_with_notifications(self):
""" Post an activity to a context which needs the user to be subscribed to read and write
and we have previously subscribed the user.
"""
from .mockers import subscribe_context
from .mockers import create_context_post_notifications as create_context
from .mockers import user_status_context
from hashlib import sha1
username = 'messi'
url_hash = sha1(create_context['url']).hexdigest()
self.create_user(username)
context_permissions = dict(read='subscribed', write='subscribed', subscribe='restricted', invite='restricted')
self.create_context(create_context, permissions=context_permissions)
#.........这里部分代码省略.........
示例14: FunctionalTests
# 需要导入模块: from max.tests.base import MaxTestApp [as 别名]
# 或者: from max.tests.base.MaxTestApp import post [as 别名]
class FunctionalTests(unittest.TestCase, MaxTestBase):
def setUp(self):
conf_dir = os.path.dirname(__file__)
self.app = loadapp('config:tests.ini', relative_to=conf_dir)
self.reset_database(self.app)
self.app.registry.max_store.security.insert(test_default_security)
self.patched_post = patch('requests.post', new=partial(mock_post, self))
self.patched_post.start()
self.testapp = MaxTestApp(self)
self.create_user(test_manager)
# BEGIN TESTS
def test_like_activity(self):
"""
Given a plain user
and a regular context
When i post an activity in a context
Then someone else can like this activity
"""
from .mockers import user_status_context
from .mockers import subscribe_context, create_context
username = 'messi'
username_not_me = 'xavi'
self.create_user(username)
self.create_user(username_not_me)
self.create_context(create_context)
self.admin_subscribe_user_to_context(username, subscribe_context)
self.admin_subscribe_user_to_context(username_not_me, subscribe_context)
res = self.create_activity(username, user_status_context)
activity_id = res.json['id']
res = self.testapp.post('/activities/%s/likes' % activity_id, '', oauth2Header(username_not_me), status=201)
activity = self.testapp.get('/activities/%s' % activity_id, '', oauth2Header(username), status=200)
self.assertEqual(res.json['object']['likes'][0]['username'], username_not_me)
self.assertEqual(res.json['object']['liked'], True)
self.assertEqual(res.json['object']['likesCount'], 1)
self.assertEqual(activity.json['likes'][0]['username'], username_not_me)
self.assertEqual(activity.json['liked'], False)
self.assertEqual(activity.json['likesCount'], 1)
def test_like_already_liked_activity(self):
"""
Given a plain user
and a regular context
When i post an activity in a context
And someone likes this activity
Then this someone else can't like twice this activity
"""
from .mockers import user_status_context
from .mockers import subscribe_context, create_context
username = 'messi'
username_not_me = 'xavi'
self.create_user(username)
self.create_user(username_not_me)
self.create_context(create_context)
self.admin_subscribe_user_to_context(username, subscribe_context)
self.admin_subscribe_user_to_context(username_not_me, subscribe_context)
res = self.create_activity(username, user_status_context)
activity_id = res.json['id']
res = self.testapp.post('/activities/%s/likes' % activity_id, '', oauth2Header(username_not_me), status=201)
res = self.testapp.post('/activities/%s/likes' % activity_id, '', oauth2Header(username_not_me), status=200)
self.assertEqual(res.json['object']['likes'][0]['username'], username_not_me)
self.assertEqual(res.json['object']['liked'], True)
self.assertEqual(res.json['object']['likesCount'], 1)
def test_unlike_activity(self):
"""
Given a plain user
and a regular context
When i post an activity in a context
Then i can remove previously like mark from this activity
"""
from .mockers import user_status_context
from .mockers import subscribe_context, create_context
username = 'messi'
username_not_me = 'xavi'
self.create_user(username)
self.create_user(username_not_me)
self.create_context(create_context)
self.admin_subscribe_user_to_context(username, subscribe_context)
self.admin_subscribe_user_to_context(username_not_me, subscribe_context)
res = self.create_activity(username, user_status_context)
activity_id = res.json['id']
self.testapp.post('/activities/%s/likes' % activity_id, '', oauth2Header(username_not_me), status=201)
self.testapp.delete('/activities/%s/likes/%s' % (activity_id, username_not_me), '', oauth2Header(username_not_me), status=204)
activity = self.testapp.get('/activities/%s' % activity_id, '', oauth2Header(username), status=200)
self.assertEqual(activity.json['likes'], [])
self.assertEqual(activity.json['liked'], False)
self.assertEqual(activity.json['likesCount'], 0)
def test_like_activity_by_various(self):
"""
Given a plain user
and a regular context
#.........这里部分代码省略.........
示例15: FunctionalTests
# 需要导入模块: from max.tests.base import MaxTestApp [as 别名]
# 或者: from max.tests.base.MaxTestApp import post [as 别名]
class FunctionalTests(unittest.TestCase, MaxTestBase):
def setUp(self):
conf_dir = os.path.dirname(__file__)
self.app = loadapp('config:tests.ini', relative_to=conf_dir)
self.reset_database(self.app)
self.app.registry.max_store.security.insert(test_default_security)
self.patched_post = patch('requests.post', new=partial(mock_post, self))
self.patched_post.start()
self.testapp = MaxTestApp(self)
self.create_user(test_manager)
# BEGIN TESTS
def test_add_public_context_with_valid_parameters_that_needs_formating(self):
"""
Test formatters acting correctly by testing the extraction of the "@" and "#"
on receiving a twitter @username and #hashtag containing extra chars (@,# and trailing/leading whitespace)
"""
from .mockers import create_context_full
new_context = dict(create_context_full)
new_context['twitterUsername'] = '@%s ' % create_context_full['twitterUsername']
new_context['twitterHashtag'] = ' #%s' % create_context_full['twitterHashtag']
res = self.testapp.post('/contexts', json.dumps(new_context), oauth2Header(test_manager), status=201)
result = json.loads(res.text)
self.assertEqual(result.get('twitterUsername', None), create_context_full['twitterUsername'])
self.assertEqual(result.get('twitterHashtag', None), create_context_full['twitterHashtag'])
def test_modify_public_context_with_valid_parameters_that_need_formating(self):
"""
Test validation failure on receiving a invalid twitter username
"""
from .mockers import create_context_full
from hashlib import sha1
res = self.testapp.post('/contexts', json.dumps(create_context_full), oauth2Header(test_manager), status=201)
url_hash = sha1(create_context_full['url']).hexdigest()
res = self.testapp.put('/contexts/%s' % url_hash, json.dumps({"twitterUsername": "@maxupcnet", "twitterHashtag": "#atenea"}), oauth2Header(test_manager), status=200)
result = json.loads(res.text)
self.assertEqual(result.get('twitterUsername', None), 'maxupcnet')
self.assertEqual(result.get('twitterHashtag', None), 'atenea')
def test_add_public_context_with_bad_twitter_username(self):
"""
Test validation failure on receiving a invalid twitter username
"""
from .mockers import create_context_full
bad_context = dict(create_context_full)
bad_context['twitterUsername'] = '@@badusername'
res = self.testapp.post('/contexts', json.dumps(bad_context), oauth2Header(test_manager), status=400)
result = json.loads(res.text)
self.assertEqual(result.get('error', None), 'ValidationError')
def test_add_public_context_with_bad_hashtag(self):
"""
Test validation failure on receiving a invalid twitter hashtag
"""
from .mockers import create_context_full
bad_context = dict(create_context_full)
bad_context['twitterHashtag'] = '##badhashtag'
res = self.testapp.post('/contexts', json.dumps(bad_context), oauth2Header(test_manager), status=400)
result = json.loads(res.text)
self.assertEqual(result.get('error', None), 'ValidationError')