Пример #1
0
    def test_replace_troublemaker_tag(self):
        self.client = AddGuestClient(self.phone_number, "hello world")
        name_before = self.guest.name
        self.client.replace_troublemaker_tag()
        assert name_before == self.guest.name

        self.guest.name = "TROUBLEMAKER"
        self.guest.save()
        self.client = AddGuestClient(self.phone_number, "hello world")
        self.client.replace_troublemaker_tag()
        assert self.client.guest.name == "hello world"
Пример #2
0
    def test_add_to_database(self):
        # non-member
        self.client = AddGuestClient("+1098765432", "hello world")
        self.client.add_to_database()

        guest = Guest.objects.filter(phone="+1098765432")
        assert len(guest) == 1
        assert guest.first().id == self.client.guest.id
        assert guest.first().name == "hello world"

        # member
        self.client = AddGuestClient(self.member.phone, "hello world")
        self.client.add_to_database()

        guest = Guest.objects.filter(phone=self.member.phone)
        assert len(guest) == 1
        assert guest.first().id == self.client.guest.id
        assert guest.first().member.id == self.member.id
    def create(self, request):
        """
        Handles the SMS Requests POSTed In by Twilio
        """
        logger.info(f"Received request: {request}")
        logger.info(f"Request body: {request.body}")

        headers = request.headers

        try:
            body = extract_twilio_body(request.body)
        except Exception as e:
            logger.error("Encountered error when parsing Twilio request body")
            return Response(
                {
                    "improper_format":
                    f"Was unable to parse Twilio data from your requests",
                    "body": f"{request.body}",
                    "headers": f"{headers}",
                    "error_message": f"{e}",
                },
                status=status.HTTP_400_BAD_REQUEST,
            )

        # Twilio's verification will sporadically decide to fail. Who knows why or how. This code
        # enables a server setting to bypass verification checking as a bandaid.
        # TODO: Remove this bandaid and actually determine why verification is failing. Will probably need logging to go off.
        is_valid_message = verify_twilio_request(headers, body)

        # By default, we'll check all messages for authenticity.
        server_setting = server_setting_get_or_none("verify-twilio-requests")
        checking_message = bool(
            server_setting) if server_setting is not None else True

        logger.warning(f"Valid Twilio Signature? {is_valid_message}")
        logger.warning(
            f"For Body: {body}, Uri: {request.get_full_path()}, Signature: {headers['X-Twilio-Signature']}"
        )

        if checking_message and not is_valid_message:
            return Response(
                {
                    "signature_invalid":
                    f"The signature included with the request is invalid. Who are you?",
                    "body": f"{body}",
                    "signature": f"{headers['X-Twilio-Signature']}",
                    "uri": f"{request.get_full_path()}",
                },
                status=status.HTTP_400_BAD_REQUEST,
            )

        # Check for flag
        if help_flag_is_present(body["From"]):
            # For this flow, this is CRITICAL. In the event of an emergency coinciding with something breaking, we need
            # to provide some support still.
            try:
                emergency_response = EmergencyResponseClient(
                    body["From"]).get_more_info_or_fwd_sms(body["Body"])
                return HttpResponse(
                    emergency_response,
                    status=status.HTTP_200_OK,
                    content_type="text/xml",
                )
            except Exception as e:
                logger.error(
                    "Error in EmergencyResponseClient, get_more_info_or_fwd_sms"
                )
                logger.error(e)
                return HttpResponse(
                    get_messaging_response(code_is_breaking_message()),
                    status=status.HTTP_200_OK,
                    content_type="text/xml",
                )

        # Check if there's an event going on
        if len(get_active_event()) == 0:
            return HttpResponse(
                get_messaging_response(no_event_message()),
                status=status.HTTP_200_OK,
                content_type="text/xml",
            )

        # Check if text is the default
        if text_is_default(body["Body"]):
            troublemaker_response = TroublemakerClient(
                body["From"]).process_troublemaker()
            return HttpResponse(
                troublemaker_response,
                status=status.HTTP_200_OK,
                content_type="text/xml",
            )

        # Checking if the message says help me
        if text_is_help(body["Body"]):
            # For this flow, this is CRITICAL. In the event of an emergency coinciding with something breaking, we need
            # to provide some support still.
            try:
                return HttpResponse(
                    EmergencyResponseClient(
                        body["From"]).handle_request_for_help(),
                    status=status.HTTP_200_OK,
                    content_type="text/xml",
                )
            except Exception as e:
                logger.error(
                    "Error in EmergencyResponseClient, handle_request_for_help"
                )
                logger.error(e)
                return HttpResponse(
                    get_messaging_response(code_is_breaking_message()),
                    status=status.HTTP_200_OK,
                    content_type="text/xml",
                )

        else:
            logger.warning(
                "All pre-message checks passed, continuing with add guest flow"
            )
            add_guest_client_response = AddGuestClient(
                body["From"], body["Body"]).process_guest()
            return HttpResponse(
                add_guest_client_response,
                status=status.HTTP_200_OK,
                content_type="text/xml",
            )
Пример #4
0
class TestAddGuestClient(TestCase):
    def setUp(self):
        self.guest = create_guest()
        self.member = generate_fake_new_user()
        self.phone_number = "+11234567890"

    # all of the methods used in process_guest and in check_troublemaker_and_alias
    # have already been tested, so there's no real reason to test them again here.

    def test_add_to_database(self):
        # non-member
        self.client = AddGuestClient("+1098765432", "hello world")
        self.client.add_to_database()

        guest = Guest.objects.filter(phone="+1098765432")
        assert len(guest) == 1
        assert guest.first().id == self.client.guest.id
        assert guest.first().name == "hello world"

        # member
        self.client = AddGuestClient(self.member.phone, "hello world")
        self.client.add_to_database()

        guest = Guest.objects.filter(phone=self.member.phone)
        assert len(guest) == 1
        assert guest.first().id == self.client.guest.id
        assert guest.first().member.id == self.member.id

    def test_check_and_add_alias(self):
        self.client = AddGuestClient(self.phone_number, "hello world")
        self.client.check_and_add_alias(self.guest.name)
        alias = Alias.objects.filter(guest=self.guest)
        assert len(alias) == 1
        assert alias.first().alias == "hello world"

    def test_alias_doesnt_exist(self):
        self.client = AddGuestClient(self.phone_number, "hello world")
        assert self.client.alias_doesnt_exist() is True
        alias = Alias.objects.create(guest=self.guest, alias="hello world")
        alias.save()
        assert self.client.alias_doesnt_exist() is False

    def test_replace_troublemaker_tag(self):
        self.client = AddGuestClient(self.phone_number, "hello world")
        name_before = self.guest.name
        self.client.replace_troublemaker_tag()
        assert name_before == self.guest.name

        self.guest.name = "TROUBLEMAKER"
        self.guest.save()
        self.client = AddGuestClient(self.phone_number, "hello world")
        self.client.replace_troublemaker_tag()
        assert self.client.guest.name == "hello world"

    def test_is_troublemaker(self):
        self.client = AddGuestClient(self.phone_number, "hello world")
        assert self.client.is_troublemaker() is False
        self.guest.name = "TROUBLEMAKER"
        self.client.guest = self.guest
        assert self.client.is_troublemaker() is True

    def test_is_number_in_members(self):
        self.client = AddGuestClient(self.phone_number, "hello world")
        assert self.client.is_number_in_members() is False
        self.member.phone = self.phone_number
        self.client.member = self.member
        assert self.client.is_number_in_db() is True
Пример #5
0
 def test_is_number_in_members(self):
     self.client = AddGuestClient(self.phone_number, "hello world")
     assert self.client.is_number_in_members() is False
     self.member.phone = self.phone_number
     self.client.member = self.member
     assert self.client.is_number_in_db() is True
Пример #6
0
 def test_is_troublemaker(self):
     self.client = AddGuestClient(self.phone_number, "hello world")
     assert self.client.is_troublemaker() is False
     self.guest.name = "TROUBLEMAKER"
     self.client.guest = self.guest
     assert self.client.is_troublemaker() is True
Пример #7
0
 def test_alias_doesnt_exist(self):
     self.client = AddGuestClient(self.phone_number, "hello world")
     assert self.client.alias_doesnt_exist() is True
     alias = Alias.objects.create(guest=self.guest, alias="hello world")
     alias.save()
     assert self.client.alias_doesnt_exist() is False
Пример #8
0
 def test_check_and_add_alias(self):
     self.client = AddGuestClient(self.phone_number, "hello world")
     self.client.check_and_add_alias(self.guest.name)
     alias = Alias.objects.filter(guest=self.guest)
     assert len(alias) == 1
     assert alias.first().alias == "hello world"