def test_dashboard_album_resource_post_with_valid_token_and_role_and_valid_album_id_with_valid_data(self): r = Role(name='admin') u = User(username='******', email='*****@*****.**') u.set_password('test') u.roles.append(r) a = Album(path='/album') db.session.add(r) db.session.add(u) db.session.add(a) db.session.commit() album_id = a.id response = self.client.post(api.url_for(LoginResource), data=json.dumps({'username': '******', 'password': '******'}), headers=self.get_headers()) json_data = json.loads(response.data.decode('utf-8')) token = json_data['token'] user_id = json_data['user']['id'] response = self.client.post(api.url_for(DashboardAlbumResource,album_id=album_id), data=json.dumps({'title': 'title', 'description': 'description', 'timestamp_from': 'Wed, 02 Oct 2002 13:00:00 GMT', 'timestamp_to': 'Wed, 02 Oct 2002 13:00:00 GMT'}), headers=self.get_headers(token)) print response.data self.assertEqual(response.status_code,204)
def test_dashboard_album_photos_list_resource_get_with_valid_token(self): r = Role(name='admin') u = User(username='******', email='*****@*****.**') u.set_password('test') u.roles.append(r) a = Album(path='/album') p1 = Photo(path='/photo1') p2 = Photo(path='/photo2') p3 = Photo(path='/photo3') a.photos.append(p1) a.photos.append(p2) a.photos.append(p3) db.session.add(u) db.session.add(r) db.session.add(a) db.session.add(p1) db.session.add(p2) db.session.add(p3) db.session.commit() album_id = a.id response = self.client.post(api.url_for(LoginResource), data=json.dumps({'username': '******', 'password': '******'}), headers=self.get_headers()) json_data = json.loads(response.data.decode('utf-8')) token = json_data['token'] response = self.client.get(api.url_for(DashboardAlbumPhotosListResource,album_id=album_id), headers=self.get_headers(token)) self.assertEqual(response.status_code,200) photos = json.loads(response.data.decode('utf-8')) self.assertEqual(len(photos),3) self.assertTrue('id' in photos[0]) self.assertTrue('title' in photos[0]) self.assertTrue('description' in photos[0]) self.assertTrue('timestamp' in photos[0]) self.assertTrue('path' in photos[0]) self.assertTrue('created_at' in photos[0])
def test_album_children_list_resource_get_with_valid_token(self): u = User(username='******', email='*****@*****.**') u.set_password('test') a1 = Album(path='/album1') a2 = Album(path='/album2') a3 = Album(path='/album3') a4 = Album(path='/album4') a1.children.append(a2) a1.children.append(a3) a1.children.append(a4) db.session.add(u) db.session.add(a1) db.session.add(a2) db.session.add(a3) db.session.add(a4) db.session.commit() album_id = a1.id response = self.client.post(api.url_for(LoginResource), data=json.dumps({'username': '******', 'password': '******'}), headers=self.get_headers()) json_data = json.loads(response.data.decode('utf-8')) token = json_data['token'] response = self.client.get(api.url_for(AlbumChildrenListResource,album_id=album_id), headers=self.get_headers(token)) self.assertEqual(response.status_code,200) albums = json.loads(response.data.decode('utf-8')) self.assertEqual(len(albums),3) self.assertTrue('id' in albums[0]) self.assertTrue('title' in albums[0]) self.assertTrue('description' in albums[0]) self.assertTrue('timestamp_from' in albums[0]) self.assertTrue('timestamp_to' in albums[0]) self.assertFalse('path' in albums[0]) self.assertFalse('created_at' in albums[0])
def get(self): args = parser.parse_args() per_page = min(args['per_page'], 100) page = args['page'] query = Customer.to_collection_dict(Customer.query, page, per_page) data = { 'customers': query['items'], 'page': page, 'per_page': per_page, 'total_pages': query['_meta']['total_pages'], 'total_items': query['_meta']['total_items'], } if query['has_next']: data.update( { 'next_page': api.url_for( CustomersService, page=page + 1, per_page=per_page ) } ) if query['has_prev']: data.update( { 'prev_page': api.url_for( CustomersService, page=page - 1, per_page=per_page ) } ) return data
def test_dashboard_album_resource_get_with_valid_token_and_role_and_valid_album_id(self): r = Role(name='admin') u = User(username='******', email='*****@*****.**') u.set_password('test') u.roles.append(r) a = Album(path='/album') db.session.add(r) db.session.add(u) db.session.add(a) db.session.commit() album_id=a.id response = self.client.post(api.url_for(LoginResource), data=json.dumps({'username': '******', 'password': '******'}), headers=self.get_headers()) json_data = json.loads(response.data.decode('utf-8')) token = json_data['token'] response = self.client.get(api.url_for(DashboardAlbumResource,album_id=album_id), headers=self.get_headers(token)) self.assertEqual(response.status_code,200) album = json.loads(response.data.decode('utf-8')) self.assertEqual(album['path'],'/album') self.assertTrue('title' in album) self.assertTrue('description' in album) self.assertTrue('id' in album) self.assertTrue('timestamp_from' in album) self.assertTrue('timestamp_to' in album)
def test_dashboard_user_roles_resource_delete_with_valid_token_but_without_role(self): u = User(username='******', email='*****@*****.**') u.set_password('test') db.session.add(u) db.session.commit() response = self.client.post(api.url_for(LoginResource), data=json.dumps({'username': '******', 'password': '******'}), headers=self.get_headers()) json_data = json.loads(response.data.decode('utf-8')) token = json_data['token'] response = self.client.delete(api.url_for(DashboardUserRolesResource,user_id=0, role_name='Role'), headers=self.get_headers(token)) self.assertEqual(response.status_code,403)
def test_user_resource_get_with_valid_token_and_invalid_user_id(self): u = User(username='******', email='*****@*****.**') u.set_password('test') db.session.add(u) db.session.commit() response = self.client.post(api.url_for(LoginResource), data=json.dumps({'username': '******', 'password': '******'}), headers=self.get_headers()) json_data = json.loads(response.data.decode('utf-8')) token = json_data['token'] user_id = json_data['user']['id'] response = self.client.get(api.url_for(UserResource,user_id=user_id+1), headers=self.get_headers(token)) self.assertEqual(response.status_code,404)
def test_dashboard_album_resource_get_with_valid_token_and_role_and_invalid_album_id(self): r = Role(name='admin') u = User(username='******', email='*****@*****.**') u.set_password('test') u.roles.append(r) db.session.add(r) db.session.add(u) db.session.commit() response = self.client.post(api.url_for(LoginResource), data=json.dumps({'username': '******', 'password': '******'}), headers=self.get_headers()) json_data = json.loads(response.data.decode('utf-8')) token = json_data['token'] response = self.client.get(api.url_for(DashboardAlbumResource,album_id=0), headers=self.get_headers(token)) self.assertEqual(response.status_code,404)
def test_dashboard_album_photos_list_resource_get_with_invalid_token(self): a = Album(path='/album') db.session.add(a) db.session.commit() album_id = a.id response = self.client.get(api.url_for(DashboardAlbumPhotosListResource,album_id=album_id), headers=self.get_headers(token='THIS_IS_AN_INVALID_TOKEN')) self.assertEqual(response.status_code,401)
def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client() self.url = api.url_for(RegisterResource)
def setUp(self): self.app = create_app("testing") self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client() self.url = api.url_for(DashboardAlbumListResource)
def test_dashboard_user_list_resource_get_with_valid_token_and_role(self): r = Role(name='admin') u = User(username='******', email='*****@*****.**') u.set_password('test') u.roles.append(r) u1 = User(username='******', email='*****@*****.**') u2 = User(username='******', email='*****@*****.**') db.session.add(r) db.session.add(u) db.session.add(u1) db.session.add(u2) db.session.commit() response = self.client.post(api.url_for(LoginResource), data=json.dumps({'username': '******', 'password': '******'}), headers=self.get_headers()) json_data = json.loads(response.data.decode('utf-8')) token = json_data['token'] response = self.client.get(self.url, headers=self.get_headers(token)) self.assertEqual(response.status_code,200) users = json.loads(response.data.decode('utf-8')) self.assertEqual(len(users),3) self.assertEqual(users[0]['username'],'test') self.assertEqual(users[1]['username'],'foo') self.assertEqual(users[2]['username'],'bar') self.assertTrue('email' in users[0]) self.assertTrue('registered_at' in users[0]) self.assertTrue('last_seen' in users[0]) self.assertTrue('id' in users[0]) self.assertTrue('roles' in users[0])
def test_dashboard_album_list_resource_get_with_valid_token(self): u = User(username="******", email="*****@*****.**") r = Role(name="admin") u.set_password("test") u.roles.append(r) a1 = Album(path="/album1") a2 = Album(path="/album2") a3 = Album(path="/album3") a4 = Album(path="/album4") db.session.add(u) db.session.add(r) db.session.add(a1) db.session.add(a2) db.session.add(a3) db.session.add(a4) db.session.commit() response = self.client.post( api.url_for(LoginResource), data=json.dumps({"username": "******", "password": "******"}), headers=self.get_headers(), ) json_data = json.loads(response.data.decode("utf-8")) token = json_data["token"] response = self.client.get(self.url, headers=self.get_headers(token)) self.assertEqual(response.status_code, 200) albums = json.loads(response.data.decode("utf-8")) self.assertEqual(len(albums), 4) self.assertTrue("id" in albums[0]) self.assertTrue("title" in albums[0]) self.assertTrue("description" in albums[0]) self.assertTrue("timestamp_from" in albums[0]) self.assertTrue("timestamp_to" in albums[0]) self.assertTrue("path" in albums[0]) self.assertTrue("created_at" in albums[0])
def get_player_json(player, public=False): data = {} data["id"] = player.id data["name"] = player.name if public: data['api_url'] = "%s%s" % (api.url_for(Player), player.id) if player.custom_values: for k, v in player.custom_values.iteritems(): data[k] = v return data
def get_task_skill_json(task_skill, public=False): data = {} data["id"] = task_skill.id data["pid"] = task_skill.pid data["tid"] = task_skill.tid data["calculated_on"] = task_skill.calculated_on.isoformat() data["considered_rows"] = task_skill.considered_rows data["skill_points"] = task_skill.skill_points if public: data['api_url'] = "%s%s" % (api.url_for(TaskSkill), task_skill.id) return data
def get_task_json(task, public=False): data = {} data["id"] = task.id data["name"] = task.name data["description"] = task.description if public: data['api_url'] = "%s%s" % (api.url_for(Task), task.id) if task.custom_values: for k, v in task.custom_values.iteritems(): data[k] = v return data
def get_level_type_json(level_type, public=False): data = {} data["id"] = level_type.id data["name"] = level_type.name data["description"] = level_type.description if public: data['api_url'] = "%s%s" % (api.url_for(LevelType), level_type.id) if level_type.custom_values: for k, v in level_type.custom_values.iteritems(): data[k] = v return data
def get_event_skill_json(event_skill, public=False): data = {} data["id"] = event_skill.id data["pid"] = event_skill.pid data["eid"] = event_skill.eid data["calculated_on"] = event_skill.calculated_on.isoformat() data["considered_rows"] = event_skill.considered_rows data["skill_points"] = event_skill.skill_points if public: data['api_url'] = "%s%s" % (api.url_for(EventSkill), event_skill.id) return data
def test_user_resource_get_with_valid_token_and_valid_user_id(self): u = User(username='******', email='*****@*****.**') u.set_password('test') db.session.add(u) db.session.commit() response = self.client.post(api.url_for(LoginResource), data=json.dumps({'username': '******', 'password': '******'}), headers=self.get_headers()) json_data = json.loads(response.data.decode('utf-8')) token = json_data['token'] user_id = json_data['user']['id'] response = self.client.get(api.url_for(UserResource,user_id=user_id), headers=self.get_headers(token)) self.assertEqual(response.status_code,200) user = json.loads(response.data.decode('utf-8')) self.assertEqual(user['username'],'test') self.assertFalse('email' in user) self.assertTrue('registered_at' in user) self.assertTrue('last_seen' in user) self.assertTrue('id' in user) self.assertTrue('roles' in user)
def test_dashboard_user_roles_resource_post_with_valid_token_and_role_and_valid_user_id_and_role_name(self): r = Role(name='admin') r1 = Role(name='poweruser') u = User(username='******', email='*****@*****.**') u.set_password('test') u.roles.append(r) db.session.add(r) db.session.add(r1) db.session.add(u) db.session.commit() response = self.client.post(api.url_for(LoginResource), data=json.dumps({'username': '******', 'password': '******'}), headers=self.get_headers()) json_data = json.loads(response.data.decode('utf-8')) token = json_data['token'] user_id = json_data['user']['id'] response = self.client.post(api.url_for(DashboardUserRolesResource,user_id=user_id, role_name='poweruser'), data=json.dumps({}), headers=self.get_headers(token)) self.assertEqual(response.status_code,204)
def get_level_instance_json(level_instance, public=False): data = {} data["id"] = level_instance.id data["lid"] = level_instance.lid data["start_time"] = level_instance.start_time.isoformat() data["end_time"] = level_instance.end_time.isoformat() if level_instance.end_time else None if public: data["api_url"] = "%s%s" % (api.url_for(LevelInstance), level_instance.id) if level_instance.custom_values: for k, v in level_instance.custom_values.iteritems(): data[k] = v return data
def get_level_type_skill_json(level_type_skill, public=False): data = {} data["id"] = level_type_skill.id data["pid"] = level_type_skill.pid data["ltid"] = level_type_skill.ltid data["calculated_on"] = level_type_skill.calculated_on.isoformat() data["considered_rows"] = level_type_skill.considered_rows data["skill_points"] = level_type_skill.skill_points data["high_score"] = level_type_skill.high_score if public: data['api_url'] = "%s%s" % (api.url_for(LevelTypeSkill), level_type_skill) return data
def get_level_json(level, public=False): data = {} data["id"] = level.id data["name"] = level.name data["description"] = level.description data["level_types"] = [lt.id for lt in level.level_types] if public: data["api_url"] = "%s%s" % (api.url_for(Level), level.id) if level.custom_values: for k, v in level.custom_values.iteritems(): data[k] = v return data
def test_photo_resource_get_with_valid_token(self): u = User(username='******', email='*****@*****.**') u.set_password('test') p = Photo(path='/photo') db.session.add(u) db.session.add(p) db.session.commit() photo_id = p.id response = self.client.post(api.url_for(LoginResource), data=json.dumps({'username': '******', 'password': '******'}), headers=self.get_headers()) json_data = json.loads(response.data.decode('utf-8')) token = json_data['token'] response = self.client.get(api.url_for(PhotoResource,photo_id=photo_id), headers=self.get_headers(token)) self.assertEqual(response.status_code,200) album = json.loads(response.data.decode('utf-8')) self.assertTrue('id' in album) self.assertTrue('title' in album) self.assertTrue('description' in album) self.assertTrue('timestamp' in album) self.assertFalse('path' in album) self.assertFalse('created_at' in album)
def test_profile_resource_post_with_valid_token_and_valid_data(self): u = User(username='******', email='*****@*****.**') u.set_password('test') db.session.add(u) db.session.commit() response = self.client.post(api.url_for(LoginResource), data=json.dumps({'username': '******', 'password': '******'}), headers=self.get_headers()) json_data = json.loads(response.data.decode('utf-8')) token = json_data['token'] response = self.client.post(self.url, data=json.dumps({'password': '******', 'email': '*****@*****.**'}), headers=self.get_headers(token)) self.assertEqual(response.status_code,204)
def get_participation_json(participation, public=False): data = {} data["id"] = participation.id data["pid"] = participation.pid data["liid"] = participation.liid data["start_time"] = participation.start_time.isoformat() data["end_time"] = participation.end_time.isoformat()\ if participation.end_time else None if public: data['api_url'] = "%s%s" % ( api.url_for(Participation), participation.id) if participation.custom_values: for k, v in participation.custom_values.iteritems(): data[k] = v return data
def get_triggered_event_json(triggered_event, public=False): data = {} data["id"] = triggered_event.id data["paid"] = triggered_event.paid data["eid"] = triggered_event.eid data["timestamp"] = triggered_event.timestamp.isoformat() data["given_skill_points"] = triggered_event.given_skill_points; data["given_score_points"] = triggered_event.given_score_points; if public: data['api_url'] = "%s%s" % ( api.url_for(TriggeredEvent), triggered_event.id) if triggered_event.custom_values: for k, v in triggered_event.custom_values.iteritems(): data[k] = v return data
def post(self): """ Implement POST method """ data = request.get_json() or {} if 'title' not in data or \ 'deadline' not in data: return bad_request('must include at least title and deadline') is_valid, message = date_validate(data['deadline']) if not is_valid: return bad_request(message) task = Task() task.from_dict(data) db.session.add(task) db.session.commit() response = jsonify(task.to_dict()) response.status_code = 201 response.headers['Location'] = api.url_for(ApiTaskId, task_id=task.task_id) return response
def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() self.client = self.app.test_client() self.url = api.url_for(ConfigResource)
def test_album_children_list_resource_delete(self): response = self.client.delete(api.url_for(AlbumChildrenListResource,album_id=0), headers=self.get_headers()) self.assertEqual(response.status_code,405)
def test_album_children_list_resource_get_with_invalid_token(self): response = self.client.get(api.url_for(AlbumChildrenListResource,album_id=0), headers=self.get_headers(token='THIS_IS_AN_INVALID_TOKEN')) self.assertEqual(response.status_code,401)
def test_album_children_list_resource_get_without_token(self): response = self.client.get(api.url_for(AlbumChildrenListResource,album_id=0), headers=self.get_headers()) self.assertEqual(response.status_code,401)