Exemple #1
0
def lambda_handler(event, context):
    try:
        usr = user.deserialize(event["user"])
        #usr = user.deserialize(event["user"])
        password = event["password"]
    except:
        return respond("400", user.User(-1, "", "", "userAdmin").serialize())

    if not all(x is not None for x in [usr, password]):
        return respond("400", user.User(-1, "", "", "userAdmin").serialize())

    try:
        if usr.role == "citizen":
            _usr = user.User.create_new_user(usr.name, usr.email, password,
                                             usr.role, usr.address, usr.city,
                                             usr.postnr)
            _usr.token = get_auth_token(_usr)
            return respond("200", _usr.serialize())
        else:
            _usr = user.User.create_new_user(usr.name, usr.email, password,
                                             usr.role)
            _usr.token = get_auth_token(_usr)
            return respond("200", _usr.serialize())

    except Exception as ex:
        raise ex
        return respond("400", user.User(-1, "", "", "userAdmin").serialize())
Exemple #2
0
    def test_device_delete(self):
        _device = device.AlexaDevice(-1, "temp_token1234")
        device_uri = "https://prbw36cvje.execute-api.us-east-1.amazonaws.com/dev/citizen/" + str(
            self._citizen.id) + "/device"
        device_post_header = {
            "device":
            _device.serialize(),
            "token":
            "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiMjM0IiwidXNlcl9yb2xlIjoidXNlckFkbWluIn0._zMvLSI7elh4ebv-1iaE8nXYdbzjyvpBNlDNhNSJMc0"
        }

        _device = device.deserialize(
            get_response(requests.post(device_uri,
                                       headers=device_post_header)))
        device_delete_header = {
            "device":
            _device.serialize(),
            "token":
            "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiMjM0IiwidXNlcl9yb2xlIjoidXNlckFkbWluIn0._zMvLSI7elh4ebv-1iaE8nXYdbzjyvpBNlDNhNSJMc0"
        }

        print("Delete" + str(
            device.deserialize(
                get_response(
                    requests.delete(device_uri,
                                    headers=device_delete_header))).__dict__))

        user_get_header = {"email": "*****@*****.**", "password": "******"}
        user_get_uri = "https://prbw36cvje.execute-api.us-east-1.amazonaws.com/dev/user/"

        _citizen_response = user.deserialize(
            get_response(requests.get(user_get_uri, headers=user_get_header)))

        self.assertEqual(len(_citizen_response.devices), 0)
Exemple #3
0
def lambda_handler(event, context):
    try:
        ctz_id = int(event["id"])
    except:
        return respond("400", alarm.Alarm(-1, None, None).serialize())

    if not ctz_id:
        return respond("400", alarm.Alarm(-1, None, None).serialize())

    # Create alarm
    # To create an alarm, we need database access, so it has to happen on another VPC enabled lambda.
    # But if we are VPC enabled we are unable to interact with SNS services, so we have to split the task up.
    try:
        lambda_client = boto3.client(
            'lambda',
            region_name=region_name,
            aws_access_key_id=aws_access_key_id,
            aws_secret_access_key=aws_secret_access_key)

        arg = bytes(json.dumps({"id": ctz_id}), 'utf-8')
        response = lambda_client.invoke(FunctionName=arn_alarm_post_endpoint,
                                        InvocationType="RequestResponse",
                                        Payload=arg)

        data = response["Payload"].read().decode()
    except:
        return respond("400", alarm.Alarm(-1, None, None).serialize())

    # Get the alarm
    try:
        dt = json.loads(json.loads(data))
        usr = user.deserialize(
            str(dt["activatedby"]
                ))  #user.Citizen(str(dt["activatedby"]).replace("'", '"'))
        alm = alarm.Alarm(dt["status"], usr, None)
    except:
        return respond("400", alarm.Alarm(-1, None, None).serialize())

    for c in alm.activatedby.contacts:
        for d in c.devices:
            if d.devicetype == "appdevice":
                if not d.arn or not d.token:
                    continue
                push_message(d.arn, alm.serialize())
            # elif d.devicetype == "smsdevice":
            # send_sms(d.phone_number, c.activatedby.name + " has had an falling accident, and requests help.")

    for d in alm.activatedby.devices:
        if d.devicetype == "iftttdevice":
            urllib.request.urlopen(
                "https://maker.ifttt.com/trigger/fall_detected/with/key/" +
                d.token).read()

    return respond("200", alm.serialize())
Exemple #4
0
def lambda_handler(event, context):
    try:
        usr = deserialize(event["user"])
    except:
        return respond("400", event["user"])

    if not usr:
        return respond("400", event["user"])

    usr.update()

    return respond("200", usr.serialize())
Exemple #5
0
    def setUp(self):
        self._contact = user.Contact(-1, "Contactium", "*****@*****.**",
                                     [])

        contact_header = {
            "user": self._contact.serialize().replace("\"", "\\\""),
            "password": "******"
        }
        self._contact = user.deserialize(
            get_response(
                requests.post(
                    "https://prbw36cvje.execute-api.us-east-1.amazonaws.com/dev/user/",
                    headers=contact_header)))
Exemple #6
0
    def setUp(self):
        self._citizen = user.Citizen(-1, "Test Testington",
                                     "*****@*****.**", [], [],
                                     "Teststrasse 10", "Testerup", "1000")

        citizen_header = {
            "user": self._citizen.serialize().replace("\"", "\\\"")
        }
        self._citizen = user.deserialize(
            get_response(
                requests.post(
                    "https://prbw36cvje.execute-api.us-east-1.amazonaws.com/dev/user/",
                    headers=citizen_header)))
Exemple #7
0
    def test_get_and_post_user_citizen(self):
        _user = user.Citizen(-1, "Test Testington", "*****@*****.**", [],
                             [], "Teststrasse 10", "Testerup", "1000")
        user_post_uri = "https://prbw36cvje.execute-api.us-east-1.amazonaws.com/dev/user/"
        user_delete_uri = "https://prbw36cvje.execute-api.us-east-1.amazonaws.com/dev/user/"

        user_post_header = {
            "user": _user.serialize().replace("\"", "\\\""),
            "password": "******"
        }
        user_get_header = {"email": "*****@*****.**", "password": "******"}

        _user = user.deserialize(
            get_response(requests.post(user_post_uri,
                                       headers=user_post_header)))

        user_get_uri = "https://prbw36cvje.execute-api.us-east-1.amazonaws.com/dev/user/"

        _user_response = user.deserialize(
            get_response(requests.get(user_get_uri, headers=user_get_header)))
        user_delete_header = {
            "user":
            _user_response.serialize().replace("\"", "\\\""),
            "token":
            "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiMjM0IiwidXNlcl9yb2xlIjoidXNlckFkbWluIn0._zMvLSI7elh4ebv-1iaE8nXYdbzjyvpBNlDNhNSJMc0"
        }

        _delete_response = user.deserialize(
            get_response(
                requests.delete(user_delete_uri, headers=user_delete_header)))

        _user.token = ""
        _user_response.token = ""

        self.assertDictEqual(
            json.loads(_user.serialize().replace("'", "\"")),
            json.loads(_user_response.serialize().replace("'", "\"")))
        self.assertEqual(-1, _delete_response.id)
Exemple #8
0
    def test_get_citizen(self):
        user_get_header = {'token': self._citizen.token}
        user_get_uri = "https://prbw36cvje.execute-api.us-east-1.amazonaws.com/dev/citizen/" + str(
            self._citizen.id)

        _citizen_response = user.deserialize(
            get_response(requests.get(user_get_uri, headers=user_get_header)))

        self._citizen.token = ""
        _citizen_response.token = ""

        self.assertDictEqual(
            json.loads(self._citizen.serialize().replace("'", "\"")),
            json.loads(_citizen_response.serialize().replace("'", "\"")))
Exemple #9
0
def lambda_handler(event, context):
    try:
        usr = usr = user.deserialize(str(event["user"]))
    except:
        return respond("400", event["user"])

    if not user:
        return respond("400", event["user"])

    try:
        usr.delete()
        return respond("200",
                       user.User(-1, "", "", "citizenAdmin").serialize())
    except:
        return respond("400", event["user"])
Exemple #10
0
    def test_get_contact(self):
        user_get_header = {
            "token":
            "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiMjM0IiwidXNlcl9yb2xlIjoidXNlckFkbWluIn0._zMvLSI7elh4ebv-1iaE8nXYdbzjyvpBNlDNhNSJMc0"
        }
        user_get_uri = "https://prbw36cvje.execute-api.us-east-1.amazonaws.com/dev/contact/" + str(
            self._contact.id)

        _citizen_response = user.deserialize(
            get_response(requests.get(user_get_uri, headers=user_get_header)))
        self._contact.token = ""
        _citizen_response.token = ""
        self.assertDictEqual(
            json.loads(self._contact.serialize().replace("'", "\"")),
            json.loads(_citizen_response.serialize().replace("'", "\"")))