def test_this(self): group = { 'title': 'Main Berlin Group', 'description': 'Description', 'city_id': 1, } subgroup = { 'title': 'Main Berlin Subgroup', 'description': 'Description', 'city_id': 1, } # AUTHENTICATION here logged_user = auth_user(self.app, 'angelo', 'ciao') self.assertIsNotNone(logged_user) token = logged_user.get('token') # group creation resp = send_call(self.app, 'post', '/groups', group, token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) # subgroup creation subgroup['parent_group_id'] = resp_data.get('id') resp = send_call(self.app, 'post', '/groups', subgroup, token=token) self.assertEquals(resp.status[:3], '200')
def test_hospitality_requests(self): today = date.today() token, logged_user = self._sender_login() # message failure here - not allowed request = { 'subject': 'A Hospitality Request', 'text': 'A test text...', 'recipient_list_ids': [2], } resp = send_call(self.app, 'post', '/messages/in/hospitality_requests', request, token=token) self.assertEquals(resp.status[:3], '403') # message failure here - too many recipients request = { 'subject': 'A Hospitality Request', 'text': 'A test text...', 'recipient_list_ids': [2, 3], 'date_from': (today + timedelta(days=10)).strftime('%Y-%m-%d'), 'date_to': (today + timedelta(days=12)).strftime('%Y-%m-%d'), } resp = send_call(self.app, 'post', '/messages/out/hospitality_requests', request, token=token) self.assertEquals(resp.status[:3], '400') # message failure here - missing date_to request = { 'subject': 'A Hospitality Request', 'text': 'A test text...', 'recipient_list_ids': [2], 'date_from': (today + timedelta(days=10)).strftime('%Y-%m-%d'), } resp = send_call(self.app, 'post', '/messages/out/hospitality_requests', request, token=token) self.assertEquals(resp.status[:3], '400') # message failure here - invalid date request = { 'subject': 'A Hospitality Request', 'text': 'A test text...', 'recipient_list_ids': [2], 'date_from': (today - timedelta(days=10)).strftime('%Y-%m-%d'), 'date_to': (today + timedelta(days=12)).strftime('%Y-%m-%d'), } resp = send_call(self.app, 'post', '/messages/out/hospitality_requests', request, token=token) self.assertEquals(resp.status[:3], '400') # message being sent here request = { 'subject': 'A Hospitality Request', 'text': 'A test text...', 'recipient_list_ids': [2], 'date_from': (today + timedelta(days=10)).strftime('%Y-%m-%d'), 'date_to': (today + timedelta(days=12)).strftime('%Y-%m-%d'), } resp = send_call(self.app, 'post', '/messages/out/hospitality_requests', request, token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) self.assertEquals(logged_user.get('id'), resp_data.get('sender_id')) msg_id = resp_data.get('id') self.assertIsNotNone(msg_id)
def test_this(self): data = { 'rating': Decimal('1.41199860'), 'name': 'Hobro', 'country_id': 11, # Germany 'latitude': Decimal('56.6332999999999984'), 'timezone': 1, 'slug': 'hobro', 'longitude': Decimal('9.8000000000000007'), 'wikiname': 'Hobro', 'type': 'cities'} data2 = { 'rating': Decimal('1.80019736'), 'name': 'Hoddesdon', 'country_id': 11, # Germany 'latitude': Decimal('51.7500000000000000'), 'timezone': 0, 'type': 'cities', 'slug': 'hoddesdon', 'longitude': Decimal('0E-16'), 'wikiname': 'Hoddesdon'} # CREATING CITY [ERROR] logged_user = auth_user(self.app, 'angelo', 'ciao') self.assertIsNotNone(logged_user) token = logged_user.get('token') data['type'] = 'not_a_cities' resp = send_call(self.app, 'post', '/locations/%s' % data['type'], data, token=token) self.assertEquals(resp.status[:3], '400') # CREATING CITY [ERROR] data['type'] = 'cities' resp = send_call(self.app, 'post', '/locations/%s' % data['type'], data, token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) self.assertTrue(resp_data['id'] > 0) # GET EXPLICIT LOCATION - NOW WORKS resp = send_call(self.app, 'get', '/locations/%s/%s' % ( resp_data['type'], resp_data['id']), token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) # GET EXPLICIT LOCATION - NOW WORKS resp = send_call(self.app, 'get', '/locations/%s/%s' % ( resp_data['type'], resp_data['id'] + 100000), token=token) self.assertEquals(resp.status[:3], '404')
def validate_hospitality_request(cls, user): request_json = json_loads(request.data) success, params = cls._validate_message(HospitalityRequest, user, { 'date_from': request_json.get('date_from'), 'date_to': request_json.get('date_to')}) # TODO - status validation return (success, params)
def test_this(self): data = { 'decimal': Decimal('1.41199860'), 'string': 'Hobro', 'integer': 1, 'date': date(2012, 11, 11), 'time': time(15, 30), 'datetime': datetime(2012, 12, 10, 16, 43), } base_one = json_dumps(data) base_two = json_loads(base_one) self.assertEquals(data, base_two)
def patch(self, direction, msgtype, id): user = get_logged_user() request_json = json_loads(request.data) if msgtype not in MESSAGE_TYPE_MAPPING: return ('TYPE', 400, []) msg_dict = MESSAGE_TYPE_MAPPING[msgtype] msg_notification_class = msg_dict['notification_class'] if direction == 'out': # PATCH op is allowed only for incoming msgs return ('FORBIDDEN', 403, []) elif direction != 'in': # Invalid direction value return ('DIRECTION', 400, []) msg_list = filter_by_direction( msgtype, 'in', user, additional_filters={'id': id}) if not msg_list: return ('NOT FOUND', 404, []) msg = msg_list[0] added_flags, removed_flags = ( request_json.get('flags_in'), request_json.get('flags_out')) msg.change_flags(added_flags, removed_flags) msg_notification = msg.get_notification(user) errors = [] try: msg_notification.change_status(request_json.get('message_status')) except ValueError: # Invalid transition errors.append('Invalid "message_status" value.') if msgtype == 'hospitality_request': request_status = request_json.get('request_status') if (request_status and ((direction == 'out' and request_status == 'canceled') or direction == 'in' and msg.status != 'canceled')): try: msg.change_status(request_status) except ValueError: # Invalid transition errors.append('Invalid "request_status" value.') if errors: # Errors found, returns error structure return (jsonify(build_error_dict(errors)), 400, []) # in case of success, returns the updated message serialization msg.save() msg_notification.save(commit=True) return jsonify(msg.serialized_func(user))
def run(self, model=None, filters=None): """ Export fixtures. """ if model: filters_dict = json_loads(filters) if filters else None model_class = getattr(models, model) resp = fixtures.export_fixture(model_class, filters=filters_dict) else: resp = fixtures.export_all_fixtures() output_filename = "/tmp/output.json" with open(output_filename, "w") as fh: fh.write(resp)
def _validate_message(cls, user, param_extend={}): """ Expected form: {"subject": str, "text": str, "recipient_list_ids": [int*]} """ request_json = json_loads(request.data) # recipient list validation recipient_list_objects = User.query.filter(and_( User.id.in_(request_json.get('recipient_list_ids')), User.id != user.id, User.is_active)).all() recipient_list_ids = [o.id for o in recipient_list_objects] # param dict built here params = dict( # mandatory params here subject=request_json.get('subject'), sender_id=user.id, text=request_json.get('text'), recipient_list_ids=recipient_list_ids, # optional values here message_status=request_json.get('message_status'), reply_to_id=request_json.get('reply_to_id')) params.update(param_extend) # checking for mandatory parameters resp = cls.validate_values(params) errors = ( resp['errors.mandatory'] + resp['errors.type'] + resp['errors.other']) if errors: return (False, build_error_dict(errors)) # preparing the full param dict to be returned for (k, v) in params.items(): if v in (None, '', u''): del params[k] # success! return (True, params)
def test_reference(self): # AUTHENTICATION here logged_user = auth_user(self.app, 'angelo', 'ciao') self.assertIsNotNone(logged_user) token = logged_user.get('token') # message failure(s) here # 1. invalid status request = { 'text': 'A reference', 'user_id': 2, 'reference_type': 'invalid', } resp = send_call(self.app, 'post', '/connections/references', request, token=token) self.assertEquals(resp.status[:3], '400') # 2. invalid user request = { 'text': 'A reference', 'user_id': 66666, 'reference_type': 'positive', } resp = send_call(self.app, 'post', '/connections/references', request, token=token) self.assertEquals(resp.status[:3], '400') # 3. missing description request = { 'text': '', 'user_id': 2, 'reference_type': 'positive', } resp = send_call(self.app, 'post', '/connections/references', request, token=token) self.assertEquals(resp.status[:3], '400') # SUCCESSFUL HERE request = { 'text': 'A reference', 'user_id': 2, 'reference_type': 'positive', } resp = send_call(self.app, 'post', '/connections/references', request, token=token) self.assertEquals(resp.status[:3], '200') # fetching all friendship requests resp = send_call(self.app, 'get', '/connections/references', token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) self.assertTrue(isinstance(resp_data, list)) self.assertEquals(len(resp_data), 1) this_one = resp_data[0] this_one_id = this_one.get('id') self.assertTrue(this_one_id > 0) self.assertTrue(all(v == this_one[k] for k, v in request.iteritems())) # fetching this friendship request resp = send_call(self.app, 'get', '/connections/references/%s' % this_one_id, token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) self.assertTrue(isinstance(resp_data, dict)) self.assertTrue(all(v == this_one[k] for k, v in resp_data.iteritems())) # fetching a non-existing friendship request (404 expected) resp = send_call(self.app, 'get', '/connections/references/%s' % (this_one_id + 1000), token=token) self.assertEquals(resp.status[:3], '404') # AUTHENTICATION for other user here logged_user = auth_user(self.app, 'delgog', 'ciao') self.assertIsNotNone(logged_user) token = logged_user.get('token') # fetching this friendship request resp = send_call(self.app, 'get', '/connections/references/%s' % this_one_id, token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) self.assertTrue(isinstance(resp_data, dict)) self.assertTrue(all(v == this_one[k] for k, v in resp_data.iteritems() if k != 'user_id')) self.assertEquals(resp_data.get('user_id'), 1)
def auth_user(app, username, password): data = {'username': username, 'password': password} resp = send_call(app, 'post', '/auth', data) if resp.status_code == 200: return json_loads(resp.data)
def remove_fixtures_from_files(filelist): for this_file in reversed(filelist): with open(this_file, 'r') as fh: file_content = json_loads(fh.read()) _remove_fixture(file_content)
def test_private_messages(self): request = { 'subject': 'A Test Message', 'text': 'A test text...', 'recipient_list_ids': [2], } token, logged_user = self._sender_login() # message failure here resp = send_call(self.app, 'post', '/messages/in/privates', request, token=token) self.assertEquals(resp.status[:3], '403') # message being sent here resp = send_call(self.app, 'post', '/messages/out/privates', request, token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) msg_id = resp_data.get('id') self.assertEquals(logged_user.get('id'), resp_data.get('sender_id')) self.assertIsNotNone(msg_id) # message retrieval resp = send_call(self.app, 'get', '/messages/out/privates', request, token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) self.assertTrue(isinstance(resp_data, list) and len(resp_data) == 1) resp_data = resp_data[0] self.assertTrue(all( v == resp_data.get(k) for (k, v) in request.iteritems())) self.assertEquals(logged_user.get('id'), resp_data.get('sender_id')) # message retrieval (explicit ID) resp = send_call(self.app, 'get', '/messages/out/privates/%s' % msg_id, request, token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) self.assertTrue(isinstance(resp_data, dict)) self.assertTrue(all( v == resp_data.get(k) for (k, v) in request.iteritems())) self.assertEquals(logged_user.get('id'), resp_data.get('sender_id')) # changing message status is allowed only for the recipients resp = send_call(self.app, 'patch', '/messages/out/privates/%s' % msg_id, {'message_status': 'read'}, token=token) self.assertEquals(resp.status[:3], '403') # ensures the other user has successfully received the message # -- 1. authentication sender_user_id = logged_user.get('id') token, logged_user = self._recipient_login() # -- 2. retrieval resp = send_call(self.app, 'get', '/messages/in/privates', request, token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) self.assertTrue(isinstance(resp_data, list) and len(resp_data) == 1) resp_data = resp_data[0] self.assertTrue(all( v == resp_data.get(k) for (k, v) in request.iteritems())) self.assertEquals(sender_user_id, resp_data.get('sender_id')) self.assertEquals(resp_data.get('message_status'), 'unread') msg_id = resp_data.get('id') # changing message status is not allowed here - token must be passed resp = send_call(self.app, 'patch', '/messages/in/privates/%s' % msg_id, {'message_status': 'read'}) self.assertEquals(resp.status[:3], '401') # changing message status is allowed here resp = send_call(self.app, 'patch', '/messages/in/privates/%s' % msg_id, {'message_status': 'read'}, token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) self.assertEquals(resp_data.get('message_status'), 'read') # changing message status is not allowed here - wrong value resp = send_call(self.app, 'patch', '/messages/in/privates/%s' % msg_id, {'message_status': 'misread'}, token=token) self.assertEquals(resp.status[:3], '400') # writes a reply request = { 'subject': 'A Test Message', 'text': 'A test text...', 'recipient_list_ids': [1], 'reply_to_id': msg_id, } resp = send_call(self.app, 'post', '/messages/out/privates', request, token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) msg_id = resp_data.get('id') self.assertEquals(logged_user.get('id'), resp_data.get('sender_id')) self.assertTrue(all( v == resp_data.get(k) for (k, v) in request.iteritems())) # time to read the reply token, logged_user = self._sender_login() resp = send_call(self.app, 'get', '/messages/in/privates/%s' % msg_id) # no token - 401 self.assertEquals(resp.status[:3], '401') # token specified - 200 resp = send_call(self.app, 'get', '/messages/in/privates/%s' % msg_id, token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) self.assertTrue(all( v == resp_data.get(k) for (k, v) in request.iteritems())) self.assertEquals(resp_data.get('message_status'), 'unread')
def import_fixtures_from_files(filelist): for this_file in filelist: with open(this_file, 'r') as fh: file_content = json_loads(fh.read()) _import_fixture(file_content)
def import_fixture(fixture): return json_loads(_import_fixture(fixture))
def test_activity(self): # AUTHENTICATION here logged_user = auth_user(self.app, 'angelo', 'ciao') self.assertIsNotNone(logged_user) token = logged_user.get('token') today = datetime.now().replace( hour=0, minute=0, second=0, microsecond=0) # message failure(s) here # 1. invalid status request = { 'city_id': 1, 'location': 'A location', 'title': 'Activity name', 'description': 'Activity description', 'scheduled_from': today - timedelta(days=3, hours=19), 'scheduled_until': today + timedelta(days=3, hours=22), } resp = send_call(self.app, 'post', '/activities', request, token=token) self.assertEquals(resp.status[:3], '400') # 2. invalid city request = { 'city_id': 3353335, # non existing city 'location': 'A location', 'title': 'Activity name', 'description': 'Activity description', 'scheduled_from': today + timedelta(days=3, hours=19), 'scheduled_until': today + timedelta(days=3, hours=22), } resp = send_call(self.app, 'post', '/activities', request, token=token) self.assertEquals(resp.status[:3], '400') # 3. missing name request = { 'city_id': 3353335, # non existing city 'location': 'A location', 'description': 'Activity description', 'scheduled_from': today + timedelta(days=3, hours=19), 'scheduled_until': today + timedelta(days=3, hours=22), } resp = send_call(self.app, 'post', '/activities', request, token=token) self.assertEquals(resp.status[:3], '400') # 4. valid - success request = { 'city_id': 1, # Berlin 'location': 'A location', 'title': 'Activity name', 'description': 'Activity description', 'scheduled_from': today + timedelta(days=3, hours=19), 'scheduled_until': today + timedelta(days=3, hours=22), } resp = send_call(self.app, 'post', '/activities', request, token=token) resp_data = json_loads(resp.data) self.assertEquals(resp.status[:3], '200') self.assertTrue(all(v == resp_data[k] for k, v in request.iteritems())) # fetching all activities - token not needed resp = send_call(self.app, 'get', '/activities') self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) self.assertTrue(isinstance(resp_data, list)) self.assertEquals(len(resp_data), 1) this_one = resp_data[0] this_one_id = resp_data[0].get('id') self.assertTrue(this_one_id > 0) self.assertTrue(all(v == this_one[k] for k, v in request.iteritems())) attending_count = this_one.get('attending_count') self.assertTrue(isinstance(attending_count, dict)) self.assertTrue(all( k in attending_count for k in ('yes', 'no', 'maybe'))) self.assertEquals(attending_count['yes'], 1) self.assertEquals(attending_count['maybe'], 0) self.assertEquals(attending_count['no'], 0)
def test_friendship(self): # AUTHENTICATION here logged_user = auth_user(self.app, 'angelo', 'ciao') self.assertIsNotNone(logged_user) token = logged_user.get('token') # message failure(s) here # 1. invalid status request = { 'description': 'A friend', 'user_id': 2, 'friendship_level': 'invalid', } resp = send_call(self.app, 'post', '/connections/friendships', request, token=token) self.assertEquals(resp.status[:3], '400') # 2. invalid user request = { 'description': 'A friend', 'user_id': 666666, 'friendship_level': 'friend', } resp = send_call(self.app, 'post', '/connections/friendships', request, token=token) self.assertEquals(resp.status[:3], '400') # 3. missing description request = { 'description': '', 'user_id': 2, 'friendship_level': 'friend', } resp = send_call(self.app, 'post', '/connections/friendships', request, token=token) self.assertEquals(resp.status[:3], '400') # SUCCESSFUL HERE request = { 'description': 'A friend.', 'user_id': 2, 'friendship_level': 'friend', } resp = send_call(self.app, 'post', '/connections/friendships', request, token=token) self.assertEquals(resp.status[:3], '200') # fetching all friendship requests resp = send_call(self.app, 'get', '/connections/friendships', token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) self.assertTrue(isinstance(resp_data, list)) self.assertEquals(len(resp_data), 1) this_one = resp_data[0] this_one_id = resp_data[0].get('id') self.assertTrue(this_one_id > 0) self.assertTrue(all(v == this_one[k] for k, v in request.iteritems())) # fetching this friendship request resp = send_call(self.app, 'get', '/connections/friendships/%s' % this_one_id, token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) self.assertTrue(isinstance(resp_data, dict)) self.assertTrue(all(v == this_one[k] for k, v in resp_data.iteritems())) # trying to "approve" friendship status (can be done only by the other # counterpart) patch_request = {'type_status': 'accepted'} resp = send_call(self.app, 'patch', '/connections/friendships/%s' % this_one_id, patch_request, token=token) self.assertEquals(resp.status[:3], '405') # fetching a non-existing friendship request (404 expected) resp = send_call(self.app, 'get', '/connections/friendships/%s' % (this_one_id + 1000), token=token) self.assertEquals(resp.status[:3], '404') # AUTHENTICATION for other user here logged_user = auth_user(self.app, 'delgog', 'ciao') self.assertIsNotNone(logged_user) token = logged_user.get('token') # fetching this friendship request resp = send_call(self.app, 'get', '/connections/friendships/%s' % this_one_id, token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) self.assertTrue(isinstance(resp_data, dict)) self.assertTrue(all(v == this_one[k] for k, v in resp_data.iteritems() if k != 'user_id')) self.assertEquals(resp_data.get('user_id'), 1) # trying to "approve" friendship status (can be done only by the other # counterpart) # 1. invalid type status patch_request = {'type_status': 'invalid'} resp = send_call(self.app, 'patch', '/connections/friendships/%s' % this_one_id, patch_request, token=token) self.assertEquals(resp.status[:3], '400') # 2. valid type status - success patch_request = {'type_status': 'accepted'} resp = send_call(self.app, 'patch', '/connections/friendships/%s' % this_one_id, patch_request, token=token) self.assertEquals(resp.status[:3], '200') resp_data = json_loads(resp.data) self.assertEquals(resp_data.get('type_status'), 'accepted')
def test_this(self): today = date.today() data = { 'username': '******', 'password': '******', 'first_name': 'Angelo', 'last_name': 'Romano', 'email': '*****@*****.**', 'gender': '1', 'birth_date': date(today.year - 21, today.month, today.day), } data2 = { 'username': '******', 'password': '******', 'first_name': 'Angelo1', 'last_name': 'Romano1', 'email': '*****@*****.**', 'gender': '1', 'birth_date': date(today.year - 31, today.month, today.day), } # CREATING ACCOUNT 1 resp = send_call(self.app, 'post', '/users', data) resp_data = json_loads(resp.data) self.assertTrue(all(data.get(key) == resp_data.get(key) for key in data.iterkeys() if key != 'password')) id_one = resp_data['id'] self.assertTrue(id_one > 0) # CREATING ACCOUNT 2 resp = send_call(self.app, 'post', '/users', data2) resp_data2 = json_loads(resp.data) self.assertTrue(all(data2[key] == resp_data2[key] for key in data.iterkeys() if key != 'password')) id_two = resp_data2['id'] self.assertTrue(id_two > 0) # GET EXPLICIT USER - AUTH FAILURE resp = send_call(self.app, 'get', '/current_user') self.assertEquals(resp.status[:3], '401') # AUTHENTICATION COMES HERE resp = auth_user(self.app, data['username'], data['password'] + 'no') self.assertIsNone(resp) resp = auth_user(self.app, data['username'], data['password']) self.assertIsNotNone(resp) token = resp.get('token') # GET EXPLICIT CURRENT USER - NOW WORKS resp = send_call(self.app, 'get', '/current_user', token=token) resp_data = json_loads(resp.data) self.assertEquals(resp.status[:3], '200') self.assertEquals(resp_data.get('id'), id_one) # GET EXPLICIT USERS resp = send_call(self.app, 'get', '/users/%s' % id_one, token=token) resp_data = json_loads(resp.data) self.assertEquals(resp.status[:3], '200') self.assertTrue(all(data[key] == resp_data[key] for key in data.iterkeys() if key != 'password')) resp = send_call(self.app, 'get', '/users/%s' % id_two, token=token) resp_data = json_loads(resp.data) self.assertEquals(resp.status[:3], '200') self.assertTrue(all(data2[key] == resp_data[key] for key in data.iterkeys() if key != 'password')) # UPDATE USER patch_data = { 'details': { 'websites': ['http://www.angeloromano.com'], 'sections': { 'summary': 'This is a summary.', 'couch_info': 'This is my couch info.', }, 'profile_details': { 'occupation': 'My occupation.', }, }, } resp = send_call(self.app, 'patch', '/current_user', patch_data, token=token) resp_data = json_loads(resp.data) self.assertEquals(resp.status[:3], '200') self.assertTrue(all(data[key] == resp_data[key] for key in data.iterkeys() if key != 'password')) self.assertEquals(resp_data.get('details'), patch_data['details'])