def post(self): data = request.get_json() system_id = data.get('system_id') user_email = data.get('user_email') if None in (system_id, user_email): abort(400) system = System.from_system_id(system_id) user = User.from_email(user_email) if None in (system, user): abort(400) if current_user == user: abort(400) if not Owner.is_owner_of(current_user, system): abort(401) sec_obj = SecondaryModel.create(user, system) sec_obj.put() secondaries = SecondaryModel.from_system(system) if sec_obj not in secondaries: secondaries.append(sec_obj) ret = self.get_response_map_from_secondaries(secondaries) return jsonify(ret)
def get(self, oauth_id=None): """ Gets current user information. :param oauth_id: :return: models/user/User plus owned_systems, secondary_systems """ if oauth_id is None: oauth_id = current_user.oauth_id if current_user.oauth_id != oauth_id: # TODO: Check for admin status here. abort(403) data = current_user.to_json() owned = OwnerModel.from_user(current_user) try: owned_data = owned.system_key.get().to_json() except AttributeError: data['owned_systems'] = [] else: data['owned_systems'] = [owned_data, ] secondary = Secondary.from_user(current_user) secondary_data = [s.system_key.get().to_json() for s in secondary] data['secondary_systems'] = secondary_data return jsonify(data)
def test_from_user(random_secondary): random_user = random_secondary.user_key.get() users = Secondary.from_user(random_user) assert len(users) > 0 assert all(sec.user_key == random_user.key for sec in users)
def get_pubsub_data(self, devicedata_entry): # type: (DeviceData) -> dict data = devicedata_entry.to_json() # Previous locations device = devicedata_entry.device_key.get() previous_5 = devicedata_entry.get_last(device, n=6) try: previous_5.remove(devicedata_entry) except ValueError: pass data['previous'] = [prev.to_json() for prev in previous_5] # Phone data system = device.system_key.get() owned = Owner.from_system(system) secondary_nums = Secondary.get_all_contact_numbers(system) try: phone_numbers = [owned.get_contact_number()] + secondary_nums except AttributeError: raise LookupError("No system owner.") data['phones'] = phone_numbers return data
def get(self, system_id): system = System.from_system_id(system_id) if system is None or not Owner.is_owner_of(current_user, system): abort(400) secondaries = SecondaryModel.from_system(system) ret = self.get_response_map_from_secondaries(secondaries) return jsonify(ret)
def test_create(random_owner, user_factory): other = user_factory.get() system = random_owner.system_key.get() sec = Secondary.create(other, system) sec.put() assert sec.system_key == system.key assert sec.user_key == other.key
def test_secondary_delete(random_owner, user_factory, logged_in_app): other_user = user_factory.get() sec = Secondary.create(other_user, random_owner.system_key.get()) sec.put() is_sec, sec_obj = Secondary.is_secondary_of(other_user, random_owner.system_key.get()) assert is_sec is True with logged_in_app.application.app_context(): resp = logged_in_app.delete( url_for('secondary', secondary_id=sec_obj.key.integer_id()) ) assert resp.status_code == 200 is_sec, _ = Secondary.is_secondary_of(other_user, random_owner.system_key.get()) assert is_sec is False
def delete(self, secondary_id): secondary = SecondaryModel.from_id(secondary_id) if secondary is None: abort(400, "That secondary does not exist.") system = secondary.system_key.get() if not Owner.is_owner_of(current_user, system): abort(401) secondaries = SecondaryModel.from_system(system) if secondary in secondaries: secondaries.remove(secondary) ret = self.get_response_map_from_secondaries(secondaries) try: secondary.key.delete() except AttributeError: pass return jsonify(ret)
def get(self, system_id): system = SystemModel.from_system_id(system_id) is_owner = OwnerModel.is_owner_of(current_user, system) is_sec, _ = SecondaryModel.is_secondary_of(current_user, system) if not (is_owner or is_sec): abort(401) frames = system.get_latest_data_frames() devid_loc = { f.device_key.get().serial_num: get_download_url(f.location) for f in frames } return jsonify(devid_loc)
def test_secondary_get(random_owner, random_system, logged_in_app, user_factory): other_user = user_factory.get() sec = Secondary.create(other_user, random_system) sec.put() with logged_in_app.application.app_context(): resp = logged_in_app.get( url_for('secondary', system_id=random_system.key.integer_id()) ) assert resp.status_code == 200 data = json.loads(resp.data) for k, v in data.items(): assert k == str(sec.key.integer_id()) assert v == other_user.to_json()
def test_secondary_post(random_owner, logged_in_app, user_factory): other_user = user_factory.get() data = { "user_email": other_user.email, "system_id": random_owner.system_key.integer_id() } with logged_in_app.application.app_context(): resp = logged_in_app.post(url_for('secondary'), data=json.dumps(data), headers={'content-type': 'application/json'}) assert resp.status_code == 200 assert len(json.loads(resp.data)) > 0 is_secondary, model = Secondary.is_secondary_of( other_user, random_owner.system_key.get() ) assert is_secondary is True
def put(self, system_id): data = request.get_json() if data is None or not SystemModel.valid_update_keys(data.keys()): abort(401) ks_status = data.get("ks_enabled") if ks_status is None: abort(400) current_system = SystemModel.get_by_id(system_id) owner = OwnerModel.is_owner_of(current_user, current_system) is_sec, _ = SecondaryModel.is_secondary_of(current_user, current_system) if not (owner or is_sec): abort(401) current_system.ks_enabled = ks_status current_system.put() return jsonify(current_system.to_json())
def random_secondary(random_user, random_system): secondary = Secondary.create(random_user, random_system) secondary.put() yield secondary secondary.key.delete()
def test_from_user_system(random_secondary): user = random_secondary.user_key.get() system = random_secondary.system_key.get() assert random_secondary == Secondary.from_system_user(system, user)
def test_from_system(random_secondary): system = random_secondary.system_key.get() assert random_secondary in Secondary.from_system(system)
def test_from_id(random_secondary): id_ = random_secondary.key.integer_id() assert Secondary.from_id(id_) == random_secondary