def test_user_with_permission_should_do_allowed_operations(self): self.sync_permissions() issue = self.create_issue() user = self.create_user('new_user', 'password', '*****@*****.**') logged_user = self.logged_user.user issue_read_permission = Perm.objects.get( codename='{}__{}'.format('issue', 'read')) issue_create_permission = Perm.objects.get( codename='{}__{}'.format('issue', 'create')) user_delete_permission = Perm.objects.get( codename='{}__{}'.format('user', 'delete')) user_update_permission = Perm.objects.get( codename='{}__{}'.format('user', 'update')) logged_user.fperms.add(issue_read_permission, issue_create_permission, user_delete_permission, user_update_permission) # API # Generic read, post assert_http_forbidden(self.get('/api/user/')) assert_http_ok(self.get('/api/issue/')) assert_http_forbidden(self.post('/api/user/', {})) assert_http_bad_request(self.post('/api/issue/', {})) # API # Generic read, put, patch, delete assert_http_forbidden(self.get('/api/user/{}/'.format(user.pk))) assert_http_ok(self.get('/api/issue/{}/'.format(issue.pk))) assert_http_bad_request(self.put('/api/user/{}/'.format(user.pk), {})) assert_http_forbidden(self.put('/api/issue/{}/'.format(issue.pk), {})) assert_http_accepted(self.delete('/api/user/{}/'.format(user.pk))) assert_http_forbidden(self.delete('/api/issue/{}/'.format(issue.pk)))
def test_create_issue_via_user_one_to_one_renamed_fail( self, number, issue_data, user_data): user_data['leading_issue_renamed'] = {} resp = self.post(self.USER_WITH_FORM_API_URL, data=user_data) assert_http_bad_request(resp) assert_in('leading_issue_renamed', self.deserialize(resp).get('messages', {}).get('errors'))
def test_foreign_key_filter(self): issue1 = IssueFactory() issue2 = IssueFactory(solver=issue1.created_by) data = self.deserialize(self.get(build_url(self.ISSUE_API_URL, created_by=issue1.created_by.pk))) assert_equal(len(data), 1) assert_equal(data[0]['id'], issue1.pk) data = self.deserialize(self.get(build_url(self.ISSUE_API_URL, created_by__not=issue1.created_by.pk))) assert_equal(len(data), 1) assert_equal(data[0]['id'], issue2.pk) data = self.deserialize( self.get( build_url( self.ISSUE_API_URL, created_by__in='[{}, {}]'.format(issue1.created_by.pk, issue2.created_by.pk) ) ) ) assert_equal(len(data), 2) data = self.deserialize(self.get(build_url(self.ISSUE_API_URL, solver='__none__'))) assert_equal(len(data), 1) assert_equal(data[0]['id'], issue1.pk) assert_http_bad_request(self.get(build_url(self.ISSUE_API_URL, solver='invalid')))
def test_boolean_filter(self): user = UserFactory(is_superuser=False) superuser = UserFactory(is_superuser=True) data = self.deserialize(self.get(build_url( self.USER_API_URL, is_superuser=0))) assert_equal(len(data), 1) assert_equal(data[0]['id'], user.pk) data = self.deserialize(self.get(build_url(self.USER_API_URL, is_superuser=1))) assert_equal(len(data), 1) assert_equal(data[0]['id'], superuser.pk) data = self.deserialize(self.get(build_url( self.USER_API_URL, is_superuser__not=0))) assert_equal(len(data), 1) assert_equal(data[0]['id'], superuser.pk) data = self.deserialize(self.get(build_url( self.USER_API_URL, is_superuser__gt=0))) assert_equal(len(data), 1) assert_equal(data[0]['id'], superuser.pk) data = self.deserialize(self.get(build_url( self.USER_API_URL, is_superuser__lt=0))) assert_equal(len(data), 0) data = self.deserialize(self.get(build_url( self.USER_API_URL, is_superuser__lt=1))) assert_equal(len(data), 1) assert_equal(data[0]['id'], user.pk) assert_http_bad_request(self.get(build_url(self.USER_API_URL, is_superuser='******'))) assert_http_bad_request(self.get(build_url(self.USER_API_URL, is_superuser=3))) assert_http_bad_request(self.get(build_url(self.USER_API_URL, is_superuser__gt=3))) assert_http_bad_request(self.get(build_url(self.USER_API_URL, is_superuser__not='invalid'))) assert_http_bad_request(self.get(build_url(self.USER_API_URL, is_superuser__not='__none__')))
def test_custom_method_filter(self): user1 = UserFactory() user2 = UserFactory() issue1 = IssueFactory() issue2 = IssueFactory() issue1.watched_by.add(user1, user2) issue2.watched_by.add(user1) assert_http_bad_request(self.get(build_url(self.USER_API_URL, watched_issues_count='invalid'))) data = self.deserialize( self.get( build_url( self.USER_API_URL, watched_issues_count=2 ) ) ) assert_equal(len(data), 1) assert_equal(data[0]['id'], user1.pk) data = self.deserialize( self.get( build_url( self.USER_API_URL, watched_issues_count=1 ) ) ) assert_equal(len(data), 1) assert_equal(data[0]['id'], user2.pk)
def test_foreign_key_filter(self): issue1 = IssueFactory() issue2 = IssueFactory(solver=issue1.created_by) data = self.deserialize( self.get( build_url(self.ISSUE_API_URL, created_by=issue1.created_by.pk))) assert_equal(len(data), 1) assert_equal(data[0]['id'], issue1.pk) data = self.deserialize( self.get( build_url(self.ISSUE_API_URL, created_by__not=issue1.created_by.pk))) assert_equal(len(data), 1) assert_equal(data[0]['id'], issue2.pk) data = self.deserialize( self.get( build_url(self.ISSUE_API_URL, created_by__in='[{}, {}]'.format( issue1.created_by.pk, issue2.created_by.pk)))) assert_equal(len(data), 2) data = self.deserialize( self.get(build_url(self.ISSUE_API_URL, solver='__none__'))) assert_equal(len(data), 1) assert_equal(data[0]['id'], issue1.pk) assert_http_bad_request( self.get(build_url(self.ISSUE_API_URL, solver='invalid')))
def test_filter_by_decorator(self): issue1 = IssueFactory(description='test1') issue2 = IssueFactory(description='test2') assert_http_bad_request(self.get(build_url(self.ISSUE_API_URL, description='test1'))) data = self.deserialize( self.get( build_url( self.ISSUE_API_URL, short_description__contains='test' ) ) ) assert_equal(len(data), 2) data = self.deserialize( self.get( build_url( self.ISSUE_API_URL, short_description='test1' ) ) ) assert_equal(len(data), 1) assert_equal(data[0]['id'], issue1.pk)
def test_override_extra_order_fields(self): assert_http_bad_request( self.get(build_url(self.USER_API_URL, order='created_at'))) assert_valid_JSON_response( self.get( build_url(self.USER_API_URL, order='email,-solving_issue__short_description')))
def test_partial_update_user(self, number, data): resp = self.post(self.USER_API_URL, data=data) assert_valid_JSON_created_response(resp) pk = self.get_pk(resp) assert_http_bad_request(self.put('%s%s/' % (self.USER_API_URL, pk), data={})) assert_valid_JSON_response(self.patch('%s%s/' % (self.USER_API_URL, pk), data={}))
def test_create_issue_single_related_field_leader_should_not_be_set_via_id( self): issue_data = self.get_issue_data() issue_data['created_by'] = self.get_user_data() issue_data['leader'] = 1 issue_data['tags_list'] = ['taga', 'tagb'] resp = self.post(self.ISSUE_WITH_FORM_API_URL, data=issue_data) assert_http_bad_request(resp)
def test_issue_can_filter_only_with_readable_fields_and_extra_field(self): assert_valid_JSON_response( self.get(build_url(self.ISSUE_API_URL, filter='solver__created_at="1.1.2017"')) ) assert_valid_JSON_response(self.get(build_url(self.ISSUE_API_URL, filter='created_at>"1.1.2017"'))) assert_http_bad_request( self.get(build_url(self.ISSUE_API_URL, filter='solver__manual_created_date>"1.1.2017"')) )
def test_reverse_with_defined_field_created_issues_renamed_fail(self, number, issue_data, user_data): issues_before_count = Issue.objects.all().count() user_data['created_issues_renamed'] = {'add': (self.get_issue_data(), self.get_issue_data(), self.get_issue_data())} resp = self.post(self.USER_WITH_FORM_API_URL, data=user_data) assert_http_bad_request(resp) assert_in('created_issues_renamed', self.deserialize(resp).get('messages', {}).get('errors')) assert_equal(issues_before_count + 0, Issue.objects.all().count())
def test_create_issue_multiple_related_filed_another_users_should_not_contains_none( self): issue_data = self.get_issue_data() issue_data['created_by'] = self.get_user_data() issue_data['leader'] = self.get_user_data() issue_data['another_users'] = [None] resp = self.post(self.ISSUE_WITH_FORM_API_URL, data=issue_data) assert_http_bad_request(resp)
def test_override_extra_filter_fields(self): assert_http_bad_request( self.get( build_url(self.USER_API_URL, filter='created_at__gt="1.1.1980"'))) assert_valid_JSON_response( self.get( build_url(self.USER_API_URL, filter='email contains "*****@*****.**"')))
def test_error_during_creating_user_with_file_and_not_defined_content_type(self, number, data): data['contract'] = { 'filename': 'contract', 'content': base64.b64encode( ('Contract of %s code: šří+áýšé' % data['email']).encode('utf-8') ).decode('utf-8') } resp = self.post(self.USER_API_URL, data=data) assert_http_bad_request(resp)
def test_order_by_decorator(self): [IssueFactory(description=str(i)) for i in range(10)] data = self.deserialize(self.get(build_url(self.ISSUE_API_URL, order='short_description'))) assert_equal([v['short_description'] for v in data], [str(i) for i in range(10)]) data = self.deserialize(self.get(build_url(self.ISSUE_API_URL, order='-short_description'))) assert_equal([v['short_description'] for v in data], [str(i) for i in range(10)][::-1]) assert_valid_JSON_response(self.get(build_url(self.USER_API_URL, order='solving_issue__short_description'))) assert_valid_JSON_response(self.get(build_url(self.USER_API_URL, order='-solving_issue__short_description'))) assert_http_bad_request(self.get(build_url(self.ISSUE_API_URL, order='description')))
def test_should_raise_bad_request_if_url_is_not_valid(self, number, data): data['contract'] = { 'filename': 'testfile.pdf', 'url': 'hs://foo.bar/testfile.pdf', } resp = self.post(self.USER_API_URL, data=data) assert_http_bad_request(resp) errors = self.deserialize(resp).get('messages', {}).get('errors') assert_in('contract', errors) assert_equal(errors['contract']['url'], ugettext('Enter a valid URL.'))
def test_issue_can_order_only_with_readable_fields_and_extra_field(self): assert_valid_JSON_response( self.get(build_url(self.ISSUE_API_URL, order='solver__created_at'))) assert_valid_JSON_response( self.get(build_url(self.ISSUE_API_URL, order='created_at'))) assert_http_bad_request( self.get( build_url(self.ISSUE_API_URL, order='solver__manual_created_date')))
def test_error_during_creating_user_with_file_and_not_defined_content_type( self, number, data): data['contract'] = { 'filename': 'contract', 'content': base64.b64encode(('Contract of %s code: šří+áýšé' % data['email']).encode('utf-8')).decode('utf-8') } resp = self.post(self.USER_API_URL, data=data) assert_http_bad_request(resp)
def test_create_issue_via_user_one_to_one_bad_request(self, number, issue_data, user_data): issue_data['created_by'] = self.get_user_data() user_data['leading_issue'] = {} resp = self.post(self.USER_API_URL, data=user_data) assert_http_bad_request(resp) assert_in('leading_issue', self.deserialize(resp).get('messages', {}).get('errors')) user_data['leading_issue'] = 'bad data' resp = self.post(self.USER_API_URL, data=user_data) assert_http_bad_request(resp) assert_in('leading_issue', self.deserialize(resp).get('messages', {}).get('errors'))
def test_should_raise_bad_request_if_file_is_unreachable(self, number, data): url = 'http://foo.bar/testfile.pdf' data['contract'] = { 'filename': 'testfile.pdf', 'url': url, } resp = self.post(self.USER_API_URL, data=data) assert_http_bad_request(resp) errors = self.deserialize(resp).get('messages', {}).get('errors') assert_in('contract', errors) assert_equal(errors['contract']['url'], ugettext('File is unreachable on the URL address'))
def test_should_raise_bad_request_if_file_content_is_not_in_base64(self, number, data): data['contract'] = { 'content_type': 'text/plain', 'filename': 'contract.txt', 'content': 'abc', } resp = self.post(self.USER_API_URL, data=data) assert_http_bad_request(resp) errors = self.deserialize(resp).get('messages', {}).get('errors') assert_in('contract', errors) assert_equal(errors['contract']['content'], ugettext('File content must be in base64 format'))
def test_create_issue_should_tags_with_invalid_value_should_not_be_accepted(self): issue_data = self.get_issue_data() issue_data['created_by'] = self.get_user_data() issue_data['leader'] = self.get_user_data() issue_data['tags_list'] = 'invalid' resp = self.post(self.ISSUE_WITH_FORM_API_URL, data=issue_data) assert_http_bad_request(resp) issue_data['tags_list'] = ['too long tag'] resp = self.post(self.ISSUE_WITH_FORM_API_URL, data=issue_data) assert_http_bad_request(resp)
def test_reverse_with_defined_field_created_issues_renamed_invalid_issue_name(self, number, issue_data, user_data): issues_before_count = Issue.objects.all().count() users_before_count = Issue.objects.all().count() user_data['created_issues_renamed'] = ( self.get_issue_data(), self.get_issue_data(name='invalid'), self.get_issue_data() ) resp = self.post(self.USER_WITH_FORM_API_URL, data=user_data) assert_http_bad_request(resp) assert_equal(issues_before_count, Issue.objects.all().count()) assert_equal(users_before_count, User.objects.all().count())
def test_reverse_with_defined_field_created_issues_renamed_fail( self, number, issue_data, user_data): issues_before_count = Issue.objects.all().count() user_data['created_issues_renamed'] = { 'add': (self.get_issue_data(), self.get_issue_data(), self.get_issue_data()) } resp = self.post(self.USER_WITH_FORM_API_URL, data=user_data) assert_http_bad_request(resp) assert_in('created_issues_renamed', self.deserialize(resp).get('messages', {}).get('errors')) assert_equal(issues_before_count + 0, Issue.objects.all().count())
def test_create_issue_should_tags_with_invalid_value_should_not_be_accepted( self): issue_data = self.get_issue_data() issue_data['created_by'] = self.get_user_data() issue_data['leader'] = self.get_user_data() issue_data['tags_list'] = 'invalid' resp = self.post(self.ISSUE_WITH_FORM_API_URL, data=issue_data) assert_http_bad_request(resp) issue_data['tags_list'] = ['too long tag'] resp = self.post(self.ISSUE_WITH_FORM_API_URL, data=issue_data) assert_http_bad_request(resp)
def test_should_raise_bad_request_if_required_items_are_not_provided( self, number, data): REQUIRED_ITEMS = {'content'} REQUIRED_URL_ITEMS = {'url'} data['contract'] = {} resp = self.post(self.USER_API_URL, data=data) assert_http_bad_request(resp) errors = self.deserialize(resp).get('messages', {}).get('errors') assert_in('contract', errors) msg = ugettext('File data item must contains {} or {}').format( ', '.join(REQUIRED_ITEMS), ', '.join(REQUIRED_URL_ITEMS)) assert_equal(errors['contract'], msg)
def test_should_raise_bad_request_if_required_items_are_not_provided(self, number, data): REQUIRED_ITEMS = {'content'} REQUIRED_URL_ITEMS = {'url'} data['contract'] = {} resp = self.post(self.USER_API_URL, data=data) assert_http_bad_request(resp) errors = self.deserialize(resp).get('messages', {}).get('errors') assert_in('contract', errors) msg = ugettext('File data item must contains {} or {}').format( ', '.join(REQUIRED_ITEMS), ', '.join(REQUIRED_URL_ITEMS) ) assert_equal(errors['contract'], msg)
def test_update_error_user(self, number, data): resp = self.post(self.USER_API_URL, data=data) assert_valid_JSON_created_response(resp) pk = self.get_pk(resp) assert_valid_JSON_response(self.put('{}{}/'.format(self.USER_API_URL, pk), data={'email': '*****@*****.**'})) assert_http_bad_request( self.put('{}{}/'.format(self.USER_API_URL, pk), data={'email': 'invalid_email'}) ) assert_http_not_found(self.put('{}{}/'.format(self.USER_API_URL, 0), data={}))
def test_should_raise_bad_request_if_file_content_is_not_in_base64( self, number, data): data['contract'] = { 'content_type': 'text/plain', 'filename': 'contract.txt', 'content': 'abc', } resp = self.post(self.USER_API_URL, data=data) assert_http_bad_request(resp) errors = self.deserialize(resp).get('messages', {}).get('errors') assert_in('contract', errors) assert_equal(errors['contract']['content'], ugettext('File content must be in base64 format'))
def test_should_raise_bad_request_if_file_is_unreachable( self, number, data): url = 'http://foo.bar/testfile.pdf' data['contract'] = { 'filename': 'testfile.pdf', 'url': url, } resp = self.post(self.USER_API_URL, data=data) assert_http_bad_request(resp) errors = self.deserialize(resp).get('messages', {}).get('errors') assert_in('contract', errors) assert_equal(errors['contract']['url'], ugettext('File is unreachable on the URL address'))
def test_reverse_with_defined_field_created_issues_renamed_invalid_issue_name( self, number, issue_data, user_data): issues_before_count = Issue.objects.all().count() users_before_count = Issue.objects.all().count() user_data['created_issues_renamed'] = (self.get_issue_data(), self.get_issue_data( name='invalid'), self.get_issue_data()) resp = self.post(self.USER_WITH_FORM_API_URL, data=user_data) assert_http_bad_request(resp) assert_equal(issues_before_count, Issue.objects.all().count()) assert_equal(users_before_count, User.objects.all().count())
def test_create_issue_via_user_one_to_one_bad_request( self, number, issue_data, user_data): issue_data['created_by'] = self.get_user_data() user_data['leading_issue'] = {} resp = self.post(self.USER_API_URL, data=user_data) assert_http_bad_request(resp) assert_in('leading_issue', self.deserialize(resp).get('messages', {}).get('errors')) user_data['leading_issue'] = 'bad data' resp = self.post(self.USER_API_URL, data=user_data) assert_http_bad_request(resp) assert_in('leading_issue', self.deserialize(resp).get('messages', {}).get('errors'))
def test_atomic_add_delete_and_set_issues_with_errors(self, number, issue_data, user_data): user_data['createdIssues'] = {'set': (None, '', None, {}, [None])} resp = self.post(self.USER_API_URL, data=user_data) assert_http_bad_request(resp) assert_in('set', self.deserialize(resp).get('messages', {}).get('errors', {}).get('createdIssues', {})) user_data['createdIssues'] = {'add': (None, '', None, [], {}, {'id': 500}), 'remove': (None, '', None, {}, {'id': 500}, [])} resp = self.post(self.USER_API_URL, data=user_data) assert_http_bad_request(resp) assert_in('add', self.deserialize(resp).get('messages', {}).get('errors', {}).get('createdIssues', {})) assert_in('remove', self.deserialize(resp).get('messages', {}).get('errors', {}).get('createdIssues', {})) user_data['createdIssues'] = {'add': None, 'remove': None} resp = self.post(self.USER_API_URL, data=user_data) assert_http_bad_request(resp) assert_in('add', self.deserialize(resp).get('messages', {}).get('errors', {}).get('createdIssues', {})) assert_in('remove', self.deserialize(resp).get('messages', {}).get('errors', {}).get('createdIssues', {})) user_data['createdIssues'] = (self.get_issue_data(), self.get_issue_data(), 'invalid') resp = self.post(self.USER_API_URL, data=user_data) assert_equal( self.deserialize(resp).get('messages', {}).get('errors', {}).get('createdIssues')[0]['_index'], 2 ) assert_http_bad_request(resp)
def test_should_raise_bad_request_if_response_is_too_large(self, number, data): data['contract'] = { 'content_type': 'text/plain', 'filename': 'contract.txt', 'content': base64.b64encode( ('Contract of %s code: šří+áýšé' % data['email']).encode('utf-8') ).decode('utf-8') } resp = self.get_file_url_response(data) assert_http_bad_request(resp) errors = self.deserialize(resp).get('messages', {}).get('errors') assert_in('contract', errors) msg = ugettext('Response too large, maximum size is {} bytes').format(pyston_settings.FILE_SIZE_LIMIT) assert_equal(errors['contract']['url'], msg)
def test_should_raise_bad_request_with_invalid_filename(self, number, data): data['contract'] = { 'filename': 'contract', 'content': base64.b64encode( ('Contract of %s code: šří+áýšé' % data['email']).encode('utf-8') ).decode('utf-8') } resp = self.post(self.USER_API_URL, data=data) assert_http_bad_request(resp) errors = self.deserialize(resp).get('messages', {}).get('errors') assert_in('contract', errors) assert_equal( errors['contract'], ugettext('Content type cannot be evaluated from input data please send it') )
def test_many_to_one_filter(self): issue1 = IssueFactory() issue2 = IssueFactory() issue3 = IssueFactory(created_by=issue1.created_by) user1 = issue1.created_by assert_http_bad_request( self.get(build_url(self.USER_API_URL, created_issues=user1))) data = self.deserialize( self.get( build_url(self.USER_API_URL, created_issues__in='({},{})'.format( issue1.pk, issue3.pk)))) assert_equal(len(data), 1) assert_equal(data[0]['id'], user1.pk) data = self.deserialize( self.get( build_url(self.USER_API_URL, created_issues__in='({})'.format(issue1.pk)))) assert_equal(len(data), 1) assert_equal(data[0]['id'], user1.pk) data = self.deserialize( self.get( build_url(self.USER_API_URL, created_issues__in='({},{},{})'.format( issue1.pk, issue2.pk, issue3.pk)))) assert_equal(len(data), 2) data = self.deserialize( self.get( build_url(self.USER_API_URL, created_issues__all='({},{},{})'.format( issue1.pk, issue2.pk, issue3.pk)))) assert_equal(len(data), 0) data = self.deserialize( self.get( build_url(self.USER_API_URL, created_issues__all='({},{})'.format( issue1.pk, issue3.pk)))) assert_equal(len(data), 1) assert_equal(data[0]['id'], user1.pk)
def test_filter_by_decorator(self): issue1 = IssueFactory(description='test1') issue2 = IssueFactory(description='test2') assert_http_bad_request( self.get(build_url(self.ISSUE_API_URL, description='test1'))) data = self.deserialize( self.get( build_url(self.ISSUE_API_URL, short_description__contains='test'))) assert_equal(len(data), 2) data = self.deserialize( self.get(build_url(self.ISSUE_API_URL, short_description='test1'))) assert_equal(len(data), 1) assert_equal(data[0]['id'], issue1.pk)
def test_many_to_many_filter(self): user1 = UserFactory() user2 = UserFactory() issue1 = IssueFactory() issue2 = IssueFactory() issue1.watched_by.add(user1, user2) issue2.watched_by.add(user1) assert_http_bad_request( self.get(build_url(self.ISSUE_API_URL, watched_by=user1.pk))) data = self.deserialize( self.get( build_url(self.ISSUE_API_URL, watched_by__in='({},{})'.format(user1.pk, user2.pk)))) assert_equal(len(data), 2) data = self.deserialize( self.get( build_url(self.ISSUE_API_URL, watched_by__in='({})'.format(user1.pk)))) assert_equal(len(data), 2) data = self.deserialize( self.get( build_url(self.ISSUE_API_URL, watched_by__in='({})'.format(user2.pk)))) assert_equal(len(data), 1) assert_equal(data[0]['id'], issue1.pk) data = self.deserialize( self.get( build_url(self.ISSUE_API_URL, watched_by__all='({},{})'.format(user1.pk, user2.pk)))) assert_equal(len(data), 1) assert_equal(data[0]['id'], issue1.pk) data = self.deserialize( self.get( build_url(self.ISSUE_API_URL, watched_by__all='({})'.format(user1.pk)))) assert_equal(len(data), 2)
def test_boolean_filter(self): user = UserFactory(is_superuser=False) superuser = UserFactory(is_superuser=True) data = self.deserialize( self.get(build_url(self.USER_API_URL, is_superuser=0))) assert_equal(len(data), 1) assert_equal(data[0]['id'], user.pk) data = self.deserialize( self.get(build_url(self.USER_API_URL, is_superuser=1))) assert_equal(len(data), 1) assert_equal(data[0]['id'], superuser.pk) data = self.deserialize( self.get(build_url(self.USER_API_URL, is_superuser__not=0))) assert_equal(len(data), 1) assert_equal(data[0]['id'], superuser.pk) data = self.deserialize( self.get(build_url(self.USER_API_URL, is_superuser__gt=0))) assert_equal(len(data), 1) assert_equal(data[0]['id'], superuser.pk) data = self.deserialize( self.get(build_url(self.USER_API_URL, is_superuser__lt=0))) assert_equal(len(data), 0) data = self.deserialize( self.get(build_url(self.USER_API_URL, is_superuser__lt=1))) assert_equal(len(data), 1) assert_equal(data[0]['id'], user.pk) assert_http_bad_request( self.get(build_url(self.USER_API_URL, is_superuser='******'))) assert_http_bad_request( self.get(build_url(self.USER_API_URL, is_superuser=3))) assert_http_bad_request( self.get(build_url(self.USER_API_URL, is_superuser__gt=3))) assert_http_bad_request( self.get(build_url(self.USER_API_URL, is_superuser__not='invalid'))) assert_http_bad_request( self.get(build_url(self.USER_API_URL, is_superuser__not='__none__')))
def test_should_raise_bad_request_if_response_is_too_large( self, number, data): data['contract'] = { 'content_type': 'text/plain', 'filename': 'contract.txt', 'content': base64.b64encode(('Contract of %s code: šří+áýšé' % data['email']).encode('utf-8')).decode('utf-8') } resp = self.get_file_url_response(data) assert_http_bad_request(resp) errors = self.deserialize(resp).get('messages', {}).get('errors') assert_in('contract', errors) msg = ugettext('Response too large, maximum size is {} bytes').format( pyston_settings.FILE_SIZE_LIMIT) assert_equal(errors['contract']['url'], msg)
def test_should_raise_bad_request_with_invalid_filename( self, number, data): data['contract'] = { 'filename': 'contract', 'content': base64.b64encode(('Contract of %s code: šří+áýšé' % data['email']).encode('utf-8')).decode('utf-8') } resp = self.post(self.USER_API_URL, data=data) assert_http_bad_request(resp) errors = self.deserialize(resp).get('messages', {}).get('errors') assert_in('contract', errors) assert_equal( errors['contract'], ugettext( 'Content type cannot be evaluated from input data please send it' ))
def test_atomic_add_delete_and_set_issues_with_errors( self, number, issue_data, user_data): user_data['createdIssues'] = {'set': (None, '', None, {}, [None])} resp = self.post(self.USER_API_URL, data=user_data) assert_http_bad_request(resp) assert_in( 'set', self.deserialize(resp).get('messages', {}).get('errors', {}).get('createdIssues', {})) user_data['createdIssues'] = { 'add': (None, '', None, [], {}, { 'id': 500 }), 'remove': (None, '', None, {}, { 'id': 500 }, []) } resp = self.post(self.USER_API_URL, data=user_data) assert_http_bad_request(resp) assert_in( 'add', self.deserialize(resp).get('messages', {}).get('errors', {}).get('createdIssues', {})) assert_in( 'remove', self.deserialize(resp).get('messages', {}).get('errors', {}).get('createdIssues', {})) user_data['createdIssues'] = {'add': None, 'remove': None} resp = self.post(self.USER_API_URL, data=user_data) assert_http_bad_request(resp) assert_in( 'add', self.deserialize(resp).get('messages', {}).get('errors', {}).get('createdIssues', {})) assert_in( 'remove', self.deserialize(resp).get('messages', {}).get('errors', {}).get('createdIssues', {})) user_data['createdIssues'] = (self.get_issue_data(), self.get_issue_data(), 'invalid') resp = self.post(self.USER_API_URL, data=user_data) assert_equal( self.deserialize(resp).get('messages', {}).get( 'errors', {}).get('createdIssues')[0]['_index'], 2) assert_http_bad_request(resp)
def test_user_should_not_register_same_device_keys(self, user): logged_in_resp = self.post(API_LOGIN_URL, { 'username': '******', 'password': '******' }) registration_mobile_resp = self.post( self.API_REQUEST_MOBILE_TOKEN_URL, headers={ 'HTTP_AUTHORIZATION': 'Bearer {}'.format(logged_in_resp.json()['token']) }, data={'uuid': UUID}) assert_http_ok(registration_mobile_resp) registration_mobile_resp = self.post( self.API_REQUEST_MOBILE_TOKEN_URL, headers={ 'HTTP_AUTHORIZATION': 'Bearer {}'.format(logged_in_resp.json()['token']) }, data={'uuid': UUID}) assert_http_bad_request(registration_mobile_resp)
def test_order_by_decorator(self): [IssueFactory(description=str(i)) for i in range(10)] data = self.deserialize( self.get(build_url(self.ISSUE_API_URL, order='short_description'))) assert_equal([v['short_description'] for v in data], [str(i) for i in range(10)]) data = self.deserialize( self.get(build_url(self.ISSUE_API_URL, order='-short_description'))) assert_equal([v['short_description'] for v in data], [str(i) for i in range(10)][::-1]) assert_valid_JSON_response( self.get( build_url(self.USER_API_URL, order='solving_issue__short_description'))) assert_valid_JSON_response( self.get( build_url(self.USER_API_URL, order='-solving_issue__short_description'))) assert_http_bad_request( self.get(build_url(self.ISSUE_API_URL, order='description')))
def test_custom_method_filter(self): user1 = UserFactory() user2 = UserFactory() issue1 = IssueFactory() issue2 = IssueFactory() issue1.watched_by.add(user1, user2) issue2.watched_by.add(user1) assert_http_bad_request( self.get( build_url(self.USER_API_URL, watched_issues_count='invalid'))) data = self.deserialize( self.get(build_url(self.USER_API_URL, watched_issues_count=2))) assert_equal(len(data), 1) assert_equal(data[0]['id'], user1.pk) data = self.deserialize( self.get(build_url(self.USER_API_URL, watched_issues_count=1))) assert_equal(len(data), 1) assert_equal(data[0]['id'], user2.pk)
def test_superuser_should_do_all_operations(self): issue = self.create_issue() user = self.create_user('new_user', 'password', '*****@*****.**') # Generic read, post assert_http_ok(self.get('/api/user/')) assert_http_ok(self.get('/api/issue/')) assert_http_bad_request(self.post('/api/user/', {})) assert_http_bad_request(self.post('/api/issue/', {})) # Generic read, put, patch, delete assert_http_ok(self.get('/api/user/{}/'.format(user.pk))) assert_http_ok(self.get('/api/issue/{}/'.format(issue.pk))) assert_http_bad_request(self.put('/api/user/{}/'.format(user.pk), {})) assert_http_bad_request(self.put('/api/issue/{}/'.format(issue.pk), {})) assert_http_accepted(self.delete('/api/user/{}/'.format(user.pk))) assert_http_accepted(self.delete('/api/issue/{}/'.format(issue.pk)))
def test_custom_field_filter(self): user = UserFactory(email='*****@*****.**') assert_http_bad_request(self.get(build_url(self.USER_API_URL, email='*****@*****.**'))) assert_http_bad_request(self.get(build_url(self.USER_API_URL, email__icontains='test1'))) assert_http_bad_request(self.get(build_url(self.USER_API_URL, email__not='test1'))) data = self.deserialize( self.get( build_url( self.USER_API_URL, email__contains='test' ) ) ) assert_equal(len(data), 1) assert_equal(data[0]['id'], user.pk)
def test_read_querystring_paginator_user(self, number, data): resp = self.post(self.USER_API_URL, data=data) assert_valid_JSON_created_response(resp) querystring = {'_offset': '0', '_base': '5'} resp = self.get('%s?%s' % (self.USER_API_URL, urlencode(querystring))) assert_equal(len(self.deserialize(resp)), min(int(resp['x-total']), 5)) querystring = {'_offset': '2', '_base': '5'} resp = self.get('%s?%s' % (self.USER_API_URL, urlencode(querystring))) assert_equal(len(self.deserialize(resp)), min(max(int(resp['x-total']) - 2, 0), 5)) querystring = {'_offset': '2', '_base': '-5'} resp = self.get('%s?%s' % (self.USER_API_URL, urlencode(querystring))) assert_http_bad_request(resp) querystring = {'_offset': '-2', '_base': '5'} resp = self.get('%s?%s' % (self.USER_API_URL, urlencode(querystring))) assert_http_bad_request(resp) querystring = {'_offset': 'error', '_base': 'error'} resp = self.get('%s?%s' % (self.USER_API_URL, urlencode(querystring))) assert_http_bad_request(resp)
def test_read_headers_paginator_user(self, number, data): resp = self.post(self.USER_API_URL, data=data) assert_valid_JSON_created_response(resp) headers = {'HTTP_X_OFFSET': '0', 'HTTP_X_BASE': '5'} resp = self.get(self.USER_API_URL, headers=headers) assert_equal(len(self.deserialize(resp)), min(int(resp['x-total']), 5)) headers = {'HTTP_X_OFFSET': '2', 'HTTP_X_BASE': '5'} resp = self.get(self.USER_API_URL, headers=headers) assert_equal(len(self.deserialize(resp)), min(max(int(resp['x-total']) - 2, 0), 5)) headers = {'HTTP_X_OFFSET': '2', 'HTTP_X_BASE': '-5'} resp = self.get(self.USER_API_URL, headers=headers) assert_http_bad_request(resp) headers = {'HTTP_X_OFFSET': '-2', 'HTTP_X_BASE': '5'} resp = self.get(self.USER_API_URL, headers=headers) assert_http_bad_request(resp) headers = {'HTTP_X_OFFSET': 'error', 'HTTP_X_BASE': 'error'} resp = self.get(self.USER_API_URL, headers=headers) assert_http_bad_request(resp)
def test_create_error_user(self, number, data): resp = self.post(self.USER_API_URL, data={}) assert_http_bad_request(resp) resp = self.post(self.USER_API_URL, data={'email': 'invalid_email'}) assert_http_bad_request(resp)
def test_not_valid_string_input_data(self): resp = self.post(self.USER_API_URL, data='string_data') assert_http_bad_request(resp)
def test_create_user_with_file_url_too_large(self, number, data): resp = self.get_file_url_response(data) assert_http_bad_request(resp) assert_in('contract', self.deserialize(resp).get('messages', {}).get('errors', {}))
def test_many_to_one_filter(self): issue1 = IssueFactory() issue2 = IssueFactory() issue3 = IssueFactory(created_by=issue1.created_by) user1 = issue1.created_by assert_http_bad_request(self.get(build_url(self.USER_API_URL, created_issues=user1))) data = self.deserialize( self.get( build_url( self.USER_API_URL, created_issues__in='({},{})'.format(issue1.pk, issue3.pk) ) ) ) assert_equal(len(data), 1) assert_equal(data[0]['id'], user1.pk) data = self.deserialize( self.get( build_url( self.USER_API_URL, created_issues__in='({})'.format(issue1.pk) ) ) ) assert_equal(len(data), 1) assert_equal(data[0]['id'], user1.pk) data = self.deserialize( self.get( build_url( self.USER_API_URL, created_issues__in='({},{},{})'.format(issue1.pk, issue2.pk, issue3.pk) ) ) ) assert_equal(len(data), 2) data = self.deserialize( self.get( build_url( self.USER_API_URL, created_issues__all='({},{},{})'.format(issue1.pk, issue2.pk, issue3.pk) ) ) ) assert_equal(len(data), 0) data = self.deserialize( self.get( build_url( self.USER_API_URL, created_issues__all='({},{})'.format(issue1.pk, issue3.pk) ) ) ) assert_equal(len(data), 1) assert_equal(data[0]['id'], user1.pk)
def test_many_to_many_filter(self): user1 = UserFactory() user2 = UserFactory() issue1 = IssueFactory() issue2 = IssueFactory() issue1.watched_by.add(user1, user2) issue2.watched_by.add(user1) assert_http_bad_request(self.get(build_url(self.ISSUE_API_URL, watched_by=user1.pk))) data = self.deserialize( self.get( build_url( self.ISSUE_API_URL, watched_by__in='({},{})'.format(user1.pk, user2.pk) ) ) ) assert_equal(len(data), 2) data = self.deserialize( self.get( build_url( self.ISSUE_API_URL, watched_by__in='({})'.format(user1.pk) ) ) ) assert_equal(len(data), 2) data = self.deserialize( self.get( build_url( self.ISSUE_API_URL, watched_by__in='({})'.format(user2.pk) ) ) ) assert_equal(len(data), 1) assert_equal(data[0]['id'], issue1.pk) data = self.deserialize( self.get( build_url( self.ISSUE_API_URL, watched_by__all='({},{})'.format(user1.pk, user2.pk) ) ) ) assert_equal(len(data), 1) assert_equal(data[0]['id'], issue1.pk) data = self.deserialize( self.get( build_url( self.ISSUE_API_URL, watched_by__all='({})'.format(user1.pk) ) ) ) assert_equal(len(data), 2)
def test_override_extra_filter_fields(self): assert_http_bad_request(self.get(build_url(self.USER_API_URL, filter='created_at__gt="1.1.1980"'))) assert_valid_JSON_response( self.get(build_url(self.USER_API_URL, filter='email contains "*****@*****.**"')) )
def test_invalid_filter_format(self): assert_http_bad_request(self.get(build_url(self.USER_API_URL, filter='email="*****@*****.**" AND'))) assert_http_bad_request(self.get(build_url(self.USER_API_URL, filter='invalid'))) assert_http_bad_request(self.get(build_url(self.USER_API_URL, filter='email="*****@*****.**" AND OR email="*****@*****.**"')))